From 3138cce7b223be3c872da53f97b9c44a61eebe71 Mon Sep 17 00:00:00 2001 From: wangying Date: Wed, 19 Jan 2022 01:50:20 +0800 Subject: [PATCH] feat: support configure a custom launch url for flutter web (#95002) --- .../flutter_tools/lib/src/commands/run.dart | 1 + packages/flutter_tools/lib/src/device.dart | 5 ++++ .../lib/src/runner/flutter_command.dart | 4 ++++ .../flutter_tools/lib/src/web/web_device.dart | 13 +++++++++- .../commands.shard/hermetic/run_test.dart | 24 +++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 2bf805017b..78594d6062 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -228,6 +228,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment webRunHeadless: featureFlags.isWebEnabled && boolArg('web-run-headless'), webBrowserDebugPort: browserDebugPort, webEnableExpressionEvaluation: featureFlags.isWebEnabled && boolArg('web-enable-expression-evaluation'), + webLaunchUrl: featureFlags.isWebEnabled ? stringArg('web-launch-url') : null, vmserviceOutFile: stringArg('vmservice-out-file'), fastStart: argParser.options.containsKey('fast-start') && boolArg('fast-start') diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 3a9d229d03..edaaed95ca 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -763,6 +763,7 @@ class DebuggingOptions { this.webRunHeadless = false, this.webBrowserDebugPort, this.webEnableExpressionEvaluation = false, + this.webLaunchUrl, this.vmserviceOutFile, this.fastStart = false, this.nullAssertions = false, @@ -779,6 +780,7 @@ class DebuggingOptions { this.webUseSseForInjectedClient = true, this.webRunHeadless = false, this.webBrowserDebugPort, + this.webLaunchUrl, this.cacheSkSL = false, this.traceAllowlist, }) : debuggingEnabled = false, @@ -852,6 +854,9 @@ class DebuggingOptions { /// Enable expression evaluation for web target. final bool webEnableExpressionEvaluation; + /// Allow developers to customize the browser's launch URL + final String? webLaunchUrl; + /// A file where the VM Service URL should be written after the application is started. final String? vmserviceOutFile; final bool fastStart; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 6a69357e23..267f7c366f 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -263,6 +263,10 @@ abstract class FlutterCommand extends Command { help: 'Enables expression evaluation in the debugger.', hide: !verboseHelp, ); + argParser.addOption('web-launch-url', + help: 'The URL to provide to the browser. Defaults to an HTTP URL with the host ' + 'name of "--web-hostname", the port of "--web-port", and the path set to "/".', + ); } void usesTargetOption() { diff --git a/packages/flutter_tools/lib/src/web/web_device.dart b/packages/flutter_tools/lib/src/web/web_device.dart index 73ef855029..7a277b6507 100644 --- a/packages/flutter_tools/lib/src/web/web_device.dart +++ b/packages/flutter_tools/lib/src/web/web_device.dart @@ -6,6 +6,7 @@ import 'package:meta/meta.dart'; import 'package:process/process.dart'; import '../application_package.dart'; +import '../base/common.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; @@ -128,7 +129,17 @@ abstract class ChromiumDevice extends Device { }) async { // See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart // for the web initialization and server logic. - final String url = platformArgs['uri']! as String; + String url; + if (debuggingOptions.webLaunchUrl != null) { + final RegExp pattern = RegExp(r'^((http)?:\/\/)[^\s]+'); + if (pattern.hasMatch(debuggingOptions.webLaunchUrl!)) { + url = debuggingOptions.webLaunchUrl!; + } else { + throwToolExit('"${debuggingOptions.webLaunchUrl}" is not a vaild HTTP URL.'); + } + } else { + url = platformArgs['uri']! as String; + } final bool launchChrome = platformArgs['no-launch-chrome'] != true; if (launchChrome) { _chrome = await chromeLauncher.launch( diff --git a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart index 0511ac1b3d..e282859207 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/run_test.dart @@ -594,6 +594,30 @@ void main() { FileSystem: () => MemoryFileSystem.test(), ProcessManager: () => FakeProcessManager.any(), }); + + testUsingContext('fails when "--web-launch-url" is not supported', () async { + final RunCommand command = RunCommand(); + await expectLater( + () => createTestCommandRunner(command).run([ + 'run', + '--web-launch-url=http://flutter.dev', + ]), + throwsA(isException.having( + (Exception exception) => exception.toString(), + 'toString', + isNot(contains('web-launch-url')), + )), + ); + + final DebuggingOptions options = await command.createDebuggingOptions(true); + expect(options.webLaunchUrl, 'http://flutter.dev'); + + final RegExp pattern = RegExp(r'^((http)?:\/\/)[^\s]+'); + expect(pattern.hasMatch(options.webLaunchUrl), true); + }, overrides: { + ProcessManager: () => FakeProcessManager.any(), + Logger: () => BufferLogger.test(), + }); } class FakeDeviceManager extends Fake implements DeviceManager {