| 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 #library('dart2js'); | 5 #library('dart2js'); |
| 6 | 6 |
| 7 #import('dart:io'); | 7 #import('dart:io'); |
| 8 #import('dart:uri'); | 8 #import('dart:uri'); |
| 9 #import('dart:utf'); | 9 #import('dart:utf'); |
| 10 | 10 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 } | 49 } |
| 50 | 50 |
| 51 void compile(List<String> argv) { | 51 void compile(List<String> argv) { |
| 52 Uri cwd = getCurrentDirectory(); | 52 Uri cwd = getCurrentDirectory(); |
| 53 bool throwOnError = false; | 53 bool throwOnError = false; |
| 54 bool showWarnings = true; | 54 bool showWarnings = true; |
| 55 bool verbose = false; | 55 bool verbose = false; |
| 56 Uri libraryRoot = cwd; | 56 Uri libraryRoot = cwd; |
| 57 Uri out = cwd.resolve('out.js'); | 57 Uri out = cwd.resolve('out.js'); |
| 58 List<String> options = new List<String>(); | 58 List<String> options = new List<String>(); |
| 59 bool explicitOut = false; |
| 59 | 60 |
| 60 passThrough(String argument) => options.add(argument); | 61 passThrough(String argument) => options.add(argument); |
| 61 | 62 |
| 62 List<String> arguments = <String>[]; | 63 List<String> arguments = <String>[]; |
| 63 List<OptionHandler> handlers = <OptionHandler>[ | 64 List<OptionHandler> handlers = <OptionHandler>[ |
| 64 new OptionHandler('--throw-on-error', (_) => throwOnError = true), | 65 new OptionHandler('--throw-on-error', (_) => throwOnError = true), |
| 65 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), | 66 new OptionHandler('--suppress-warnings', (_) => showWarnings = false), |
| 66 new OptionHandler('--verbose', (_) => verbose = true), | 67 new OptionHandler('--verbose', (_) => verbose = true), |
| 67 new OptionHandler('--library-root=.*', (String argument) { | 68 new OptionHandler('--library-root=.*', (String argument) { |
| 68 String path = nativeToUriPath(extractParameter(argument)); | 69 String path = nativeToUriPath(extractParameter(argument)); |
| 69 if (!path.endsWith("/")) path = "$path/"; | 70 if (!path.endsWith("/")) path = "$path/"; |
| 70 libraryRoot = cwd.resolve(path); | 71 libraryRoot = cwd.resolve(path); |
| 71 }), | 72 }), |
| 72 new OptionHandler('--out=.*', (String argument) { | 73 new OptionHandler('--out=.*', (String argument) { |
| 74 explicitOut = true; |
| 73 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); | 75 out = cwd.resolve(nativeToUriPath(extractParameter(argument))); |
| 74 }), | 76 }), |
| 75 new OptionHandler('--allow-mock-compilation', passThrough), | 77 new OptionHandler('--allow-mock-compilation', passThrough), |
| 76 new OptionHandler('--no-colors', (_) => colors.enabled = false), | 78 new OptionHandler('--no-colors', (_) => colors.enabled = false), |
| 77 new OptionHandler('--enable-checked-mode|--checked|-c', | 79 new OptionHandler('--enable-checked-mode|--checked|-c', |
| 78 (_) => passThrough('--enable_checked_mode')), | 80 (_) => passThrough('--enable_checked_mode')), |
| 79 new OptionHandler('--help', (_) => helpAndExit()), | 81 new OptionHandler('--help', (_) => helpAndExit()), |
| 80 // The following two options must come last. | 82 // The following two options must come last. |
| 81 new OptionHandler('-.*', (String argument) { | 83 new OptionHandler('-.*', (String argument) { |
| 82 fail('Error: unknown option "$argument".'); | 84 helpAndFail('Error: Unknown option "$argument".'); |
| 83 }), | 85 }), |
| 84 new OptionHandler('.*', (String argument) { | 86 new OptionHandler('.*', (String argument) { |
| 85 arguments.add(nativeToUriPath(argument)); | 87 arguments.add(nativeToUriPath(argument)); |
| 86 }) | 88 }) |
| 87 ]; | 89 ]; |
| 88 | 90 |
| 89 parseCommandLine(handlers, argv); | 91 parseCommandLine(handlers, argv); |
| 90 | 92 |
| 91 if (arguments.isEmpty()) { | 93 if (arguments.isEmpty()) { |
| 92 helpAndFail('Error: no file to compile.'); | 94 helpAndFail('Error: No Dart file specified.'); |
| 93 } | 95 } |
| 94 if (arguments.length > 1) { | 96 if (arguments.length > 1) { |
| 95 var extra = arguments.getRange(1, arguments.length - 1); | 97 var extra = arguments.getRange(1, arguments.length - 1); |
| 96 helpAndFail('Error: extra arguments: ${Strings.join(extra, " ")}'); | 98 helpAndFail('Error: Extra arguments: ${Strings.join(extra, " ")}'); |
| 97 } | 99 } |
| 98 | 100 |
| 99 Map<String, SourceFile> sourceFiles = <SourceFile>{}; | 101 Map<String, SourceFile> sourceFiles = <SourceFile>{}; |
| 100 int dartBytesRead = 0; | 102 int dartBytesRead = 0; |
| 101 | 103 |
| 102 Future<String> provider(Uri uri) { | 104 Future<String> provider(Uri uri) { |
| 103 if (uri.scheme != 'file') { | 105 if (uri.scheme != 'file') { |
| 104 throw new IllegalArgumentException(uri); | 106 throw new IllegalArgumentException(uri); |
| 105 } | 107 } |
| 106 String source = readAll(uriPathToNative(uri.path)); | 108 String source = readAll(uriPathToNative(uri.path)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 131 if (fatal && throwOnError) throw new AbortLeg(message); | 133 if (fatal && throwOnError) throw new AbortLeg(message); |
| 132 } | 134 } |
| 133 | 135 |
| 134 Uri uri = cwd.resolve(arguments[0]); | 136 Uri uri = cwd.resolve(arguments[0]); |
| 135 info('compiling $uri'); | 137 info('compiling $uri'); |
| 136 | 138 |
| 137 // TODO(ahe): We expect the future to be complete and call value | 139 // TODO(ahe): We expect the future to be complete and call value |
| 138 // directly. In effect, we don't support truly asynchronous API. | 140 // directly. In effect, we don't support truly asynchronous API. |
| 139 String code = api.compile(uri, libraryRoot, provider, handler, options).value; | 141 String code = api.compile(uri, libraryRoot, provider, handler, options).value; |
| 140 if (code === null) { | 142 if (code === null) { |
| 141 fail('Error: compilation failed.'); | 143 fail('Error: Compilation failed.'); |
| 142 } | 144 } |
| 143 writeString(out, code); | 145 writeString(out, code); |
| 144 int jsBytesWritten = code.length; | 146 int jsBytesWritten = code.length; |
| 145 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' | 147 info('compiled $dartBytesRead bytes Dart -> $jsBytesWritten bytes JS ' |
| 146 + 'in ${relativize(cwd, out)}'); | 148 + 'in ${relativize(cwd, out)}'); |
| 149 if (!explicitOut) { |
| 150 String input = uriPathToNative(arguments[0]); |
| 151 String output = relativize(cwd, out); |
| 152 print('Dart file $input compiled to JavaScript: $output'); |
| 153 } |
| 147 } | 154 } |
| 148 | 155 |
| 149 class AbortLeg { | 156 class AbortLeg { |
| 150 final message; | 157 final message; |
| 151 AbortLeg(this.message); | 158 AbortLeg(this.message); |
| 152 toString() => 'Aborted due to --throw-on-error: $message'; | 159 toString() => 'Aborted due to --throw-on-error: $message'; |
| 153 } | 160 } |
| 154 | 161 |
| 155 void writeString(Uri uri, String text) { | 162 void writeString(Uri uri, String text) { |
| 156 if (uri.scheme != 'file') { | 163 if (uri.scheme != 'file') { |
| 157 fail('Error: unhandled scheme ${uri.scheme}.'); | 164 fail('Error: Unhandled scheme ${uri.scheme}.'); |
| 158 } | 165 } |
| 159 var file = new File(uriPathToNative(uri.path)).openSync(FileMode.WRITE); | 166 var file = new File(uriPathToNative(uri.path)).openSync(FileMode.WRITE); |
| 160 file.writeStringSync(text); | 167 file.writeStringSync(text); |
| 161 file.closeSync(); | 168 file.closeSync(); |
| 162 } | 169 } |
| 163 | 170 |
| 164 String readAll(String filename) { | 171 String readAll(String filename) { |
| 165 var file = (new File(filename)).openSync(FileMode.READ); | 172 var file = (new File(filename)).openSync(FileMode.READ); |
| 166 var length = file.lengthSync(); | 173 var length = file.lengthSync(); |
| 167 var buffer = new List<int>(length); | 174 var buffer = new List<int>(length); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 180 List<String> argv = ['--library-root=${options.script}$root']; | 187 List<String> argv = ['--library-root=${options.script}$root']; |
| 181 argv.addAll(options.arguments); | 188 argv.addAll(options.arguments); |
| 182 compile(argv); | 189 compile(argv); |
| 183 } | 190 } |
| 184 | 191 |
| 185 void help() { | 192 void help() { |
| 186 // This message should be no longer than 22 lines. The default | 193 // This message should be no longer than 22 lines. The default |
| 187 // terminal size normally 80x24. Two lines are used for the prompts | 194 // terminal size normally 80x24. Two lines are used for the prompts |
| 188 // before and after running the compiler. | 195 // before and after running the compiler. |
| 189 print(''' | 196 print(''' |
| 190 dart2js [OPTIONS...] DART-FILE | 197 Usage: dart2js [OPTIONS] DARTFILE |
| 191 | 198 |
| 192 A Dart to JavaScript compiler. | 199 Compiles Dart to JavaScript. |
| 193 | |
| 194 By default, the compiled JavaScript code is saved to a file named | |
| 195 out.js in the current directory. | |
| 196 | 200 |
| 197 Common options: | 201 Common options: |
| 198 | |
| 199 --help Display this message. | 202 --help Display this message. |
| 200 | 203 --out=FILE Save the output to FILE (default: out.js). |
| 201 --out=FILE Save the output to FILE (default is out.js). | 204 --checked Turn on checked mode in generated JavaScript code.'''); |
| 202 | |
| 203 --checked Turn on checked mode in generated JavaScript code. | |
| 204 '''); | |
| 205 } | 205 } |
| 206 | 206 |
| 207 void helpAndExit() { | 207 void helpAndExit() { |
| 208 help(); | 208 help(); |
| 209 exit(0); | 209 exit(0); |
| 210 } | 210 } |
| 211 | 211 |
| 212 void helpAndFail(String message) { | 212 void helpAndFail(String message) { |
| 213 help(); | 213 help(); |
| 214 print(''); |
| 214 fail(message); | 215 fail(message); |
| 215 } | 216 } |
| 216 | 217 |
| 217 void main() { | 218 void main() { |
| 218 try { | 219 try { |
| 219 compilerMain(new Options()); | 220 compilerMain(new Options()); |
| 220 } catch (var exception, var trace) { | 221 } catch (var exception, var trace) { |
| 221 try { | 222 try { |
| 222 print('Internal error: $exception'); | 223 print('Internal error: $exception'); |
| 223 } catch (var ignored) { | 224 } catch (var ignored) { |
| 224 print('Internal error: error while printing exception'); | 225 print('Internal error: error while printing exception'); |
| 225 } | 226 } |
| 226 try { | 227 try { |
| 227 print(trace); | 228 print(trace); |
| 228 } finally { | 229 } finally { |
| 229 exit(253); // 253 is recognized as a crash by our test scripts. | 230 exit(253); // 253 is recognized as a crash by our test scripts. |
| 230 } | 231 } |
| 231 } | 232 } |
| 232 } | 233 } |
| OLD | NEW |