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 void init(String commandTemplate, List argumentTemplates, int timeout) { |
| 12 this.commandTemplate = commandTemplate; |
| 13 this.argumentTemplates = argumentTemplates; |
| 14 this.timeout = timeout; |
| 15 } |
| 16 |
| 17 RunProcessTask(); |
| 18 |
| 19 void execute(Path testfile, List stdout, List stderr, bool logging, |
| 20 Function exitHandler) { |
| 21 var cmd = expandMacros(commandTemplate, testfile); |
| 22 List args = new List(); |
| 23 for (var i = 0; i < argumentTemplates.length; i++) { |
| 24 args.add(expandMacros(argumentTemplates[i], testfile)); |
| 25 } |
| 26 |
| 27 if (logging) { |
| 28 stdout.add('Running $cmd ${Strings.join(args, " ")}'); |
| 29 } |
| 30 var timer = null; |
| 31 var process = Process.start(cmd, args); |
| 32 process.onStart = () { |
| 33 timer = new Timer(1000 * timeout, (t) { |
| 34 timer = null; |
| 35 process.kill(); |
| 36 }); |
| 37 }; |
| 38 process.onExit = (exitCode) { |
| 39 if (timer != null) { |
| 40 timer.cancel(); |
| 41 } |
| 42 process.close(); |
| 43 exitHandler(exitCode); |
| 44 }; |
| 45 process.onError = (e) { |
| 46 print("Error starting process:"); |
| 47 print(" Command: $cmd"); |
| 48 print(" Error: $e"); |
| 49 exitHandler(-1); |
| 50 }; |
| 51 |
| 52 StringInputStream stdoutStringStream = |
| 53 new StringInputStream(process.stdout); |
| 54 StringInputStream stderrStringStream = |
| 55 new StringInputStream(process.stderr); |
| 56 stdoutStringStream.onLine = makeReadHandler(stdoutStringStream, stdout); |
| 57 stderrStringStream.onLine = makeReadHandler(stderrStringStream, stderr); |
| 58 } |
| 59 |
| 60 Function makeReadHandler(StringInputStream source, List<String> destination) { |
| 61 return () { |
| 62 if (source.closed) return; |
| 63 var line = source.readLine(); |
| 64 while (null != line) { |
| 65 if (config.immediateOutput && line.startsWith('###')) { |
| 66 _outStream.writeString(line.substring(3)); |
| 67 _outStream.writeString('\n'); |
| 68 } else { |
| 69 destination.add(line); |
| 70 } |
| 71 line = source.readLine(); |
| 72 } |
| 73 }; |
| 74 } |
| 75 } |
OLD | NEW |