Chromium Code Reviews| Index: utils/pub/io.dart |
| diff --git a/utils/pub/io.dart b/utils/pub/io.dart |
| index 5c505c287c8a81524d8d56279be089524cdd15f7..4f6d39d4f03748e082572fb031dacaf54f2d6df9 100644 |
| --- a/utils/pub/io.dart |
| +++ b/utils/pub/io.dart |
| @@ -50,11 +50,38 @@ String basename(file) { |
| } |
| /** |
| + * Gets the the leading directory path for [file], which can either be a |
| + * [String], [File], or [Directory]. |
| + */ |
| +String dirname(file) { |
| + return fs.dirname(_getPath(file)); |
| +} |
| + |
| +/** |
| + * Asynchronously determines if [path], which can be a [String] file path, a |
| + * [File], or a [Directory] exists on the file system. Returns a [Future] that |
| + * completes with the result. |
| + */ |
| +Future<bool> exists(path) { |
| + path = _getPath(path); |
| + return Futures.wait([fileExists(path), dirExists(path)]).transform((results) { |
| + return results[0] || results[1]; |
| + }); |
| +} |
| + |
| +/** |
| * Asynchronously determines if [file], which can be a [String] file path or a |
| * [File], exists on the file system. Returns a [Future] that completes with |
| * the result. |
| */ |
| Future<bool> fileExists(file) { |
| + // TODO(nweiz): Currently File#exists will not detect the existence of |
| + // symlinks. Issue 2765 |
| + return runProcess('stat', [_getPath(file)]). |
| + transform((result) => result.exitCode == 0); |
| + |
| + // Real code: |
| + /* |
| final completer = new Completer<bool>(); |
| file = new File(_getPath(file)); |
| @@ -62,6 +89,7 @@ Future<bool> fileExists(file) { |
| file.exists((exists) => completer.complete(exists)); |
| return completer.future; |
| + */ |
| } |
| /** |
| @@ -110,6 +138,34 @@ Future<Directory> createDir(dir) { |
| } |
| /** |
| + * Ensures that [path] and all its parent directories exist. If they don't |
| + * exist, creates them. Returns a [Future] that completes once all the |
| + * directories are created. |
| + */ |
| +Future<Directory> ensureDir(path) { |
| + if (path == '.') return new Future.immediate(new Directory('.')); |
|
Bob Nystrom
2012/05/04 17:02:07
What if path == new File('.')?
Should probably do
nweiz
2012/05/07 18:28:35
Done.
|
| + |
| + path = _getPath(path); |
| + return dirExists(path).chain((exists) { |
| + if (exists) return new Future.immediate(new Directory(path)); |
| + return ensureDir(dirname(path)); |
| + }).chain((_) { |
| + var completer = new Completer<Directory>(); |
| + var future = createDir(path); |
| + future.handleException((error) { |
| + if (error is! DirectoryIOException) return false; |
| + // Error 17 means the directory already exists. |
| + if (error.osError.errorCode != 17) return false; |
| + |
| + completer.complete(_getDirectory(dir)); |
| + return true; |
| + }); |
| + future.then(completer.complete); |
| + return completer.future; |
| + }); |
| +} |
| + |
| +/** |
| * Creates a temp directory whose name will be based on [dir] with a unique |
| * suffix appended to it. Returns a [Future] that completes when the directory |
| * is created. |