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