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; |
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 /// Returns the name of the git command-line app, or null if Git could not be | |
484 /// found on the user's PATH. | |
485 Future<String> get gitCommand { | |
Bob Nystrom
2012/09/11 16:18:55
I would unify this with _runGit and just have a si
nweiz
2012/09/11 19:25:11
Done.
| |
486 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. | |
487 if (_gitCommand != null) return sleep(0).transform((_) => _gitCommand); | |
488 | |
489 return _tryGitCommand("git").chain((success) { | |
490 if (success) return new Future.immediate("git"); | |
491 | |
492 // Git is sometimes installed on Windows as `git.cmd` | |
493 return _tryGitCommand("git.cmd").transform((success) { | |
494 if (success) return "git.cmd"; | |
495 return null; | |
496 }); | |
497 }); | |
498 } | |
499 | |
500 /// Checks whether [command] is the Git command for this computer. | |
501 Future<bool> _tryGitCommand(String command) { | |
486 var completer = new Completer<bool>(); | 502 var completer = new Completer<bool>(); |
487 | 503 |
488 // If "git --version" prints something familiar, git is working. | 504 // If "git --version" prints something familiar, git is working. |
489 var future = runProcess("git", ["--version"]); | 505 var future = runProcess(command, ["--version"]); |
490 | 506 |
491 future.then((results) { | 507 future.then((results) { |
492 var regex = new RegExp("^git version"); | 508 var regex = new RegExp("^git version"); |
493 completer.complete(results.stdout.length == 1 && | 509 completer.complete(results.stdout.length == 1 && |
494 regex.hasMatch(results.stdout[0])); | 510 regex.hasMatch(results.stdout[0])); |
495 }); | 511 }); |
496 | 512 |
497 future.handleException((err) { | 513 future.handleException((err) { |
498 // If the process failed, they probably don't have it. | 514 // If the process failed, they probably don't have it. |
499 completer.complete(false); | 515 completer.complete(false); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 return new Directory(entry); | 590 return new Directory(entry); |
575 } | 591 } |
576 | 592 |
577 /** | 593 /** |
578 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 594 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
579 */ | 595 */ |
580 Uri _getUri(uri) { | 596 Uri _getUri(uri) { |
581 if (uri is Uri) return uri; | 597 if (uri is Uri) return uri; |
582 return new Uri.fromString(uri); | 598 return new Uri.fromString(uri); |
583 } | 599 } |
OLD | NEW |