| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 var file = new File(fileName); | 43 var file = new File(fileName); |
| 44 var ostream = file.openOutputStream(FileMode.WRITE); | 44 var ostream = file.openOutputStream(FileMode.WRITE); |
| 45 ostream.writeString(contents); | 45 ostream.writeString(contents); |
| 46 ostream.close(); | 46 ostream.close(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 /* | 49 /* |
| 50 * Run an external process [cmd] with command line arguments [args]. | 50 * Run an external process [cmd] with command line arguments [args]. |
| 51 * [timeout] can be used to forcefully terminate the process after | 51 * [timeout] can be used to forcefully terminate the process after |
| 52 * some number of seconds. This is used by runCommand and startProcess. | 52 * some number of seconds. This is used by runCommand and startProcess. |
| 53 * If [procId] is 0 (runCommand) then this will return a [Future] for | 53 * If [procId] is non-zero (i.e. called from startProcess) then a reference |
| 54 * when the process terminates; if [procId] is instead non-zero | 54 * to the [Process] will be put in a map with key [procId]; in this case |
| 55 * (startProcess) then a reference to the [Process] will be put in a | 55 * the process can be terminated later by calling [stopProcess] and |
| 56 * map with key [procId]; in this case the process can be terminated | 56 * passing in the [procId]. |
| 57 * later by calling [stopProcess] and passing in the [procId]. | 57 * [outputMonitor] is an optional function that will be called back with each |
| 58 * line of output from the process. |
| 59 * Returns a [Future] for when the process terminates. |
| 58 */ | 60 */ |
| 59 Future _processHelper(String command, List<String> args, | 61 Future _processHelper(String command, List<String> args, |
| 60 [int timeout = 300, int procId = 0]) { | 62 [int timeout = 300, int procId = 0, Function outputMonitor]) { |
| 61 var completer = null; | 63 var completer = new Completer(); |
| 62 log.add('Running $command ${Strings.join(args, " ")}'); | 64 log.add('Running $command ${Strings.join(args, " ")}'); |
| 63 var timer = null; | 65 var timer = null; |
| 64 var stdoutHandler, stderrHandler; | 66 var stdoutHandler, stderrHandler; |
| 65 var process = Process.start(command, args); | 67 var process = Process.start(command, args); |
| 66 if (procId == 0) { | 68 if (procId != 0) { |
| 67 completer = new Completer(); | |
| 68 } else { | |
| 69 _procs[procId] = process; | 69 _procs[procId] = process; |
| 70 } | 70 } |
| 71 process.onStart = () { | 71 process.onStart = () { |
| 72 timer = new Timer(1000 * timeout, (t) { | 72 timer = new Timer(1000 * timeout, (t) { |
| 73 timer = null; | 73 timer = null; |
| 74 process.kill(); | 74 process.kill(); |
| 75 }); | 75 }); |
| 76 }; | 76 }; |
| 77 process.onExit = (exitCode) { | 77 process.onExit = (exitCode) { |
| 78 if (timer != null) { | 78 if (timer != null) { |
| 79 timer.cancel(); | 79 timer.cancel(); |
| 80 } | 80 } |
| 81 process.close(); | 81 process.close(); |
| 82 if (completer != null) { | 82 if (completer != null) { |
| 83 completer.complete(exitCode); | 83 completer.complete(exitCode); |
| 84 } | 84 } |
| 85 }; | 85 }; |
| 86 process.onError = (e) { | 86 process.onError = (e) { |
| 87 stderr.add("Error starting process:"); | 87 stderr.add("Error starting process:"); |
| 88 stderr.add(" Command: $command"); | 88 stderr.add(" Command: $command"); |
| 89 stderr.add(" Error: $e"); | 89 stderr.add(" Error: $e"); |
| 90 completePipeline(-1); | 90 completePipeline(-1); |
| 91 }; | 91 }; |
| 92 | 92 |
| 93 _pipeStream(process.stdout, stdout); | 93 _pipeStream(process.stdout, stdout, outputMonitor); |
| 94 _pipeStream(process.stderr, stderr); | 94 _pipeStream(process.stderr, stderr, outputMonitor); |
| 95 | 95 |
| 96 return (completer == null) ? null : completer.future; | 96 return completer.future; |
| 97 } | 97 } |
| 98 | 98 |
| 99 void _pipeStream(InputStream stream, List<String> destination) { | 99 void _pipeStream(InputStream stream, List<String> destination, |
| 100 Function outputMonitor) { |
| 100 var source = new StringInputStream(stream); | 101 var source = new StringInputStream(stream); |
| 101 source.onLine = () { | 102 source.onLine = () { |
| 102 if (source.available() == 0) return; | 103 if (source.available() == 0) return; |
| 103 var line = source.readLine(); | 104 var line = source.readLine(); |
| 104 while (null != line) { | 105 while (null != line) { |
| 105 if (config["immediate"] && line.startsWith('###')) { | 106 if (config["immediate"] && line.startsWith('###')) { |
| 106 // TODO - when we dump the list later skip '###' messages if immediate. | 107 // TODO - when we dump the list later skip '###' messages if immediate. |
| 107 print(line.substring(3)); | 108 print(line.substring(3)); |
| 108 } | 109 } |
| 110 if (outputMonitor != null) { |
| 111 outputMonitor(line); |
| 112 } |
| 109 destination.add(line); | 113 destination.add(line); |
| 110 line = source.readLine(); | 114 line = source.readLine(); |
| 111 } | 115 } |
| 112 }; | 116 }; |
| 113 } | 117 } |
| 114 | 118 |
| 115 /** | 119 /** |
| 116 * Run an external process [cmd] with command line arguments [args]. | 120 * Run an external process [cmd] with command line arguments [args]. |
| 117 * [timeout] can be used to forcefully terminate the process after | 121 * [timeout] can be used to forcefully terminate the process after |
| 118 * some number of seconds. | 122 * some number of seconds. |
| 119 * Returns a [Future] for when the process terminates. | 123 * Returns a [Future] for when the process terminates. |
| 120 */ | 124 */ |
| 121 Future runCommand(String command, List<String> args, [int timeout = 300]) { | 125 Future runCommand(String command, List<String> args, |
| 122 return _processHelper(command, args, timeout); | 126 [int timeout = 300, Function outputMonitor]) { |
| 127 return _processHelper(command, args, timeout, outputMonitor:outputMonitor); |
| 123 } | 128 } |
| 124 | 129 |
| 125 /** | 130 /** |
| 126 * Start an external process [cmd] with command line arguments [args]. | 131 * Start an external process [cmd] with command line arguments [args]. |
| 127 * Returns an ID by which it can later be stopped. | 132 * Returns an ID by which it can later be stopped. |
| 128 */ | 133 */ |
| 129 int startProcess(String command, List<String> args) { | 134 int startProcess(String command, List<String> args, [Function outputMonitor]) { |
| 130 int id = _procId++; | 135 int id = _procId++; |
| 131 _processHelper(command, args, 3000, id); | 136 _processHelper(command, args, 3000, id, |
| 137 outputMonitor:outputMonitor).then((e) { |
| 138 _procs.remove(id); |
| 139 }); |
| 132 return id; | 140 return id; |
| 133 } | 141 } |
| 134 | 142 |
| 143 /** Checks if a process is still running. */ |
| 144 bool isProcessRunning(int id) { |
| 145 return _procs.containsKey(id); |
| 146 } |
| 147 |
| 135 /** | 148 /** |
| 136 * Stop a process previously started with [startProcess] or [runCommand], | 149 * Stop a process previously started with [startProcess] or [runCommand], |
| 137 * given the id string. | 150 * given the id string. |
| 138 */ | 151 */ |
| 139 void stopProcess(int id) { | 152 void stopProcess(int id) { |
| 140 Process p = _procs.remove(id); | 153 if (_procs.containsKey(id)) { |
| 141 p.kill(); | 154 Process p = _procs.remove(id); |
| 155 p.kill(); |
| 156 } |
| 142 } | 157 } |
| 143 | 158 |
| 144 /** Delete a file named [fname] if it exists. */ | 159 /** Delete a file named [fname] if it exists. */ |
| 145 bool cleanup(String fname) { | 160 bool cleanup(String fname) { |
| 146 if (fname != null && !config['keep-files']) { | 161 if (fname != null && !config['keep-files']) { |
| 147 var f = new File(fname); | 162 var f = new File(fname); |
| 148 try { | 163 try { |
| 149 if (f.existsSync()) { | 164 if (f.existsSync()) { |
| 150 logMessage('Removing $fname'); | 165 logMessage('Removing $fname'); |
| 151 f.deleteSync(); | 166 f.deleteSync(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 stderr = new List(); | 178 stderr = new List(); |
| 164 log = new List(); | 179 log = new List(); |
| 165 } | 180 } |
| 166 | 181 |
| 167 void completePipeline([exitCode = 0]) { | 182 void completePipeline([exitCode = 0]) { |
| 168 replyPort.send([stdout, stderr, log, exitCode]); | 183 replyPort.send([stdout, stderr, log, exitCode]); |
| 169 } | 184 } |
| 170 | 185 |
| 171 /** Utility function to log diagnostic messages. */ | 186 /** Utility function to log diagnostic messages. */ |
| 172 void logMessage(msg) => log.add(msg); | 187 void logMessage(msg) => log.add(msg); |
| OLD | NEW |