Chromium Code Reviews| Index: dart/pkg/dart2js_incremental/lib/compiler.dart |
| diff --git a/dart/pkg/dart2js_incremental/lib/compiler.dart b/dart/pkg/dart2js_incremental/lib/compiler.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..24d10927acbd130da17bac6fbc6fc16e3229328a |
| --- /dev/null |
| +++ b/dart/pkg/dart2js_incremental/lib/compiler.dart |
| @@ -0,0 +1,124 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:io'; |
| + |
| +import 'dart:async' show |
| + StreamSubscription, |
| + EventSink, |
| + Future; |
| + |
| +import 'package:dart2js_incremental/dart2js_incremental.dart' show |
| + IncrementalCompilationFailed, |
| + IncrementalCompiler; |
| + |
| +import 'package:compiler/src/source_file_provider.dart' show |
| + FormattingDiagnosticHandler; |
| + |
| +import 'watcher.dart'; |
| + |
| +main(List<String> arguments) async { |
| + Uri originalInput = Uri.base.resolve(arguments.first); |
| + var watcher = new Watcher(); |
| + |
| + Uri libraryRoot = Uri.base.resolve('sdk/'); |
| + Uri packageRoot = Uri.base.resolve('packages/'); |
| + |
| + FormattingDiagnosticHandler diagnosticHandler = |
| + new FormattingDiagnosticHandler(); |
| + |
| + OutputProvider outputProvider = new OutputProvider(); |
| + |
| + void resilientDiagnosticHandler( |
| + Uri uri, int begin, int end, String message, kind) { |
| + try { |
| + diagnosticHandler(uri, begin, end, message, kind); |
| + } catch (e) { |
| + String name = diagnosticHandler.provider.relativizeUri(uri); |
| + print('$name@$begin+${end - begin}: [$kind] $message}'); |
| + } |
| + } |
| + |
| + 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.
|
| + if (uri.scheme == "file") { |
| + watcher.watchFile(uri); |
| + } |
| + return diagnosticHandler.provider(uri); |
| + } |
| + |
| + while (true) { |
| + IncrementalCompiler compiler = new IncrementalCompiler( |
| + libraryRoot: libraryRoot, |
| + packageRoot: packageRoot, |
| + inputProvider: inputProvider, |
| + diagnosticHandler: resilientDiagnosticHandler, |
| + outputProvider: outputProvider); |
| + |
| + if (!await compiler.compile(originalInput)) { |
| + print("Compilation failed"); |
| + } else { |
| + print('// Compiled JavaScript:'); |
| + print(outputProvider['.js']); |
| + } |
| + |
|
kasperl
2015/01/20 10:23:00
Only one newline.
ahe
2015/01/20 10:45:52
Done.
|
| + |
| + int updateCount = 1; |
|
kasperl
2015/01/20 10:23:00
0 and then use prefix ++?
ahe
2015/01/20 10:45:52
Done.
|
| + while (await watcher.hasChanges()) { |
| + try { |
| + Map<Uri, Uri> changes = watcher.readChanges(); |
| + |
| + Stopwatch sw = new Stopwatch()..start(); |
| + await compiler.compileUpdates(changes); |
| + |
| + String updates = '${compiler.allUpdates()}'; |
| + |
| + sw.stop(); |
| + print('// Patch after ${updateCount++} updates,'); |
| + print('// computed in ${sw.elapsedMicroseconds/1000000} seconds:'); |
| + print(updates); |
| + } on IncrementalCompilationFailed catch (error) { |
| + print("Incremental compilation failed due to:\n${error.reason}"); |
| + break; |
| + } |
| + } |
| + } |
| +} |
| + |
| +/// Output provider which collect output in [output]. |
|
kasperl
2015/01/20 10:23:00
collect -> collects
ahe
2015/01/20 10:45:52
Done.
|
| +class OutputProvider { |
| + final Map<String, String> output = new Map<String, String>(); |
| + |
| + EventSink<String> call(String name, String extension) { |
| + return new StringEventSink((String data) { |
| + output['$name.$extension'] = data; |
| + }); |
| + } |
| + |
| + 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.
|
| +} |
| + |
| +/// Helper class to collect sources. |
| +class StringEventSink implements EventSink<String> { |
| + List<String> data = <String>[]; |
| + |
| + final Function onClose; |
| + |
| + StringEventSink(this.onClose); |
| + |
| + void add(String event) { |
| + if (data == null) throw 'StringEventSink is closed.'; |
| + data.add(event); |
| + } |
| + |
| + void addError(errorEvent, [StackTrace stackTrace]) { |
| + throw 'addError($errorEvent, $stackTrace)'; |
| + } |
| + |
| + void close() { |
| + if (data != null) { |
| + onClose(data.join()); |
| + data = null; |
| + } |
| + } |
| +} |