| 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);
|
| + });
|
| +}
|
| +
|
| +/**
|
| * 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
|
| + 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.
|
| + 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) {
|
| + 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.
|
|
|