| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015, 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 import 'dart:io'; |
| 6 |
| 7 import 'dart:async' show |
| 8 StreamSubscription, |
| 9 EventSink, |
| 10 Future; |
| 11 |
| 12 import 'package:dart2js_incremental/dart2js_incremental.dart' show |
| 13 IncrementalCompilationFailed, |
| 14 IncrementalCompiler; |
| 15 |
| 16 import 'package:compiler/src/source_file_provider.dart' show |
| 17 FormattingDiagnosticHandler; |
| 18 |
| 19 import 'watcher.dart'; |
| 20 |
| 21 main(List<String> arguments) async { |
| 22 Uri originalInput = Uri.base.resolve(arguments.first); |
| 23 var watcher = new Watcher(); |
| 24 |
| 25 Uri libraryRoot = Uri.base.resolve('sdk/'); |
| 26 Uri packageRoot = Uri.base.resolve('packages/'); |
| 27 |
| 28 FormattingDiagnosticHandler diagnosticHandler = |
| 29 new FormattingDiagnosticHandler(); |
| 30 |
| 31 OutputProvider outputProvider = new OutputProvider(); |
| 32 |
| 33 void resilientDiagnosticHandler( |
| 34 Uri uri, int begin, int end, String message, kind) { |
| 35 try { |
| 36 diagnosticHandler(uri, begin, end, message, kind); |
| 37 } catch (e) { |
| 38 String name = diagnosticHandler.provider.relativizeUri(uri); |
| 39 print('$name@$begin+${end - begin}: [$kind] $message}'); |
| 40 } |
| 41 } |
| 42 |
| 43 Future inputProvider(Uri uri) { |
| 44 if (uri.scheme == "file") { |
| 45 watcher.watchFile(uri); |
| 46 } |
| 47 return diagnosticHandler.provider(uri); |
| 48 } |
| 49 |
| 50 while (true) { |
| 51 IncrementalCompiler compiler = new IncrementalCompiler( |
| 52 libraryRoot: libraryRoot, |
| 53 packageRoot: packageRoot, |
| 54 inputProvider: inputProvider, |
| 55 diagnosticHandler: resilientDiagnosticHandler, |
| 56 outputProvider: outputProvider); |
| 57 |
| 58 if (!await compiler.compile(originalInput)) { |
| 59 print("Compilation failed"); |
| 60 } else { |
| 61 print('// Compiled JavaScript:'); |
| 62 print(outputProvider['.js']); |
| 63 } |
| 64 |
| 65 int updateCount = 0; |
| 66 while (await watcher.hasChanges()) { |
| 67 try { |
| 68 Map<Uri, Uri> changes = watcher.readChanges(); |
| 69 |
| 70 Stopwatch sw = new Stopwatch()..start(); |
| 71 await compiler.compileUpdates(changes); |
| 72 |
| 73 String updates = '${compiler.allUpdates()}'; |
| 74 |
| 75 sw.stop(); |
| 76 print('// Patch after ${++updateCount} updates,'); |
| 77 print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:'); |
| 78 print(updates); |
| 79 } on IncrementalCompilationFailed catch (error) { |
| 80 print("Incremental compilation failed due to:\n${error.reason}"); |
| 81 break; |
| 82 } |
| 83 } |
| 84 } |
| 85 } |
| 86 |
| 87 /// Output provider which collects output in [output]. |
| 88 class OutputProvider { |
| 89 final Map<String, String> output = new Map<String, String>(); |
| 90 |
| 91 EventSink<String> call(String name, String extension) { |
| 92 return new StringEventSink((String data) { |
| 93 output['$name.$extension'] = data; |
| 94 }); |
| 95 } |
| 96 |
| 97 String operator[](String key) => output[key]; |
| 98 } |
| 99 |
| 100 /// Helper class to collect sources. |
| 101 class StringEventSink implements EventSink<String> { |
| 102 List<String> data = <String>[]; |
| 103 |
| 104 final Function onClose; |
| 105 |
| 106 StringEventSink(this.onClose); |
| 107 |
| 108 void add(String event) { |
| 109 if (data == null) throw 'StringEventSink is closed.'; |
| 110 data.add(event); |
| 111 } |
| 112 |
| 113 void addError(errorEvent, [StackTrace stackTrace]) { |
| 114 throw 'addError($errorEvent, $stackTrace)'; |
| 115 } |
| 116 |
| 117 void close() { |
| 118 if (data != null) { |
| 119 onClose(data.join()); |
| 120 data = null; |
| 121 } |
| 122 } |
| 123 } |
| OLD | NEW |