[flutter_tools] update compilation to use package config (#54467)

This commit is contained in:
Jonah Williams
2020-04-20 14:02:49 -07:00
committed by GitHub
parent e5ea6ddb24
commit b7e30cfc26
25 changed files with 420 additions and 381 deletions

View File

@@ -448,7 +448,7 @@ class FlutterPlatform extends PlatformPlugin {
if (precompiledDillPath == null && precompiledDillFiles == null) {
// Lazily instantiate compiler so it is built only if it is actually used.
compiler ??= TestCompiler(buildMode, trackWidgetCreation, flutterProject);
mainDart = await compiler.compile(mainDart);
mainDart = await compiler.compile(globals.fs.file(mainDart).uri);
if (mainDart == null) {
controller.sink.addError(_getErrorMessage('Compilation failed', testPath, shellPath));

View File

@@ -869,7 +869,7 @@ class TestGoldenComparator {
// Lazily create the compiler
_compiler = _compiler ?? compilerFactory();
final String output = await _compiler.compile(listenerFile.path);
final String output = await _compiler.compile(listenerFile.uri);
final List<String> command = <String>[
shellPath,
'--disable-observatory',

View File

@@ -5,6 +5,7 @@
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import '../artifacts.dart';
import '../base/file_system.dart';
@@ -19,8 +20,9 @@ import '../project.dart';
/// A request to the [TestCompiler] for recompilation.
class _CompilationRequest {
_CompilationRequest(this.path, this.result);
String path;
_CompilationRequest(this.mainUri, this.result);
Uri mainUri;
Completer<String> result;
}
@@ -70,7 +72,7 @@ class TestCompiler {
// Whether to report compiler messages.
bool _suppressOutput = false;
Future<String> compile(String mainDart) {
Future<String> compile(Uri mainDart) {
final Completer<String> completer = Completer<String>();
compilerController.add(_CompilationRequest(mainDart, completer));
return completer.future;
@@ -95,13 +97,13 @@ class TestCompiler {
Future<ResidentCompiler> createCompiler() async {
final ResidentCompiler residentCompiler = ResidentCompiler(
globals.artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
packagesPath: PackageMap.globalPackagesPath,
buildMode: buildMode,
trackWidgetCreation: trackWidgetCreation,
compilerMessageConsumer: _reportCompilerMessage,
initializeFromDill: testFilePath,
unsafePackageSerialization: false,
dartDefines: const <String>[],
packagesPath: PackageMap.globalPackagesPath,
);
if (flutterProject.hasBuilders) {
return CodeGeneratingResidentCompiler.create(
@@ -112,6 +114,8 @@ class TestCompiler {
return residentCompiler;
}
PackageConfig _packageConfig;
// Handle a compilation request.
Future<void> _onCompilationRequest(_CompilationRequest request) async {
final bool isEmpty = compilationQueue.isEmpty;
@@ -122,9 +126,19 @@ class TestCompiler {
if (!isEmpty) {
return;
}
_packageConfig ??= await loadPackageConfigUri(
globals.fs.file(PackageMap.globalPackagesPath).absolute.uri,
loader: (Uri uri) async {
final File file = globals.fs.file(uri);
if (!file.existsSync()) {
return null;
}
return file.readAsBytes();
}
);
while (compilationQueue.isNotEmpty) {
final _CompilationRequest request = compilationQueue.first;
globals.printTrace('Compiling ${request.path}');
globals.printTrace('Compiling ${request.mainUri}');
final Stopwatch compilerTime = Stopwatch()..start();
bool firstCompile = false;
if (compiler == null) {
@@ -133,9 +147,10 @@ class TestCompiler {
}
_suppressOutput = false;
final CompilerOutput compilerOutput = await compiler.recompile(
request.path,
<Uri>[Uri.parse(request.path)],
request.mainUri,
<Uri>[request.mainUri],
outputPath: outputDill.path,
packageConfig: _packageConfig,
);
final String outputPath = compilerOutput?.outputFilename;
@@ -143,12 +158,13 @@ class TestCompiler {
// errors, pass [null] upwards to the consumer and shutdown the
// compiler to avoid reusing compiler that might have gotten into
// a weird state.
final String path = request.mainUri.toFilePath(windows: globals.platform.isWindows);
if (outputPath == null || compilerOutput.errorCount > 0) {
request.result.complete(null);
await _shutdown();
} else {
final File outputFile = globals.fs.file(outputPath);
final File kernelReadyToRun = await outputFile.copy('${request.path}.dill');
final File kernelReadyToRun = await outputFile.copy('$path.dill');
final File testCache = globals.fs.file(testFilePath);
if (firstCompile || !testCache.existsSync() || (testCache.lengthSync() < outputFile.lengthSync())) {
// The idea is to keep the cache file up-to-date and include as
@@ -161,7 +177,7 @@ class TestCompiler {
compiler.accept();
compiler.reset();
}
globals.printTrace('Compiling ${request.path} took ${compilerTime.elapsedMilliseconds}ms');
globals.printTrace('Compiling $path took ${compilerTime.elapsedMilliseconds}ms');
// Only remove now when we finished processing the element
compilationQueue.removeAt(0);
}