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 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 listedDirectories.add(resolvedPath); | 180 listedDirectories.add(resolvedPath); |
181 | 181 |
182 log.io("Listing directory ${dir.path}."); | 182 log.io("Listing directory ${dir.path}."); |
183 var lister = dir.list(); | 183 var lister = dir.list(); |
184 | 184 |
185 lister.onDone = (done) { | 185 lister.onDone = (done) { |
186 // TODO(rnystrom): May need to sort here if it turns out onDir and onFile | 186 // TODO(rnystrom): May need to sort here if it turns out onDir and onFile |
187 // aren't guaranteed to be called in a certain order. So far, they seem to
. | 187 // aren't guaranteed to be called in a certain order. So far, they seem to
. |
188 if (done) { | 188 if (done) { |
189 log.fine("Listed directory ${dir.path}:\n" | 189 log.fine("Listed directory ${dir.path}:\n" |
190 "${Strings.join(contents, '\n')}"); | 190 "${contents.join('\n')}"); |
191 completer.complete(contents); | 191 completer.complete(contents); |
192 } | 192 } |
193 }; | 193 }; |
194 | 194 |
195 // TODO(nweiz): remove this when issue 4061 is fixed. | 195 // TODO(nweiz): remove this when issue 4061 is fixed. |
196 var stackTrace; | 196 var stackTrace; |
197 try { | 197 try { |
198 throw ""; | 198 throw ""; |
199 } catch (_, localStackTrace) { | 199 } catch (_, localStackTrace) { |
200 stackTrace = localStackTrace; | 200 stackTrace = localStackTrace; |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 /// [milliseconds] have passed, then the return value completes in the same way. | 674 /// [milliseconds] have passed, then the return value completes in the same way. |
675 /// However, if [milliseconds] pass before [input] has completed, it completes | 675 /// However, if [milliseconds] pass before [input] has completed, it completes |
676 /// with a [TimeoutException] with [description] (which should be a fragment | 676 /// with a [TimeoutException] with [description] (which should be a fragment |
677 /// describing the action that timed out). | 677 /// describing the action that timed out). |
678 /// | 678 /// |
679 /// Note that timing out will not cancel the asynchronous operation behind | 679 /// Note that timing out will not cancel the asynchronous operation behind |
680 /// [input]. | 680 /// [input]. |
681 Future timeout(Future input, int milliseconds, String description) { | 681 Future timeout(Future input, int milliseconds, String description) { |
682 bool completed = false; | 682 bool completed = false; |
683 var completer = new Completer(); | 683 var completer = new Completer(); |
684 var timer = new Timer(new Duration(milliseconds: milliseconds), () { | 684 var timer = new Timer(new Duration(milliseconds: milliseconds), (_) { |
685 completed = true; | 685 completed = true; |
686 completer.completeError(new TimeoutException( | 686 completer.completeError(new TimeoutException( |
687 'Timed out while $description.')); | 687 'Timed out while $description.')); |
688 }); | 688 }); |
689 input.then((value) { | 689 input.then((value) { |
690 if (completed) return; | 690 if (completed) return; |
691 timer.cancel(); | 691 timer.cancel(); |
692 completer.complete(value); | 692 completer.complete(value); |
693 }).catchError((e) { | 693 }).catchError((e) { |
694 if (completed) return; | 694 if (completed) return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 // Write the archive to a temp file. | 763 // Write the archive to a temp file. |
764 return createFileFromStream(stream, join(tempDir, 'data.tar.gz')).then((_) { | 764 return createFileFromStream(stream, join(tempDir, 'data.tar.gz')).then((_) { |
765 // 7zip can't unarchive from gzip -> tar -> destination all in one step | 765 // 7zip can't unarchive from gzip -> tar -> destination all in one step |
766 // first we un-gzip it to a tar file. | 766 // first we un-gzip it to a tar file. |
767 // Note: Setting the working directory instead of passing in a full file | 767 // Note: Setting the working directory instead of passing in a full file |
768 // path because 7zip says "A full path is not allowed here." | 768 // path because 7zip says "A full path is not allowed here." |
769 return runProcess(command, ['e', 'data.tar.gz'], workingDir: tempDir); | 769 return runProcess(command, ['e', 'data.tar.gz'], workingDir: tempDir); |
770 }).then((result) { | 770 }).then((result) { |
771 if (result.exitCode != 0) { | 771 if (result.exitCode != 0) { |
772 throw 'Could not un-gzip (exit code ${result.exitCode}). Error:\n' | 772 throw 'Could not un-gzip (exit code ${result.exitCode}). Error:\n' |
773 '${Strings.join(result.stdout, "\n")}\n' | 773 '${result.stdout.join("\n")}\n' |
774 '${Strings.join(result.stderr, "\n")}'; | 774 '${result.stderr.join("\n")}'; |
775 } | 775 } |
776 // Find the tar file we just created since we don't know its name. | 776 // Find the tar file we just created since we don't know its name. |
777 return listDir(tempDir); | 777 return listDir(tempDir); |
778 }).then((files) { | 778 }).then((files) { |
779 var tarFile; | 779 var tarFile; |
780 for (var file in files) { | 780 for (var file in files) { |
781 if (path.extension(file) == '.tar') { | 781 if (path.extension(file) == '.tar') { |
782 tarFile = file; | 782 tarFile = file; |
783 break; | 783 break; |
784 } | 784 } |
785 } | 785 } |
786 | 786 |
787 if (tarFile == null) throw 'The gzip file did not contain a tar file.'; | 787 if (tarFile == null) throw 'The gzip file did not contain a tar file.'; |
788 | 788 |
789 // Untar the archive into the destination directory. | 789 // Untar the archive into the destination directory. |
790 return runProcess(command, ['x', tarFile], workingDir: destination); | 790 return runProcess(command, ['x', tarFile], workingDir: destination); |
791 }).then((result) { | 791 }).then((result) { |
792 if (result.exitCode != 0) { | 792 if (result.exitCode != 0) { |
793 throw 'Could not un-tar (exit code ${result.exitCode}). Error:\n' | 793 throw 'Could not un-tar (exit code ${result.exitCode}). Error:\n' |
794 '${Strings.join(result.stdout, "\n")}\n' | 794 '${result.stdout.join("\n")}\n' |
795 '${Strings.join(result.stderr, "\n")}'; | 795 '${result.stderr.join("\n")}'; |
796 } | 796 } |
797 return true; | 797 return true; |
798 }); | 798 }); |
799 }); | 799 }); |
800 } | 800 } |
801 | 801 |
802 /// Create a .tar.gz archive from a list of entries. Each entry can be a | 802 /// Create a .tar.gz archive from a list of entries. Each entry can be a |
803 /// [String], [Directory], or [File] object. The root of the archive is | 803 /// [String], [Directory], or [File] object. The root of the archive is |
804 /// considered to be [baseDir], which defaults to the current working directory. | 804 /// considered to be [baseDir], which defaults to the current working directory. |
805 /// Returns a [ByteStream] that will emit the contents of the archive. | 805 /// Returns a [ByteStream] that will emit the contents of the archive. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 Directory _getDirectory(entry) { | 920 Directory _getDirectory(entry) { |
921 if (entry is Directory) return entry; | 921 if (entry is Directory) return entry; |
922 return new Directory(entry); | 922 return new Directory(entry); |
923 } | 923 } |
924 | 924 |
925 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 925 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
926 Uri _getUri(uri) { | 926 Uri _getUri(uri) { |
927 if (uri is Uri) return uri; | 927 if (uri is Uri) return uri; |
928 return Uri.parse(uri); | 928 return Uri.parse(uri); |
929 } | 929 } |
OLD | NEW |