OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Helper functionality for invoking Git. | 5 /// Helper functionality for invoking Git. |
6 library pub.git; | 6 library pub.git; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'io.dart'; | 11 import 'io.dart'; |
12 import 'log.dart' as log; | 12 import 'log.dart' as log; |
13 | 13 |
14 /// An exception thrown because a git command failed. | |
15 class GitException implements Exception { | |
Bob Nystrom
2014/04/01 23:43:34
I think we tend to import this library with a pref
nweiz
2014/04/02 00:41:19
I think shadowing core types is worse than having
| |
16 /// The arguments to the git command. | |
17 final List<String> args; | |
18 | |
19 /// The standard error emitted by git. | |
20 final String stderr; | |
21 | |
22 String get message => 'Git error. Command: git ${args.join(" ")}\n$stderr'; | |
23 | |
24 GitException(Iterable<String> args, this.stderr) | |
25 : args = args.toList(); | |
26 | |
27 String toString() => message; | |
28 } | |
29 | |
14 /// Tests whether or not the git command-line app is available for use. | 30 /// Tests whether or not the git command-line app is available for use. |
15 Future<bool> get isInstalled { | 31 Future<bool> get isInstalled { |
16 if (_isGitInstalledCache != null) { | 32 if (_isGitInstalledCache != null) { |
17 return new Future.value(_isGitInstalledCache); | 33 return new Future.value(_isGitInstalledCache); |
18 } | 34 } |
19 | 35 |
20 return _gitCommand.then((git) => git != null); | 36 return _gitCommand.then((git) => git != null); |
21 } | 37 } |
22 | 38 |
23 /// Run a git process with [args] from [workingDir]. Returns the stdout as a | 39 /// Run a git process with [args] from [workingDir]. Returns the stdout as a |
24 /// list of strings if it succeeded. Completes to an exception if it failed. | 40 /// list of strings if it succeeded. Completes to an exception if it failed. |
25 Future<List<String>> run(List<String> args, | 41 Future<List<String>> run(List<String> args, |
26 {String workingDir, Map<String, String> environment}) { | 42 {String workingDir, Map<String, String> environment}) { |
27 return _gitCommand.then((git) { | 43 return _gitCommand.then((git) { |
28 return runProcess(git, args, workingDir: workingDir, | 44 return runProcess(git, args, workingDir: workingDir, |
29 environment: environment); | 45 environment: environment); |
30 }).then((result) { | 46 }).then((result) { |
31 if (!result.success) throw new Exception( | 47 if (!result.success) throw new GitException(args, result.stderr.join("\n")); |
32 'Git error. Command: git ${args.join(" ")}\n' | |
33 '${result.stderr.join("\n")}'); | |
34 | |
35 return result.stdout; | 48 return result.stdout; |
36 }); | 49 }); |
37 } | 50 } |
38 | 51 |
39 bool _isGitInstalledCache; | 52 bool _isGitInstalledCache; |
40 | 53 |
41 /// The cached Git command. | 54 /// The cached Git command. |
42 String _gitCommandCache; | 55 String _gitCommandCache; |
43 | 56 |
44 /// Returns the name of the git command-line app, or null if Git could not be | 57 /// Returns the name of the git command-line app, or null if Git could not be |
(...skipping 27 matching lines...) Expand all Loading... | |
72 }).catchError((err, stackTrace) { | 85 }).catchError((err, stackTrace) { |
73 // If the process failed, they probably don't have it. | 86 // If the process failed, they probably don't have it. |
74 if (err is ProcessException) { | 87 if (err is ProcessException) { |
75 log.io('Git command is not "$command": $err\n$stackTrace'); | 88 log.io('Git command is not "$command": $err\n$stackTrace'); |
76 return false; | 89 return false; |
77 } | 90 } |
78 | 91 |
79 throw err; | 92 throw err; |
80 }); | 93 }); |
81 } | 94 } |
OLD | NEW |