Unverified Commit 3138cce7 authored by wangying's avatar wangying Committed by GitHub

feat: support configure a custom launch url for flutter web (#95002)

parent 2c393757
...@@ -228,6 +228,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment ...@@ -228,6 +228,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
webRunHeadless: featureFlags.isWebEnabled && boolArg('web-run-headless'), webRunHeadless: featureFlags.isWebEnabled && boolArg('web-run-headless'),
webBrowserDebugPort: browserDebugPort, webBrowserDebugPort: browserDebugPort,
webEnableExpressionEvaluation: featureFlags.isWebEnabled && boolArg('web-enable-expression-evaluation'), webEnableExpressionEvaluation: featureFlags.isWebEnabled && boolArg('web-enable-expression-evaluation'),
webLaunchUrl: featureFlags.isWebEnabled ? stringArg('web-launch-url') : null,
vmserviceOutFile: stringArg('vmservice-out-file'), vmserviceOutFile: stringArg('vmservice-out-file'),
fastStart: argParser.options.containsKey('fast-start') fastStart: argParser.options.containsKey('fast-start')
&& boolArg('fast-start') && boolArg('fast-start')
......
...@@ -763,6 +763,7 @@ class DebuggingOptions { ...@@ -763,6 +763,7 @@ class DebuggingOptions {
this.webRunHeadless = false, this.webRunHeadless = false,
this.webBrowserDebugPort, this.webBrowserDebugPort,
this.webEnableExpressionEvaluation = false, this.webEnableExpressionEvaluation = false,
this.webLaunchUrl,
this.vmserviceOutFile, this.vmserviceOutFile,
this.fastStart = false, this.fastStart = false,
this.nullAssertions = false, this.nullAssertions = false,
...@@ -779,6 +780,7 @@ class DebuggingOptions { ...@@ -779,6 +780,7 @@ class DebuggingOptions {
this.webUseSseForInjectedClient = true, this.webUseSseForInjectedClient = true,
this.webRunHeadless = false, this.webRunHeadless = false,
this.webBrowserDebugPort, this.webBrowserDebugPort,
this.webLaunchUrl,
this.cacheSkSL = false, this.cacheSkSL = false,
this.traceAllowlist, this.traceAllowlist,
}) : debuggingEnabled = false, }) : debuggingEnabled = false,
...@@ -852,6 +854,9 @@ class DebuggingOptions { ...@@ -852,6 +854,9 @@ class DebuggingOptions {
/// Enable expression evaluation for web target. /// Enable expression evaluation for web target.
final bool webEnableExpressionEvaluation; 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. /// A file where the VM Service URL should be written after the application is started.
final String? vmserviceOutFile; final String? vmserviceOutFile;
final bool fastStart; final bool fastStart;
......
...@@ -263,6 +263,10 @@ abstract class FlutterCommand extends Command<void> { ...@@ -263,6 +263,10 @@ abstract class FlutterCommand extends Command<void> {
help: 'Enables expression evaluation in the debugger.', help: 'Enables expression evaluation in the debugger.',
hide: !verboseHelp, 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() { void usesTargetOption() {
......
...@@ -6,6 +6,7 @@ import 'package:meta/meta.dart'; ...@@ -6,6 +6,7 @@ import 'package:meta/meta.dart';
import 'package:process/process.dart'; import 'package:process/process.dart';
import '../application_package.dart'; import '../application_package.dart';
import '../base/common.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart'; import '../base/io.dart';
import '../base/logger.dart'; import '../base/logger.dart';
...@@ -128,7 +129,17 @@ abstract class ChromiumDevice extends Device { ...@@ -128,7 +129,17 @@ abstract class ChromiumDevice extends Device {
}) async { }) async {
// See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart // See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart
// for the web initialization and server logic. // 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; final bool launchChrome = platformArgs['no-launch-chrome'] != true;
if (launchChrome) { if (launchChrome) {
_chrome = await chromeLauncher.launch( _chrome = await chromeLauncher.launch(
......
...@@ -594,6 +594,30 @@ void main() { ...@@ -594,6 +594,30 @@ void main() {
FileSystem: () => MemoryFileSystem.test(), FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
testUsingContext('fails when "--web-launch-url" is not supported', () async {
final RunCommand command = RunCommand();
await expectLater(
() => createTestCommandRunner(command).run(<String>[
'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: <Type, Generator>{
ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
});
} }
class FakeDeviceManager extends Fake implements DeviceManager { class FakeDeviceManager extends Fake implements DeviceManager {
......
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