OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // A pipeline task to run a process and capture the output. |
| 6 class RunProcessTask extends PipelineTask { |
| 7 String commandTemplate; |
| 8 List argumentTemplates; |
| 9 int timeout; |
| 10 |
| 11 RunProcessTask(this.commandTemplate, this.argumentTemplates, this.timeout); |
| 12 |
| 13 void execute(Path testfile, List stdout, List stderr, bool verboseLogging, |
| 14 Function exitHandler) { |
| 15 var cmd = concretize(commandTemplate, testfile); |
| 16 List args = new List(); |
| 17 for (var i = 0; i < argumentTemplates.length; i++) { |
| 18 args.add(concretize(argumentTemplates[i], testfile)); |
| 19 } |
| 20 |
| 21 if (verboseLogging) { |
| 22 stdout.add('Running $cmd ${Strings.join(args, " ")}'); |
| 23 } |
| 24 var timer = null; |
| 25 var process = Process.start(cmd, args); |
| 26 process.onStart = () { |
| 27 timer = new Timer(1000 * timeout, (t) { |
| 28 timer = null; |
| 29 process.kill(); |
| 30 }); |
| 31 }; |
| 32 process.onExit = (exitCode) { |
| 33 if (timer != null) { |
| 34 timer.cancel(); |
| 35 } |
| 36 process.close(); |
| 37 exitHandler(exitCode); |
| 38 }; |
| 39 process.onError = (e) { |
| 40 print("Error starting process:"); |
| 41 print(" Command: $cmd"); |
| 42 print(" Error: $e"); |
| 43 exitHandler(-1); |
| 44 }; |
| 45 |
| 46 StringInputStream stdoutStringStream = |
| 47 new StringInputStream(process.stdout); |
| 48 StringInputStream stderrStringStream = |
| 49 new StringInputStream(process.stderr); |
| 50 stdoutStringStream.onLine = makeReadHandler(stdoutStringStream, stdout); |
| 51 stderrStringStream.onLine = makeReadHandler(stderrStringStream, stderr); |
| 52 } |
| 53 |
| 54 Function makeReadHandler(StringInputStream source, List<String> destination) { |
| 55 return () { |
| 56 if (source.closed) return; |
| 57 var line = source.readLine(); |
| 58 while (null != line) { |
| 59 destination.add(line); |
| 60 line = source.readLine(); |
| 61 } |
| 62 }; |
| 63 } |
| 64 } |
OLD | NEW |