mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 08:24:39 +00:00
[dartdev] [dds] Share DevTools arg parsing with serve_local script
Change-Id: Ia8f95b205fda1201a7ec50805a84d374a6201f39 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253460 Reviewed-by: Ben Konyi <bkonyi@google.com> Reviewed-by: Kenzie Davisson <kenzieschmoll@google.com> Commit-Queue: Ben Konyi <bkonyi@google.com>
This commit is contained in:
parent
c9750b2e4b
commit
09bcc1f4ae
|
@ -4,7 +4,6 @@
|
|||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:dds/devtools_server.dart';
|
||||
import 'package:dds/src/devtools/utils.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../core.dart';
|
||||
|
@ -15,7 +14,7 @@ class DevToolsCommand extends DartdevCommand {
|
|||
DevToolsCommand({
|
||||
this.customDevToolsPath,
|
||||
bool verbose = false,
|
||||
}) : _argParser = DevToolsServer.buildArgParser(
|
||||
}) : argParser = DevToolsServer.buildArgParser(
|
||||
verbose: verbose,
|
||||
includeHelpOption: false,
|
||||
usageLineLength: dartdevUsageLineLength,
|
||||
|
@ -29,8 +28,7 @@ class DevToolsCommand extends DartdevCommand {
|
|||
final String? customDevToolsPath;
|
||||
|
||||
@override
|
||||
ArgParser get argParser => _argParser;
|
||||
late final ArgParser _argParser;
|
||||
final ArgParser argParser;
|
||||
|
||||
@override
|
||||
String get name => 'devtools';
|
||||
|
@ -44,87 +42,16 @@ class DevToolsCommand extends DartdevCommand {
|
|||
@override
|
||||
Future<int> run() async {
|
||||
final args = argResults!;
|
||||
final bool version = args[DevToolsServer.argVersion];
|
||||
final bool machineMode = args[DevToolsServer.argMachine];
|
||||
// launchBrowser defaults based on machine-mode if not explicitly supplied.
|
||||
final bool launchBrowser = args.wasParsed(DevToolsServer.argLaunchBrowser)
|
||||
? args[DevToolsServer.argLaunchBrowser]
|
||||
: !machineMode;
|
||||
final bool enableNotifications =
|
||||
args[DevToolsServer.argEnableNotifications];
|
||||
final bool allowEmbedding = args.wasParsed(DevToolsServer.argAllowEmbedding)
|
||||
? args[DevToolsServer.argAllowEmbedding]
|
||||
: true;
|
||||
|
||||
final port = args[DevToolsServer.argPort] != null
|
||||
? int.tryParse(args[DevToolsServer.argPort]) ?? 0
|
||||
: 0;
|
||||
|
||||
final bool headlessMode = args[DevToolsServer.argHeadlessMode];
|
||||
final bool debugMode = args[DevToolsServer.argDebugMode];
|
||||
|
||||
final numPortsToTry = args[DevToolsServer.argTryPorts] != null
|
||||
? int.tryParse(args[DevToolsServer.argTryPorts]) ?? 0
|
||||
: DevToolsServer.defaultTryPorts;
|
||||
|
||||
final bool verboseMode = args[DevToolsServer.argVerbose];
|
||||
final String? hostname = args[DevToolsServer.argHost];
|
||||
final String? appSizeBase = args[DevToolsServer.argAppSizeBase];
|
||||
final String? appSizeTest = args[DevToolsServer.argAppSizeTest];
|
||||
|
||||
final sdkDir = path.dirname(sdk.dart);
|
||||
final fullSdk = sdkDir.endsWith('bin');
|
||||
final devToolsBinaries =
|
||||
fullSdk ? sdk.devToolsBinaries : path.absolute(sdkDir, 'devtools');
|
||||
|
||||
if (version) {
|
||||
final versionStr = await DevToolsUtils.getVersion(devToolsBinaries);
|
||||
DevToolsUtils.printOutput(
|
||||
'Dart DevTools version $versionStr',
|
||||
{
|
||||
'version': versionStr,
|
||||
},
|
||||
machineMode: machineMode,
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Prefer getting the VM URI from the rest args; fall back on the 'vm-url'
|
||||
// option otherwise.
|
||||
String? serviceProtocolUri;
|
||||
if (args.rest.isNotEmpty) {
|
||||
serviceProtocolUri = args.rest.first;
|
||||
} else if (args.wasParsed(DevToolsServer.argVmUri)) {
|
||||
serviceProtocolUri = args[DevToolsServer.argVmUri];
|
||||
}
|
||||
|
||||
// Support collecting profile data.
|
||||
String? profileFilename;
|
||||
if (args.wasParsed(DevToolsServer.argProfileMemory)) {
|
||||
profileFilename = args[DevToolsServer.argProfileMemory];
|
||||
}
|
||||
if (profileFilename != null && !path.isAbsolute(profileFilename)) {
|
||||
profileFilename = path.absolute(profileFilename);
|
||||
}
|
||||
|
||||
final server = await DevToolsServer().serveDevTools(
|
||||
machineMode: machineMode,
|
||||
debugMode: debugMode,
|
||||
launchBrowser: launchBrowser,
|
||||
enableNotifications: enableNotifications,
|
||||
allowEmbedding: allowEmbedding,
|
||||
port: port,
|
||||
headlessMode: headlessMode,
|
||||
numPortsToTry: numPortsToTry,
|
||||
customDevToolsPath: customDevToolsPath ?? devToolsBinaries,
|
||||
serviceProtocolUri: serviceProtocolUri,
|
||||
profileFilename: profileFilename,
|
||||
verboseMode: verboseMode,
|
||||
hostname: hostname,
|
||||
appSizeBase: appSizeBase,
|
||||
appSizeTest: appSizeTest,
|
||||
final server = await DevToolsServer().serveDevToolsWithArgs(
|
||||
args.arguments,
|
||||
customDevToolsPath: devToolsBinaries,
|
||||
);
|
||||
|
||||
return server == null ? -1 : 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import 'package:dds/devtools_server.dart';
|
|||
const argDevToolsBuild = 'devtools-build';
|
||||
|
||||
void main(List<String> args) {
|
||||
final argParser = ArgParser()
|
||||
final argParser = DevToolsServer.buildArgParser()
|
||||
..addOption(
|
||||
argDevToolsBuild,
|
||||
help: 'The location of the DevTools build to serve from DevTools server '
|
||||
|
@ -21,7 +21,8 @@ void main(List<String> args) {
|
|||
try {
|
||||
final ArgResults argResults = argParser.parse(args);
|
||||
unawaited(
|
||||
DevToolsServer().serveDevTools(
|
||||
DevToolsServer().serveDevToolsWithArgs(
|
||||
_removeDevToolsBuildOption(args),
|
||||
customDevToolsPath: argResults[argDevToolsBuild],
|
||||
),
|
||||
);
|
||||
|
@ -31,3 +32,23 @@ void main(List<String> args) {
|
|||
print(argParser.usage);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes the --devtools-build option from [args].
|
||||
List<String> _removeDevToolsBuildOption(List<String> args) {
|
||||
// Create a new list to mutate as the args list is fixed.
|
||||
args = args.toList();
|
||||
|
||||
final option = '--$argDevToolsBuild';
|
||||
|
||||
// serve_local.dart --devtools-build foo
|
||||
final index = args.indexOf(option);
|
||||
if (index != -1) {
|
||||
args.removeRange(index, index + 2);
|
||||
}
|
||||
|
||||
// serve_local.dart --devtools-build=foo
|
||||
// serve_local.dart --devtools-build="foo"
|
||||
args.removeWhere((arg) => arg.startsWith('${option}='));
|
||||
|
||||
return args;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue