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 = {}; | |
| 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); | |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
fix indentation, hopefully that will make the next
gram
2012/10/01 21:10:51
Weird, I don't see this indentation issue, althoug
| |
| 17 return '$tmpDir${Platform.pathSeparator}${p.filenameWithoutExtension}${s uffix}'; | |
| 18 } | |
| 19 | |
| 20 /** Create a file [fileName] and populate it with [contents]. */ | |
| 21 void writeFile(String fileName, String contents) { | |
| 22 var file = new File(fileName); | |
| 23 var ostream = file.openOutputStream(FileMode.WRITE); | |
| 24 ostream.writeString(contents); | |
| 25 ostream.close(); | |
| 26 } | |
| 27 | |
| 28 /** | |
| 29 * Run an external process [cmd] with command line arguments [args]. | |
| 30 * [timeout] can be used to forcefully terminate the process after | |
| 31 * some number of seconds. If [procId] is null then this will | |
| 32 * return a [Future] for when the process terminates; if [procId] | |
| 33 * is instead a non-null string then a reference to the [Process] | |
| 34 * will be put in a map with key [procId] and [procId] will be | |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
why not always return the Future?
If I passed th
gram
2012/10/01 21:10:51
I see your point, but it is a usage issue. There a
Siggi Cherem (dart-lang)
2012/10/02 17:45:20
I really find it confusing when functions return s
gram
2012/10/02 23:01:59
That's somewhat like going back to the way I had i
| |
| 35 * returned; in this case the process can be terminated later by | |
| 36 * calling [stopProcess] and passing in the [procId]. | |
| 37 */ | |
| 38 runCommand(String command, List<String> args, | |
| 39 [int timeout = 300, String procId = null]) { | |
| 40 var completer = null; | |
| 41 if (procId == null) { | |
| 42 completer = new Completer(); | |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
nit: un-indent
gram
2012/10/01 21:10:51
Again, I'm not seeing this indent issue. Very stra
| |
| 43 } | |
| 44 log.add('Running $command ${Strings.join(args, " ")}'); | |
| 45 var timer = null; | |
| 46 var process = Process.start(command, args); | |
| 47 | |
| 48 process.onStart = () { | |
| 49 timer = new Timer(1000 * timeout, (t) { | |
| 50 timer = null; | |
| 51 process.kill(); | |
| 52 }); | |
| 53 }; | |
| 54 process.onExit = (exitCode) { | |
| 55 if (timer != null) { | |
| 56 timer.cancel(); | |
| 57 } | |
| 58 process.close(); | |
| 59 if (completer != null) { | |
| 60 completer.complete(exitCode); | |
| 61 } | |
| 62 }; | |
| 63 process.onError = (e) { | |
| 64 stderr.add("Error starting process:"); | |
| 65 stderr.add(" Command: $command"); | |
| 66 stderr.add(" Error: $e"); | |
| 67 completePipeline(-1); | |
| 68 }; | |
| 69 | |
| 70 StringInputStream stdoutStringStream = | |
| 71 new StringInputStream(process.stdout); | |
| 72 StringInputStream stderrStringStream = | |
| 73 new StringInputStream(process.stderr); | |
| 74 stdoutStringStream.onLine = _makeReadHandler(stdoutStringStream, stdout); | |
| 75 stderrStringStream.onLine = _makeReadHandler(stderrStringStream, stderr); | |
| 76 if (procId == null) { | |
| 77 return completer.future; | |
| 78 } else { | |
| 79 _procs[procId] = process; | |
| 80 return procId; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 Function _makeReadHandler(StringInputStream source, List<String> destination) { | |
| 85 return () { | |
| 86 if (source.closed) return; | |
| 87 var line = source.readLine(); | |
| 88 while (null != line) { | |
| 89 if (config["immediate"] && line.startsWith('###')) { | |
| 90 // TODO - when we dump the list later skip '###' messages if immediate. | |
| 91 print(line.substring(3)); | |
| 92 } | |
| 93 destination.add(line); | |
| 94 line = source.readLine(); | |
| 95 } | |
| 96 }; | |
| 97 } | |
| 98 | |
| 99 /** | |
| 100 * Start an external process [cmd] with command line arguments [args]. | |
| 101 * Returns an ID by which it can later be stopped. | |
| 102 */ | |
| 103 String startProcess(String command, List<String> args) { | |
| 104 return runCommand(command, args, 3000, '${_procnum++}'); | |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
ditto
gram
2012/10/01 21:10:51
ditto
| |
| 105 } | |
| 106 | |
| 107 /** | |
| 108 * Stop a process previously started with [startProcess] or [runCommand], | |
| 109 * given the id string. | |
| 110 */ | |
| 111 void stopProcess(String id) { | |
| 112 Process p = _procs.remove(id); | |
| 113 p.kill(); | |
| 114 } | |
| 115 | |
| 116 /** Delete a file named [fname] if it exists. */ | |
| 117 bool cleanup(String fname) { | |
| 118 if (!config['keep-files']) { | |
| 119 var f = new File(fname); | |
| 120 try { | |
| 121 if (f.existsSync()) { | |
|
Siggi Cherem (dart-lang)
2012/09/28 01:53:51
ditto
gram
2012/10/01 21:10:51
ditto!
| |
| 122 f.deleteSync(); | |
| 123 } | |
| 124 } catch (e) { | |
| 125 return false; | |
| 126 } | |
| 127 } | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 initPipeline(port) { | |
| 132 replyPort = port; | |
| 133 stdout = new List(); | |
| 134 stderr = new List(); | |
| 135 log = new List(); | |
| 136 } | |
| 137 | |
| 138 void completePipeline([exitCode = 0]) { | |
| 139 replyPort.send([stdout, stderr, log, exitCode]); | |
| 140 } | |
| 141 | |
| 142 /** Utility function to log diagnostic messages. */ | |
| 143 void logMessage(msg) => log.add(msg); | |
| 144 | |
| OLD | NEW |