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

Side by Side 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: Fix Chromium review errors? 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 /** 44 /**
45 * Gets the basename, the file name without any leading directory path, for 45 * Gets the basename, the file name without any leading directory path, for
46 * [file], which can either be a [String], [File], or [Directory]. 46 * [file], which can either be a [String], [File], or [Directory].
47 */ 47 */
48 String basename(file) { 48 String basename(file) {
49 return fs.basename(_getPath(file)); 49 return fs.basename(_getPath(file));
50 } 50 }
51 51
52 /** 52 /**
53 * Gets the the leading directory path for [file], which can either be a
54 * [String], [File], or [Directory].
55 */
56 String dirname(file) {
57 return fs.dirname(_getPath(file));
58 }
59
60 /**
61 * Asynchronously determines if [path], which can be a [String] file path, a
62 * [File], or a [Directory] exists on the file system. Returns a [Future] that
63 * completes with the result.
64 */
65 Future<bool> exists(path) {
66 path = _getPath(path);
67 return fileExists(path).chain((exists) {
68 if (exists) return new Future.immediate(true);
69 return dirExists(path);
70 });
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.
71 }
72
73 /**
53 * Asynchronously determines if [file], which can be a [String] file path or a 74 * Asynchronously determines if [file], which can be a [String] file path or a
54 * [File], exists on the file system. Returns a [Future] that completes with 75 * [File], exists on the file system. Returns a [Future] that completes with
55 * the result. 76 * the result.
56 */ 77 */
57 Future<bool> fileExists(file) { 78 Future<bool> fileExists(file) {
79 // 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.
80 return runProcess('stat', [_getPath(file)]).
81 transform((result) => result.exitCode == 0);
82
83 // Real code:
84 /*
58 final completer = new Completer<bool>(); 85 final completer = new Completer<bool>();
59 86
60 file = new File(_getPath(file)); 87 file = new File(_getPath(file));
61 file.onError = (error) => completer.completeException(error); 88 file.onError = (error) => completer.completeException(error);
62 file.exists((exists) => completer.complete(exists)); 89 file.exists((exists) => completer.complete(exists));
63 90
64 return completer.future; 91 return completer.future;
92 */
65 } 93 }
66 94
67 /** 95 /**
68 * Reads the contents of the text file [file], which can either be a [String] or 96 * Reads the contents of the text file [file], which can either be a [String] or
69 * a [File]. 97 * a [File].
70 */ 98 */
71 Future<String> readTextFile(file) { 99 Future<String> readTextFile(file) {
72 file = new File(_getPath(file)); 100 file = new File(_getPath(file));
73 final completer = new Completer<String>(); 101 final completer = new Completer<String>();
74 file.onError = (error) => completer.completeException(error); 102 file.onError = (error) => completer.completeException(error);
(...skipping 28 matching lines...) Expand all
103 Future<Directory> createDir(dir) { 131 Future<Directory> createDir(dir) {
104 final completer = new Completer<Directory>(); 132 final completer = new Completer<Directory>();
105 dir = _getDirectory(dir); 133 dir = _getDirectory(dir);
106 dir.onError = (error) => completer.completeException(error); 134 dir.onError = (error) => completer.completeException(error);
107 dir.create(() => completer.complete(dir)); 135 dir.create(() => completer.complete(dir));
108 136
109 return completer.future; 137 return completer.future;
110 } 138 }
111 139
112 /** 140 /**
141 * Ensures that [dir] exists and is a directory. If it doesn't exist, creates
142 * it. Returns a [Future] that completes once the directory is created.
143 */
144 Future<Directory> ensureDir(dir) {
145 var completer = new Completer<Directory>();
146 var future = createDir(dir);
147 future.handleException((error) {
148 if (error is! DirectoryIOException) return false;
149 if (error.osError.errorCode != 17) return false;
150
151 // 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.
152 completer.complete(_getDirectory(dir));
153 return true;
154 });
155 future.then(completer.complete);
156 return completer.future;
157 }
158
159 /**
160 * Ensures that [path] and all its parent directories exist. If they don't
161 * exist, creates them. Returns a [Future] that completes once all the
162 * directories are created.
163 */
164 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.
165 if (path == '.') return new Future.immediate(new Directory('.'));
166
167 path = _getPath(path);
168 return dirExists(path).chain((exists) {
169 if (exists) return new Future.immediate(new Directory(path));
170 return ensureNestedDir(dirname(path));
171 }).chain((_) => ensureDir(path));
172 }
173
174 /**
113 * Creates a temp directory whose name will be based on [dir] with a unique 175 * Creates a temp directory whose name will be based on [dir] with a unique
114 * suffix appended to it. Returns a [Future] that completes when the directory 176 * suffix appended to it. Returns a [Future] that completes when the directory
115 * is created. 177 * is created.
116 */ 178 */
117 Future<Directory> createTempDir(dir) { 179 Future<Directory> createTempDir(dir) {
118 final completer = new Completer<Directory>(); 180 final completer = new Completer<Directory>();
119 dir = _getDirectory(dir); 181 dir = _getDirectory(dir);
120 dir.onError = (error) => completer.completeException(error); 182 dir.onError = (error) => completer.completeException(error);
121 dir.createTemp(() => completer.complete(dir)); 183 dir.createTemp(() => completer.complete(dir));
122 184
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 } 380 }
319 381
320 /** 382 /**
321 * Gets a [Directory] for [entry], which can either already be one, or be a 383 * Gets a [Directory] for [entry], which can either already be one, or be a
322 * [String]. 384 * [String].
323 */ 385 */
324 Directory _getDirectory(entry) { 386 Directory _getDirectory(entry) {
325 if (entry is Directory) return entry; 387 if (entry is Directory) return entry;
326 return new Directory(entry); 388 return new Directory(entry);
327 } 389 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698