| 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 [uri], which may be a |
| 268 * [String] or [Uri]. |
| 269 */ |
| 270 InputStream httpGet(uri) { |
| 271 var resultStream = new ListInputStream(); |
| 272 var connection = new HttpClient().getUrl(_getUri(uri)); |
| 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 $uri failed with status ${response.statusCode}"); |
| 281 } |
| 282 |
| 283 pipeInputToInput(response.inputStream, resultStream); |
| 284 }; |
| 285 |
| 286 return resultStream; |
| 287 } |
| 288 |
| 289 /** |
| 290 * Takes all input from [source] and writes it to [sink]. |
| 291 * |
| 292 * [onClosed] is called when [source] is closed. |
| 293 */ |
| 294 void pipeInputToInput(InputStream source, ListInputStream sink, |
| 295 [void onClosed()]) { |
| 296 source.onClosed = () { |
| 297 sink.markEndOfStream(); |
| 298 if (onClosed != null) onClosed(); |
| 299 }; |
| 300 source.onData = () => sink.write(source.read()); |
| 301 // TODO(nweiz): propagate this error to the sink. See issue 3657. |
| 302 source.onError = (e) { throw e; }; |
| 303 } |
| 304 |
| 305 /** |
| 306 * Buffers all input from an InputStream and returns it as a future. |
| 307 */ |
| 308 Future<List<int>> consumeInputStream(InputStream stream) { |
| 309 var completer = new Completer<List<int>>(); |
| 310 var buffer = <int>[]; |
| 311 stream.onClosed = () => completer.complete(buffer); |
| 312 stream.onData = () => buffer.addAll(stream.read()); |
| 313 stream.onError = (e) => completer.completeException(e); |
| 314 return completer.future; |
| 315 } |
| 316 |
| 317 /** |
| 266 * Spawns and runs the process located at [executable], passing in [args]. | 318 * 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 | 319 * Returns a [Future] that will complete the results of the process after it |
| 268 * has ended. | 320 * has ended. |
| 269 * | 321 * |
| 270 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | 322 * 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 | 323 * output streams are sent to the parent process's output streams. Output from |
| 272 * piped streams won't be available in the result object. | 324 * piped streams won't be available in the result object. |
| 273 */ | 325 */ |
| 274 Future<PubProcessResult> runProcess(String executable, List<String> args, | 326 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 275 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { | 327 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 future.handleException((err) { | 402 future.handleException((err) { |
| 351 // If the process failed, they probably don't have it. | 403 // If the process failed, they probably don't have it. |
| 352 completer.complete(false); | 404 completer.complete(false); |
| 353 return true; | 405 return true; |
| 354 }); | 406 }); |
| 355 | 407 |
| 356 return completer.future; | 408 return completer.future; |
| 357 } | 409 } |
| 358 | 410 |
| 359 /** | 411 /** |
| 412 * Extracts a `.tar.gz` file from [stream] to [destination], which can be a |
| 413 * directory or a path. Returns whether or not the extraction was successful. |
| 414 */ |
| 415 Future<bool> extractTarGz(InputStream stream, destination) { |
| 416 var process = Process.start("tar", |
| 417 ["--extract", "--gunzip", "--directory", _getPath(destination)]); |
| 418 var completer = new Completer<int>(); |
| 419 |
| 420 stream.pipe(process.stdin); |
| 421 process.stdout.pipe(stdout, close: false); |
| 422 process.stderr.pipe(stderr, close: false); |
| 423 |
| 424 process.onExit = completer.complete; |
| 425 process.onError = completer.completeException; |
| 426 return completer.future.transform((exitCode) => exitCode == 0); |
| 427 } |
| 428 |
| 429 /** |
| 360 * Contains the results of invoking a [Process] and waiting for it to complete. | 430 * Contains the results of invoking a [Process] and waiting for it to complete. |
| 361 */ | 431 */ |
| 362 class PubProcessResult { | 432 class PubProcessResult { |
| 363 final List<String> stdout; | 433 final List<String> stdout; |
| 364 final List<String> stderr; | 434 final List<String> stderr; |
| 365 final int exitCode; | 435 final int exitCode; |
| 366 | 436 |
| 367 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 437 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
| 368 | 438 |
| 369 bool get success() => exitCode == 0; | 439 bool get success() => exitCode == 0; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 382 } | 452 } |
| 383 | 453 |
| 384 /** | 454 /** |
| 385 * Gets a [Directory] for [entry], which can either already be one, or be a | 455 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 386 * [String]. | 456 * [String]. |
| 387 */ | 457 */ |
| 388 Directory _getDirectory(entry) { | 458 Directory _getDirectory(entry) { |
| 389 if (entry is Directory) return entry; | 459 if (entry is Directory) return entry; |
| 390 return new Directory(entry); | 460 return new Directory(entry); |
| 391 } | 461 } |
| 462 |
| 463 /** |
| 464 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 465 */ |
| 466 Uri _getUri(uri) { |
| 467 if (uri is Uri) return uri; |
| 468 return new Uri.fromString(uri); |
| 469 } |
| OLD | NEW |