Chromium Code Reviews| Index: utils/pub/io.dart |
| diff --git a/utils/pub/io.dart b/utils/pub/io.dart |
| index 5c505c287c8a81524d8d56279be089524cdd15f7..25053ceec84a52e2f6a4c60d5ce2443ff74075b7 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 fileExists(path).chain((exists) { |
| + if (exists) return new Future.immediate(true); |
| + return dirExists(path); |
| + }); |
|
Bob Nystrom
2012/05/03 00:15:49
I hate to suggest this, but ideally you could kick
nweiz
2012/05/04 01:03:43
Done.
|
| +} |
| + |
| +/** |
| * 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) { |
| + // Currently File#exists will not detect the existence of symlinks. Issue 2765 |
|
Bob Nystrom
2012/05/03 00:15:49
Make this a TODO so it's greppable.
nweiz
2012/05/04 01:03:43
Done.
|
| + 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,40 @@ Future<Directory> createDir(dir) { |
| } |
| /** |
| + * Ensures that [dir] exists and is a directory. If it doesn't exist, creates |
| + * it. Returns a [Future] that completes once the directory is created. |
| + */ |
| +Future<Directory> ensureDir(dir) { |
| + var completer = new Completer<Directory>(); |
| + var future = createDir(dir); |
| + future.handleException((error) { |
| + if (error is! DirectoryIOException) return false; |
| + if (error.osError.errorCode != 17) return false; |
| + |
| + // Error 17 means the directory already exists. |
|
Bob Nystrom
2012/05/03 00:15:49
Move comment above preceding line.
nweiz
2012/05/04 01:03:43
Done.
|
| + completer.complete(_getDirectory(dir)); |
| + return true; |
| + }); |
| + future.then(completer.complete); |
| + return completer.future; |
| +} |
| + |
| +/** |
| + * 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> ensureNestedDir(path) { |
|
Bob Nystrom
2012/05/03 00:15:49
Is there any reason someone would prefer ensureDir
nweiz
2012/05/04 01:03:43
Done.
|
| + if (path == '.') return new Future.immediate(new Directory('.')); |
| + |
| + path = _getPath(path); |
| + return dirExists(path).chain((exists) { |
| + if (exists) return new Future.immediate(new Directory(path)); |
| + return ensureNestedDir(dirname(path)); |
| + }).chain((_) => ensureDir(path)); |
| +} |
| + |
| +/** |
| * 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. |