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 11 matching lines...) Expand all Loading... |
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 /// Returns whether or not [entry] is nested somewhere within [dir]. This just | 25 /// Returns whether or not [entry] is nested somewhere within [dir]. This just |
26 /// performs a path comparison; it doesn't look at the actual filesystem. | 26 /// performs a path comparison; it doesn't look at the actual filesystem. |
27 bool isBeneath(String entry, String dir) { | 27 bool isBeneath(String entry, String dir) { |
28 var relative = path.relative(entry, from: dir); | 28 var relative = path.relative(entry, from: dir); |
29 return !path.isAbsolute(relative) && path.split(relative)[0] != '..'; | 29 return !path.isAbsolute(relative) && path.split(relative)[0] != '..'; |
30 } | 30 } |
31 | 31 |
32 /// Determines if a file or directory at [path] exists. | 32 /// Determines if a file or directory exists at [path]. |
33 bool entryExists(String path) => fileExists(path) || dirExists(path); | 33 bool entryExists(String path) => dirExists(path) || fileExists(path); |
34 | 34 |
35 /// Determines if [file] exists on the file system. | 35 /// Determines if [file] exists on the file system. Will also return `true` if |
36 bool fileExists(String file) => new File(file).existsSync(); | 36 /// [file] points to a symlink, even a directory symlink. |
| 37 bool fileExists(String file) => |
| 38 new File(file).existsSync() || new Link(file).existsSync(); |
37 | 39 |
38 /// Reads the contents of the text file [file]. | 40 /// Reads the contents of the text file [file]. |
39 String readTextFile(String file) => | 41 String readTextFile(String file) => |
40 new File(file).readAsStringSync(Encoding.UTF_8); | 42 new File(file).readAsStringSync(Encoding.UTF_8); |
41 | 43 |
42 /// Reads the contents of the binary file [file]. | 44 /// Reads the contents of the binary file [file]. |
43 List<int> readBinaryFile(String file) { | 45 List<int> readBinaryFile(String file) { |
44 log.io("Reading binary file $file."); | 46 log.io("Reading binary file $file."); |
45 var contents = new File(file).readAsBytesSync(); | 47 var contents = new File(file).readAsBytesSync(); |
46 log.io("Read ${contents.length} bytes from $file."); | 48 log.io("Read ${contents.length} bytes from $file."); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 bool dirExists(String dir) => new Directory(dir).existsSync(); | 207 bool dirExists(String dir) => new Directory(dir).existsSync(); |
206 | 208 |
207 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a | 209 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a |
208 /// new empty directory will be created. Returns a [Future] that completes when | 210 /// new empty directory will be created. Returns a [Future] that completes when |
209 /// the new clean directory is created. | 211 /// the new clean directory is created. |
210 Future<String> cleanDir(String dir) { | 212 Future<String> cleanDir(String dir) { |
211 return defer(() { | 213 return defer(() { |
212 if (dirExists(dir)) { | 214 if (dirExists(dir)) { |
213 // Delete it first. | 215 // Delete it first. |
214 return deleteDir(dir).then((_) => createDir(dir)); | 216 return deleteDir(dir).then((_) => createDir(dir)); |
215 } else { | 217 } |
216 // Just create it. | 218 |
| 219 if (fileExists(dir)) { |
| 220 // If there is a non-directory there (file or symlink), delete it. |
| 221 deleteFile(dir); |
217 return createDir(dir); | 222 return createDir(dir); |
218 } | 223 } |
| 224 |
| 225 // Just create it. |
| 226 return createDir(dir); |
219 }); | 227 }); |
220 } | 228 } |
221 | 229 |
222 /// Renames (i.e. moves) the directory [from] to [to]. Returns a [Future] with | 230 /// Renames (i.e. moves) the directory [from] to [to]. Returns a [Future] with |
223 /// the destination directory. | 231 /// the destination directory. |
224 Future<String> renameDir(String from, String to) { | 232 Future<String> renameDir(String from, String to) { |
225 log.io("Renaming directory $from to $to."); | 233 log.io("Renaming directory $from to $to."); |
226 | 234 |
227 return _attemptRetryable(() => new Directory(from).rename(to)).then((dir) { | 235 return _attemptRetryable(() => new Directory(from).rename(to)).then((dir) { |
228 log.fine("Renamed directory $from to $to."); | 236 log.fine("Renamed directory $from to $to."); |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 810 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
803 | 811 |
804 bool get success => exitCode == 0; | 812 bool get success => exitCode == 0; |
805 } | 813 } |
806 | 814 |
807 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 815 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
808 Uri _getUri(uri) { | 816 Uri _getUri(uri) { |
809 if (uri is Uri) return uri; | 817 if (uri is Uri) return uri; |
810 return Uri.parse(uri); | 818 return Uri.parse(uri); |
811 } | 819 } |
OLD | NEW |