Unverified Commit ffbca11f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] delete BuildRunnerWebCompilationProxy and WebCompilationProxy (#70914)

parent 18167785
......@@ -50,10 +50,8 @@ import 'src/globals.dart' as globals;
import 'src/isolated/devtools_launcher.dart';
import 'src/isolated/mustache_template.dart';
import 'src/isolated/resident_web_runner.dart';
import 'src/isolated/web_compilation_delegate.dart';
import 'src/resident_runner.dart';
import 'src/runner/flutter_command.dart';
import 'src/web/compile.dart';
import 'src/web/web_runner.dart';
/// Main entry point for commands.
......@@ -145,7 +143,6 @@ Future<void> main(List<String> args) async {
muteCommandLogging: muteCommandLogging,
verboseHelp: verboseHelp,
overrides: <Type, Generator>{
WebCompilationProxy: () => BuildRunnerWebCompilationProxy(),
// The web runner is not supported in google3 because it depends
// on dwds.
WebRunnerFactory: () => DwdsWebRunnerFactory(),
......
......@@ -9,6 +9,7 @@ import 'package:pool/pool.dart';
import 'asset.dart';
import 'base/common.dart';
import 'base/config.dart';
import 'base/file_system.dart';
import 'base/logger.dart';
import 'build_info.dart';
......@@ -40,6 +41,8 @@ String getDefaultCachedKernelPath({
@required bool trackWidgetCreation,
@required List<String> dartDefines,
@required List<String> extraFrontEndOptions,
FileSystem fileSystem,
Config config,
}) {
final StringBuffer buffer = StringBuffer();
buffer.writeAll(dartDefines);
......@@ -51,7 +54,10 @@ String getDefaultCachedKernelPath({
buildPrefix = '${hex.encode(digest.bytes)}.';
}
return getKernelPathForTransformerOptions(
globals.fs.path.join(getBuildDirectory(), '${buildPrefix}cache.dill'),
(fileSystem ?? globals.fs).path.join(getBuildDirectory(
config ?? globals.config,
fileSystem ?? globals.fs
), '${buildPrefix}cache.dill'),
trackWidgetCreation: trackWidgetCreation,
);
}
......
......@@ -12,12 +12,12 @@ import '../build_info.dart';
import '../globals.dart' as globals;
import '../project.dart';
import '../web/chrome.dart';
import '../web/compile.dart';
import '../web/memory_fs.dart';
import 'flutter_platform.dart' as loader;
import 'flutter_web_platform.dart';
import 'test_wrapper.dart';
import 'watcher.dart';
import 'web_test_compiler.dart';
/// A class that abstracts launching the test process from the test runner.
abstract class FlutterTestRunner {
......@@ -123,7 +123,14 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner {
.absolute
.uri
.toFilePath();
final WebMemoryFS result = await webCompilationProxy.initialize(
final WebMemoryFS result = await WebTestCompiler(
logger: globals.logger,
fileSystem: globals.fs,
platform: globals.platform,
artifacts: globals.artifacts,
processManager: globals.processManager,
config: globals.config,
).initialize(
projectDirectory: flutterProject.directory,
testOutputDir: tempBuildDir,
testFiles: testFiles,
......
......@@ -4,25 +4,44 @@
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import 'package:process/process.dart';
import '../artifacts.dart';
import '../base/common.dart';
import '../base/config.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../build_info.dart';
import '../bundle.dart';
import '../compile.dart';
import '../dart/language_version.dart';
import '../globals.dart' as globals;
import '../web/compile.dart';
import '../web/bootstrap.dart';
import '../web/memory_fs.dart';
// TODO(jonahwilliams): this class was kept around to reduce the diff in the migration
// from build_runner to the frontend_server, but should be removed/refactored to be
// similar to the test_compiler pattern used for regular flutter tests
class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
BuildRunnerWebCompilationProxy();
/// A web compiler for the test runner.
class WebTestCompiler {
WebTestCompiler({
@required FileSystem fileSystem,
@required Logger logger,
@required Artifacts artifacts,
@required Platform platform,
@required ProcessManager processManager,
@required Config config,
}) : _logger = logger,
_fileSystem = fileSystem,
_artifacts = artifacts,
_platform = platform,
_processManager = processManager,
_config = config;
final Logger _logger;
final FileSystem _fileSystem;
final Artifacts _artifacts;
final Platform _platform;
final ProcessManager _processManager;
final Config _config;
@override
Future<WebMemoryFS> initialize({
@required Directory projectDirectory,
@required String testOutputDir,
......@@ -47,32 +66,43 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
}
}
final Directory outputDirectory = globals.fs.directory(testOutputDir)
final Directory outputDirectory = _fileSystem.directory(testOutputDir)
..createSync(recursive: true);
final List<File> generatedFiles = <File>[];
for (final String testFilePath in testFiles) {
final List<String> relativeTestSegments = globals.fs.path.split(
globals.fs.path.relative(testFilePath, from: projectDirectory.childDirectory('test').path));
final File generatedFile = globals.fs.file(
globals.fs.path.join(outputDirectory.path, '${relativeTestSegments.join('_')}.test.dart'));
final List<String> relativeTestSegments = _fileSystem.path.split(
_fileSystem.path.relative(testFilePath, from: projectDirectory.childDirectory('test').path));
final File generatedFile = _fileSystem.file(
_fileSystem.path.join(outputDirectory.path, '${relativeTestSegments.join('_')}.test.dart'));
generatedFile
..createSync(recursive: true)
..writeAsStringSync(_generateEntrypoint(relativeTestSegments.join('/'), testFilePath, languageVersion));
..writeAsStringSync(generateTestEntrypoint(
relativeTestPath: relativeTestSegments.join('/'),
absolutePath: testFilePath,
languageVersion: languageVersion,
));
generatedFiles.add(generatedFile);
}
// Generate a fake main file that imports all tests to be executed. This will force
// each of them to be compiled.
final StringBuffer buffer = StringBuffer('// @dart=${languageVersion.major}.${languageVersion.minor}\n');
for (final File generatedFile in generatedFiles) {
buffer.writeln('import "${globals.fs.path.basename(generatedFile.path)}";');
buffer.writeln('import "${_fileSystem.path.basename(generatedFile.path)}";');
}
buffer.writeln('void main() {}');
globals.fs.file(globals.fs.path.join(outputDirectory.path, 'main.dart'))
_fileSystem.file(_fileSystem.path.join(outputDirectory.path, 'main.dart'))
..createSync()
..writeAsStringSync(buffer.toString());
final String cachedKernelPath = getDefaultCachedKernelPath(
trackWidgetCreation: buildInfo.trackWidgetCreation,
dartDefines: buildInfo.dartDefines,
extraFrontEndOptions: extraFrontEndOptions,
fileSystem: _fileSystem,
config: _config,
);
final ResidentCompiler residentCompiler = ResidentCompiler(
globals.artifacts.getArtifactPath(Artifact.flutterWebSdk, mode: buildInfo.mode),
_artifacts.getArtifactPath(Artifact.flutterWebSdk, mode: buildInfo.mode),
buildMode: buildInfo.mode,
trackWidgetCreation: buildInfo.trackWidgetCreation,
fileSystemRoots: <String>[
......@@ -82,24 +112,20 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
// Override the filesystem scheme so that the frontend_server can find
// the generated entrypoint code.
fileSystemScheme: 'org-dartlang-app',
initializeFromDill: getDefaultCachedKernelPath(
trackWidgetCreation: buildInfo.trackWidgetCreation,
dartDefines: buildInfo.dartDefines,
extraFrontEndOptions: extraFrontEndOptions,
),
initializeFromDill: cachedKernelPath,
targetModel: TargetModel.dartdevc,
extraFrontEndOptions: extraFrontEndOptions,
platformDill: globals.fs.file(globals.artifacts
platformDill: _fileSystem.file(_artifacts
.getArtifactPath(platformDillArtifact, mode: buildInfo.mode))
.absolute.uri.toString(),
dartDefines: buildInfo.dartDefines,
librariesSpec: globals.fs.file(globals.artifacts
librariesSpec: _fileSystem.file(_artifacts
.getArtifactPath(Artifact.flutterWebLibrariesJson)).uri.toString(),
packagesPath: buildInfo.packagesPath,
artifacts: globals.artifacts,
processManager: globals.processManager,
logger: globals.logger,
platform: globals.platform,
artifacts: _artifacts,
processManager: _processManager,
logger: _logger,
platform: _platform,
);
final CompilerOutput output = await residentCompiler.recompile(
......@@ -111,6 +137,10 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
if (output.errorCount > 0) {
throwToolExit('Failed to compile');
}
// Cache the output kernel file to speed up subsequent compiles.
_fileSystem.file(cachedKernelPath).parent.createSync(recursive: true);
_fileSystem.file(output.outputFilename).copySync(cachedKernelPath);
final File codeFile = outputDirectory.childFile('${output.outputFilename}.sources');
final File manifestFile = outputDirectory.childFile('${output.outputFilename}.json');
final File sourcemapFile = outputDirectory.childFile('${output.outputFilename}.map');
......@@ -118,70 +148,4 @@ class BuildRunnerWebCompilationProxy extends WebCompilationProxy {
return WebMemoryFS()
..write(codeFile, manifestFile, sourcemapFile, metadataFile);
}
String _generateEntrypoint(String relativeTestPath, String absolutePath, LanguageVersion languageVersion) {
return '''
// @dart = ${languageVersion.major}.${languageVersion.minor}
import 'org-dartlang-app:///$relativeTestPath' as test;
import 'dart:ui' as ui;
import 'dart:html';
import 'dart:js';
import 'package:stream_channel/stream_channel.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_api/src/backend/stack_trace_formatter.dart'; // ignore: implementation_imports
import 'package:test_api/src/remote_listener.dart'; // ignore: implementation_imports
import 'package:test_api/src/suite_channel_manager.dart'; // ignore: implementation_imports
// Extra initialization for flutter_web.
// The following parameters are hard-coded in Flutter's test embedder. Since
// we don't have an embedder yet this is the lowest-most layer we can put
// this stuff in.
Future<void> main() async {
ui.debugEmulateFlutterTesterEnvironment = true;
await ui.webOnlyInitializePlatform();
webGoldenComparator = DefaultWebGoldenComparator(Uri.parse('$absolutePath'));
(ui.window as dynamic).debugOverrideDevicePixelRatio(3.0);
(ui.window as dynamic).webOnlyDebugPhysicalSizeOverride = const ui.Size(2400, 1800);
internalBootstrapBrowserTest(() => test.main);
}
void internalBootstrapBrowserTest(Function getMain()) {
var channel = serializeSuite(getMain, hidePrints: false);
postMessageChannel().pipe(channel);
}
StreamChannel serializeSuite(Function getMain(), {bool hidePrints = true}) => RemoteListener.start(getMain, hidePrints: hidePrints);
StreamChannel suiteChannel(String name) {
var manager = SuiteChannelManager.current;
if (manager == null) {
throw StateError('suiteChannel() may only be called within a test worker.');
}
return manager.connectOut(name);
}
StreamChannel postMessageChannel() {
var controller = StreamChannelController(sync: true);
window.onMessage.firstWhere((message) {
return message.origin == window.location.origin && message.data == "port";
}).then((message) {
var port = message.ports.first;
var portSubscription = port.onMessage.listen((message) {
controller.local.sink.add(message.data);
});
controller.local.stream.listen((data) {
port.postMessage({"data": data});
}, onDone: () {
port.postMessage({"event": "done"});
portSubscription.cancel();
});
});
context['parent'].callMethod('postMessage', [
JsObject.jsify({"href": window.location.href, "ready": true}),
window.location.origin,
]);
return controller.foreign;
}
''';
}
}
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
/// The JavaScript bootstrap script to support in-browser hot restart.
///
......@@ -95,6 +96,75 @@ define("$bootstrapModule", ["$entrypoint", "dart_sdk"], function(app, dart_sdk)
''';
}
/// Generates the bootstrap logic required for a flutter test running in a browser.
///
/// This hard-codes the device pixel ratio to 3.0 and a 2400 x 1800 window size.
String generateTestEntrypoint({
@required String relativeTestPath,
@required String absolutePath,
@required LanguageVersion languageVersion,
}) {
return '''
// @dart = ${languageVersion.major}.${languageVersion.minor}
import 'org-dartlang-app:///$relativeTestPath' as test;
import 'dart:ui' as ui;
import 'dart:html';
import 'dart:js';
import 'package:stream_channel/stream_channel.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:test_api/src/backend/stack_trace_formatter.dart'; // ignore: implementation_imports
import 'package:test_api/src/remote_listener.dart'; // ignore: implementation_imports
import 'package:test_api/src/suite_channel_manager.dart'; // ignore: implementation_imports
Future<void> main() async {
ui.debugEmulateFlutterTesterEnvironment = true;
await ui.webOnlyInitializePlatform();
webGoldenComparator = DefaultWebGoldenComparator(Uri.parse('$absolutePath'));
(ui.window as dynamic).debugOverrideDevicePixelRatio(3.0);
(ui.window as dynamic).webOnlyDebugPhysicalSizeOverride = const ui.Size(2400, 1800);
internalBootstrapBrowserTest(() => test.main);
}
void internalBootstrapBrowserTest(Function getMain()) {
var channel = serializeSuite(getMain, hidePrints: false);
postMessageChannel().pipe(channel);
}
StreamChannel serializeSuite(Function getMain(), {bool hidePrints = true}) => RemoteListener.start(getMain, hidePrints: hidePrints);
StreamChannel suiteChannel(String name) {
var manager = SuiteChannelManager.current;
if (manager == null) {
throw StateError('suiteChannel() may only be called within a test worker.');
}
return manager.connectOut(name);
}
StreamChannel postMessageChannel() {
var controller = StreamChannelController(sync: true);
window.onMessage.firstWhere((message) {
return message.origin == window.location.origin && message.data == "port";
}).then((message) {
var port = message.ports.first;
var portSubscription = port.onMessage.listen((message) {
controller.local.sink.add(message.data);
});
controller.local.stream.listen((data) {
port.postMessage({"data": data});
}, onDone: () {
port.postMessage({"event": "done"});
portSubscription.cancel();
});
});
context['parent'].callMethod('postMessage', [
JsObject.jsify({"href": window.location.href, "ready": true}),
window.location.origin,
]);
return controller.foreign;
}
''';
}
/// Generate the unit test bootstrap file.
String generateTestBootstrapFileContents(String mainUri, String requireUrl, String mapperUrl) {
return '''
......
......@@ -2,11 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
import '../artifacts.dart';
import '../base/common.dart';
import '../base/context.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../build_info.dart';
......@@ -19,10 +16,6 @@ import '../globals.dart' as globals;
import '../platform_plugins.dart';
import '../plugins.dart';
import '../project.dart';
import '../web/memory_fs.dart';
/// The [WebCompilationProxy] instance.
WebCompilationProxy get webCompilationProxy => context.get<WebCompilationProxy>();
Future<void> buildWeb(
FlutterProject flutterProject,
......@@ -91,23 +84,6 @@ Future<void> buildWeb(
globals.flutterUsage.sendTiming('build', 'dart2js', Duration(milliseconds: sw.elapsedMilliseconds));
}
/// An indirection on web compilation.
///
/// Avoids issues with syncing build_runner_core to other repos.
class WebCompilationProxy {
const WebCompilationProxy();
/// Initialize the web compiler from the `projectDirectory`.
Future<WebMemoryFS> initialize({
@required Directory projectDirectory,
@required String testOutputDir,
@required List<String> testFiles,
@required BuildInfo buildInfo,
}) async {
throw UnimplementedError();
}
}
/// Web rendering backend mode.
enum WebRendererMode {
/// Auto detects which rendering backend to use.
......
......@@ -14,9 +14,9 @@ import 'package:flutter_tools/src/test/test_compiler.dart';
import 'package:mockito/mockito.dart';
import 'package:package_config/package_config_types.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/testbed.dart';
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/testbed.dart';
final Platform linuxPlatform = FakePlatform(
operatingSystem: 'linux',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment