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 /// A wrapper on top of the [IncrementalKernelGenerator] that tracks | 5 /// A wrapper on top of the [IncrementalKernelGenerator] that tracks |
6 /// file modifications between subsequent compilation requests and only | 6 /// file modifications between subsequent compilation requests and only |
7 /// invalidates those files that appear to be modified. | 7 /// invalidates those files that appear to be modified. |
8 library front_end.example.incremental_reload.compiler_with_invalidation; | 8 library front_end.example.incremental_reload.compiler_with_invalidation; |
9 | 9 |
10 import 'dart:io'; | 10 import 'dart:io'; |
11 import 'dart:async'; | 11 import 'dart:async'; |
12 import 'dart:convert' show JSON; | |
13 | 12 |
14 import 'package:front_end/compiler_options.dart'; | 13 import 'package:front_end/compiler_options.dart'; |
15 import 'package:front_end/incremental_kernel_generator.dart'; | 14 import 'package:front_end/incremental_kernel_generator.dart'; |
16 import 'package:front_end/src/incremental/file_byte_store.dart'; | 15 import 'package:front_end/src/incremental/file_byte_store.dart'; |
17 import 'package:front_end/src/incremental/byte_store.dart'; | 16 import 'package:front_end/src/incremental/byte_store.dart'; |
18 import 'package:kernel/ast.dart'; | 17 import 'package:kernel/ast.dart'; |
19 import 'package:kernel/binary/limited_ast_to_binary.dart'; | 18 import 'package:kernel/binary/limited_ast_to_binary.dart'; |
20 | 19 |
21 /// Create an instance of an [IncrementalCompiler] to compile a program whose | 20 /// Create an instance of an [IncrementalCompiler] to compile a program whose |
22 /// main entry point file is [entry]. This uses some default options | 21 /// main entry point file is [entry]. This uses some default options |
23 /// for the location of the sdk and temporary folder to save intermediate | 22 /// for the location of the sdk and temporary folder to save intermediate |
24 /// results. | 23 /// results. |
25 // TODO(sigmund): make this example work outside of the SDK repo. | 24 // TODO(sigmund): make this example work outside of the SDK repo. |
26 Future<IncrementalCompiler> createIncrementalCompiler(String entry, | 25 Future<IncrementalCompiler> createIncrementalCompiler(String entry, |
27 {bool persistent: true}) { | 26 {bool persistent: true}) { |
28 var entryUri = Uri.base.resolve(entry); | 27 var entryUri = Uri.base.resolve(entry); |
29 var dartVm = Uri.base.resolve(Platform.resolvedExecutable); | 28 var dartVm = Uri.base.resolve(Platform.resolvedExecutable); |
30 var sdkRoot = dartVm.resolve("patched_sdk/"); | 29 var sdkRoot = dartVm.resolve("patched_sdk/"); |
31 var tmpDir = Directory.systemTemp.createTempSync('ikg_cache'); | 30 var tmpDir = Directory.systemTemp.createTempSync('ikg_cache'); |
32 var options = new CompilerOptions() | 31 var options = new CompilerOptions() |
33 ..sdkRoot = sdkRoot | 32 ..sdkRoot = sdkRoot |
34 ..packagesFileUri = Uri.base.resolve('.packages') | 33 ..packagesFileUri = Uri.base.resolve('.packages') |
35 ..strongMode = false | 34 ..strongMode = false |
36 ..dartLibraries = loadDartLibraries(sdkRoot) | |
37 // Note: we do not report error on the console because the incremental | 35 // Note: we do not report error on the console because the incremental |
38 // compiler is an ongoing background service that shouldn't polute stdout. | 36 // compiler is an ongoing background service that shouldn't polute stdout. |
39 // TODO(sigmund): do something with the errors. | 37 // TODO(sigmund): do something with the errors. |
40 ..onError = (_) {} | 38 ..onError = (_) {} |
41 ..byteStore = | 39 ..byteStore = |
42 persistent ? new FileByteStore(tmpDir.path) : new MemoryByteStore(); | 40 persistent ? new FileByteStore(tmpDir.path) : new MemoryByteStore(); |
43 return IncrementalCompiler.create(options, entryUri); | 41 return IncrementalCompiler.create(options, entryUri); |
44 } | 42 } |
45 | 43 |
46 /// Reads the `libraries.json` file for an SDK to provide the location of the | |
47 /// SDK files. | |
48 // TODO(sigmund): this should be handled by package:front_end internally. | |
49 Map<String, Uri> loadDartLibraries(Uri sdkRoot) { | |
50 var libraries = sdkRoot.resolve('lib/libraries.json'); | |
51 var map = | |
52 JSON.decode(new File.fromUri(libraries).readAsStringSync())['libraries']; | |
53 var dartLibraries = <String, Uri>{}; | |
54 map.forEach((k, v) => dartLibraries[k] = libraries.resolve(v)); | |
55 return dartLibraries; | |
56 } | |
57 | |
58 /// An incremental compiler that monitors file modifications on disk and | 44 /// An incremental compiler that monitors file modifications on disk and |
59 /// invalidates only files that have been modified since the previous time the | 45 /// invalidates only files that have been modified since the previous time the |
60 /// compiler was invoked. | 46 /// compiler was invoked. |
61 class IncrementalCompiler { | 47 class IncrementalCompiler { |
62 /// Underlying incremental compiler implementation. | 48 /// Underlying incremental compiler implementation. |
63 IncrementalKernelGenerator _generator; | 49 IncrementalKernelGenerator _generator; |
64 | 50 |
65 /// Last modification for each tracked input file. | 51 /// Last modification for each tracked input file. |
66 Map<Uri, DateTime> lastModified = {}; | 52 Map<Uri, DateTime> lastModified = {}; |
67 | 53 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 result.errorSeen = true; | 162 result.errorSeen = true; |
177 } | 163 } |
178 | 164 |
179 result.changed = compiler.changed; | 165 result.changed = compiler.changed; |
180 result.totalFiles = compiler.lastModified.length; | 166 result.totalFiles = compiler.lastModified.length; |
181 result.invalidateTime = compiler.invalidateTime; | 167 result.invalidateTime = compiler.invalidateTime; |
182 result.compileTime = compiler.compileTime; | 168 result.compileTime = compiler.compileTime; |
183 result.reloadTime = 0; | 169 result.reloadTime = 0; |
184 return result; | 170 return result; |
185 } | 171 } |
OLD | NEW |