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 | 12 |
| 12 /** Gets the current working directory. */ | 13 /** Gets the current working directory. */ |
| 13 String get workingDir() => new File('.').fullPathSync(); | 14 String get workingDir() => new File('.').fullPathSync(); |
| 14 | 15 |
| 15 /** | 16 /** |
| 16 * Prints the given string to `stderr` on its own line. | 17 * Prints the given string to `stderr` on its own line. |
| 17 */ | 18 */ |
| 18 void printError(value) { | 19 void printError(value) { |
| 19 stderr.writeString(value.toString()); | 20 stderr.writeString(value.toString()); |
| 20 stderr.writeString('\n'); | 21 stderr.writeString('\n'); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 } | 257 } |
| 257 | 258 |
| 258 /** | 259 /** |
| 259 * Given [entry] which may be a [String], [File], or [Directory] relative to | 260 * Given [entry] which may be a [String], [File], or [Directory] relative to |
| 260 * the current working directory, returns its full canonicalized path. | 261 * the current working directory, returns its full canonicalized path. |
| 261 */ | 262 */ |
| 262 // TODO(rnystrom): Should this be async? | 263 // TODO(rnystrom): Should this be async? |
| 263 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 264 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 264 | 265 |
| 265 /** | 266 /** |
| 267 * Opens an input stream for a HTTP GET request to [url], which may be a | |
| 268 * [String] or [Uri]. | |
| 269 */ | |
| 270 InputStream httpGet(url) { | |
| 271 var resultStream = new ListInputStream(); | |
| 272 var connection = new HttpClient().getUrl(_getUri(url)); | |
| 273 | |
| 274 // TODO(nweiz): propagate this error to the return value. See issue 3657. | |
| 275 connection.onError = (e) { throw e; }; | |
| 276 connection.onResponse = (response) { | |
| 277 if (response.statusCode >= 400) { | |
| 278 // TODO(nweiz): propagate this error to the return value. See issue 3657. | |
| 279 throw new Exception( | |
| 280 "HTTP request for $url failed with status ${response.statusCode}"); | |
| 281 return; | |
|
Bob Nystrom
2012/06/22 22:35:44
The return is unnecessary.
nweiz
2012/06/25 22:25:17
Done.
| |
| 282 } | |
| 283 | |
| 284 pipeInputToInput(response.inputStream, resultStream); | |
| 285 }; | |
| 286 | |
| 287 return resultStream; | |
| 288 } | |
| 289 | |
| 290 /** | |
| 291 * Takes all input from [source] and writes it to [sink]. | |
| 292 */ | |
| 293 void pipeInputToInput(InputStream source, ListInputStream sink) { | |
| 294 source.onClosed = sink.markEndOfStream; | |
| 295 source.onData = () => sink.write(source.read()); | |
| 296 // TODO(nweiz): propagate this error to the sink. See issue 3657. | |
| 297 source.onError = (e) { throw e; }; | |
| 298 } | |
| 299 | |
| 300 /** | |
| 301 * Buffers all input from an InputStream and returns it as a future. | |
| 302 */ | |
| 303 Future<List<int>> consumeInputStream(InputStream stream) { | |
| 304 var completer = new Completer<List<int>>(); | |
| 305 var buffer = <int>[]; | |
| 306 stream.onClosed = () => completer.complete(buffer); | |
| 307 stream.onData = () => buffer.addAll(stream.read()); | |
| 308 stream.onError = (e) => completer.completeException(e); | |
| 309 return completer.future; | |
| 310 } | |
| 311 | |
| 312 /** | |
| 266 * Spawns and runs the process located at [executable], passing in [args]. | 313 * Spawns and runs the process located at [executable], passing in [args]. |
| 267 * Returns a [Future] that will complete the results of the process after it | 314 * Returns a [Future] that will complete the results of the process after it |
| 268 * has ended. | 315 * has ended. |
| 269 * | 316 * |
| 270 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | 317 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's |
| 271 * output streams are sent to the parent process's output streams. Output from | 318 * output streams are sent to the parent process's output streams. Output from |
| 272 * piped streams won't be available in the result object. | 319 * piped streams won't be available in the result object. |
| 273 */ | 320 */ |
| 274 Future<PubProcessResult> runProcess(String executable, List<String> args, | 321 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 275 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { | 322 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 } | 429 } |
| 383 | 430 |
| 384 /** | 431 /** |
| 385 * Gets a [Directory] for [entry], which can either already be one, or be a | 432 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 386 * [String]. | 433 * [String]. |
| 387 */ | 434 */ |
| 388 Directory _getDirectory(entry) { | 435 Directory _getDirectory(entry) { |
| 389 if (entry is Directory) return entry; | 436 if (entry is Directory) return entry; |
| 390 return new Directory(entry); | 437 return new Directory(entry); |
| 391 } | 438 } |
| 439 | |
| 440 /** | |
| 441 * Gets a [Uri] for [url], which can either already be one, or be a [String]. | |
| 442 */ | |
| 443 Uri _getUri(url) { | |
|
Bob Nystrom
2012/06/22 22:35:44
Mixing "uri" and "url" here is a bit confusing. Pi
nweiz
2012/06/25 22:25:17
Done.
| |
| 444 if (url is Uri) return url; | |
| 445 return new Uri.fromString(url); | |
| 446 } | |
| OLD | NEW |