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 | 13 |
14 import 'utils.dart'; | 14 import 'utils.dart'; |
15 | 15 |
16 bool _isGitInstalledCache; | 16 bool _isGitInstalledCache; |
17 | 17 |
18 /// The cached Git command. | 18 /// The cached Git command. |
19 String _gitCommandCache; | 19 String _gitCommandCache; |
20 | 20 |
21 /** Gets the current working directory. */ | 21 /** Gets the current working directory. */ |
22 String get workingDir => new File('.').fullPathSync(); | 22 String get workingDir => new File('.').fullPathSync(); |
23 | 23 |
| 24 const Pattern NEWLINE_PATTERN = const RegExp("\r\n?|\n\r?"); |
| 25 |
24 /** | 26 /** |
25 * Prints the given string to `stderr` on its own line. | 27 * Prints the given string to `stderr` on its own line. |
26 */ | 28 */ |
27 void printError(value) { | 29 void printError(value) { |
28 stderr.writeString(value.toString()); | 30 stderr.writeString(value.toString()); |
29 stderr.writeString('\n'); | 31 stderr.writeString('\n'); |
30 } | 32 } |
31 | 33 |
32 /** | 34 /** |
33 * Joins a number of path string parts into a single path. Handles | 35 * Joins a number of path string parts into a single path. Handles |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 args = flatten(["/c", executable, args]); | 494 args = flatten(["/c", executable, args]); |
493 executable = "cmd"; | 495 executable = "cmd"; |
494 } | 496 } |
495 | 497 |
496 final options = new ProcessOptions(); | 498 final options = new ProcessOptions(); |
497 if (workingDir != null) { | 499 if (workingDir != null) { |
498 options.workingDirectory = _getDirectory(workingDir).path; | 500 options.workingDirectory = _getDirectory(workingDir).path; |
499 } | 501 } |
500 options.environment = environment; | 502 options.environment = environment; |
501 | 503 |
502 final process = Process.start(executable, args, options); | 504 var future = Process.run(executable, args, options); |
503 | 505 return future.transform((result) { |
504 final outStream = new StringInputStream(process.stdout); | 506 // TODO(rnystrom): Remove this and change to returning one string. |
505 final processStdout = <String>[]; | 507 List<String> toLines(String output) { |
506 | 508 var lines = output.split(NEWLINE_PATTERN); |
507 final errStream = new StringInputStream(process.stderr); | 509 if (!lines.isEmpty() && lines.last() == "") lines.removeLast(); |
508 final processStderr = <String>[]; | 510 return lines; |
509 | 511 } |
510 final completer = new Completer<PubProcessResult>(); | 512 return new PubProcessResult(toLines(result.stdout), |
511 | 513 toLines(result.stderr), |
512 checkComplete() { | 514 result.exitCode); |
513 // Wait until the process is done and its output streams are closed. | 515 }); |
514 if (!pipeStdout && !outStream.closed) return; | |
515 if (!pipeStderr && !errStream.closed) return; | |
516 if (exitCode == null) return; | |
517 | |
518 completer.complete(new PubProcessResult( | |
519 processStdout, processStderr, exitCode)); | |
520 } | |
521 | |
522 if (pipeStdout) { | |
523 process.stdout.pipe(stdout, close: false); | |
524 } else { | |
525 outStream.onLine = () => processStdout.add(outStream.readLine()); | |
526 outStream.onClosed = checkComplete; | |
527 outStream.onError = (error) => completer.completeException(error); | |
528 } | |
529 | |
530 if (pipeStderr) { | |
531 process.stderr.pipe(stderr, close: false); | |
532 } else { | |
533 errStream.onLine = () => processStderr.add(errStream.readLine()); | |
534 errStream.onClosed = checkComplete; | |
535 errStream.onError = (error) => completer.completeException(error); | |
536 } | |
537 | |
538 process.onExit = (actualExitCode) { | |
539 exitCode = actualExitCode; | |
540 checkComplete(); | |
541 }; | |
542 | |
543 process.onError = (error) => completer.completeException(error); | |
544 | |
545 return completer.future; | |
546 } | 516 } |
547 | 517 |
548 /** | 518 /** |
549 * Wraps [input] to provide a timeout. If [input] completes before | 519 * Wraps [input] to provide a timeout. If [input] completes before |
550 * [milliseconds] have passed, then the return value completes in the same way. | 520 * [milliseconds] have passed, then the return value completes in the same way. |
551 * However, if [milliseconds] pass before [input] has completed, it completes | 521 * However, if [milliseconds] pass before [input] has completed, it completes |
552 * with a [TimeoutException] with [message]. | 522 * with a [TimeoutException] with [message]. |
553 * | 523 * |
554 * Note that timing out will not cancel the asynchronous operation behind | 524 * Note that timing out will not cancel the asynchronous operation behind |
555 * [input]. | 525 * [input]. |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 return new Directory(entry); | 755 return new Directory(entry); |
786 } | 756 } |
787 | 757 |
788 /** | 758 /** |
789 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 759 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
790 */ | 760 */ |
791 Uri _getUri(uri) { | 761 Uri _getUri(uri) { |
792 if (uri is Uri) return uri; | 762 if (uri is Uri) return uri; |
793 return new Uri.fromString(uri); | 763 return new Uri.fromString(uri); |
794 } | 764 } |
OLD | NEW |