Chromium Code Reviews| 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) { | |
|
nweiz
2013/01/31 22:38:53
I'm not a big fan of this intermediate duplication
Bob Nystrom
2013/02/01 16:53:29
Yup. Once that patch lands, this will be gone.
| |
| 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 /// Creates [file] (which can either be a [String] or a [File]), and writes | 106 /// Creates [file] (which can either be a [String] or a [File]), and writes |
| 89 /// [contents] to it. Completes when the file is written and closed. | 107 /// [contents] to it. Completes when the file is written and closed. |
| 90 /// | 108 /// |
| 91 /// If [dontLogContents] is true, the contents of the file will never be logged. | 109 /// If [dontLogContents] is true, the contents of the file will never be logged. |
| 92 Future<File> writeTextFile(file, String contents, {dontLogContents: false}) { | 110 Future<File> writeTextFile(file, String contents, {dontLogContents: false}) { |
| 93 var path = _getPath(file); | 111 var path = _getPath(file); |
| 94 file = new File(path); | 112 file = new File(path); |
| 95 | 113 |
| 96 // Sanity check: don't spew a huge file. | 114 // Sanity check: don't spew a huge file. |
| 97 log.io("Writing ${contents.length} characters to text file $path."); | 115 log.io("Writing ${contents.length} characters to text file $path."); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 return Future.wait(children).then((childContents) { | 300 return Future.wait(children).then((childContents) { |
| 283 contents.addAll(flatten(childContents)); | 301 contents.addAll(flatten(childContents)); |
| 284 return contents; | 302 return contents; |
| 285 }); | 303 }); |
| 286 }); | 304 }); |
| 287 } | 305 } |
| 288 | 306 |
| 289 return doList(_getDirectory(dir), new Set<String>()); | 307 return doList(_getDirectory(dir), new Set<String>()); |
| 290 } | 308 } |
| 291 | 309 |
| 310 // TODO(rnystrom): Migrate everything over to the sync one and get rid of this. | |
| 292 /// Asynchronously determines if [dir], which can be a [String] directory path | 311 /// Asynchronously determines if [dir], which can be a [String] directory path |
| 293 /// or a [Directory], exists on the file system. Returns a [Future] that | 312 /// or a [Directory], exists on the file system. Returns a [Future] that |
| 294 /// completes with the result. | 313 /// completes with the result. |
| 295 Future<bool> dirExists(dir) { | 314 Future<bool> dirExists(dir) { |
| 296 dir = _getDirectory(dir); | 315 dir = _getDirectory(dir); |
| 297 return log.ioAsync("Seeing if directory ${dir.path} exists.", | 316 return log.ioAsync("Seeing if directory ${dir.path} exists.", |
| 298 dir.exists(), | 317 dir.exists(), |
| 299 (exists) => "Directory ${dir.path} " | 318 (exists) => "Directory ${dir.path} " |
| 300 "${exists ? 'exists' : 'does not exist'}."); | 319 "${exists ? 'exists' : 'does not exist'}."); |
| 301 } | 320 } |
| 302 | 321 |
| 322 /// Determines if [dir], which can be a [String] directory path or a | |
| 323 /// [Directory], exists on the file system. Returns a [Future] that completes | |
| 324 /// with the result. | |
| 325 bool dirExistsSync(dir) => _getDirectory(dir).existsSync(); | |
| 326 | |
| 303 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a | 327 /// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a |
| 304 /// new empty directory will be created. Returns a [Future] that completes when | 328 /// new empty directory will be created. Returns a [Future] that completes when |
| 305 /// the new clean directory is created. | 329 /// the new clean directory is created. |
| 306 Future<Directory> cleanDir(dir) { | 330 Future<Directory> cleanDir(dir) { |
| 307 return dirExists(dir).then((exists) { | 331 return dirExists(dir).then((exists) { |
| 308 if (exists) { | 332 if (exists) { |
| 309 // Delete it first. | 333 // Delete it first. |
| 310 return deleteDir(dir).then((_) => createDir(dir)); | 334 return deleteDir(dir).then((_) => createDir(dir)); |
| 311 } else { | 335 } else { |
| 312 // Just create it. | 336 // Just create it. |
| (...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 966 Directory _getDirectory(entry) { | 990 Directory _getDirectory(entry) { |
| 967 if (entry is Directory) return entry; | 991 if (entry is Directory) return entry; |
| 968 return new Directory(entry); | 992 return new Directory(entry); |
| 969 } | 993 } |
| 970 | 994 |
| 971 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 995 /// Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 972 Uri _getUri(uri) { | 996 Uri _getUri(uri) { |
| 973 if (uri is Uri) return uri; | 997 if (uri is Uri) return uri; |
| 974 return Uri.parse(uri); | 998 return Uri.parse(uri); |
| 975 } | 999 } |
| OLD | NEW |