From 6f5f037689689fc0c9f919e4502cc9333d0571c4 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 26 Feb 2019 16:58:24 -0800 Subject: [PATCH] Add basic web device and run support (#28302) --- dev/integration_tests/web/web/index.html | 10 + .../lib/src/application_package.dart | 4 +- .../flutter_tools/lib/src/context_runner.dart | 2 + packages/flutter_tools/lib/src/device.dart | 10 +- packages/flutter_tools/lib/src/project.dart | 32 +++ .../lib/src/resident_runner.dart | 11 +- .../flutter_tools/lib/src/web/compile.dart | 1 + .../flutter_tools/lib/src/web/web_device.dart | 206 ++++++++++++++++++ .../templates/web/index.html.tmpl | 10 + .../flutter_tools/test/web/devices_test.dart | 46 ++++ 10 files changed, 328 insertions(+), 4 deletions(-) create mode 100644 dev/integration_tests/web/web/index.html create mode 100644 packages/flutter_tools/lib/src/web/web_device.dart create mode 100644 packages/flutter_tools/templates/web/index.html.tmpl create mode 100644 packages/flutter_tools/test/web/devices_test.dart diff --git a/dev/integration_tests/web/web/index.html b/dev/integration_tests/web/web/index.html new file mode 100644 index 0000000000..9fa55fed16 --- /dev/null +++ b/dev/integration_tests/web/web/index.html @@ -0,0 +1,10 @@ + + + + + web_integration + + + + + diff --git a/packages/flutter_tools/lib/src/application_package.dart b/packages/flutter_tools/lib/src/application_package.dart index 3822ea260b..6cd131470b 100644 --- a/packages/flutter_tools/lib/src/application_package.dart +++ b/packages/flutter_tools/lib/src/application_package.dart @@ -21,6 +21,7 @@ import 'ios/plist_utils.dart' as plist; import 'macos/application_package.dart'; import 'project.dart'; import 'tester/flutter_tester.dart'; +import 'web/web_device.dart'; class ApplicationPackageFactory { static ApplicationPackageFactory get instance => context[ApplicationPackageFactory]; @@ -50,10 +51,11 @@ class ApplicationPackageFactory { return applicationBinary != null ? MacOSApp.fromPrebuiltApp(applicationBinary) : null; + case TargetPlatform.web: + return WebApplicationPackage(await FlutterProject.current()); case TargetPlatform.linux_x64: case TargetPlatform.windows_x64: case TargetPlatform.fuchsia: - case TargetPlatform.web: return null; } assert(platform != null); diff --git a/packages/flutter_tools/lib/src/context_runner.dart b/packages/flutter_tools/lib/src/context_runner.dart index af2976ddc2..0f23de02e9 100644 --- a/packages/flutter_tools/lib/src/context_runner.dart +++ b/packages/flutter_tools/lib/src/context_runner.dart @@ -41,6 +41,7 @@ import 'run_hot.dart'; import 'usage.dart'; import 'version.dart'; import 'web/compile.dart'; +import 'web/web_device.dart'; import 'windows/windows_workflow.dart'; Future runInContext( @@ -65,6 +66,7 @@ Future runInContext( CocoaPods: () => CocoaPods(), CocoaPodsValidator: () => const CocoaPodsValidator(), Config: () => Config(), + ChromeLauncher: () => const ChromeLauncher(), DevFSConfig: () => DevFSConfig(), DeviceManager: () => DeviceManager(), Doctor: () => const Doctor(), diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 52a315357e..cad682f65b 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -20,6 +20,7 @@ import 'ios/simulators.dart'; import 'linux/linux_device.dart'; import 'macos/macos_device.dart'; import 'tester/flutter_tester.dart'; +import 'web/web_device.dart'; import 'windows/windows_device.dart'; DeviceManager get deviceManager => context[DeviceManager]; @@ -36,7 +37,7 @@ class DeviceManager { IOSSimulators(), FuchsiaDevices(), FlutterTesterDevices(), - ] + _conditionalDesktopDevices); + ] + _conditionalDesktopDevices + _conditionalWebDevices); /// Only add desktop devices if the flag is enabled. static List get _conditionalDesktopDevices { @@ -47,6 +48,13 @@ class DeviceManager { ] : []; } + /// Only add web devices if the flag is enabled. + static List get _conditionalWebDevices { + return flutterWebEnabled ? [ + WebDevices(), + ] : []; + } + String _specifiedDeviceId; /// A user-specified device ID. diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index dc7587c1e4..b2a0fde224 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -19,6 +19,7 @@ import 'ios/plist_utils.dart' as plist; import 'ios/xcodeproj.dart' as xcode; import 'plugins.dart'; import 'template.dart'; +import 'web/web_device.dart'; /// Represents the contents of a Flutter project at the specified [directory]. /// @@ -95,6 +96,9 @@ class FlutterProject { /// The Android sub project of this project. AndroidProject get android => AndroidProject._(this); + /// The web sub project of this project. + WebProject get web => WebProject._(this); + /// The `pubspec.yaml` file of this project. File get pubspecFile => directory.childFile('pubspec.yaml'); @@ -150,6 +154,9 @@ class FlutterProject { refreshPluginsList(this); await android.ensureReadyForPlatformSpecificTooling(); await ios.ensureReadyForPlatformSpecificTooling(); + if (flutterWebEnabled) { + await web.ensureReadyForPlatformSpecificTooling(); + } await injectPlugins(this); } @@ -454,6 +461,31 @@ class AndroidProject { } } +/// Represents the web sub-project of a Flutter project. +class WebProject { + WebProject._(this.parent); + + final FlutterProject parent; + + Future ensureReadyForPlatformSpecificTooling() async { + /// Generate index.html in build/web. Eventually we could support + /// a custom html under the web sub directory. + final Directory outputDir = fs.directory(getWebBuildDirectory()); + if (!outputDir.existsSync()) { + outputDir.createSync(recursive: true); + } + final Template template = Template.fromName('web/index.html.tmpl'); + template.render( + outputDir, + { + 'appName': parent.manifest.appName, + }, + printStatusWhenWriting: false, + overwriteExisting: true, + ); + } +} + /// Deletes [directory] with all content. void _deleteIfExistsSync(Directory directory) { if (directory.existsSync()) diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index 8db4102a09..2addb5929d 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -326,7 +326,11 @@ class FlutterDevice { await stopEchoingDeviceLog(); return 2; } - observatoryUris = [result.observatoryUri]; + if (result.hasObservatory) { + observatoryUris = [result.observatoryUri]; + } else { + observatoryUris = []; + } return 0; } @@ -384,8 +388,11 @@ class FlutterDevice { await stopEchoingDeviceLog(); return 2; } - if (result.hasObservatory) + if (result.hasObservatory) { observatoryUris = [result.observatoryUri]; + } else { + observatoryUris = []; + } return 0; } diff --git a/packages/flutter_tools/lib/src/web/compile.dart b/packages/flutter_tools/lib/src/web/compile.dart index 4dab7488f8..51febc0b9c 100644 --- a/packages/flutter_tools/lib/src/web/compile.dart +++ b/packages/flutter_tools/lib/src/web/compile.dart @@ -38,6 +38,7 @@ class WebCompiler { if (!processManager.canRun(engineDartPath)) { throwToolExit('Unable to find Dart binary at $engineDartPath'); } + /// Compile Dart to JavaScript. final List command = [ engineDartPath, dart2jsPath, diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart new file mode 100644 index 0000000000..d98bb97e94 --- /dev/null +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -0,0 +1,206 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import '../application_package.dart'; +import '../base/context.dart'; +import '../base/file_system.dart'; +import '../base/io.dart'; +import '../base/logger.dart'; +import '../base/platform.dart'; +import '../base/process_manager.dart'; +import '../build_info.dart'; +import '../device.dart'; +import '../globals.dart'; +import '../project.dart'; +import '../web/compile.dart'; + +ChromeLauncher get chromeLauncher => context[ChromeLauncher]; + +/// Only launch or display web devices if `FLUTTER_WEB` +/// environment variable is set to true. +bool get flutterWebEnabled { + _flutterWebEnabled = platform.environment['FLUTTER_WEB']?.toLowerCase() == 'true'; + return _flutterWebEnabled; +} +bool _flutterWebEnabled; + + +class WebApplicationPackage extends ApplicationPackage { + WebApplicationPackage(this._flutterProject) : super(id: _flutterProject.manifest.appName); + + final FlutterProject _flutterProject; + + @override + String get name => _flutterProject.manifest.appName; + + /// The location of the web source assets. + Directory get webSourcePath => _flutterProject.directory.childDirectory('web'); +} + + +class WebDevice extends Device { + WebDevice() : super('web'); + + HttpServer _server; + WebApplicationPackage _package; + + @override + bool get supportsHotReload => false; + + @override + bool get supportsHotRestart => false; + + @override + bool get supportsStartPaused => true; + + @override + bool get supportsStopApp => true; + + @override + bool get supportsScreenshot => false; + + @override + void clearLogs() {} + + @override + DeviceLogReader getLogReader({ApplicationPackage app}) { + return NoOpDeviceLogReader(app.name); + } + + @override + Future installApp(ApplicationPackage app) async => true; + + @override + Future isAppInstalled(ApplicationPackage app) async => true; + + @override + Future isLatestBuildInstalled(ApplicationPackage app) async => true; + + @override + Future get isLocalEmulator async => false; + + @override + bool isSupported() => flutterWebEnabled; + + @override + String get name => 'web'; + + @override + DevicePortForwarder get portForwarder => const NoOpDevicePortForwarder(); + + @override + Future get sdkNameAndVersion async => 'web'; + + @override + Future startApp( + covariant WebApplicationPackage package, { + String mainPath, + String route, + DebuggingOptions debuggingOptions, + Map platformArgs, + bool prebuiltApplication = false, + bool applicationNeedsRebuild = false, + bool usesTerminalUi = true, + bool ipv6 = false, + }) async { + final Status status = logger.startProgress('Compiling ${package.name} to JavaScript...', timeout: null); + final int result = await webCompiler.compile(target: mainPath, minify: false, enabledAssertions: true); + status.stop(); + if (result != 0) { + printError('Failed to compile ${package.name} to JavaScript'); + return LaunchResult.failed(); + } + _package = package; + _server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0); + _server.listen(_basicAssetServer); + printStatus('Serving assets from http:localhost:${_server.port}'); + await chromeLauncher.launch('http:localhost:${_server.port}'); + return LaunchResult.succeeded(observatoryUri: null); + } + + // Note: we don't currently have a way to track which chrome processes + // belong to the flutter tool, so we'll err on the side of caution by + // keeping these open. + @override + Future stopApp(ApplicationPackage app) async { + await _server?.close(); + _server = null; + return true; + } + + @override + Future get targetPlatform async => TargetPlatform.web; + + @override + Future uninstallApp(ApplicationPackage app) async => true; + + Future _basicAssetServer(HttpRequest request) async { + if (request.method != 'GET') { + request.response.statusCode = HttpStatus.forbidden; + await request.response.close(); + return; + } + // Resolve all get requests to the build/web/asset directory. + final Uri uri = request.uri; + File file; + String contentType; + if (uri.path == '/') { + file = _package.webSourcePath.childFile('index.html'); + contentType = 'text/html'; + } else if (uri.path == '/main.dart.js') { + file = fs.file(fs.path.join(getWebBuildDirectory(), 'main.dart.js')); + contentType = 'text/javascript'; + } else { + file = fs.file(fs.path.join(getAssetBuildDirectory(), uri.path)); + } + if (!file.existsSync()) { + request.response.statusCode = HttpStatus.notFound; + await request.response.close(); + return; + } + request.response.statusCode = HttpStatus.ok; + if (contentType != null) { + request.response.headers.add(HttpHeaders.contentTypeHeader, contentType); + } + await request.response.addStream(file.openRead()); + await request.response.close(); + } +} + +class WebDevices extends PollingDeviceDiscovery { + WebDevices() : super('web'); + + final WebDevice _webDevice = WebDevice(); + + @override + bool get canListAnything => flutterWebEnabled; + + @override + Future> pollingGetDevices() async { + return [ + _webDevice, + ]; + } + + @override + bool get supportsPlatform => flutterWebEnabled; + +} + +// Responsible for launching chrome with devtools configured. +class ChromeLauncher { + const ChromeLauncher(); + + static const String _kMacosLocation = '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'; + + Future launch(String host) async { + if (platform.isMacOS) { + await processManager.start([ + _kMacosLocation, + host, + ]); + } + throw UnsupportedError('$platform is not supported'); + } +} diff --git a/packages/flutter_tools/templates/web/index.html.tmpl b/packages/flutter_tools/templates/web/index.html.tmpl new file mode 100644 index 0000000000..96507b54d5 --- /dev/null +++ b/packages/flutter_tools/templates/web/index.html.tmpl @@ -0,0 +1,10 @@ + + + + + {{appName}} + + + + + diff --git a/packages/flutter_tools/test/web/devices_test.dart b/packages/flutter_tools/test/web/devices_test.dart new file mode 100644 index 0000000000..562e9bf6a4 --- /dev/null +++ b/packages/flutter_tools/test/web/devices_test.dart @@ -0,0 +1,46 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/base/platform.dart'; +import 'package:flutter_tools/src/project.dart'; +import 'package:flutter_tools/src/web/compile.dart'; +import 'package:flutter_tools/src/web/web_device.dart'; +import 'package:mockito/mockito.dart'; + +import '../src/common.dart'; +import '../src/context.dart'; + +void main() { + group(WebDevice, () { + final MockWebCompiler mockWebCompiler = MockWebCompiler(); + final MockChromeLauncher mockChromeLauncher = MockChromeLauncher(); + final MockPlatform mockPlatform = MockPlatform(); + FlutterProject flutterProject; + + setUp(() async { + flutterProject = await FlutterProject.fromPath(fs.path.join(getFlutterRoot(), 'dev', 'integration_tests', 'web')); + when(mockWebCompiler.compile( + target: anyNamed('target'), + minify: anyNamed('minify'), + enabledAssertions: anyNamed('enabledAssertions'), + )).thenAnswer((Invocation invocation) async => 0); + when(mockChromeLauncher.launch(any)).thenAnswer((Invocation invocation) async {}); + }); + + testUsingContext('can build and connect to chrome', () async { + final WebDevice device = WebDevice(); + await device.startApp(WebApplicationPackage(flutterProject)); + }, overrides: { + ChromeLauncher: () => mockChromeLauncher, + WebCompiler: () => mockWebCompiler, + Platform: () => mockPlatform, + }); + }); +} + +class MockChromeLauncher extends Mock implements ChromeLauncher {} +class MockWebCompiler extends Mock implements WebCompiler {} +class MockPlatform extends Mock implements Platform {} +