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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 /// Asynchronously determines if [file], which can be a [String] file path or a | 62 /// Asynchronously determines if [file], which can be a [String] file path or a |
63 /// [File], exists on the file system. Returns a [Future] that completes with | 63 /// [File], exists on the file system. Returns a [Future] that completes with |
64 /// the result. | 64 /// the result. |
65 Future<bool> fileExists(file) { | 65 Future<bool> fileExists(file) { |
66 var path = _getPath(file); | 66 var path = _getPath(file); |
67 return log.ioAsync("Seeing if file $path exists.", | 67 return log.ioAsync("Seeing if file $path exists.", |
68 new File(path).exists(), | 68 new File(path).exists(), |
69 (exists) => "File $path ${exists ? 'exists' : 'does not exist'}."); | 69 (exists) => "File $path ${exists ? 'exists' : 'does not exist'}."); |
70 } | 70 } |
71 | 71 |
| 72 // TODO(rnystrom): Get rid of this and only use sync. |
72 /// Reads the contents of the text file [file], which can either be a [String] | 73 /// Reads the contents of the text file [file], which can either be a [String] |
73 /// or a [File]. | 74 /// or a [File]. |
74 Future<String> readTextFile(file) { | 75 Future<String> readTextFile(file) { |
75 var path = _getPath(file); | 76 var path = _getPath(file); |
76 return log.ioAsync("Reading text file $path.", | 77 return log.ioAsync("Reading text file $path.", |
77 new File(path).readAsString(Encoding.UTF_8), | 78 new File(path).readAsString(Encoding.UTF_8), |
78 (contents) { | 79 (contents) { |
79 // Sanity check: don't spew a huge file. | 80 // Sanity check: don't spew a huge file. |
80 if (contents.length < 1024 * 1024) { | 81 if (contents.length < 1024 * 1024) { |
81 return "Read $path. Contents:\n$contents"; | 82 return "Read $path. Contents:\n$contents"; |
82 } else { | 83 } else { |
83 return "Read ${contents.length} characters from $path."; | 84 return "Read ${contents.length} characters from $path."; |
84 } | 85 } |
85 }); | 86 }); |
86 } | 87 } |
87 | 88 |
| 89 /// Reads the contents of the text file [file], which can either be a [String] |
| 90 /// or a [File]. |
| 91 String readTextFileSync(file) { |
| 92 var path = _getPath(file); |
| 93 log.io("Reading text file $path."); |
| 94 var contents = new File(path).readAsStringSync(Encoding.UTF_8); |
| 95 |
| 96 // Sanity check: don't spew a huge file. |
| 97 if (contents.length < 1024 * 1024) { |
| 98 log.fine("Read $path. Contents:\n$contents"); |
| 99 } else { |
| 100 log.fine("Read ${contents.length} characters from $path."); |
| 101 } |
| 102 |
| 103 return contents; |
| 104 } |
| 105 |
88 /// Reads the contents of the binary file [file], which can either be a [String] | 106 /// Reads the contents of the binary file [file], which can either be a [String] |
89 /// or a [File]. | 107 /// or a [File]. |
90 List<int> readBinaryFile(file) { | 108 List<int> readBinaryFile(file) { |
91 var path = _getPath(file); | 109 var path = _getPath(file); |
92 log.io("Reading binary file $path."); | 110 log.io("Reading binary file $path."); |
93 var contents = new File(path).readAsBytesSync(); | 111 var contents = new File(path).readAsBytesSync(); |
94 log.io("Read ${contents.length} bytes from $path."); | 112 log.io("Read ${contents.length} bytes from $path."); |
95 return contents; | 113 return contents; |
96 } | 114 } |
97 | 115 |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 return Future.wait(children).then((childContents) { | 324 return Future.wait(children).then((childContents) { |
307 contents.addAll(flatten(childContents)); | 325 contents.addAll(flatten(childContents)); |
308 return contents; | 326 return contents; |
309 }); | 327 }); |
310 }); | 328 }); |
311 } | 329 } |
312 | 330 |
313 return doList(_getDirectory(dir), new Set<String>()); | 331 return doList(_getDirectory(dir), new Set<String>()); |
314 } | 332 } |
315 | 333 |
| 334 // TODO(rnystrom): Migrate everything over to the sync one and get rid of this. |
316 /// Asynchronously determines if [dir], which can be a [String] directory path | 335 /// Asynchronously determines if [dir], which can be a [String] directory path |
317 /// or a [Directory], exists on the file system. Returns a [Future] that | 336 /// or a [Directory], exists on the file system. Returns a [Future] that |
318 /// completes with the result. | 337 /// completes with the result. |
319 Future<bool> dirExists(dir) { | 338 Future<bool> dirExists(dir) { |
320 dir = _getDirectory(dir); | 339 dir = _getDirectory(dir); |
321 return log.ioAsync("Seeing if directory ${dir.path} exists.", | 340 return log.ioAsync("Seeing if directory ${dir.path} exists.", |
322 dir.exists(), | 341 dir.exists(), |
323 (exists) => "Directory ${dir.path} " | 342 (exists) => "Directory ${dir.path} " |
324 "${exists ? 'exists' : 'does not exist'}."); | 343 "${exists ? 'exists' : 'does not exist'}."); |
325 } | 344 } |
326 | 345 |
| 346 /// Determines if [dir], which can be a [String] directory path or a |
| 347 /// [Directory], exists on the file system. Returns a [Future] that completes |
| 348 /// with the result. |
| 349 bool dirExistsSync(dir) => _getDirectory(dir).existsSync(); |
| 350 |
327 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a | 351 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a |
328 /// new empty directory will be created. Returns a [Future] that completes when | 352 /// new empty directory will be created. Returns a [Future] that completes when |
329 /// the new clean directory is created. | 353 /// the new clean directory is created. |
330 Future<Directory> cleanDir(dir) { | 354 Future<Directory> cleanDir(dir) { |
331 return dirExists(dir).then((exists) { | 355 return dirExists(dir).then((exists) { |
332 if (exists) { | 356 if (exists) { |
333 // Delete it first. | 357 // Delete it first. |
334 return deleteDir(dir).then((_) => createDir(dir)); | 358 return deleteDir(dir).then((_) => createDir(dir)); |
335 } else { | 359 } else { |
336 // Just create it. | 360 // Just create it. |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 Directory _getDirectory(entry) { | 1014 Directory _getDirectory(entry) { |
991 if (entry is Directory) return entry; | 1015 if (entry is Directory) return entry; |
992 return new Directory(entry); | 1016 return new Directory(entry); |
993 } | 1017 } |
994 | 1018 |
995 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1019 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
996 Uri _getUri(uri) { | 1020 Uri _getUri(uri) { |
997 if (uri is Uri) return uri; | 1021 if (uri is Uri) return uri; |
998 return Uri.parse(uri); | 1022 return Uri.parse(uri); |
999 } | 1023 } |
OLD | NEW |