OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library io; | 6 library io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 /// Resolves [target] relative to the location of pub.dart. | 255 /// Resolves [target] relative to the location of pub.dart. |
256 String relativeToPub(String target) { | 256 String relativeToPub(String target) { |
257 var scriptPath = new File(new Options().script).fullPathSync(); | 257 var scriptPath = new File(new Options().script).fullPathSync(); |
258 | 258 |
259 // Walk up until we hit the "util(s)" directory. This lets us figure out where | 259 // Walk up until we hit the "util(s)" directory. This lets us figure out where |
260 // we are if this function is called from pub.dart, or one of the tests, | 260 // we are if this function is called from pub.dart, or one of the tests, |
261 // which also live under "utils", or from the SDK where pub is in "util". | 261 // which also live under "utils", or from the SDK where pub is in "util". |
262 var utilDir = path.dirname(scriptPath); | 262 var utilDir = path.dirname(scriptPath); |
263 while (path.basename(utilDir) != 'utils' && | 263 while (path.basename(utilDir) != 'utils' && |
264 path.basename(utilDir) != 'util') { | 264 path.basename(utilDir) != 'util') { |
265 if (path.basename(utilDir) == '') throw 'Could not find path to pub.'; | 265 if (path.basename(utilDir) == '') { |
| 266 throw new Exception('Could not find path to pub.'); |
| 267 } |
266 utilDir = path.dirname(utilDir); | 268 utilDir = path.dirname(utilDir); |
267 } | 269 } |
268 | 270 |
269 return path.normalize(path.join(utilDir, 'pub', target)); | 271 return path.normalize(path.join(utilDir, 'pub', target)); |
270 } | 272 } |
271 | 273 |
272 /// A line-by-line stream of standard input. | 274 /// A line-by-line stream of standard input. |
273 final Stream<String> stdinLines = streamToLines( | 275 final Stream<String> stdinLines = streamToLines( |
274 new ByteStream(stdin).toStringStream()); | 276 new ByteStream(stdin).toStringStream()); |
275 | 277 |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 // std{out,err}Sink. | 539 // std{out,err}Sink. |
538 store(process.stdout.handleError((_) {}), stdout, closeSink: false); | 540 store(process.stdout.handleError((_) {}), stdout, closeSink: false); |
539 store(process.stderr.handleError((_) {}), stderr, closeSink: false); | 541 store(process.stderr.handleError((_) {}), stderr, closeSink: false); |
540 return Future.wait([ | 542 return Future.wait([ |
541 store(stream, process.stdin), | 543 store(stream, process.stdin), |
542 process.exitCode | 544 process.exitCode |
543 ]); | 545 ]); |
544 }).then((results) { | 546 }).then((results) { |
545 var exitCode = results[1]; | 547 var exitCode = results[1]; |
546 if (exitCode != 0) { | 548 if (exitCode != 0) { |
547 throw "Failed to extract .tar.gz stream to $destination (exit code " | 549 throw new Exception("Failed to extract .tar.gz stream to $destination " |
548 "$exitCode)."; | 550 "(exit code $exitCode)."); |
549 } | 551 } |
550 log.fine("Extracted .tar.gz stream to $destination. Exit code $exitCode."); | 552 log.fine("Extracted .tar.gz stream to $destination. Exit code $exitCode."); |
551 }); | 553 }); |
552 } | 554 } |
553 | 555 |
554 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, | 556 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, |
555 String destination) { | 557 String destination) { |
556 // TODO(rnystrom): In the repo's history, there is an older implementation of | 558 // TODO(rnystrom): In the repo's history, there is an older implementation of |
557 // this that does everything in memory by piping streams directly together | 559 // this that does everything in memory by piping streams directly together |
558 // instead of writing out temp files. The code is simpler, but unfortunately, | 560 // instead of writing out temp files. The code is simpler, but unfortunately, |
(...skipping 10 matching lines...) Expand all Loading... |
569 // Write the archive to a temp file. | 571 // Write the archive to a temp file. |
570 var dataFile = path.join(tempDir, 'data.tar.gz'); | 572 var dataFile = path.join(tempDir, 'data.tar.gz'); |
571 return createFileFromStream(stream, dataFile).then((_) { | 573 return createFileFromStream(stream, dataFile).then((_) { |
572 // 7zip can't unarchive from gzip -> tar -> destination all in one step | 574 // 7zip can't unarchive from gzip -> tar -> destination all in one step |
573 // first we un-gzip it to a tar file. | 575 // first we un-gzip it to a tar file. |
574 // Note: Setting the working directory instead of passing in a full file | 576 // Note: Setting the working directory instead of passing in a full file |
575 // path because 7zip says "A full path is not allowed here." | 577 // path because 7zip says "A full path is not allowed here." |
576 return runProcess(command, ['e', 'data.tar.gz'], workingDir: tempDir); | 578 return runProcess(command, ['e', 'data.tar.gz'], workingDir: tempDir); |
577 }).then((result) { | 579 }).then((result) { |
578 if (result.exitCode != 0) { | 580 if (result.exitCode != 0) { |
579 throw 'Could not un-gzip (exit code ${result.exitCode}). Error:\n' | 581 throw new Exception('Could not un-gzip (exit code ${result.exitCode}). ' |
| 582 'Error:\n' |
580 '${result.stdout.join("\n")}\n' | 583 '${result.stdout.join("\n")}\n' |
581 '${result.stderr.join("\n")}'; | 584 '${result.stderr.join("\n")}'); |
582 } | 585 } |
583 | 586 |
584 // Find the tar file we just created since we don't know its name. | 587 // Find the tar file we just created since we don't know its name. |
585 var tarFile = listDir(tempDir).firstWhere( | 588 var tarFile = listDir(tempDir).firstWhere( |
586 (file) => path.extension(file) == '.tar', | 589 (file) => path.extension(file) == '.tar', |
587 orElse: () { | 590 orElse: () { |
588 throw 'The gzip file did not contain a tar file.'; | 591 throw new FormatException('The gzip file did not contain a tar file.'); |
589 }); | 592 }); |
590 | 593 |
591 // Untar the archive into the destination directory. | 594 // Untar the archive into the destination directory. |
592 return runProcess(command, ['x', tarFile], workingDir: destination); | 595 return runProcess(command, ['x', tarFile], workingDir: destination); |
593 }).then((result) { | 596 }).then((result) { |
594 if (result.exitCode != 0) { | 597 if (result.exitCode != 0) { |
595 throw 'Could not un-tar (exit code ${result.exitCode}). Error:\n' | 598 throw new Exception('Could not un-tar (exit code ${result.exitCode}). ' |
| 599 'Error:\n' |
596 '${result.stdout.join("\n")}\n' | 600 '${result.stdout.join("\n")}\n' |
597 '${result.stderr.join("\n")}'; | 601 '${result.stderr.join("\n")}'); |
598 } | 602 } |
599 return true; | 603 return true; |
600 }); | 604 }); |
601 }); | 605 }); |
602 } | 606 } |
603 | 607 |
604 /// Create a .tar.gz archive from a list of entries. Each entry can be a | 608 /// Create a .tar.gz archive from a list of entries. Each entry can be a |
605 /// [String], [Directory], or [File] object. The root of the archive is | 609 /// [String], [Directory], or [File] object. The root of the archive is |
606 /// considered to be [baseDir], which defaults to the current working directory. | 610 /// considered to be [baseDir], which defaults to the current working directory. |
607 /// Returns a [ByteStream] that will emit the contents of the archive. | 611 /// Returns a [ByteStream] that will emit the contents of the archive. |
608 ByteStream createTarGz(List contents, {baseDir}) { | 612 ByteStream createTarGz(List contents, {baseDir}) { |
609 var buffer = new StringBuffer(); | 613 var buffer = new StringBuffer(); |
610 buffer.write('Creating .tag.gz stream containing:\n'); | 614 buffer.write('Creating .tag.gz stream containing:\n'); |
611 contents.forEach((file) => buffer.write('$file\n')); | 615 contents.forEach((file) => buffer.write('$file\n')); |
612 log.fine(buffer.toString()); | 616 log.fine(buffer.toString()); |
613 | 617 |
614 var controller = new StreamController<List<int>>(); | 618 var controller = new StreamController<List<int>>(); |
615 | 619 |
616 if (baseDir == null) baseDir = path.current; | 620 if (baseDir == null) baseDir = path.current; |
617 baseDir = path.absolute(baseDir); | 621 baseDir = path.absolute(baseDir); |
618 contents = contents.map((entry) { | 622 contents = contents.map((entry) { |
619 entry = path.absolute(entry); | 623 entry = path.absolute(entry); |
620 if (!isBeneath(entry, baseDir)) { | 624 if (!isBeneath(entry, baseDir)) { |
621 throw 'Entry $entry is not inside $baseDir.'; | 625 throw new ArgumentError('Entry $entry is not inside $baseDir.'); |
622 } | 626 } |
623 return path.relative(entry, from: baseDir); | 627 return path.relative(entry, from: baseDir); |
624 }).toList(); | 628 }).toList(); |
625 | 629 |
626 if (Platform.operatingSystem != "windows") { | 630 if (Platform.operatingSystem != "windows") { |
627 var args = ["--create", "--gzip", "--directory", baseDir]; | 631 var args = ["--create", "--gzip", "--directory", baseDir]; |
628 args.addAll(contents); | 632 args.addAll(contents); |
629 // TODO(nweiz): It's possible that enough command-line arguments will make | 633 // TODO(nweiz): It's possible that enough command-line arguments will make |
630 // the process choke, so at some point we should save the arguments to a | 634 // the process choke, so at some point we should save the arguments to a |
631 // file and pass them in via --files-from for tar and -i@filename for 7zip. | 635 // file and pass them in via --files-from for tar and -i@filename for 7zip. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 699 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
696 | 700 |
697 bool get success => exitCode == 0; | 701 bool get success => exitCode == 0; |
698 } | 702 } |
699 | 703 |
700 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 704 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
701 Uri _getUri(uri) { | 705 Uri _getUri(uri) { |
702 if (uri is Uri) return uri; | 706 if (uri is Uri) return uri; |
703 return Uri.parse(uri); | 707 return Uri.parse(uri); |
704 } | 708 } |
OLD | NEW |