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