| 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('pub_io'); | 8 #library('pub_io'); |
| 9 | 9 |
| 10 #import('dart:io'); | 10 #import('dart:io'); |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 * Given [entry] which may be a [String], [File], or [Directory] relative to | 251 * Given [entry] which may be a [String], [File], or [Directory] relative to |
| 252 * the current working directory, returns its full canonicalized path. | 252 * the current working directory, returns its full canonicalized path. |
| 253 */ | 253 */ |
| 254 // TODO(rnystrom): Should this be async? | 254 // TODO(rnystrom): Should this be async? |
| 255 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 255 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * Spawns and runs the process located at [executable], passing in [args]. | 258 * Spawns and runs the process located at [executable], passing in [args]. |
| 259 * Returns a [Future] that will complete the results of the process after it | 259 * Returns a [Future] that will complete the results of the process after it |
| 260 * has ended. | 260 * has ended. |
| 261 * | |
| 262 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | |
| 263 * output streams are sent to the parent process's output streams. Output from | |
| 264 * piped streams won't be available in the result object. | |
| 265 */ | 261 */ |
| 266 Future<PubProcessResult> runProcess(String executable, List<String> args, | 262 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 267 [String workingDir, bool pipeStdout = false, bool pipeStderr = false]) { | 263 [String workingDir]) { |
| 268 int exitCode; | 264 int exitCode; |
| 269 | 265 |
| 270 final options = new ProcessOptions(); | 266 final options = new ProcessOptions(); |
| 271 if (workingDir != null) options.workingDirectory = workingDir; | 267 if (workingDir != null) options.workingDirectory = workingDir; |
| 272 | 268 |
| 273 final process = Process.start(executable, args, options); | 269 final process = Process.start(executable, args, options); |
| 274 | 270 |
| 275 final outStream = new StringInputStream(process.stdout); | 271 final outStream = new StringInputStream(process.stdout); |
| 276 final processStdout = <String>[]; | 272 final processStdout = <String>[]; |
| 277 | 273 |
| 278 final errStream = new StringInputStream(process.stderr); | 274 final errStream = new StringInputStream(process.stderr); |
| 279 final processStderr = <String>[]; | 275 final processStderr = <String>[]; |
| 280 | 276 |
| 281 final completer = new Completer<PubProcessResult>(); | 277 final completer = new Completer<PubProcessResult>(); |
| 282 | 278 |
| 283 checkComplete() { | 279 checkComplete() { |
| 284 // Wait until the process is done and its output streams are closed. | 280 // Wait until the process is done and its output streams are closed. |
| 285 if (!pipeStdout && !outStream.closed) return; | 281 if (!outStream.closed) return; |
| 286 if (!pipeStderr && !errStream.closed) return; | 282 if (!errStream.closed) return; |
| 287 if (exitCode == null) return; | 283 if (exitCode == null) return; |
| 288 | 284 |
| 289 completer.complete(new PubProcessResult( | 285 completer.complete(new PubProcessResult( |
| 290 processStdout, processStderr, exitCode)); | 286 processStdout, processStderr, exitCode)); |
| 291 } | 287 } |
| 292 | 288 |
| 293 if (pipeStdout) { | 289 outStream.onLine = () => processStdout.add(outStream.readLine()); |
| 294 process.stdout.pipe(stdout, close: false); | 290 outStream.onClosed = checkComplete; |
| 295 } else { | 291 outStream.onError = (error) => completer.completeException(error); |
| 296 outStream.onLine = () => processStdout.add(outStream.readLine()); | |
| 297 outStream.onClosed = checkComplete; | |
| 298 outStream.onError = (error) => completer.completeException(error); | |
| 299 } | |
| 300 | 292 |
| 301 if (pipeStderr) { | 293 errStream.onLine = () => processStderr.add(errStream.readLine()); |
| 302 process.stderr.pipe(stderr, close: false); | 294 errStream.onClosed = checkComplete; |
| 303 } else { | 295 errStream.onError = (error) => completer.completeException(error); |
| 304 errStream.onLine = () => processStderr.add(errStream.readLine()); | |
| 305 errStream.onClosed = checkComplete; | |
| 306 errStream.onError = (error) => completer.completeException(error); | |
| 307 } | |
| 308 | 296 |
| 309 process.onExit = (actualExitCode) { | 297 process.onExit = (actualExitCode) { |
| 310 exitCode = actualExitCode; | 298 exitCode = actualExitCode; |
| 311 checkComplete(); | 299 checkComplete(); |
| 312 }; | 300 }; |
| 313 | 301 |
| 314 process.onError = (error) => completer.completeException(error); | 302 process.onError = (error) => completer.completeException(error); |
| 315 | 303 |
| 316 return completer.future; | 304 return completer.future; |
| 317 } | 305 } |
| 318 | 306 |
| 319 /** | 307 /** |
| 320 * Contains the results of invoking a [Process] and waiting for it to complete. | 308 * Contains the results of invoking a [Process] and waiting for it to complete. |
| 321 */ | 309 */ |
| 322 class PubProcessResult { | 310 class PubProcessResult { |
| 323 final List<String> stdout; | 311 final List<String> stdout; |
| 324 final List<String> stderr; | 312 final List<String> stderr; |
| 325 final int exitCode; | 313 final int exitCode; |
| 326 | 314 |
| 327 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 315 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
| 328 | |
| 329 bool get success() => exitCode == 0; | |
| 330 } | 316 } |
| 331 | 317 |
| 332 /** | 318 /** |
| 333 * Gets the path string for [entry], which can either already be a path string, | 319 * Gets the path string for [entry], which can either already be a path string, |
| 334 * or be a [File] or [Directory]. Allows working generically with "file-like" | 320 * or be a [File] or [Directory]. Allows working generically with "file-like" |
| 335 * objects. | 321 * objects. |
| 336 */ | 322 */ |
| 337 String _getPath(entry) { | 323 String _getPath(entry) { |
| 338 if (entry is String) return entry; | 324 if (entry is String) return entry; |
| 339 if (entry is File) return entry.name; | 325 if (entry is File) return entry.name; |
| 340 if (entry is Directory) return entry.path; | 326 if (entry is Directory) return entry.path; |
| 341 throw 'Entry $entry is not a supported type.'; | 327 throw 'Entry $entry is not a supported type.'; |
| 342 } | 328 } |
| 343 | 329 |
| 344 /** | 330 /** |
| 345 * Gets a [Directory] for [entry], which can either already be one, or be a | 331 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 346 * [String]. | 332 * [String]. |
| 347 */ | 333 */ |
| 348 Directory _getDirectory(entry) { | 334 Directory _getDirectory(entry) { |
| 349 if (entry is Directory) return entry; | 335 if (entry is Directory) return entry; |
| 350 return new Directory(entry); | 336 return new Directory(entry); |
| 351 } | 337 } |
| OLD | NEW |