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 import 'package:kernel/target/targets.dart'; |
| 51 |
49 import '../../tool/vm/reload.dart'; | 52 import '../../tool/vm/reload.dart'; |
50 | 53 |
51 import 'compiler_with_invalidation.dart'; | 54 import 'compiler_with_invalidation.dart'; |
52 | 55 |
| 56 ArgParser argParser = new ArgParser(allowTrailingOptions: true) |
| 57 ..addOption('sdk-root', help: 'Path to sdk for compilation') |
| 58 ..addOption('output', |
| 59 help: 'Output dill file', defaultsTo: 'out.dill', abbr: 'o') |
| 60 ..addOption('target', |
| 61 help: 'One of none, vm, vm_fasta, vmcc, vmreify, flutter, flutter_fasta', |
| 62 defaultsTo: 'vm'); |
| 63 |
| 64 String usage = ''' |
| 65 Usage: dart [options] input.dart |
| 66 |
| 67 Runs console-driven incremental compiler. |
| 68 |
| 69 Options: |
| 70 ${argParser.usage} |
| 71 '''; |
| 72 |
53 RemoteVm remoteVm = new RemoteVm(); | 73 RemoteVm remoteVm = new RemoteVm(); |
54 AnsiTerminal terminal = new AnsiTerminal(); | 74 AnsiTerminal terminal = new AnsiTerminal(); |
55 | 75 |
56 main(List<String> args) async { | 76 main(List<String> args) async { |
57 if (args.length <= 1) { | 77 ArgResults options = argParser.parse(args); |
58 print('usage: dart incremental_compile.dart input.dart out.dill'); | 78 if (options.rest.isEmpty) { |
| 79 print('Need an input file'); |
| 80 print(usage); |
59 exit(1); | 81 exit(1); |
60 } | 82 } |
61 | 83 |
62 var compiler = await createIncrementalCompiler(args[0]); | 84 var compiler = await createIncrementalCompiler(options.rest[0], |
63 var outputUri = Uri.base.resolve(args[1]); | 85 sdkRoot: |
| 86 options['sdk-root'] != null ? Uri.parse(options['sdk-root']) : null, |
| 87 target: targets[options['target']](new TargetFlags())); |
| 88 var outputUri = Uri.base.resolve(options['output']); |
64 | 89 |
65 showHeader(); | 90 showHeader(); |
66 listenOnKeyPress(compiler, outputUri) | 91 listenOnKeyPress(compiler, outputUri) |
67 .whenComplete(() => remoteVm.disconnect()); | 92 .whenComplete(() => remoteVm.disconnect()); |
68 } | 93 } |
69 | 94 |
70 /// Implements the interactive UI by listening for input keys from the user. | 95 /// Implements the interactive UI by listening for input keys from the user. |
71 Future listenOnKeyPress(IncrementalCompiler compiler, Uri outputUri) { | 96 Future listenOnKeyPress(IncrementalCompiler compiler, Uri outputUri) { |
72 var completer = new Completer(); | 97 var completer = new Completer(); |
73 terminal.singleCharMode = true; | 98 terminal.singleCharMode = true; |
74 StreamSubscription subscription; | 99 StreamSubscription subscription; |
75 subscription = terminal.onCharInput.listen((String char) async { | 100 subscription = terminal.onCharInput.listen((String char) async { |
76 try { | 101 try { |
77 CompilationResult compilationResult; | 102 CompilationResult compilationResult; |
78 ReloadResult reloadResult; | 103 ReloadResult reloadResult; |
79 switch (char) { | 104 switch (char.trim()) { |
80 case 'r': | 105 case 'r': |
81 compilationResult = await rebuild(compiler, outputUri); | 106 compilationResult = await rebuild(compiler, outputUri); |
82 if (!compilationResult.errorSeen && | 107 if (!compilationResult.errorSeen && |
83 compilationResult.program != null && | 108 compilationResult.program != null && |
84 compilationResult.program.libraries.isNotEmpty) { | 109 compilationResult.program.libraries.isNotEmpty) { |
85 reloadResult = await reload(outputUri); | 110 reloadResult = await reload(outputUri); |
86 } | 111 } |
87 break; | 112 break; |
88 case 'c': | 113 case 'c': |
89 compilationResult = await rebuild(compiler, outputUri); | 114 compilationResult = await rebuild(compiler, outputUri); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 /// Setting the line mode can throw for some terminals (with "Operation not | 260 /// Setting the line mode can throw for some terminals (with "Operation not |
236 /// supported on socket"), but the error can be safely ignored. | 261 /// supported on socket"), but the error can be safely ignored. |
237 static const List<int> _lineModeIgnorableErrors = const <int>[ | 262 static const List<int> _lineModeIgnorableErrors = const <int>[ |
238 _ENXIO, | 263 _ENXIO, |
239 _ENOTTY, | 264 _ENOTTY, |
240 _ENETRESET, | 265 _ENETRESET, |
241 _INVALID_HANDLE, | 266 _INVALID_HANDLE, |
242 ]; | 267 ]; |
243 | 268 |
244 String bolden(String message) => wrap(message, _bold); | 269 String bolden(String message) => wrap(message, _bold); |
| 270 |
245 String green(String message) => wrap(message, _green); | 271 String green(String message) => wrap(message, _green); |
| 272 |
246 String red(String message) => wrap(message, _red); | 273 String red(String message) => wrap(message, _red); |
247 | 274 |
248 String wrap(String message, String escape) { | 275 String wrap(String message, String escape) { |
249 final StringBuffer buffer = new StringBuffer(); | 276 final StringBuffer buffer = new StringBuffer(); |
250 for (String line in message.split('\n')) | 277 for (String line in message.split('\n')) |
251 buffer.writeln('$escape$line$_reset'); | 278 buffer.writeln('$escape$line$_reset'); |
252 final String result = buffer.toString(); | 279 final String result = buffer.toString(); |
253 // avoid introducing a new newline to the emboldened text | 280 // avoid introducing a new newline to the emboldened text |
254 return (!message.endsWith('\n') && result.endsWith('\n')) | 281 return (!message.endsWith('\n') && result.endsWith('\n')) |
255 ? result.substring(0, result.length - 1) | 282 ? result.substring(0, result.length - 1) |
(...skipping 19 matching lines...) Expand all Loading... |
275 } on StdinException catch (error) { | 302 } on StdinException catch (error) { |
276 if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode)) rethrow; | 303 if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode)) rethrow; |
277 } | 304 } |
278 } | 305 } |
279 | 306 |
280 /// Return keystrokes from the console. | 307 /// Return keystrokes from the console. |
281 /// | 308 /// |
282 /// Useful when the console is in [singleCharMode]. | 309 /// Useful when the console is in [singleCharMode]. |
283 Stream<String> get onCharInput => stdin.transform(ASCII.decoder); | 310 Stream<String> get onCharInput => stdin.transform(ASCII.decoder); |
284 } | 311 } |
OLD | NEW |