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 /** | 5 /** |
6 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
7 */ | 7 */ |
8 #library('io'); | 8 #library('io'); |
9 | 9 |
10 #import('dart:io'); | 10 #import('dart:io'); |
11 #import('dart:isolate'); | 11 #import('dart:isolate'); |
12 #import('dart:uri'); | 12 #import('dart:uri'); |
13 #import('utils.dart'); | |
13 | 14 |
14 /** Gets the current working directory. */ | 15 /** Gets the current working directory. */ |
15 String get workingDir => new File('.').fullPathSync(); | 16 String get workingDir => new File('.').fullPathSync(); |
16 | 17 |
17 /** | 18 /** |
18 * Prints the given string to `stderr` on its own line. | 19 * Prints the given string to `stderr` on its own line. |
19 */ | 20 */ |
20 void printError(value) { | 21 void printError(value) { |
21 stderr.writeString(value.toString()); | 22 stderr.writeString(value.toString()); |
22 stderr.writeString('\n'); | 23 stderr.writeString('\n'); |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 return true; | 467 return true; |
467 }); | 468 }); |
468 input.then((value) { | 469 input.then((value) { |
469 if (completer.future.isComplete) return; | 470 if (completer.future.isComplete) return; |
470 timer.cancel(); | 471 timer.cancel(); |
471 completer.complete(value); | 472 completer.complete(value); |
472 }); | 473 }); |
473 return completer.future; | 474 return completer.future; |
474 } | 475 } |
475 | 476 |
476 /** | 477 /// The cached Git command. |
477 * Tests whether or not the git command-line app is available for use. | 478 String __gitCommand; |
Bob Nystrom
2012/09/11 19:53:55
"_gitCommandCache"
| |
478 */ | |
479 Future<bool> get isGitInstalled { | |
480 // TODO(rnystrom): We could cache this after the first check. We aren't right | |
481 // now because Future.immediate() will invoke its callback synchronously. | |
482 // That does bad things in cases where the caller expects futures to always | |
483 // be async. In particular, withGit() in the pub tests which calls | |
484 // expectAsync() will fail horribly if the test isn't actually async. | |
485 | 479 |
480 /// Tests whether or not the git command-line app is available for use. | |
481 Future<bool> get isGitInstalled => _gitCommand.transform((git) => git != null); | |
482 | |
483 /// Run a git process with [args] from [workingDir]. | |
484 Future<PubProcessResult> runGit(List<String> args, [String workingDir]) => | |
485 _gitCommand.chain((git) => runProcess(git, args, workingDir)); | |
486 | |
487 /// Returns the name of the git command-line app, or null if Git could not be | |
488 /// found on the user's PATH. | |
489 Future<String> get _gitCommand { | |
490 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. | |
491 if (__gitCommand != null) return sleep(0).transform((_) => __gitCommand); | |
492 | |
493 return _tryGitCommand("git").chain((success) { | |
494 if (success) return new Future.immediate("git"); | |
495 | |
496 // Git is sometimes installed on Windows as `git.cmd` | |
497 return _tryGitCommand("git.cmd").transform((success) { | |
498 if (success) return "git.cmd"; | |
499 return null; | |
500 }); | |
501 }).transform((command) { | |
502 __gitCommand = command; | |
503 return command; | |
504 }); | |
505 } | |
506 | |
507 /// Checks whether [command] is the Git command for this computer. | |
508 Future<bool> _tryGitCommand(String command) { | |
486 var completer = new Completer<bool>(); | 509 var completer = new Completer<bool>(); |
487 | 510 |
488 // If "git --version" prints something familiar, git is working. | 511 // If "git --version" prints something familiar, git is working. |
489 var future = runProcess("git", ["--version"]); | 512 var future = runProcess(command, ["--version"]); |
490 | 513 |
491 future.then((results) { | 514 future.then((results) { |
492 var regex = new RegExp("^git version"); | 515 var regex = new RegExp("^git version"); |
493 completer.complete(results.stdout.length == 1 && | 516 completer.complete(results.stdout.length == 1 && |
494 regex.hasMatch(results.stdout[0])); | 517 regex.hasMatch(results.stdout[0])); |
495 }); | 518 }); |
496 | 519 |
497 future.handleException((err) { | 520 future.handleException((err) { |
498 // If the process failed, they probably don't have it. | 521 // If the process failed, they probably don't have it. |
499 completer.complete(false); | 522 completer.complete(false); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 return new Directory(entry); | 597 return new Directory(entry); |
575 } | 598 } |
576 | 599 |
577 /** | 600 /** |
578 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 601 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
579 */ | 602 */ |
580 Uri _getUri(uri) { | 603 Uri _getUri(uri) { |
581 if (uri is Uri) return uri; | 604 if (uri is Uri) return uri; |
582 return new Uri.fromString(uri); | 605 return new Uri.fromString(uri); |
583 } | 606 } |
OLD | NEW |