Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(581)

Unified Diff: utils/pub/io.dart

Issue 10340005: Add support for pub install. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « utils/pub/command_update.dart ('k') | utils/pub/package.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: utils/pub/io.dart
diff --git a/utils/pub/io.dart b/utils/pub/io.dart
index 5c505c287c8a81524d8d56279be089524cdd15f7..a27956293211e2263762ca0ec5b54c9731173732 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) {
+ path = _getPath(path);
+ if (path == '.') return new Future.immediate(new Directory('.'));
+
+ 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.
« no previous file with comments | « utils/pub/command_update.dart ('k') | utils/pub/package.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698