| 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 execute(Path testfile, List stdout, List stderr, bool logging, | |
| 14 Function exitHandler) { | |
| 15 var cmd = expandMacros(_commandTemplate, testfile); | |
| 16 List args = new List(); | |
| 17 for (var i = 0; i < _argumentTemplates.length; i++) { | |
| 18 args.add(expandMacros(_argumentTemplates[i], testfile)); | |
| 19 } | |
| 20 | |
| 21 if (logging) { | |
| 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 return process; | |
| 53 } | |
| 54 | |
| 55 Function makeReadHandler(StringInputStream source, List<String> destination) { | |
| 56 return () { | |
| 57 if (source.closed) return; | |
| 58 var line = source.readLine(); | |
| 59 while (null != line) { | |
| 60 if (config.immediateOutput && line.startsWith('###')) { | |
| 61 _outStream.writeString(line.substring(3)); | |
| 62 _outStream.writeString('\n'); | |
| 63 } else { | |
| 64 destination.add(line); | |
| 65 } | |
| 66 line = source.readLine(); | |
| 67 } | |
| 68 }; | |
| 69 } | |
| 70 } | |
| OLD | NEW |