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."); |
47 return contents; | 49 return contents; |
48 } | 50 } |
49 | 51 |
50 /// Creates [file] and writes [contents] to it. | 52 /// Creates [file] and writes [contents] to it. |
51 /// | 53 /// |
52 /// If [dontLogContents] is true, the contents of the file will never be logged. | 54 /// If [dontLogContents] is true, the contents of the file will never be logged. |
53 String writeTextFile(String file, String contents, {dontLogContents: false}) { | 55 String writeTextFile(String file, String contents, {dontLogContents: false}) { |
54 // Sanity check: don't spew a huge file. | 56 // Sanity check: don't spew a huge file. |
55 log.io("Writing ${contents.length} characters to text file $file."); | 57 log.io("Writing ${contents.length} characters to text file $file."); |
56 if (!dontLogContents && contents.length < 1024 * 1024) { | 58 if (!dontLogContents && contents.length < 1024 * 1024) { |
57 log.fine("Contents:\n$contents"); | 59 log.fine("Contents:\n$contents"); |
58 } | 60 } |
59 | 61 |
60 new File(file).writeAsStringSync(contents); | 62 new File(file).writeAsStringSync(contents); |
61 return file; | 63 return file; |
62 } | 64 } |
63 | 65 |
64 /// Deletes [file]. | 66 /// Deletes [file]. |
65 void deleteFile(String file) { | 67 void deleteFile(String file) { |
66 new File(file).deleteSync(); | 68 if (new Link(file).existsSync()) { |
69 // If it's a (possibly broken) symlink, handle that. | |
70 return new Link(file).deleteSync(); | |
71 } else { | |
72 // Assume it's a normal file. | |
73 new File(file).deleteSync(); | |
nweiz
2013/03/15 21:00:17
This will actually work on symlinks.
Bob Nystrom
2013/03/15 22:06:56
Done.
| |
74 } | |
67 } | 75 } |
68 | 76 |
69 /// Creates [file] and writes [contents] to it. | 77 /// Creates [file] and writes [contents] to it. |
70 String writeBinaryFile(String file, List<int> contents) { | 78 String writeBinaryFile(String file, List<int> contents) { |
71 log.io("Writing ${contents.length} bytes to binary file $file."); | 79 log.io("Writing ${contents.length} bytes to binary file $file."); |
72 new File(file).openSync(FileMode.WRITE) | 80 new File(file).openSync(FileMode.WRITE) |
73 ..writeListSync(contents, 0, contents.length) | 81 ..writeListSync(contents, 0, contents.length) |
74 ..closeSync(); | 82 ..closeSync(); |
75 log.fine("Wrote text file $file."); | 83 log.fine("Wrote text file $file."); |
76 return file; | 84 return file; |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 bool dirExists(String dir) => new Directory(dir).existsSync(); | 213 bool dirExists(String dir) => new Directory(dir).existsSync(); |
206 | 214 |
207 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a | 215 /// "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 | 216 /// new empty directory will be created. Returns a [Future] that completes when |
209 /// the new clean directory is created. | 217 /// the new clean directory is created. |
210 Future<String> cleanDir(String dir) { | 218 Future<String> cleanDir(String dir) { |
211 return defer(() { | 219 return defer(() { |
212 if (dirExists(dir)) { | 220 if (dirExists(dir)) { |
213 // Delete it first. | 221 // Delete it first. |
214 return deleteDir(dir).then((_) => createDir(dir)); | 222 return deleteDir(dir).then((_) => createDir(dir)); |
223 } if (fileExists(dir)) { | |
nweiz
2013/03/15 21:00:17
"else if"
Bob Nystrom
2013/03/15 22:06:56
Since those cases return anyway, reformatted and g
| |
224 // If there is a non-directory there (file or symlink), delete it. | |
225 deleteFile(dir); | |
226 return createDir(dir); | |
215 } else { | 227 } else { |
216 // Just create it. | 228 // Just create it. |
217 return createDir(dir); | 229 return createDir(dir); |
218 } | 230 } |
219 }); | 231 }); |
220 } | 232 } |
221 | 233 |
222 /// Renames (i.e. moves) the directory [from] to [to]. Returns a [Future] with | 234 /// Renames (i.e. moves) the directory [from] to [to]. Returns a [Future] with |
223 /// the destination directory. | 235 /// the destination directory. |
224 Future<String> renameDir(String from, String to) { | 236 Future<String> renameDir(String from, String to) { |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
802 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 814 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
803 | 815 |
804 bool get success => exitCode == 0; | 816 bool get success => exitCode == 0; |
805 } | 817 } |
806 | 818 |
807 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 819 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
808 Uri _getUri(uri) { | 820 Uri _getUri(uri) { |
809 if (uri is Uri) return uri; | 821 if (uri is Uri) return uri; |
810 return Uri.parse(uri); | 822 return Uri.parse(uri); |
811 } | 823 } |
OLD | NEW |