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'; |
(...skipping 12 matching lines...) Expand all Loading... |
23 /// The cached Git command. | 23 /// The cached Git command. |
24 String _gitCommandCache; | 24 String _gitCommandCache; |
25 | 25 |
26 final NEWLINE_PATTERN = new RegExp("\r\n?|\n\r?"); | 26 final NEWLINE_PATTERN = new RegExp("\r\n?|\n\r?"); |
27 | 27 |
28 /** | 28 /** |
29 * Joins a number of path string parts into a single path. Handles | 29 * Joins a number of path string parts into a single path. Handles |
30 * platform-specific path separators. Parts can be [String], [Directory], or | 30 * platform-specific path separators. Parts can be [String], [Directory], or |
31 * [File] objects. | 31 * [File] objects. |
32 */ | 32 */ |
33 String join(part1, [part2, part3, part4]) { | 33 String join(part1, [part2, part3, part4, part5, part6, part7, part8]) { |
34 part1 = _getPath(part1); | 34 var parts = [part1, part2, part3, part4, part5, part6, part7, part8] |
35 if (part2 != null) part2 = _getPath(part2); | 35 .map((part) => part == null ? null : _getPath(part)); |
36 if (part3 != null) part3 = _getPath(part3); | |
37 if (part4 != null) part4 = _getPath(part4); | |
38 | 36 |
39 // TODO(nweiz): Don't use "?part" in path.dart. | 37 return path.join(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], |
40 if (part4 != null) { | 38 parts[6], parts[7]); |
41 return path.join(part1, part2, part3, part4); | |
42 } else if (part3 != null) { | |
43 return path.join(part1, part2, part3); | |
44 } else if (part2 != null) { | |
45 return path.join(part1, part2); | |
46 } else { | |
47 return path.join(part1); | |
48 } | |
49 } | 39 } |
50 | 40 |
51 /// Gets the basename, the file name without any leading directory path, for | 41 /// Gets the basename, the file name without any leading directory path, for |
52 /// [file], which can either be a [String], [File], or [Directory]. | 42 /// [file], which can either be a [String], [File], or [Directory]. |
53 String basename(file) => path.basename(_getPath(file)); | 43 String basename(file) => path.basename(_getPath(file)); |
54 | 44 |
55 // TODO(nweiz): move this into path.dart. | |
56 /// Gets the the leading directory path for [file], which can either be a | 45 /// Gets the the leading directory path for [file], which can either be a |
57 /// [String], [File], or [Directory]. | 46 /// [String], [File], or [Directory]. |
58 String dirname(file) { | 47 String dirname(file) => path.dirname(_getPath(file)); |
59 file = _sanitizePath(file); | |
60 | 48 |
61 int lastSlash = file.lastIndexOf('/', file.length); | 49 /// Splits [entry] into its individual components. |
62 if (lastSlash == -1) { | 50 List<String> splitPath(entry) => path.split(_getPath(entry)); |
63 return '.'; | |
64 } else { | |
65 return file.substring(0, lastSlash); | |
66 } | |
67 } | |
68 | |
69 // TODO(nweiz): move this into path.dart. | |
70 /// Splits [path] into its individual components. | |
71 List<String> splitPath(path) => _sanitizePath(path).split('/'); | |
72 | 51 |
73 /// Returns whether or not [entry] is nested somewhere within [dir]. This just | 52 /// Returns whether or not [entry] is nested somewhere within [dir]. This just |
74 /// performs a path comparison; it doesn't look at the actual filesystem. | 53 /// performs a path comparison; it doesn't look at the actual filesystem. |
75 bool isBeneath(entry, dir) { | 54 bool isBeneath(entry, dir) { |
76 var relative = relativeTo(entry, dir); | 55 var relative = relativeTo(entry, dir); |
77 return !path.isAbsolute(relative) && splitPath(relative)[0] != '..'; | 56 return !path.isAbsolute(relative) && splitPath(relative)[0] != '..'; |
78 } | 57 } |
79 | 58 |
80 // TODO(nweiz): move this into path.dart. | |
81 /// Returns the path to [target] from [base]. | 59 /// Returns the path to [target] from [base]. |
82 String relativeTo(target, base) => | 60 String relativeTo(target, base) => path.relative(target, from: base); |
83 new path.Builder(root: base).relative(target); | |
84 | 61 |
85 /** | 62 /** |
86 * Asynchronously determines if [path], which can be a [String] file path, a | 63 * Asynchronously determines if [path], which can be a [String] file path, a |
87 * [File], or a [Directory] exists on the file system. Returns a [Future] that | 64 * [File], or a [Directory] exists on the file system. Returns a [Future] that |
88 * completes with the result. | 65 * completes with the result. |
89 */ | 66 */ |
90 Future<bool> exists(path) { | 67 Future<bool> exists(path) { |
91 path = _getPath(path); | 68 path = _getPath(path); |
92 return Futures.wait([fileExists(path), dirExists(path)]).transform((results) { | 69 return Futures.wait([fileExists(path), dirExists(path)]).transform((results) { |
93 return results[0] || results[1]; | 70 return results[0] || results[1]; |
(...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 * or be a [File] or [Directory]. Allows working generically with "file-like" | 1037 * or be a [File] or [Directory]. Allows working generically with "file-like" |
1061 * objects. | 1038 * objects. |
1062 */ | 1039 */ |
1063 String _getPath(entry) { | 1040 String _getPath(entry) { |
1064 if (entry is String) return entry; | 1041 if (entry is String) return entry; |
1065 if (entry is File) return entry.name; | 1042 if (entry is File) return entry.name; |
1066 if (entry is Directory) return entry.path; | 1043 if (entry is Directory) return entry.path; |
1067 throw 'Entry $entry is not a supported type.'; | 1044 throw 'Entry $entry is not a supported type.'; |
1068 } | 1045 } |
1069 | 1046 |
1070 /// Gets the path string for [entry], normalizing backslashes to forward slashes | |
1071 /// on Windows. | |
1072 String _sanitizePath(entry) { | |
1073 entry = _getPath(entry); | |
1074 if (Platform.operatingSystem != 'windows') return entry; | |
1075 | |
1076 var split = _splitAbsolute(entry); | |
1077 if (split.first == null) return split.last.replaceAll('\\', '/'); | |
1078 | |
1079 // For absolute Windows paths, we don't want the prefix (either "\\" or e.g. | |
1080 // "C:\") to look like a normal path component, so we ensure that it only | |
1081 // contains backslashes. | |
1082 return '${split.first.replaceAll('/', '\\')}' | |
1083 '${split.last.replaceAll('\\', '/')}'; | |
1084 } | |
1085 | |
1086 // TODO(nweiz): Add something like this to path.dart. | |
1087 /// Splits [entry] into two components: the absolute path prefix and the | |
1088 /// remaining path. Takes into account Windows' quirky absolute paths syntaxes. | |
1089 Pair<String, String> _splitAbsolute(entry) { | |
1090 var path = _getPath(entry); | |
1091 | |
1092 if (Platform.operatingSystem != 'windows') { | |
1093 return !path.startsWith('/') ? new Pair(null, path) | |
1094 : new Pair('/', path.substring(1)); | |
1095 } | |
1096 | |
1097 // An absolute path on Windows is either UNC (two leading backslashes), | |
1098 // or a drive letter followed by a colon and a slash. | |
1099 var match = new RegExp(r'^(\\\\|[a-zA-Z]:[/\\])').firstMatch(path); | |
1100 return match == null ? new Pair(null, path) | |
1101 : new Pair(match.group(0), path.substring(match.end)); | |
1102 } | |
1103 | |
1104 /** | 1047 /** |
1105 * Gets a [Directory] for [entry], which can either already be one, or be a | 1048 * Gets a [Directory] for [entry], which can either already be one, or be a |
1106 * [String]. | 1049 * [String]. |
1107 */ | 1050 */ |
1108 Directory _getDirectory(entry) { | 1051 Directory _getDirectory(entry) { |
1109 if (entry is Directory) return entry; | 1052 if (entry is Directory) return entry; |
1110 return new Directory(entry); | 1053 return new Directory(entry); |
1111 } | 1054 } |
1112 | 1055 |
1113 /** | 1056 /** |
1114 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1057 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
1115 */ | 1058 */ |
1116 Uri _getUri(uri) { | 1059 Uri _getUri(uri) { |
1117 if (uri is Uri) return uri; | 1060 if (uri is Uri) return uri; |
1118 return new Uri.fromString(uri); | 1061 return new Uri.fromString(uri); |
1119 } | 1062 } |
OLD | NEW |