Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Futures.wait([fileExists(path), dirExists(path)]).transform((results) { | |
| 68 return results[0] || results[1]; | |
| 69 }); | |
| 70 } | |
| 71 | |
| 72 /** | |
| 53 * Asynchronously determines if [file], which can be a [String] file path or a | 73 * 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 | 74 * [File], exists on the file system. Returns a [Future] that completes with |
| 55 * the result. | 75 * the result. |
| 56 */ | 76 */ |
| 57 Future<bool> fileExists(file) { | 77 Future<bool> fileExists(file) { |
| 78 // TODO(nweiz): Currently File#exists will not detect the existence of | |
| 79 // symlinks. Issue 2765 | |
| 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 Loading... | |
| 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 [path] and all its parent directories exist. If they don't | |
| 142 * exist, creates them. Returns a [Future] that completes once all the | |
| 143 * directories are created. | |
| 144 */ | |
| 145 Future<Directory> ensureDir(path) { | |
| 146 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.
| |
| 147 | |
| 148 path = _getPath(path); | |
| 149 return dirExists(path).chain((exists) { | |
| 150 if (exists) return new Future.immediate(new Directory(path)); | |
| 151 return ensureDir(dirname(path)); | |
| 152 }).chain((_) { | |
| 153 var completer = new Completer<Directory>(); | |
| 154 var future = createDir(path); | |
| 155 future.handleException((error) { | |
| 156 if (error is! DirectoryIOException) return false; | |
| 157 // Error 17 means the directory already exists. | |
| 158 if (error.osError.errorCode != 17) return false; | |
| 159 | |
| 160 completer.complete(_getDirectory(dir)); | |
| 161 return true; | |
| 162 }); | |
| 163 future.then(completer.complete); | |
| 164 return completer.future; | |
| 165 }); | |
| 166 } | |
| 167 | |
| 168 /** | |
| 113 * Creates a temp directory whose name will be based on [dir] with a unique | 169 * 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 | 170 * suffix appended to it. Returns a [Future] that completes when the directory |
| 115 * is created. | 171 * is created. |
| 116 */ | 172 */ |
| 117 Future<Directory> createTempDir(dir) { | 173 Future<Directory> createTempDir(dir) { |
| 118 final completer = new Completer<Directory>(); | 174 final completer = new Completer<Directory>(); |
| 119 dir = _getDirectory(dir); | 175 dir = _getDirectory(dir); |
| 120 dir.onError = (error) => completer.completeException(error); | 176 dir.onError = (error) => completer.completeException(error); |
| 121 dir.createTemp(() => completer.complete(dir)); | 177 dir.createTemp(() => completer.complete(dir)); |
| 122 | 178 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 } | 374 } |
| 319 | 375 |
| 320 /** | 376 /** |
| 321 * Gets a [Directory] for [entry], which can either already be one, or be a | 377 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 322 * [String]. | 378 * [String]. |
| 323 */ | 379 */ |
| 324 Directory _getDirectory(entry) { | 380 Directory _getDirectory(entry) { |
| 325 if (entry is Directory) return entry; | 381 if (entry is Directory) return entry; |
| 326 return new Directory(entry); | 382 return new Directory(entry); |
| 327 } | 383 } |
| OLD | NEW |