Chromium Code Reviews| 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 List stdout, stderr, log; | |
| 6 var replyPort; | |
| 7 int _procnum = 0; | |
| 8 Map _procs = {}; | |
|
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
it should be perfectly valid to use the int values
gram
2012/10/02 23:01:59
Done.
| |
| 9 | |
| 10 /** | |
| 11 * Create a file path for a temporary file. The file will be in the | |
| 12 * [tmpDir] directory, with name [basis], but with any extension | |
| 13 * stripped and replaced by [suffix]. | |
| 14 */ | |
| 15 String createTempName(String tmpDir, String basis, String suffix) { | |
| 16 var p = new Path(basis); | |
| 17 return '$tmpDir${Platform.pathSeparator}' | |
| 18 '${p.filenameWithoutExtension}${suffix}'; | |
| 19 } | |
| 20 | |
| 21 /** Create a file [fileName] and populate it with [contents]. */ | |
| 22 void writeFile(String fileName, String contents) { | |
| 23 var file = new File(fileName); | |
| 24 var ostream = file.openOutputStream(FileMode.WRITE); | |
| 25 ostream.writeString(contents); | |
| 26 ostream.close(); | |
| 27 } | |
| 28 | |
| 29 /* | |
| 30 * Run an external process [cmd] with command line arguments [args]. | |
| 31 * [timeout] can be used to forcefully terminate the process after | |
| 32 * some number of seconds. This is used by runCommand and startProcess. | |
| 33 * If [procId] is null (runCommand) then this will return a [Future] for | |
| 34 * when the process terminates; if [procId] is instead a non-null string | |
| 35 * (startProcess) then a reference to the [Process] will be put in a | |
| 36 * map with key [procId] and [procId] will be returned; in this case | |
| 37 * the process can be terminated later by calling [stopProcess] and | |
| 38 * passing in the [procId]. | |
| 39 */ | |
| 40 _processHelper(String command, List<String> args, | |
| 41 [int timeout = 300, String procId = null]) { | |
| 42 var completer = null; | |
| 43 if (procId == null) { | |
| 44 completer = new Completer(); | |
| 45 } | |
| 46 log.add('Running $command ${Strings.join(args, " ")}'); | |
| 47 var timer = null; | |
| 48 var process = Process.start(command, args); | |
| 49 | |
| 50 process.onStart = () { | |
| 51 timer = new Timer(1000 * timeout, (t) { | |
| 52 timer = null; | |
| 53 process.kill(); | |
| 54 }); | |
| 55 }; | |
| 56 process.onExit = (exitCode) { | |
| 57 if (timer != null) { | |
| 58 timer.cancel(); | |
| 59 } | |
| 60 process.close(); | |
| 61 if (completer != null) { | |
| 62 completer.complete(exitCode); | |
| 63 } | |
| 64 }; | |
| 65 process.onError = (e) { | |
| 66 stderr.add("Error starting process:"); | |
| 67 stderr.add(" Command: $command"); | |
| 68 stderr.add(" Error: $e"); | |
| 69 completePipeline(-1); | |
| 70 }; | |
| 71 | |
| 72 StringInputStream stdoutStringStream = | |
|
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
nits (here and below):
- remove type on the left
gram
2012/10/02 23:01:59
Done.
| |
| 73 new StringInputStream(process.stdout); | |
| 74 StringInputStream stderrStringStream = | |
|
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
ditto
gram
2012/10/02 23:01:59
Done.
| |
| 75 new StringInputStream(process.stderr); | |
| 76 stdoutStringStream.onLine = _makeReadHandler(stdoutStringStream, stdout); | |
| 77 stderrStringStream.onLine = _makeReadHandler(stderrStringStream, stderr); | |
| 78 if (procId == null) { | |
| 79 return completer.future; | |
| 80 } else { | |
| 81 _procs[procId] = process; | |
| 82 return procId; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 Function _makeReadHandler(StringInputStream source, List<String> destination) { | |
| 87 return () { | |
| 88 if (source.closed) return; | |
| 89 var line = source.readLine(); | |
| 90 while (null != line) { | |
| 91 if (config["immediate"] && line.startsWith('###')) { | |
| 92 // TODO - when we dump the list later skip '###' messages if immediate. | |
| 93 print(line.substring(3)); | |
| 94 } | |
| 95 destination.add(line); | |
| 96 line = source.readLine(); | |
| 97 } | |
| 98 }; | |
| 99 } | |
| 100 | |
| 101 /** | |
| 102 * Run an external process [cmd] with command line arguments [args]. | |
| 103 * [timeout] can be used to forcefully terminate the process after | |
| 104 * some number of seconds. | |
| 105 * Returns a [Future] for when the process terminates. | |
| 106 */ | |
| 107 runCommand(String command, List<String> args, [int timeout = 300]) { | |
|
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
add return type
gram
2012/10/02 23:01:59
Done.
| |
| 108 return _processHelper(command, args, timeout); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Start an external process [cmd] with command line arguments [args]. | |
| 113 * Returns an ID by which it can later be stopped. | |
| 114 */ | |
| 115 String startProcess(String command, List<String> args) { | |
| 116 return _processHelper(command, args, 3000, '${_procnum++}'); | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * Stop a process previously started with [startProcess] or [runCommand], | |
| 121 * given the id string. | |
| 122 */ | |
| 123 void stopProcess(String id) { | |
| 124 Process p = _procs.remove(id); | |
| 125 p.kill(); | |
| 126 } | |
| 127 | |
| 128 /** Delete a file named [fname] if it exists. */ | |
| 129 bool cleanup(String fname) { | |
| 130 if (!config['keep-files']) { | |
| 131 var f = new File(fname); | |
| 132 try { | |
| 133 if (f.existsSync()) { | |
| 134 f.deleteSync(); | |
| 135 } | |
| 136 } catch (e) { | |
| 137 return false; | |
| 138 } | |
| 139 } | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 initPipeline(port) { | |
| 144 replyPort = port; | |
| 145 stdout = new List(); | |
| 146 stderr = new List(); | |
| 147 log = new List(); | |
| 148 } | |
| 149 | |
| 150 void completePipeline([exitCode = 0]) { | |
| 151 replyPort.send([stdout, stderr, log, exitCode]); | |
| 152 } | |
| 153 | |
| 154 /** Utility function to log diagnostic messages. */ | |
| 155 void logMessage(msg) => log.add(msg); | |
| 156 | |
| OLD | NEW |