Chromium Code Reviews| 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/*<String | List<int>>*/ inputProvider(Uri uri) { | |
|
kasperl
2015/01/20 10:23:00
I probably wouldn't bother with the fancy "type in
ahe
2015/01/20 10:45:52
Done.
| |
| 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 | |
|
kasperl
2015/01/20 10:23:00
Only one newline.
ahe
2015/01/20 10:45:52
Done.
| |
| 65 | |
| 66 int updateCount = 1; | |
|
kasperl
2015/01/20 10:23:00
0 and then use prefix ++?
ahe
2015/01/20 10:45:52
Done.
| |
| 67 while (await watcher.hasChanges()) { | |
| 68 try { | |
| 69 Map<Uri, Uri> changes = watcher.readChanges(); | |
| 70 | |
| 71 Stopwatch sw = new Stopwatch()..start(); | |
| 72 await compiler.compileUpdates(changes); | |
| 73 | |
| 74 String updates = '${compiler.allUpdates()}'; | |
| 75 | |
| 76 sw.stop(); | |
| 77 print('// Patch after ${updateCount++} updates,'); | |
| 78 print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:'); | |
| 79 print(updates); | |
| 80 } on IncrementalCompilationFailed catch (error) { | |
| 81 print("Incremental compilation failed due to:\n${error.reason}"); | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 /// Output provider which collect output in [output]. | |
|
kasperl
2015/01/20 10:23:00
collect -> collects
ahe
2015/01/20 10:45:52
Done.
| |
| 89 class OutputProvider { | |
| 90 final Map<String, String> output = new Map<String, String>(); | |
| 91 | |
| 92 EventSink<String> call(String name, String extension) { | |
| 93 return new StringEventSink((String data) { | |
| 94 output['$name.$extension'] = data; | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 String operator[] (String key) => output[key]; | |
|
kasperl
2015/01/20 10:23:00
I'd remove the space between [] and (.
ahe
2015/01/20 10:45:52
Done.
| |
| 99 } | |
| 100 | |
| 101 /// Helper class to collect sources. | |
| 102 class StringEventSink implements EventSink<String> { | |
| 103 List<String> data = <String>[]; | |
| 104 | |
| 105 final Function onClose; | |
| 106 | |
| 107 StringEventSink(this.onClose); | |
| 108 | |
| 109 void add(String event) { | |
| 110 if (data == null) throw 'StringEventSink is closed.'; | |
| 111 data.add(event); | |
| 112 } | |
| 113 | |
| 114 void addError(errorEvent, [StackTrace stackTrace]) { | |
| 115 throw 'addError($errorEvent, $stackTrace)'; | |
| 116 } | |
| 117 | |
| 118 void close() { | |
| 119 if (data != null) { | |
| 120 onClose(data.join()); | |
| 121 data = null; | |
| 122 } | |
| 123 } | |
| 124 } | |
| OLD | NEW |