Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 /// Example that illustrates how to use the incremental compiler and trigger a | 5 /// Example that illustrates how to use the incremental compiler and trigger a |
| 6 /// hot-reload on the VM after recompiling the application. | 6 /// hot-reload on the VM after recompiling the application. |
| 7 /// | 7 /// |
| 8 /// This example resembles the `run` command in flutter-tools. It creates an | 8 /// This example resembles the `run` command in flutter-tools. It creates an |
| 9 /// interactive command-line program that waits for the user to tap a key to | 9 /// interactive command-line program that waits for the user to tap a key to |
| 10 /// trigger a recompile and reload. | 10 /// trigger a recompile and reload. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 39 /// | 39 /// |
| 40 /// * In terminal A, hit the "r" key to trigger a recompile and hot reload. | 40 /// * In terminal A, hit the "r" key to trigger a recompile and hot reload. |
| 41 /// | 41 /// |
| 42 /// * See the changed program in terminal B | 42 /// * See the changed program in terminal B |
| 43 library front_end.example.incremental_reload.run; | 43 library front_end.example.incremental_reload.run; |
| 44 | 44 |
| 45 import 'dart:io'; | 45 import 'dart:io'; |
| 46 import 'dart:async'; | 46 import 'dart:async'; |
| 47 import 'dart:convert' show ASCII; | 47 import 'dart:convert' show ASCII; |
| 48 | 48 |
| 49 import 'package:args/args.dart'; | |
| 50 | |
| 49 import '../../tool/vm/reload.dart'; | 51 import '../../tool/vm/reload.dart'; |
| 50 | 52 |
| 51 import 'compiler_with_invalidation.dart'; | 53 import 'compiler_with_invalidation.dart'; |
| 52 | 54 |
| 55 ArgParser argParser = new ArgParser(allowTrailingOptions: false) | |
| 56 ..addOption('sdk-root', help: 'Path to sdk for compilation') | |
| 57 ..addOption('input', help: 'Input dart file') | |
|
Siggi Cherem (dart-lang)
2017/08/15 00:37:14
minor suggestion: let's remove this option and use
aam
2017/08/15 03:59:49
Done.
| |
| 58 ..addOption('output', help: 'Output dill file', defaultsTo: 'out.dill'); | |
|
Siggi Cherem (dart-lang)
2017/08/15 00:37:15
minor suggestion: let's add `abbr: "o"`
aam
2017/08/15 03:59:49
Done.
| |
| 59 | |
| 60 String usage = ''' | |
| 61 Usage: dart [options] | |
| 62 | |
| 63 Runs console-driven incremental compiler. | |
| 64 | |
| 65 Options: | |
| 66 ${argParser.usage} | |
| 67 '''; | |
| 68 | |
| 53 RemoteVm remoteVm = new RemoteVm(); | 69 RemoteVm remoteVm = new RemoteVm(); |
| 54 AnsiTerminal terminal = new AnsiTerminal(); | 70 AnsiTerminal terminal = new AnsiTerminal(); |
| 55 | 71 |
| 56 main(List<String> args) async { | 72 main(List<String> args) async { |
| 57 if (args.length <= 1) { | 73 ArgResults options = argParser.parse(args); |
| 58 print('usage: dart incremental_compile.dart input.dart out.dill'); | 74 if (options['input'] == null) { |
| 75 print('Need input file'); | |
| 76 print(usage); | |
| 59 exit(1); | 77 exit(1); |
| 60 } | 78 } |
| 61 | 79 |
| 62 var compiler = await createIncrementalCompiler(args[0]); | 80 var compiler = await createIncrementalCompiler(options['input'], |
| 63 var outputUri = Uri.base.resolve(args[1]); | 81 argSdkRoot: |
| 82 options['sdk-root'] != null ? Uri.parse(options['sdk-root']) : null); | |
| 83 var outputUri = Uri.base.resolve(options['output']); | |
| 64 | 84 |
| 65 showHeader(); | 85 showHeader(); |
| 66 listenOnKeyPress(compiler, outputUri) | 86 listenOnKeyPress(compiler, outputUri) |
| 67 .whenComplete(() => remoteVm.disconnect()); | 87 .whenComplete(() => remoteVm.disconnect()); |
| 68 } | 88 } |
| 69 | 89 |
| 70 /// Implements the interactive UI by listening for input keys from the user. | 90 /// Implements the interactive UI by listening for input keys from the user. |
| 71 Future listenOnKeyPress(IncrementalCompiler compiler, Uri outputUri) { | 91 Future listenOnKeyPress(IncrementalCompiler compiler, Uri outputUri) { |
| 72 var completer = new Completer(); | 92 var completer = new Completer(); |
| 73 terminal.singleCharMode = true; | 93 terminal.singleCharMode = true; |
| 74 StreamSubscription subscription; | 94 StreamSubscription subscription; |
| 75 subscription = terminal.onCharInput.listen((String char) async { | 95 subscription = terminal.onCharInput.listen((String char) async { |
| 76 try { | 96 try { |
| 77 CompilationResult compilationResult; | 97 CompilationResult compilationResult; |
| 78 ReloadResult reloadResult; | 98 ReloadResult reloadResult; |
| 79 switch (char) { | 99 switch (char.trim()) { |
| 80 case 'r': | 100 case 'r': |
| 81 compilationResult = await rebuild(compiler, outputUri); | 101 compilationResult = await rebuild(compiler, outputUri); |
| 82 if (!compilationResult.errorSeen && | 102 if (!compilationResult.errorSeen && |
| 83 compilationResult.program != null && | 103 compilationResult.program != null && |
| 84 compilationResult.program.libraries.isNotEmpty) { | 104 compilationResult.program.libraries.isNotEmpty) { |
| 85 reloadResult = await reload(outputUri); | 105 reloadResult = await reload(outputUri); |
| 86 } | 106 } |
| 87 break; | 107 break; |
| 88 case 'c': | 108 case 'c': |
| 89 compilationResult = await rebuild(compiler, outputUri); | 109 compilationResult = await rebuild(compiler, outputUri); |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 } on StdinException catch (error) { | 295 } on StdinException catch (error) { |
| 276 if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode)) rethrow; | 296 if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode)) rethrow; |
| 277 } | 297 } |
| 278 } | 298 } |
| 279 | 299 |
| 280 /// Return keystrokes from the console. | 300 /// Return keystrokes from the console. |
| 281 /// | 301 /// |
| 282 /// Useful when the console is in [singleCharMode]. | 302 /// Useful when the console is in [singleCharMode]. |
| 283 Stream<String> get onCharInput => stdin.transform(ASCII.decoder); | 303 Stream<String> get onCharInput => stdin.transform(ASCII.decoder); |
| 284 } | 304 } |
| OLD | NEW |