From 7ffcd3d22d7bc1222d53d6d3bb83f59891aac2c2 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 24 Apr 2018 14:03:41 -0700 Subject: [PATCH] Extract KernelCompiler class (#16937) Wraps the compile function in a class injected via the global context, which makes it easier to mock in unit tests -- specifically tests for AOT snapshotting, which already require pretty significant amounts of mock inputs. --- packages/flutter_tools/lib/src/bundle.dart | 2 +- .../lib/src/commands/build_aot.dart | 2 +- packages/flutter_tools/lib/src/compile.dart | 149 +++++++++--------- .../flutter_tools/lib/src/context_runner.dart | 2 + packages/flutter_tools/test/compile_test.dart | 6 +- 5 files changed, 85 insertions(+), 76 deletions(-) diff --git a/packages/flutter_tools/lib/src/bundle.dart b/packages/flutter_tools/lib/src/bundle.dart index f1da29b8a8..771d8f0b72 100644 --- a/packages/flutter_tools/lib/src/bundle.dart +++ b/packages/flutter_tools/lib/src/bundle.dart @@ -106,7 +106,7 @@ Future build({ String kernelBinaryFilename; if (needBuild) { ensureDirectoryExists(applicationKernelFilePath); - final CompilerOutput compilerOutput = await compile( + final CompilerOutput compilerOutput = await kernelCompiler.compile( sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), incrementalCompilerByteStorePath: fs.path.absolute(getIncrementalCompilerByteStoreDirectory()), mainPath: fs.file(mainPath).absolute.path, diff --git a/packages/flutter_tools/lib/src/commands/build_aot.dart b/packages/flutter_tools/lib/src/commands/build_aot.dart index 716255f57f..ddf703909f 100644 --- a/packages/flutter_tools/lib/src/commands/build_aot.dart +++ b/packages/flutter_tools/lib/src/commands/build_aot.dart @@ -366,7 +366,7 @@ Future _buildAotSnapshot( } if (previewDart2) { - final CompilerOutput compilerOutput = await compile( + final CompilerOutput compilerOutput = await kernelCompiler.compile( sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), mainPath: mainPath, outputFilePath: kApplicationKernelPath, diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index 4e3d3baf4a..dfab8d34e4 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -9,10 +9,13 @@ import 'package:usage/uuid/uuid.dart'; import 'artifacts.dart'; import 'base/common.dart'; +import 'base/context.dart'; import 'base/io.dart'; import 'base/process_manager.dart'; import 'globals.dart'; +KernelCompiler get kernelCompiler => context[KernelCompiler]; + typedef void CompilerMessageConsumer(String message); class CompilerOutput { @@ -59,7 +62,10 @@ class _StdoutHandler { } } -Future compile( +class KernelCompiler { + const KernelCompiler(); + + Future compile( {String sdkRoot, String mainPath, String outputFilePath, @@ -73,80 +79,81 @@ Future compile( String packagesPath, List fileSystemRoots, String fileSystemScheme}) async { - final String frontendServer = artifacts.getArtifactPath( - Artifact.frontendServerSnapshotForEngineDartSdk - ); + final String frontendServer = artifacts.getArtifactPath( + Artifact.frontendServerSnapshotForEngineDartSdk + ); - // This is a URI, not a file path, so the forward slash is correct even on Windows. - if (!sdkRoot.endsWith('/')) - sdkRoot = '$sdkRoot/'; - final String engineDartPath = artifacts.getArtifactPath(Artifact.engineDartBinary); - if (!processManager.canRun(engineDartPath)) { - throwToolExit('Unable to find Dart binary at $engineDartPath'); - } - final List command = [ - engineDartPath, - frontendServer, - '--sdk-root', - sdkRoot, - '--strong', - '--target=flutter', - ]; - if (trackWidgetCreation) - command.add('--track-widget-creation'); - if (!linkPlatformKernelIn) - command.add('--no-link-platform'); - if (aot) { - command.add('--aot'); - command.add('--tfa'); - } - if (entryPointsJsonFiles != null) { - for (String entryPointsJson in entryPointsJsonFiles) { - command.addAll(['--entry-points', entryPointsJson]); + // This is a URI, not a file path, so the forward slash is correct even on Windows. + if (!sdkRoot.endsWith('/')) + sdkRoot = '$sdkRoot/'; + final String engineDartPath = artifacts.getArtifactPath(Artifact.engineDartBinary); + if (!processManager.canRun(engineDartPath)) { + throwToolExit('Unable to find Dart binary at $engineDartPath'); } - } - if (incrementalCompilerByteStorePath != null) { - command.add('--incremental'); - } - if (packagesPath != null) { - command.addAll(['--packages', packagesPath]); - } - if (outputFilePath != null) { - command.addAll(['--output-dill', outputFilePath]); - } - if (depFilePath != null && (fileSystemRoots == null || fileSystemRoots.isEmpty)) { - command.addAll(['--depfile', depFilePath]); - } - if (fileSystemRoots != null) { - for (String root in fileSystemRoots) { - command.addAll(['--filesystem-root', root]); + final List command = [ + engineDartPath, + frontendServer, + '--sdk-root', + sdkRoot, + '--strong', + '--target=flutter', + ]; + if (trackWidgetCreation) + command.add('--track-widget-creation'); + if (!linkPlatformKernelIn) + command.add('--no-link-platform'); + if (aot) { + command.add('--aot'); + command.add('--tfa'); } + if (entryPointsJsonFiles != null) { + for (String entryPointsJson in entryPointsJsonFiles) { + command.addAll(['--entry-points', entryPointsJson]); + } + } + if (incrementalCompilerByteStorePath != null) { + command.add('--incremental'); + } + if (packagesPath != null) { + command.addAll(['--packages', packagesPath]); + } + if (outputFilePath != null) { + command.addAll(['--output-dill', outputFilePath]); + } + if (depFilePath != null && (fileSystemRoots == null || fileSystemRoots.isEmpty)) { + command.addAll(['--depfile', depFilePath]); + } + if (fileSystemRoots != null) { + for (String root in fileSystemRoots) { + command.addAll(['--filesystem-root', root]); + } + } + if (fileSystemScheme != null) { + command.addAll(['--filesystem-scheme', fileSystemScheme]); + } + + if (extraFrontEndOptions != null) + command.addAll(extraFrontEndOptions); + command.add(mainPath); + printTrace(command.join(' ')); + final Process server = await processManager + .start(command) + .catchError((dynamic error, StackTrace stack) { + printError('Failed to start frontend server $error, $stack'); + }); + + final _StdoutHandler stdoutHandler = new _StdoutHandler(); + + server.stderr + .transform(utf8.decoder) + .listen((String s) { printError('compiler message: $s'); }); + server.stdout + .transform(utf8.decoder) + .transform(const LineSplitter()) + .listen(stdoutHandler.handler); + final int exitCode = await server.exitCode; + return exitCode == 0 ? stdoutHandler.compilerOutput.future : null; } - if (fileSystemScheme != null) { - command.addAll(['--filesystem-scheme', fileSystemScheme]); - } - - if (extraFrontEndOptions != null) - command.addAll(extraFrontEndOptions); - command.add(mainPath); - printTrace(command.join(' ')); - final Process server = await processManager - .start(command) - .catchError((dynamic error, StackTrace stack) { - printError('Failed to start frontend server $error, $stack'); - }); - - final _StdoutHandler stdoutHandler = new _StdoutHandler(); - - server.stderr - .transform(utf8.decoder) - .listen((String s) { printError('compiler message: $s'); }); - server.stdout - .transform(utf8.decoder) - .transform(const LineSplitter()) - .listen(stdoutHandler.handler); - final int exitCode = await server.exitCode; - return exitCode == 0 ? stdoutHandler.compilerOutput.future : null; } /// Wrapper around incremental frontend server compiler, that communicates with diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index ed3f48be1b..8b41406b2d 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -22,6 +22,7 @@ import 'base/platform.dart'; import 'base/port_scanner.dart'; import 'base/utils.dart'; import 'cache.dart'; +import 'compile.dart'; import 'devfs.dart'; import 'device.dart'; import 'doctor.dart'; @@ -63,6 +64,7 @@ Future runInContext( IMobileDevice: () => const IMobileDevice(), IOSSimulatorUtils: () => new IOSSimulatorUtils(), IOSWorkflow: () => const IOSWorkflow(), + KernelCompiler: () => const KernelCompiler(), Logger: () => platform.isWindows ? new WindowsStdoutLogger() : new StdoutLogger(), OperatingSystemUtils: () => new OperatingSystemUtils(), PortScanner: () => const HostPortScanner(), diff --git a/packages/flutter_tools/test/compile_test.dart b/packages/flutter_tools/test/compile_test.dart index f5931e0a8d..2fe63a8599 100644 --- a/packages/flutter_tools/test/compile_test.dart +++ b/packages/flutter_tools/test/compile_test.dart @@ -46,7 +46,7 @@ void main() { 'result abc\nline1\nline2\nabc /path/to/main.dart.dill 0' )) )); - final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot', + final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot', mainPath: '/path/to/main.dart' ); expect(mockFrontendServerStdIn.getAndClear(), isEmpty); @@ -66,7 +66,7 @@ void main() { )) )); - final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot', + final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot', mainPath: '/path/to/main.dart' ); expect(mockFrontendServerStdIn.getAndClear(), isEmpty); @@ -88,7 +88,7 @@ void main() { )) )); - final CompilerOutput output = await compile(sdkRoot: '/path/to/sdkroot', + final CompilerOutput output = await kernelCompiler.compile(sdkRoot: '/path/to/sdkroot', mainPath: '/path/to/main.dart' ); expect(mockFrontendServerStdIn.getAndClear(), isEmpty);