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 /** | 5 /** |
| 6 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
| 7 */ | 7 */ |
| 8 #library('pub_io'); | 8 #library('pub_io'); |
| 9 | 9 |
| 10 #import('dart:io'); | 10 #import('dart:io'); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 return results[0] || results[1]; | 84 return results[0] || results[1]; |
| 85 }); | 85 }); |
| 86 } | 86 } |
| 87 | 87 |
| 88 /** | 88 /** |
| 89 * Asynchronously determines if [file], which can be a [String] file path or a | 89 * Asynchronously determines if [file], which can be a [String] file path or a |
| 90 * [File], exists on the file system. Returns a [Future] that completes with | 90 * [File], exists on the file system. Returns a [Future] that completes with |
| 91 * the result. | 91 * the result. |
| 92 */ | 92 */ |
| 93 Future<bool> fileExists(file) { | 93 Future<bool> fileExists(file) { |
| 94 // TODO(nweiz): Currently File#exists will not detect the existence of | |
| 95 // symlinks. Issue 2765 | |
| 96 return runProcess('stat', [_getPath(file)]). | |
| 97 transform((result) => result.exitCode == 0); | |
| 98 | |
| 99 // Real code: | |
| 100 /* | |
| 101 final completer = new Completer<bool>(); | 94 final completer = new Completer<bool>(); |
| 102 | 95 |
| 103 file = new File(_getPath(file)); | 96 file = new File(_getPath(file)); |
| 104 file.onError = (error) => completer.completeException(error); | 97 file.onError = (error) => completer.completeException(error); |
| 105 file.exists((exists) => completer.complete(exists)); | 98 file.exists((exists) => completer.complete(exists)); |
| 106 | 99 |
| 107 return completer.future; | 100 return completer.future; |
| 108 */ | |
| 109 } | 101 } |
| 110 | 102 |
| 111 /** | 103 /** |
| 112 * Reads the contents of the text file [file], which can either be a [String] or | 104 * Reads the contents of the text file [file], which can either be a [String] or |
| 113 * a [File]. | 105 * a [File]. |
| 114 */ | 106 */ |
| 115 Future<String> readTextFile(file) { | 107 Future<String> readTextFile(file) { |
| 116 file = new File(_getPath(file)); | 108 file = new File(_getPath(file)); |
| 117 final completer = new Completer<String>(); | 109 final completer = new Completer<String>(); |
| 118 file.onError = (error) => completer.completeException(error); | 110 file.onError = (error) => completer.completeException(error); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 dir.createTemp(() => completer.complete(dir)); | 185 dir.createTemp(() => completer.complete(dir)); |
| 194 | 186 |
| 195 return completer.future; | 187 return completer.future; |
| 196 } | 188 } |
| 197 | 189 |
| 198 /** | 190 /** |
| 199 * Asynchronously recursively deletes [dir], which can be a [String] or a | 191 * Asynchronously recursively deletes [dir], which can be a [String] or a |
| 200 * [Directory]. Returns a [Future] that completes when the deletion is done. | 192 * [Directory]. Returns a [Future] that completes when the deletion is done. |
| 201 */ | 193 */ |
| 202 Future<Directory> deleteDir(dir) { | 194 Future<Directory> deleteDir(dir) { |
| 203 // TODO(rnystrom): Hack! Temporary! Right now, dart:io's Directory delete | |
| 204 // method can't handle directories with symlinks, which is exactly what pub | |
| 205 // creates and deletes. Instead, we'll just shell out to 'rm'. Remove this | |
| 206 // when dartbug.com/2646 is fixed. | |
| 207 dir = _getDirectory(dir); | |
| 208 | |
| 209 // Sanity check! | |
| 210 if (dir.path == '/' || | |
| 211 dir.path == '.' || | |
| 212 dir.path == '..' || | |
| 213 dir.path == '~' || | |
| 214 dir.path == '*') throw "(O.o) I don't think you want to do that!"; | |
| 215 | |
| 216 return runProcess('rm', ['-rf', dir.path]).transform((_) => dir); | |
| 217 // End hack! | |
| 218 | |
| 219 // Real code: | |
| 220 /* | |
| 221 final completer = new Completer<Directory>(); | 195 final completer = new Completer<Directory>(); |
| 222 dir = _getDirectory(dir); | 196 dir = _getDirectory(dir); |
| 223 dir.onError = (error) => completer.completeException(error); | 197 dir.onError = (error) => completer.completeException(error); |
| 224 dir.deleteRecursively(() => completer.complete(dir)); | 198 dir.deleteRecursively(() => completer.complete(dir)); |
| 225 | 199 |
| 226 return completer.future; | 200 return completer.future; |
| 227 */ | |
| 228 } | 201 } |
| 229 | 202 |
| 230 /** | 203 /** |
| 231 * Asynchronously lists the contents of [dir], which can be a [String] directory | 204 * Asynchronously lists the contents of [dir], which can be a [String] directory |
| 232 * path or a [Directory]. If [recursive] is `true`, lists subdirectory contents | 205 * path or a [Directory]. If [recursive] is `true`, lists subdirectory contents |
| 233 * (defaults to `false`). If [includeSpecialFiles] is `true`, includes | 206 * (defaults to `false`). If [includeSpecialFiles] is `true`, includes |
| 234 * hidden `.DS_Store` files (defaults to `false`, other hidden files may be | 207 * hidden `.DS_Store` files (defaults to `false`, other hidden files may be |
| 235 * omitted later). | 208 * omitted later). |
| 236 */ | 209 */ |
| 237 Future<List<String>> listDir(dir, | 210 Future<List<String>> listDir(dir, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 * the current working directory, returns its full canonicalized path. | 287 * the current working directory, returns its full canonicalized path. |
| 315 */ | 288 */ |
| 316 // TODO(rnystrom): Should this be async? | 289 // TODO(rnystrom): Should this be async? |
| 317 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 290 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 318 | 291 |
| 319 /** | 292 /** |
| 320 * Spawns and runs the process located at [executable], passing in [args]. | 293 * Spawns and runs the process located at [executable], passing in [args]. |
| 321 * Returns a [Future] that will complete the results of the process after it | 294 * Returns a [Future] that will complete the results of the process after it |
| 322 * has ended. | 295 * has ended. |
| 323 */ | 296 */ |
| 324 Future<ProcessResult> runProcess(String executable, List<String> args, | 297 Future<ProcessResult> runProcess(String executable, List<String> args, |
|
Mads Ager (google)
2012/05/09 10:06:23
We should see if we can use:
new Process.run(...,
Bob Nystrom
2012/05/09 15:49:11
Good call. I wrote this before Process.run was fin
| |
| 325 [String workingDir]) { | 298 [String workingDir]) { |
| 326 int exitCode; | 299 int exitCode; |
| 327 | 300 |
| 328 final options = new ProcessOptions(); | 301 final options = new ProcessOptions(); |
| 329 if (workingDir != null) options.workingDirectory = workingDir; | 302 if (workingDir != null) options.workingDirectory = workingDir; |
| 330 | 303 |
| 331 final process = new Process.start(executable, args, options); | 304 final process = new Process.start(executable, args, options); |
| 332 | 305 |
| 333 final outStream = new StringInputStream(process.stdout); | 306 final outStream = new StringInputStream(process.stdout); |
| 334 final processStdout = <String>[]; | 307 final processStdout = <String>[]; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 } | 363 } |
| 391 | 364 |
| 392 /** | 365 /** |
| 393 * Gets a [Directory] for [entry], which can either already be one, or be a | 366 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 394 * [String]. | 367 * [String]. |
| 395 */ | 368 */ |
| 396 Directory _getDirectory(entry) { | 369 Directory _getDirectory(entry) { |
| 397 if (entry is Directory) return entry; | 370 if (entry is Directory) return entry; |
| 398 return new Directory(entry); | 371 return new Directory(entry); |
| 399 } | 372 } |
| OLD | NEW |