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 pub.io; | 6 library pub.io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:collection'; | 9 import 'dart:collection'; |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
(...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
822 executable = "cmd"; | 822 executable = "cmd"; |
823 } | 823 } |
824 | 824 |
825 log.process(executable, args, workingDir == null ? '.' : workingDir); | 825 log.process(executable, args, workingDir == null ? '.' : workingDir); |
826 | 826 |
827 return fn(executable, args, | 827 return fn(executable, args, |
828 workingDirectory: workingDir, | 828 workingDirectory: workingDir, |
829 environment: environment); | 829 environment: environment); |
830 } | 830 } |
831 | 831 |
832 /// Wraps [input], an asynchronous network operation to provide a timeout. | |
833 /// | |
834 /// If [input] completes before [milliseconds] have passed, then the return | |
835 /// value completes in the same way. However, if [milliseconds] pass before | |
836 /// [input] has completed, it completes with a [TimeoutException] with | |
837 /// [description] (which should be a fragment describing the action that timed | |
838 /// out). | |
839 /// | |
840 /// [url] is the URL being accessed asynchronously. | |
841 /// | |
842 /// Note that timing out will not cancel the asynchronous operation behind | |
843 /// [input]. | |
844 Future timeout(Future input, int milliseconds, Uri url, String description) { | |
845 // TODO(nwiez): Replace this with [Future.timeout]. | |
846 var completer = new Completer(); | |
847 var duration = new Duration(milliseconds: milliseconds); | |
848 var timer = new Timer(duration, () { | |
849 // Include the duration ourselves in the message instead of passing it to | |
850 // TimeoutException since we show nicer output. | |
851 var message = 'Timed out after ${niceDuration(duration)} while ' | |
852 '$description.'; | |
853 | |
854 if (url.host == "pub.dartlang.org" || | |
855 url.host == "storage.googleapis.com") { | |
856 message += "\nThis is likely a transient error. Please try again later."; | |
857 } | |
858 | |
859 completer.completeError(new TimeoutException(message), new Chain.current()); | |
860 }); | |
861 input.then((value) { | |
862 if (completer.isCompleted) return; | |
863 timer.cancel(); | |
864 completer.complete(value); | |
865 }).catchError((e, stackTrace) { | |
866 if (completer.isCompleted) return; | |
867 timer.cancel(); | |
868 completer.completeError(e, stackTrace); | |
869 }); | |
870 return completer.future; | |
871 } | |
872 | |
873 /// Creates a temporary directory and passes its path to [fn]. | 832 /// Creates a temporary directory and passes its path to [fn]. |
874 /// | 833 /// |
875 /// Once the [Future] returned by [fn] completes, the temporary directory and | 834 /// Once the [Future] returned by [fn] completes, the temporary directory and |
876 /// all its contents are deleted. [fn] can also return `null`, in which case | 835 /// all its contents are deleted. [fn] can also return `null`, in which case |
877 /// the temporary directory is deleted immediately afterwards. | 836 /// the temporary directory is deleted immediately afterwards. |
878 /// | 837 /// |
879 /// Returns a future that completes to the value that the future returned from | 838 /// Returns a future that completes to the value that the future returned from |
880 /// [fn] completes to. | 839 /// [fn] completes to. |
881 Future withTempDir(Future fn(String path)) { | 840 Future withTempDir(Future fn(String path)) { |
882 return new Future.sync(() { | 841 return new Future.sync(() { |
883 var tempDir = createSystemTempDir(); | 842 var tempDir = createSystemTempDir(); |
884 return new Future.sync(() => fn(tempDir)) | 843 return new Future.sync(() => fn(tempDir)) |
885 .whenComplete(() => deleteEntry(tempDir)); | 844 .whenComplete(() => deleteEntry(tempDir)); |
886 }); | 845 }); |
887 } | 846 } |
888 | 847 |
889 /// Binds an [HttpServer] to [host] and [port]. | 848 /// Binds an [HttpServer] to [host] and [port]. |
890 /// | 849 /// |
891 /// If [host] is "localhost", this will automatically listen on both the IPv4 | 850 /// If [host] is "localhost", this will automatically listen on both the IPv4 |
892 /// and IPv6 loopback addresses. | 851 /// and IPv6 loopback addresses. |
893 Future<HttpServer> bindServer(String host, int port) { | 852 Future<HttpServer> bindServer(String host, int port) { |
894 if (host == 'localhost') return HttpMultiServer.loopback(port); | 853 if (host == 'localhost') return HttpMultiServer.loopback(port); |
895 return HttpServer.bind(host, port); | 854 return HttpServer.bind(host, port); |
896 } | 855 } |
897 | 856 |
898 /// Extracts a `.tar.gz` file from [stream] to [destination]. | 857 /// Extracts a `.tar.gz` file from [stream] to [destination]. |
899 /// | 858 /// |
900 /// Returns whether or not the extraction was successful. | 859 /// Returns whether or not the extraction was successful. |
901 Future<bool> extractTarGz(Stream<List<int>> stream, String destination) { | 860 Future<bool> extractTarGz(Stream<List<int>> stream, String destination) { |
Bob Nystrom
2015/07/14 00:48:42
This <bool> is bogus here. Yay for null being "boo
nweiz
2015/07/14 01:21:40
Done.
| |
902 log.fine("Extracting .tar.gz stream to $destination."); | 861 log.fine("Extracting .tar.gz stream to $destination."); |
903 | 862 |
904 if (Platform.operatingSystem == "windows") { | 863 if (Platform.operatingSystem == "windows") { |
905 return _extractTarGzWindows(stream, destination); | 864 return _extractTarGzWindows(stream, destination); |
906 } | 865 } |
907 | 866 |
908 var args = ["--extract", "--gunzip", "--directory", destination]; | 867 var args = ["--extract", "--gunzip", "--directory", destination]; |
909 if (_noUnknownKeyword) { | 868 if (_noUnknownKeyword) { |
910 // BSD tar (the default on OS X) can insert strange headers to a tarfile | 869 // BSD tar (the default on OS X) can insert strange headers to a tarfile |
911 // that GNU tar (the default on Linux) is unable to understand. This will | 870 // that GNU tar (the default on Linux) is unable to understand. This will |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
954 var major = int.parse(match[1]); | 913 var major = int.parse(match[1]); |
955 var minor = int.parse(match[2]); | 914 var minor = int.parse(match[2]); |
956 return major >= 2 || (major == 1 && minor >= 23); | 915 return major >= 2 || (major == 1 && minor >= 23); |
957 } | 916 } |
958 | 917 |
959 final String pathTo7zip = (() { | 918 final String pathTo7zip = (() { |
960 if (!runningFromDartRepo) return sdkAssetPath(path.join('7zip', '7za.exe')); | 919 if (!runningFromDartRepo) return sdkAssetPath(path.join('7zip', '7za.exe')); |
961 return path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe'); | 920 return path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe'); |
962 })(); | 921 })(); |
963 | 922 |
964 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, | 923 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, |
Bob Nystrom
2015/07/14 00:48:42
Might as well remove it here too.
nweiz
2015/07/14 01:21:40
Done.
| |
965 String destination) { | 924 String destination) { |
966 // TODO(rnystrom): In the repo's history, there is an older implementation of | 925 // TODO(rnystrom): In the repo's history, there is an older implementation of |
967 // this that does everything in memory by piping streams directly together | 926 // this that does everything in memory by piping streams directly together |
968 // instead of writing out temp files. The code is simpler, but unfortunately, | 927 // instead of writing out temp files. The code is simpler, but unfortunately, |
969 // 7zip seems to periodically fail when we invoke it from Dart and tell it to | 928 // 7zip seems to periodically fail when we invoke it from Dart and tell it to |
970 // read from stdin instead of a file. Consider resurrecting that version if | 929 // read from stdin instead of a file. Consider resurrecting that version if |
971 // we can figure out why it fails. | 930 // we can figure out why it fails. |
972 | 931 |
973 return withTempDir((tempDir) { | 932 return withTempDir((tempDir) { |
974 // Write the archive to a temp file. | 933 // Write the archive to a temp file. |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 | 1052 |
1094 // TODO(rnystrom): Remove this and change to returning one string. | 1053 // TODO(rnystrom): Remove this and change to returning one string. |
1095 static List<String> _toLines(String output) { | 1054 static List<String> _toLines(String output) { |
1096 var lines = splitLines(output); | 1055 var lines = splitLines(output); |
1097 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 1056 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
1098 return lines; | 1057 return lines; |
1099 } | 1058 } |
1100 | 1059 |
1101 bool get success => exitCode == exit_codes.SUCCESS; | 1060 bool get success => exitCode == exit_codes.SUCCESS; |
1102 } | 1061 } |
OLD | NEW |