| 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 part of pipeline; | 5 part of pipeline; |
| 6 | 6 |
| 7 List stdout, stderr, log; | 7 List stdout, stderr, log; |
| 8 var replyPort; | 8 var replyPort; |
| 9 int _procId = 1; | 9 int _procId = 1; |
| 10 Map _procs = {}; | 10 Map _procs = {}; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 * If [procId] is non-zero (i.e. called from startProcess) then a reference | 53 * If [procId] is non-zero (i.e. called from startProcess) then a reference |
| 54 * to the [Process] will be put in a map with key [procId]; in this case | 54 * to the [Process] will be put in a map with key [procId]; in this case |
| 55 * the process can be terminated later by calling [stopProcess] and | 55 * the process can be terminated later by calling [stopProcess] and |
| 56 * passing in the [procId]. | 56 * passing in the [procId]. |
| 57 * [outputMonitor] is an optional function that will be called back with each | 57 * [outputMonitor] is an optional function that will be called back with each |
| 58 * line of output from the process. | 58 * line of output from the process. |
| 59 * Returns a [Future] for when the process terminates. | 59 * Returns a [Future] for when the process terminates. |
| 60 */ | 60 */ |
| 61 Future _processHelper(String command, List<String> args, | 61 Future _processHelper(String command, List<String> args, |
| 62 [int timeout = 300, int procId = 0, Function outputMonitor]) { | 62 [int timeout = 300, int procId = 0, Function outputMonitor]) { |
| 63 var completer = new Completer(); | 63 var completer = procId == 0 ? new Completer() : null; |
| 64 log.add('Running $command ${Strings.join(args, " ")}'); | 64 log.add('Running $command ${Strings.join(args, " ")}'); |
| 65 var timer = null; | 65 var timer = null; |
| 66 var stdoutHandler, stderrHandler; | 66 var stdoutHandler, stderrHandler; |
| 67 var process = Process.start(command, args); | 67 var processFuture = Process.start(command, args); |
| 68 if (procId != 0) { | 68 processFuture.then((process) { |
| 69 _procs[procId] = process; | 69 _procs[procId] = process; |
| 70 } | 70 |
| 71 process.onStart = () { | |
| 72 timer = new Timer(1000 * timeout, (t) { | 71 timer = new Timer(1000 * timeout, (t) { |
| 73 timer = null; | 72 timer = null; |
| 74 process.kill(); | 73 process.kill(); |
| 75 }); | 74 }); |
| 76 }; | 75 |
| 77 process.onExit = (exitCode) { | 76 process.onExit = (exitCode) { |
| 78 if (timer != null) { | 77 if (timer != null) { |
| 79 timer.cancel(); | 78 timer.cancel(); |
| 80 } | 79 } |
| 81 process.close(); | 80 process.close(); |
| 82 if (completer != null) { | 81 if (completer != null) { |
| 83 completer.complete(exitCode); | 82 completer.complete(exitCode); |
| 84 } | 83 } |
| 85 }; | 84 }; |
| 86 process.onError = (e) { | 85 |
| 86 _pipeStream(process.stdout, stdout, outputMonitor); |
| 87 _pipeStream(process.stderr, stderr, outputMonitor); |
| 88 }); |
| 89 processFuture.handleException((e) { |
| 87 stderr.add("Error starting process:"); | 90 stderr.add("Error starting process:"); |
| 88 stderr.add(" Command: $command"); | 91 stderr.add(" Command: $command"); |
| 89 stderr.add(" Error: $e"); | 92 stderr.add(" Error: $e"); |
| 90 completePipeline(-1); | 93 completePipeline(-1); |
| 91 }; | 94 return true; |
| 92 | 95 }); |
| 93 _pipeStream(process.stdout, stdout, outputMonitor); | |
| 94 _pipeStream(process.stderr, stderr, outputMonitor); | |
| 95 | 96 |
| 96 return completer.future; | 97 return completer.future; |
| 97 } | 98 } |
| 98 | 99 |
| 99 void _pipeStream(InputStream stream, List<String> destination, | 100 void _pipeStream(InputStream stream, List<String> destination, |
| 100 Function outputMonitor) { | 101 Function outputMonitor) { |
| 101 var source = new StringInputStream(stream); | 102 var source = new StringInputStream(stream); |
| 102 source.onLine = () { | 103 source.onLine = () { |
| 103 if (source.available() == 0) return; | 104 if (source.available() == 0) return; |
| 104 var line = source.readLine(); | 105 var line = source.readLine(); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 stderr = new List(); | 179 stderr = new List(); |
| 179 log = new List(); | 180 log = new List(); |
| 180 } | 181 } |
| 181 | 182 |
| 182 void completePipeline([exitCode = 0]) { | 183 void completePipeline([exitCode = 0]) { |
| 183 replyPort.send([stdout, stderr, log, exitCode]); | 184 replyPort.send([stdout, stderr, log, exitCode]); |
| 184 } | 185 } |
| 185 | 186 |
| 186 /** Utility function to log diagnostic messages. */ | 187 /** Utility function to log diagnostic messages. */ |
| 187 void logMessage(msg) => log.add(msg); | 188 void logMessage(msg) => log.add(msg); |
| OLD | NEW |