| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 import 'dart:utf'; | 10 import 'dart:utf'; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 helpAndFail('Error: No Dart file specified.'); | 191 helpAndFail('Error: No Dart file specified.'); |
| 192 } | 192 } |
| 193 if (arguments.length > 1) { | 193 if (arguments.length > 1) { |
| 194 var extra = arguments.getRange(1, arguments.length - 1); | 194 var extra = arguments.getRange(1, arguments.length - 1); |
| 195 helpAndFail('Error: Extra arguments: ${Strings.join(extra, " ")}'); | 195 helpAndFail('Error: Extra arguments: ${Strings.join(extra, " ")}'); |
| 196 } | 196 } |
| 197 | 197 |
| 198 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; | 198 Map<String, SourceFile> sourceFiles = <String, SourceFile>{}; |
| 199 int dartBytesRead = 0; | 199 int dartBytesRead = 0; |
| 200 | 200 |
| 201 Future<String> provider(Uri uri) { | 201 Future<String> inputProvider(Uri uri) { |
| 202 if (uri.scheme != 'file') { | 202 if (uri.scheme != 'file') { |
| 203 throw new ArgumentError(uri); | 203 throw new ArgumentError(uri); |
| 204 } | 204 } |
| 205 String source; | 205 String source; |
| 206 try { | 206 try { |
| 207 source = readAll(uriPathToNative(uri.path)); | 207 source = readAll(uriPathToNative(uri.path)); |
| 208 } on FileIOException catch (ex) { | 208 } on FileIOException catch (ex) { |
| 209 throw 'Error: Cannot read "${relativize(cwd, uri, isWindows)}" ' | 209 throw 'Error: Cannot read "${relativize(cwd, uri, isWindows)}" ' |
| 210 '(${ex.osError}).'; | 210 '(${ex.osError}).'; |
| 211 } | 211 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 225 } | 225 } |
| 226 | 226 |
| 227 bool isAborting = false; | 227 bool isAborting = false; |
| 228 | 228 |
| 229 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; | 229 final int FATAL = api.Diagnostic.CRASH.ordinal | api.Diagnostic.ERROR.ordinal; |
| 230 final int INFO = | 230 final int INFO = |
| 231 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; | 231 api.Diagnostic.INFO.ordinal | api.Diagnostic.VERBOSE_INFO.ordinal; |
| 232 | 232 |
| 233 void handler(Uri uri, int begin, int end, String message, | 233 void handler(Uri uri, int begin, int end, String message, |
| 234 api.Diagnostic kind) { | 234 api.Diagnostic kind) { |
| 235 if (identical(kind.name, 'source map')) { | |
| 236 // TODO(podivilov): We should find a better way to return source maps from | |
| 237 // emitter. Using diagnostic handler for that purpose is a temporary hack. | |
| 238 writeString(sourceMapOut, message); | |
| 239 return; | |
| 240 } | |
| 241 | |
| 242 if (isAborting) return; | 235 if (isAborting) return; |
| 243 isAborting = identical(kind, api.Diagnostic.CRASH); | 236 isAborting = identical(kind, api.Diagnostic.CRASH); |
| 244 bool fatal = (kind.ordinal & FATAL) != 0; | 237 bool fatal = (kind.ordinal & FATAL) != 0; |
| 245 bool isInfo = (kind.ordinal & INFO) != 0; | 238 bool isInfo = (kind.ordinal & INFO) != 0; |
| 246 if (isInfo && uri == null && !identical(kind, api.Diagnostic.INFO)) { | 239 if (isInfo && uri == null && !identical(kind, api.Diagnostic.INFO)) { |
| 247 info(message, kind); | 240 info(message, kind); |
| 248 return; | 241 return; |
| 249 } | 242 } |
| 250 var color; | 243 var color; |
| 251 if (!enableColors) { | 244 if (!enableColors) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 279 } | 272 } |
| 280 } | 273 } |
| 281 | 274 |
| 282 Uri uri = cwd.resolve(arguments[0]); | 275 Uri uri = cwd.resolve(arguments[0]); |
| 283 if (packageRoot == null) { | 276 if (packageRoot == null) { |
| 284 packageRoot = uri.resolve('./packages/'); | 277 packageRoot = uri.resolve('./packages/'); |
| 285 } | 278 } |
| 286 | 279 |
| 287 info('package root is $packageRoot'); | 280 info('package root is $packageRoot'); |
| 288 | 281 |
| 289 // TODO(ahe): We expect the future to be complete and call value | 282 compilationDone(String code) { |
| 290 // directly. In effect, we don't support truly asynchronous API. | 283 if (code == null) { |
| 291 String code = deprecatedFutureValue( | 284 fail('Error: Compilation failed.'); |
| 292 api.compile(uri, libraryRoot, packageRoot, provider, handler, options)); | 285 } |
| 293 if (code == null) { | 286 writeString(new Uri.fromString('$out.deps'), getDepsOutput(sourceFiles)); |
| 294 fail('Error: Compilation failed.'); | 287 int bytesWritten = code.length; |
| 288 info('compiled $dartBytesRead bytes Dart -> $bytesWritten bytes ' |
| 289 '$outputLanguage in ${relativize(cwd, out, isWindows)}'); |
| 290 if (!explicitOut) { |
| 291 String input = uriPathToNative(arguments[0]); |
| 292 String output = relativize(cwd, out, isWindows); |
| 293 print('Dart file $input compiled to $outputLanguage: $output'); |
| 294 } |
| 295 } | 295 } |
| 296 String sourceMapFileName = | 296 |
| 297 sourceMapOut.path.substring(sourceMapOut.path.lastIndexOf('/') + 1); | 297 Sink<String> outputProvider(String name, String extension) { |
| 298 code = '$code\n//@ sourceMappingURL=${sourceMapFileName}'; | 298 Uri uri; |
| 299 writeString(out, code); | 299 String sourceMapFileName; |
| 300 writeString(new Uri.fromString('$out.deps'), getDepsOutput(sourceFiles)); | 300 if (name == '') { |
| 301 int bytesWritten = code.length; | 301 if (extension == 'js' || extension == 'dart') { |
| 302 info('compiled $dartBytesRead bytes Dart -> $bytesWritten bytes ' | 302 uri = out; |
| 303 '$outputLanguage in ${relativize(cwd, out, isWindows)}'); | 303 sourceMapFileName = |
| 304 if (!explicitOut) { | 304 sourceMapOut.path.substring(sourceMapOut.path.lastIndexOf('/') + 1); |
| 305 String input = uriPathToNative(arguments[0]); | 305 } else if (extension == 'js.map' || extension == 'dart.map') { |
| 306 String output = relativize(cwd, out, isWindows); | 306 uri = sourceMapOut; |
| 307 print('Dart file $input compiled to $outputLanguage: $output'); | 307 } else { |
| 308 fail('Error: Unknown extension: $extension'); |
| 309 } |
| 310 } else { |
| 311 uri = out.resolve('$name.$extension'); |
| 312 } |
| 313 |
| 314 if (uri.scheme != 'file') { |
| 315 fail('Error: Unhandled scheme ${uri.scheme} in $uri.'); |
| 316 } |
| 317 var outputStream = new File(uriPathToNative(uri.path)).openOutputStream(); |
| 318 |
| 319 onDone() { |
| 320 if (sourceMapFileName != null) { |
| 321 outputStream.writeString('//@ sourceMappingURL='); |
| 322 outputStream.writeString(sourceMapFileName); |
| 323 outputStream.writeString('\n'); |
| 324 } |
| 325 outputStream.close(); |
| 326 } |
| 327 |
| 328 return new StreamController<String>() |
| 329 ..listen(outputStream.writeString, onDone: onDone); |
| 308 } | 330 } |
| 331 |
| 332 api.compile(uri, libraryRoot, packageRoot, |
| 333 inputProvider, handler, |
| 334 options, outputProvider) |
| 335 .then(compilationDone); |
| 309 } | 336 } |
| 310 | 337 |
| 311 class AbortLeg { | 338 class AbortLeg { |
| 312 final message; | 339 final message; |
| 313 AbortLeg(this.message); | 340 AbortLeg(this.message); |
| 314 toString() => 'Aborted due to --throw-on-error: $message'; | 341 toString() => 'Aborted due to --throw-on-error: $message'; |
| 315 } | 342 } |
| 316 | 343 |
| 317 void writeString(Uri uri, String text) { | 344 void writeString(Uri uri, String text) { |
| 318 if (uri.scheme != 'file') { | 345 if (uri.scheme != 'file') { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 } catch (ignored) { | 493 } catch (ignored) { |
| 467 print('Internal error: error while printing exception'); | 494 print('Internal error: error while printing exception'); |
| 468 } | 495 } |
| 469 try { | 496 try { |
| 470 print(trace); | 497 print(trace); |
| 471 } finally { | 498 } finally { |
| 472 exit(253); // 253 is recognized as a crash by our test scripts. | 499 exit(253); // 253 is recognized as a crash by our test scripts. |
| 473 } | 500 } |
| 474 } | 501 } |
| 475 } | 502 } |
| OLD | NEW |