| 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'); | |
| 12 | 11 |
| 13 /** Gets the current working directory. */ | 12 /** Gets the current working directory. */ |
| 14 String get workingDir() => new File('.').fullPathSync(); | 13 String get workingDir() => new File('.').fullPathSync(); |
| 15 | 14 |
| 16 /** | 15 /** |
| 17 * Prints the given string to `stderr` on its own line. | 16 * Prints the given string to `stderr` on its own line. |
| 18 */ | 17 */ |
| 19 void printError(value) { | 18 void printError(value) { |
| 20 stderr.writeString(value.toString()); | 19 stderr.writeString(value.toString()); |
| 21 stderr.writeString('\n'); | 20 stderr.writeString('\n'); |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 } | 256 } |
| 258 | 257 |
| 259 /** | 258 /** |
| 260 * Given [entry] which may be a [String], [File], or [Directory] relative to | 259 * Given [entry] which may be a [String], [File], or [Directory] relative to |
| 261 * the current working directory, returns its full canonicalized path. | 260 * the current working directory, returns its full canonicalized path. |
| 262 */ | 261 */ |
| 263 // TODO(rnystrom): Should this be async? | 262 // TODO(rnystrom): Should this be async? |
| 264 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 263 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 265 | 264 |
| 266 /** | 265 /** |
| 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 /** | |
| 312 * Spawns and runs the process located at [executable], passing in [args]. | 266 * Spawns and runs the process located at [executable], passing in [args]. |
| 313 * Returns a [Future] that will complete the results of the process after it | 267 * Returns a [Future] that will complete the results of the process after it |
| 314 * has ended. | 268 * has ended. |
| 315 * | 269 * |
| 316 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | 270 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's |
| 317 * output streams are sent to the parent process's output streams. Output from | 271 * output streams are sent to the parent process's output streams. Output from |
| 318 * piped streams won't be available in the result object. | 272 * piped streams won't be available in the result object. |
| 319 */ | 273 */ |
| 320 Future<PubProcessResult> runProcess(String executable, List<String> args, | 274 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 321 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { | 275 [workingDir, bool pipeStdout = false, bool pipeStderr = false]) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 future.handleException((err) { | 350 future.handleException((err) { |
| 397 // If the process failed, they probably don't have it. | 351 // If the process failed, they probably don't have it. |
| 398 completer.complete(false); | 352 completer.complete(false); |
| 399 return true; | 353 return true; |
| 400 }); | 354 }); |
| 401 | 355 |
| 402 return completer.future; | 356 return completer.future; |
| 403 } | 357 } |
| 404 | 358 |
| 405 /** | 359 /** |
| 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 /** | |
| 424 * Contains the results of invoking a [Process] and waiting for it to complete. | 360 * Contains the results of invoking a [Process] and waiting for it to complete. |
| 425 */ | 361 */ |
| 426 class PubProcessResult { | 362 class PubProcessResult { |
| 427 final List<String> stdout; | 363 final List<String> stdout; |
| 428 final List<String> stderr; | 364 final List<String> stderr; |
| 429 final int exitCode; | 365 final int exitCode; |
| 430 | 366 |
| 431 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 367 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
| 432 | 368 |
| 433 bool get success() => exitCode == 0; | 369 bool get success() => exitCode == 0; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 446 } | 382 } |
| 447 | 383 |
| 448 /** | 384 /** |
| 449 * Gets a [Directory] for [entry], which can either already be one, or be a | 385 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 450 * [String]. | 386 * [String]. |
| 451 */ | 387 */ |
| 452 Directory _getDirectory(entry) { | 388 Directory _getDirectory(entry) { |
| 453 if (entry is Directory) return entry; | 389 if (entry is Directory) return entry; |
| 454 return new Directory(entry); | 390 return new Directory(entry); |
| 455 } | 391 } |
| 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(ur); | |
| 463 } | |
| OLD | NEW |