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'; |
11 import 'dart:json'; | 11 import 'dart:json'; |
12 import 'dart:uri'; | 12 import 'dart:uri'; |
13 | 13 |
14 import '../../pkg/path/lib/path.dart' as path; | 14 import '../../pkg/path/lib/path.dart' as path; |
15 import '../../pkg/http/lib/http.dart' show ByteStream; | 15 import '../../pkg/http/lib/http.dart' show ByteStream; |
16 import 'error_group.dart'; | 16 import 'error_group.dart'; |
17 import 'exit_codes.dart' as exit_codes; | 17 import 'exit_codes.dart' as exit_codes; |
18 import 'log.dart' as log; | 18 import 'log.dart' as log; |
19 import 'utils.dart'; | 19 import 'utils.dart'; |
20 | 20 |
21 export '../../pkg/http/lib/http.dart' show ByteStream; | 21 export '../../pkg/http/lib/http.dart' show ByteStream; |
22 | 22 |
23 final NEWLINE_PATTERN = new RegExp("\r\n?|\n\r?"); | 23 final NEWLINE_PATTERN = new RegExp("\r\n?|\n\r?"); |
24 | 24 |
25 /// Joins a number of path string parts into a single path. Handles | 25 /// Joins a number of path string parts into a single path. Handles |
26 /// platform-specific path separators. Parts can be [String], [Directory], or | 26 /// platform-specific path separators. Parts can be [String], [Directory], or |
27 /// [File] objects. | 27 /// [File] objects. |
28 String join(part1, [part2, part3, part4, part5, part6, part7, part8]) { | 28 String join(part1, [part2, part3, part4, part5, part6, part7, part8]) { |
29 var parts = [part1, part2, part3, part4, part5, part6, part7, part8] | 29 var parts = [part1, part2, part3, part4, part5, part6, part7, part8] |
30 .mappedBy((part) => part == null ? null : _getPath(part)).toList(); | 30 .map((part) => part == null ? null : _getPath(part)).toList(); |
31 | 31 |
32 return path.join(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], | 32 return path.join(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], |
33 parts[6], parts[7]); | 33 parts[6], parts[7]); |
34 } | 34 } |
35 | 35 |
36 /// Gets the basename, the file name without any leading directory path, for | 36 /// Gets the basename, the file name without any leading directory path, for |
37 /// [file], which can either be a [String], [File], or [Directory]. | 37 /// [file], which can either be a [String], [File], or [Directory]. |
38 String basename(file) => path.basename(_getPath(file)); | 38 String basename(file) => path.basename(_getPath(file)); |
39 | 39 |
40 /// Gets the the leading directory path for [file], which can either be a | 40 /// Gets the the leading directory path for [file], which can either be a |
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
845 buffer.add('Creating .tag.gz stream containing:\n'); | 845 buffer.add('Creating .tag.gz stream containing:\n'); |
846 contents.forEach((file) => buffer.add('$file\n')); | 846 contents.forEach((file) => buffer.add('$file\n')); |
847 log.fine(buffer.toString()); | 847 log.fine(buffer.toString()); |
848 | 848 |
849 // TODO(nweiz): Propagate errors to the returned stream (including non-zero | 849 // TODO(nweiz): Propagate errors to the returned stream (including non-zero |
850 // exit codes). See issue 3657. | 850 // exit codes). See issue 3657. |
851 var controller = new StreamController<List<int>>(); | 851 var controller = new StreamController<List<int>>(); |
852 | 852 |
853 if (baseDir == null) baseDir = path.current; | 853 if (baseDir == null) baseDir = path.current; |
854 baseDir = getFullPath(baseDir); | 854 baseDir = getFullPath(baseDir); |
855 contents = contents.mappedBy((entry) { | 855 contents = contents.map((entry) { |
856 entry = getFullPath(entry); | 856 entry = getFullPath(entry); |
857 if (!isBeneath(entry, baseDir)) { | 857 if (!isBeneath(entry, baseDir)) { |
858 throw 'Entry $entry is not inside $baseDir.'; | 858 throw 'Entry $entry is not inside $baseDir.'; |
859 } | 859 } |
860 return relativeTo(entry, baseDir); | 860 return relativeTo(entry, baseDir); |
861 }).toList(); | 861 }).toList(); |
862 | 862 |
863 if (Platform.operatingSystem != "windows") { | 863 if (Platform.operatingSystem != "windows") { |
864 var args = ["--create", "--gzip", "--directory", baseDir]; | 864 var args = ["--create", "--gzip", "--directory", baseDir]; |
865 args.addAll(contents.mappedBy(_getPath)); | 865 args.addAll(contents.map(_getPath)); |
866 // TODO(nweiz): It's possible that enough command-line arguments will make | 866 // TODO(nweiz): It's possible that enough command-line arguments will make |
867 // the process choke, so at some point we should save the arguments to a | 867 // the process choke, so at some point we should save the arguments to a |
868 // file and pass them in via --files-from for tar and -i@filename for 7zip. | 868 // file and pass them in via --files-from for tar and -i@filename for 7zip. |
869 startProcess("tar", args).then((process) { | 869 startProcess("tar", args).then((process) { |
870 store(process.stdout, controller); | 870 store(process.stdout, controller); |
871 }).catchError((e) { | 871 }).catchError((e) { |
872 // We don't have to worry about double-signaling here, since the store() | 872 // We don't have to worry about double-signaling here, since the store() |
873 // above will only be reached if startProcess succeeds. | 873 // above will only be reached if startProcess succeeds. |
874 controller.signalError(e.error, e.stackTrace); | 874 controller.signalError(e.error, e.stackTrace); |
875 controller.close(); | 875 controller.close(); |
876 }); | 876 }); |
877 return new ByteStream(controller.stream); | 877 return new ByteStream(controller.stream); |
878 } | 878 } |
879 | 879 |
880 withTempDir((tempDir) { | 880 withTempDir((tempDir) { |
881 // Create the tar file. | 881 // Create the tar file. |
882 var tarFile = join(tempDir, "intermediate.tar"); | 882 var tarFile = join(tempDir, "intermediate.tar"); |
883 var args = ["a", "-w$baseDir", tarFile]; | 883 var args = ["a", "-w$baseDir", tarFile]; |
884 args.addAll(contents.mappedBy((entry) => '-i!"$entry"')); | 884 args.addAll(contents.map((entry) => '-i!"$entry"')); |
885 | 885 |
886 // Note: This line of code gets munged by create_sdk.py to be the correct | 886 // Note: This line of code gets munged by create_sdk.py to be the correct |
887 // relative path to 7zip in the SDK. | 887 // relative path to 7zip in the SDK. |
888 var pathTo7zip = '../../third_party/7zip/7za.exe'; | 888 var pathTo7zip = '../../third_party/7zip/7za.exe'; |
889 var command = relativeToPub(pathTo7zip); | 889 var command = relativeToPub(pathTo7zip); |
890 | 890 |
891 // We're passing 'baseDir' both as '-w' and setting it as the working | 891 // We're passing 'baseDir' both as '-w' and setting it as the working |
892 // directory explicitly here intentionally. The former ensures that the | 892 // directory explicitly here intentionally. The former ensures that the |
893 // files added to the archive have the correct relative path in the archive. | 893 // files added to the archive have the correct relative path in the archive. |
894 // The latter enables relative paths in the "-i" args to be resolved. | 894 // The latter enables relative paths in the "-i" args to be resolved. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
949 Directory _getDirectory(entry) { | 949 Directory _getDirectory(entry) { |
950 if (entry is Directory) return entry; | 950 if (entry is Directory) return entry; |
951 return new Directory(entry); | 951 return new Directory(entry); |
952 } | 952 } |
953 | 953 |
954 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 954 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
955 Uri _getUri(uri) { | 955 Uri _getUri(uri) { |
956 if (uri is Uri) return uri; | 956 if (uri is Uri) return uri; |
957 return Uri.parse(uri); | 957 return Uri.parse(uri); |
958 } | 958 } |
OLD | NEW |