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('io'); | 8 #library('io'); |
| 9 | 9 |
| 10 #import('dart:io'); | 10 #import('dart:io'); |
| 11 #import('dart:uri'); | 11 #import('dart:uri'); |
| 12 | 12 |
| 13 /** Gets the current working directory. */ | 13 /** Gets the current working directory. */ |
| 14 String get workingDir() => new File('.').fullPathSync(); | 14 String get workingDir() => new File('.').fullPathSync(); |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Prints the given string to `stderr` on its own line. | 17 * Prints the given string to `stderr` on its own line. |
| 18 */ | 18 */ |
| 19 void printError(value) { | 19 void printError(value) { |
| 20 stderr.writeString(value.toString()); | 20 stderr.writeString(value.toString()); |
| 21 stderr.writeString('\n'); | 21 stderr.writeString('\n'); |
| 22 } | 22 } |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Joins a number of path string parts into a single path. Handles | 25 * Joins a number of path string parts into a single path. Handles |
| 26 * platform-specific path separators. Parts can be [String], [Directory], or | 26 * platform-specific path separators. Parts can be [String], [Directory], or |
| 27 * [File] objects. | 27 * [File] objects. |
| 28 */ | 28 */ |
| 29 String join(part1, [part2, part3, part4]) { | 29 String join(part1, [part2, part3, part4]) { |
| 30 final parts = _getPath(part1).split('/'); | 30 final parts = _getPath(part1).replaceAll('\\', '/').split('/'); |
|
nweiz
2012/07/30 23:03:39
It feels like maybe this should be part of _getPat
Bob Nystrom
2012/07/30 23:42:33
I'm hesitant to normalize paths eagerly really ear
| |
| 31 | 31 |
| 32 for (final part in [part2, part3, part4]) { | 32 for (final part in [part2, part3, part4]) { |
| 33 if (part == null) continue; | 33 if (part == null) continue; |
| 34 | 34 |
| 35 for (final piece in _getPath(part).split('/')) { | 35 for (final piece in _getPath(part).split('/')) { |
| 36 if (piece == '..' && parts.length > 0 && | 36 if (piece == '..' && parts.length > 0 && |
| 37 parts.last() != '.' && parts.last() != '..') { | 37 parts.last() != '.' && parts.last() != '..') { |
| 38 parts.removeLast(); | 38 parts.removeLast(); |
| 39 } else if (piece != '') { | 39 } else if (piece != '') { |
| 40 if (parts.length > 0 && parts.last() == '.') { | 40 if (parts.length > 0 && parts.last() == '.') { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 * Ensures that [path] and all its parent directories exist. If they don't | 145 * Ensures that [path] and all its parent directories exist. If they don't |
| 146 * exist, creates them. Returns a [Future] that completes once all the | 146 * exist, creates them. Returns a [Future] that completes once all the |
| 147 * directories are created. | 147 * directories are created. |
| 148 */ | 148 */ |
| 149 Future<Directory> ensureDir(path) { | 149 Future<Directory> ensureDir(path) { |
| 150 path = _getPath(path); | 150 path = _getPath(path); |
| 151 if (path == '.') return new Future.immediate(new Directory('.')); | 151 if (path == '.') return new Future.immediate(new Directory('.')); |
| 152 | 152 |
| 153 return dirExists(path).chain((exists) { | 153 return dirExists(path).chain((exists) { |
| 154 if (exists) return new Future.immediate(new Directory(path)); | 154 if (exists) return new Future.immediate(new Directory(path)); |
| 155 return ensureDir(dirname(path)); | 155 return ensureDir(dirname(path)).chain((p) { |
|
nweiz
2012/07/30 23:03:39
I don't understand why you've named this variable,
Bob Nystrom
2012/07/30 23:42:33
The "p" is leftover debug code. Removed.
The nest
| |
| 156 }).chain((_) { | 156 var completer = new Completer<Directory>(); |
| 157 var completer = new Completer<Directory>(); | 157 var future = createDir(path); |
| 158 var future = createDir(path); | 158 future.handleException((error) { |
| 159 future.handleException((error) { | 159 if (error is! DirectoryIOException) return false; |
| 160 if (error is! DirectoryIOException) return false; | 160 // Error 17 means the directory already exists (or 183 on Windows). |
| 161 // Error 17 means the directory already exists. | 161 if (error.osError.errorCode != 17 && |
| 162 if (error.osError.errorCode != 17) return false; | 162 error.osError.errorCode != 183) return false; |
| 163 | 163 |
| 164 completer.complete(_getDirectory(path)); | 164 completer.complete(_getDirectory(path)); |
| 165 return true; | 165 return true; |
| 166 }); | |
| 167 future.then(completer.complete); | |
| 168 return completer.future; | |
| 166 }); | 169 }); |
| 167 future.then(completer.complete); | |
| 168 return completer.future; | |
| 169 }); | 170 }); |
| 170 } | 171 } |
| 171 | 172 |
| 172 /** | 173 /** |
| 173 * Creates a temp directory whose name will be based on [dir] with a unique | 174 * Creates a temp directory whose name will be based on [dir] with a unique |
| 174 * suffix appended to it. Returns a [Future] that completes when the directory | 175 * suffix appended to it. Returns a [Future] that completes when the directory |
| 175 * is created. | 176 * is created. |
| 176 */ | 177 */ |
| 177 Future<Directory> createTempDir(dir) { | 178 Future<Directory> createTempDir(dir) { |
| 178 dir = _getDirectory(dir); | 179 dir = _getDirectory(dir); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 470 return new Directory(entry); | 471 return new Directory(entry); |
| 471 } | 472 } |
| 472 | 473 |
| 473 /** | 474 /** |
| 474 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 475 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 475 */ | 476 */ |
| 476 Uri _getUri(uri) { | 477 Uri _getUri(uri) { |
| 477 if (uri is Uri) return uri; | 478 if (uri is Uri) return uri; |
| 478 return new Uri.fromString(uri); | 479 return new Uri.fromString(uri); |
| 479 } | 480 } |
| OLD | NEW |