Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 import 'dart:io'; | 5 import 'dart:io'; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show |
| 8 StreamSubscription, | |
| 9 EventSink, | 8 EventSink, |
| 10 Future; | 9 Future, |
| 10 Stream, | |
| 11 StreamController, | |
| 12 StreamSubscription; | |
| 11 | 13 |
| 12 import 'package:dart2js_incremental/dart2js_incremental.dart' show | 14 import 'package:dart2js_incremental/dart2js_incremental.dart' show |
| 13 IncrementalCompilationFailed, | 15 IncrementalCompilationFailed, |
| 14 IncrementalCompiler; | 16 IncrementalCompiler; |
| 15 | 17 |
| 16 import 'package:compiler/src/source_file_provider.dart' show | 18 import 'package:compiler/src/source_file_provider.dart' show |
| 17 FormattingDiagnosticHandler; | 19 FormattingDiagnosticHandler; |
| 18 | 20 |
| 19 import 'watcher.dart'; | 21 import 'watcher.dart'; |
| 20 | 22 |
| 21 main(List<String> arguments) async { | 23 main(List<String> arguments) { |
| 22 Uri originalInput = Uri.base.resolve(arguments.first); | 24 int updateCount = 0; |
| 25 StreamSubscription<CompilerEvent> subscritpion = | |
|
kasperl
2015/01/20 13:58:31
subscription
ahe
2015/01/20 14:04:33
Done.
| |
| 26 compile(Uri.base.resolve(arguments.first)).listen(null); | |
| 27 subscritpion.onData((CompilerEvent event) { | |
| 28 switch (event.kind) { | |
| 29 case IncrementalKind.FULL: | |
| 30 updateCount = 0; | |
| 31 print('// Compiled JavaScript:'); | |
| 32 print(event['.js']); | |
| 33 break; | |
| 34 | |
| 35 case IncrementalKind.INCREMENTAL: | |
| 36 Stopwatch sw = event.stopwatch..start(); | |
| 37 String updates = '${event.compiler.allUpdates()}'; | |
| 38 sw.stop(); | |
| 39 | |
| 40 print('// Patch after ${++updateCount} updates,'); | |
| 41 print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:'); | |
| 42 print(updates); | |
| 43 break; | |
| 44 | |
| 45 case IncrementalKind.ERROR: | |
| 46 updateCount = 0; | |
| 47 print("Compilation failed"); | |
| 48 break; | |
| 49 | |
| 50 default: | |
| 51 throw "Unknown kind: ${event.kind}"; | |
| 52 } | |
| 53 }); | |
| 54 subscritpion.onError((error, StackTrace trace) { | |
| 55 if (error is IncrementalCompilationFailed) { | |
| 56 print("Incremental compilation failed due to:\n${error.reason}"); | |
| 57 } else { | |
| 58 throw error; | |
| 59 } | |
| 60 }); | |
| 61 } | |
| 62 | |
| 63 Stream<CompilerEvent> compile(Uri originalInput) { | |
| 64 StreamController<CompilerEvent> controller = | |
| 65 new StreamController<CompilerEvent>(); | |
| 66 compileToStream(originalInput, controller); | |
| 67 return controller.stream; | |
| 68 } | |
| 69 | |
| 70 compileToStream( | |
| 71 Uri originalInput, | |
| 72 StreamController<CompilerEvent> controller) async { | |
| 23 var watcher = new Watcher(); | 73 var watcher = new Watcher(); |
| 24 | 74 |
| 25 Uri libraryRoot = Uri.base.resolve('sdk/'); | 75 Uri libraryRoot = Uri.base.resolve('sdk/'); |
| 26 Uri packageRoot = Uri.base.resolve('packages/'); | 76 Uri packageRoot = Uri.base.resolve('packages/'); |
| 27 | 77 |
| 28 FormattingDiagnosticHandler diagnosticHandler = | 78 FormattingDiagnosticHandler diagnosticHandler = |
| 29 new FormattingDiagnosticHandler(); | 79 new FormattingDiagnosticHandler(); |
| 30 | 80 |
| 31 OutputProvider outputProvider = new OutputProvider(); | 81 OutputProvider outputProvider = new OutputProvider(); |
| 32 | 82 |
| 33 void resilientDiagnosticHandler( | 83 void resilientDiagnosticHandler( |
| 34 Uri uri, int begin, int end, String message, kind) { | 84 Uri uri, int begin, int end, String message, kind) { |
| 35 try { | 85 try { |
| 36 diagnosticHandler(uri, begin, end, message, kind); | 86 diagnosticHandler(uri, begin, end, message, kind); |
| 37 } catch (e) { | 87 } catch (e) { |
| 38 String name = diagnosticHandler.provider.relativizeUri(uri); | 88 String name = diagnosticHandler.provider.relativizeUri(uri); |
| 39 print('$name@$begin+${end - begin}: [$kind] $message}'); | 89 print('$name@$begin+${end - begin}: [$kind] $message}'); |
| 40 } | 90 } |
| 41 } | 91 } |
| 42 | 92 |
| 43 Future inputProvider(Uri uri) { | 93 Future inputProvider(Uri uri) { |
| 44 if (uri.scheme == "file") { | 94 if (uri.scheme == "file") { |
| 45 watcher.watchFile(uri); | 95 watcher.watchFile(uri); |
| 46 } | 96 } |
| 47 return diagnosticHandler.provider(uri); | 97 return diagnosticHandler.provider(uri); |
| 48 } | 98 } |
| 49 | 99 |
| 50 while (true) { | 100 while (true) { |
| 101 Stopwatch sw = new Stopwatch()..start(); | |
| 51 IncrementalCompiler compiler = new IncrementalCompiler( | 102 IncrementalCompiler compiler = new IncrementalCompiler( |
| 52 libraryRoot: libraryRoot, | 103 libraryRoot: libraryRoot, |
| 53 packageRoot: packageRoot, | 104 packageRoot: packageRoot, |
| 54 inputProvider: inputProvider, | 105 inputProvider: inputProvider, |
| 55 diagnosticHandler: resilientDiagnosticHandler, | 106 diagnosticHandler: resilientDiagnosticHandler, |
| 56 outputProvider: outputProvider); | 107 outputProvider: outputProvider); |
| 57 | 108 |
| 58 if (!await compiler.compile(originalInput)) { | 109 bool success = await compiler.compile(originalInput); |
| 59 print("Compilation failed"); | 110 sw.stop(); |
| 111 if (success) { | |
| 112 controller.add( | |
| 113 new CompilerEvent( | |
| 114 IncrementalKind.FULL, compiler, outputProvider.output, sw)); | |
| 60 } else { | 115 } else { |
| 61 print('// Compiled JavaScript:'); | 116 controller.add( |
| 62 print(outputProvider['.js']); | 117 new CompilerEvent( |
| 118 IncrementalKind.ERROR, compiler, outputProvider.output, sw)); | |
| 63 } | 119 } |
| 64 | 120 |
| 65 int updateCount = 0; | |
| 66 while (await watcher.hasChanges()) { | 121 while (await watcher.hasChanges()) { |
| 67 try { | 122 try { |
| 68 Map<Uri, Uri> changes = watcher.readChanges(); | 123 Map<Uri, Uri> changes = watcher.readChanges(); |
| 69 | 124 |
| 70 Stopwatch sw = new Stopwatch()..start(); | 125 sw = new Stopwatch()..start(); |
| 71 await compiler.compileUpdates(changes); | 126 await compiler.compileUpdates(changes); |
| 127 sw.stop(); | |
| 72 | 128 |
| 73 String updates = '${compiler.allUpdates()}'; | 129 controller.add( |
| 130 new CompilerEvent( | |
| 131 IncrementalKind.INCREMENTAL, compiler, outputProvider.output, | |
| 132 sw)); | |
| 74 | 133 |
| 75 sw.stop(); | 134 } on IncrementalCompilationFailed catch (error, trace) { |
| 76 print('// Patch after ${++updateCount} updates,'); | 135 controller.addError(error, trace); |
| 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; | 136 break; |
| 82 } | 137 } |
| 83 } | 138 } |
| 84 } | 139 } |
| 85 } | 140 } |
| 86 | 141 |
| 87 /// Output provider which collects output in [output]. | 142 /// Output provider which collects output in [output]. |
| 88 class OutputProvider { | 143 class OutputProvider { |
| 89 final Map<String, String> output = new Map<String, String>(); | 144 final Map<String, String> output = new Map<String, String>(); |
| 90 | 145 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 114 throw 'addError($errorEvent, $stackTrace)'; | 169 throw 'addError($errorEvent, $stackTrace)'; |
| 115 } | 170 } |
| 116 | 171 |
| 117 void close() { | 172 void close() { |
| 118 if (data != null) { | 173 if (data != null) { |
| 119 onClose(data.join()); | 174 onClose(data.join()); |
| 120 data = null; | 175 data = null; |
| 121 } | 176 } |
| 122 } | 177 } |
| 123 } | 178 } |
| 179 | |
| 180 enum IncrementalKind { | |
| 181 FULL, | |
| 182 INCREMENTAL, | |
| 183 ERROR, | |
| 184 } | |
| 185 | |
| 186 class CompilerEvent { | |
| 187 final IncrementalKind kind; | |
| 188 | |
| 189 final IncrementalCompiler compiler; | |
| 190 | |
| 191 final Map<String, String> _output; | |
| 192 | |
| 193 final Stopwatch stopwatch; | |
| 194 | |
| 195 CompilerEvent(this.kind, this.compiler, this._output, this.stopwatch); | |
| 196 | |
| 197 String operator[](String key) => _output[key]; | |
| 198 } | |
| OLD | NEW |