| 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 #library("total:dartcompiler"); | |
| 6 | |
| 7 #import('dart:io'); | |
| 8 | |
| 9 /** | |
| 10 * A simple wrapper around the Dart compiler (runs dart2js). | |
| 11 * TODO: automatically determine the exec path | |
| 12 */ | |
| 13 class DartCompiler { | |
| 14 final DART2JS_EXECS = const [ | |
| 15 '../../../xcodebuild/ReleaseIA32/dart-sdk/bin/dart2js', | |
| 16 '../../../out/ReleaseIA32/dart-sdk/bin/dart2js']; | |
| 17 | |
| 18 String scriptName; | |
| 19 String out = null; | |
| 20 bool warningsAsErrors = false; | |
| 21 bool enableTypeChecks = false; | |
| 22 bool checkOnly = false; | |
| 23 | |
| 24 String _dart2jsExecPath; | |
| 25 | |
| 26 DartCompiler(String this.scriptName) { | |
| 27 for (var path in DART2JS_EXECS) { | |
| 28 var f = new File(path); | |
| 29 if (f.existsSync()) { | |
| 30 _dart2jsExecPath = path; | |
| 31 break; | |
| 32 } | |
| 33 } | |
| 34 if (_dart2jsExecPath == null) { | |
| 35 throw new Exception("Can't find dart2js on path: " + DART2JS_EXECS); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void compile(void callback(int exitCode, String errorOutput)) { | |
| 40 List<String> args = new List<String>(); | |
| 41 | |
| 42 args.add("--compile-only"); | |
| 43 | |
| 44 if (out != null) { | |
| 45 args.add('--out=' + out); | |
| 46 } | |
| 47 if (warningsAsErrors) { | |
| 48 args.add('--warnings_as_errors'); | |
| 49 } | |
| 50 if (enableTypeChecks) { | |
| 51 args.add('--enable_type_checks'); | |
| 52 } | |
| 53 if (checkOnly) { | |
| 54 args.add('--check-only'); | |
| 55 } | |
| 56 | |
| 57 | |
| 58 args.add(scriptName); | |
| 59 | |
| 60 Process compiler = new Process.start(_dart2jsExecPath, args); | |
| 61 StringBuffer messages = new StringBuffer(); | |
| 62 compiler.onExit = (int status) { | |
| 63 compiler.close(); | |
| 64 callback(status, messages.toString()); | |
| 65 }; | |
| 66 | |
| 67 compiler.stdout.onData = () => _readAll(compiler.stdout, messages); | |
| 68 compiler.stderr.onData = () => _readAll(compiler.stderr, messages); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void _readAll(InputStream input, StringBuffer output) { | |
| 73 while (input.available() != 0) { | |
| 74 output.add(new String.fromCharCodes(input.read())); | |
| 75 } | |
| 76 } | |
| 77 | |
| OLD | NEW |