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 23 matching lines...) Expand all Loading... |
34 stderr.writeString('\n'); | 34 stderr.writeString('\n'); |
35 } | 35 } |
36 | 36 |
37 | 37 |
38 /** | 38 /** |
39 * Joins a number of path string parts into a single path. Handles | 39 * Joins a number of path string parts into a single path. Handles |
40 * platform-specific path separators. Parts can be [String], [Directory], or | 40 * platform-specific path separators. Parts can be [String], [Directory], or |
41 * [File] objects. | 41 * [File] objects. |
42 */ | 42 */ |
43 String join(part1, [part2, part3, part4]) { | 43 String join(part1, [part2, part3, part4]) { |
44 final parts = _sanitizePath(part1).split('/'); | 44 final parts = sanitizePath(part1).split('/'); |
45 | 45 |
46 for (final part in [part2, part3, part4]) { | 46 for (final part in [part2, part3, part4]) { |
47 if (part == null) continue; | 47 if (part == null) continue; |
48 | 48 |
49 for (final piece in _getPath(part).split('/')) { | 49 for (final piece in _getPath(part).split('/')) { |
50 if (piece == '..' && parts.length > 0 && | 50 if (piece == '..' && parts.length > 0 && |
51 parts.last != '.' && parts.last != '..') { | 51 parts.last != '.' && parts.last != '..') { |
52 parts.removeLast(); | 52 parts.removeLast(); |
53 } else if (piece != '') { | 53 } else if (piece != '') { |
54 if (parts.length > 0 && parts.last == '.') { | 54 if (parts.length > 0 && parts.last == '.') { |
55 parts.removeLast(); | 55 parts.removeLast(); |
56 } | 56 } |
57 parts.add(piece); | 57 parts.add(piece); |
58 } | 58 } |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 return Strings.join(parts, Platform.pathSeparator); | 62 return Strings.join(parts, Platform.pathSeparator); |
63 } | 63 } |
64 | 64 |
65 /// Splits [path] into its individual components. | 65 /// Splits [path] into its individual components. |
66 List<String> splitPath(path) => _sanitizePath(path).split('/'); | 66 List<String> splitPath(path) => sanitizePath(path).split('/'); |
67 | 67 |
68 /** | 68 /** |
69 * Gets the basename, the file name without any leading directory path, for | 69 * Gets the basename, the file name without any leading directory path, for |
70 * [file], which can either be a [String], [File], or [Directory]. | 70 * [file], which can either be a [String], [File], or [Directory]. |
71 */ | 71 */ |
72 // TODO(rnystrom): Copied from file_system (so that we don't have to add | 72 // TODO(rnystrom): Copied from file_system (so that we don't have to add |
73 // file_system to the SDK). Should unify. | 73 // file_system to the SDK). Should unify. |
74 String basename(file) { | 74 String basename(file) { |
75 file = _sanitizePath(file); | 75 file = sanitizePath(file); |
76 | 76 |
77 int lastSlash = file.lastIndexOf('/', file.length); | 77 int lastSlash = file.lastIndexOf('/', file.length); |
78 if (lastSlash == -1) { | 78 if (lastSlash == -1) { |
79 return file; | 79 return file; |
80 } else { | 80 } else { |
81 return file.substring(lastSlash + 1); | 81 return file.substring(lastSlash + 1); |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 /** | 85 /** |
86 * Gets the the leading directory path for [file], which can either be a | 86 * Gets the the leading directory path for [file], which can either be a |
87 * [String], [File], or [Directory]. | 87 * [String], [File], or [Directory]. |
88 */ | 88 */ |
89 // TODO(nweiz): Copied from file_system (so that we don't have to add | 89 // TODO(nweiz): Copied from file_system (so that we don't have to add |
90 // file_system to the SDK). Should unify. | 90 // file_system to the SDK). Should unify. |
91 String dirname(file) { | 91 String dirname(file) { |
92 file = _sanitizePath(file); | 92 file = sanitizePath(file); |
93 | 93 |
94 int lastSlash = file.lastIndexOf('/', file.length); | 94 int lastSlash = file.lastIndexOf('/', file.length); |
95 if (lastSlash == -1) { | 95 if (lastSlash == -1) { |
96 return '.'; | 96 return '.'; |
97 } else { | 97 } else { |
98 return file.substring(0, lastSlash); | 98 return file.substring(0, lastSlash); |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 /// Returns whether or not [entry] is nested somewhere within [dir]. This just | 102 /// Returns whether or not [entry] is nested somewhere within [dir]. This just |
103 /// performs a path comparison; it doesn't look at the actual filesystem. | 103 /// performs a path comparison; it doesn't look at the actual filesystem. |
104 bool isBeneath(entry, dir) => | 104 bool isBeneath(entry, dir) => |
105 _sanitizePath(entry).startsWith('${_sanitizePath(dir)}/'); | 105 sanitizePath(entry).startsWith('${sanitizePath(dir)}/'); |
106 | 106 |
107 /** | 107 /** |
108 * Asynchronously determines if [path], which can be a [String] file path, a | 108 * Asynchronously determines if [path], which can be a [String] file path, a |
109 * [File], or a [Directory] exists on the file system. Returns a [Future] that | 109 * [File], or a [Directory] exists on the file system. Returns a [Future] that |
110 * completes with the result. | 110 * completes with the result. |
111 */ | 111 */ |
112 Future<bool> exists(path) { | 112 Future<bool> exists(path) { |
113 path = _getPath(path); | 113 path = _getPath(path); |
114 return Futures.wait([fileExists(path), dirExists(path)]).transform((results) { | 114 return Futures.wait([fileExists(path), dirExists(path)]).transform((results) { |
115 return results[0] || results[1]; | 115 return results[0] || results[1]; |
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 * or be a [File] or [Directory]. Allows working generically with "file-like" | 987 * or be a [File] or [Directory]. Allows working generically with "file-like" |
988 * objects. | 988 * objects. |
989 */ | 989 */ |
990 String _getPath(entry) { | 990 String _getPath(entry) { |
991 if (entry is String) return entry; | 991 if (entry is String) return entry; |
992 if (entry is File) return entry.name; | 992 if (entry is File) return entry.name; |
993 if (entry is Directory) return entry.path; | 993 if (entry is Directory) return entry.path; |
994 throw 'Entry $entry is not a supported type.'; | 994 throw 'Entry $entry is not a supported type.'; |
995 } | 995 } |
996 | 996 |
997 /// Gets the path string for [entry] as in [_getPath], but normalizes | 997 /// Gets the path string for [entry], normalizing backslashes to forward slashes |
998 /// backslashes to forward slashes on Windows. | 998 /// on Windows. |
999 String _sanitizePath(entry) { | 999 String sanitizePath(entry) { |
1000 entry = _getPath(entry); | 1000 entry = _getPath(entry); |
1001 if (Platform.operatingSystem != 'windows') return entry; | 1001 if (Platform.operatingSystem != 'windows') return entry; |
1002 | 1002 |
1003 var split = _splitAbsolute(entry); | 1003 var split = _splitAbsolute(entry); |
1004 if (split.first == null) return split.last.replaceAll('\\', '/'); | 1004 if (split.first == null) return split.last.replaceAll('\\', '/'); |
1005 | 1005 |
1006 // For absolute Windows paths, we don't want the prefix (either "\\" or e.g. | 1006 // For absolute Windows paths, we don't want the prefix (either "\\" or e.g. |
1007 // "C:\") to look like a normal path component, so we ensure that it only | 1007 // "C:\") to look like a normal path component, so we ensure that it only |
1008 // contains backslashes. | 1008 // contains backslashes. |
1009 return '${split.first.replaceAll('/', '\\')}' | 1009 return '${split.first.replaceAll('/', '\\')}' |
1010 '${split.last.replaceAll('\\', '/')}'; | 1010 '${split.last.replaceAll('\\', '/')}'; |
1011 } | 1011 } |
1012 | 1012 |
1013 /** | 1013 /** |
1014 * Gets a [Directory] for [entry], which can either already be one, or be a | 1014 * Gets a [Directory] for [entry], which can either already be one, or be a |
1015 * [String]. | 1015 * [String]. |
1016 */ | 1016 */ |
1017 Directory _getDirectory(entry) { | 1017 Directory _getDirectory(entry) { |
1018 if (entry is Directory) return entry; | 1018 if (entry is Directory) return entry; |
1019 return new Directory(entry); | 1019 return new Directory(entry); |
1020 } | 1020 } |
1021 | 1021 |
1022 /** | 1022 /** |
1023 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1023 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
1024 */ | 1024 */ |
1025 Uri _getUri(uri) { | 1025 Uri _getUri(uri) { |
1026 if (uri is Uri) return uri; | 1026 if (uri is Uri) return uri; |
1027 return new Uri.fromString(uri); | 1027 return new Uri.fromString(uri); |
1028 } | 1028 } |
OLD | NEW |