Skip to content

Commit 53b9620

Browse files
authored
[tool] Add an option to specify the path of a Flutter SDK (#9250)
1 parent 5812bd1 commit 53b9620

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

tool/lib/devtools_command_runner.dart

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import 'commands/update_version.dart';
2727

2828
const _flutterFromPathFlag = 'flutter-from-path';
2929

30+
const _flutterSdkPathFlag = 'flutter-sdk-path';
31+
3032
class DevToolsCommandRunner extends CommandRunner {
3133
DevToolsCommandRunner()
3234
: super('dt', 'A repo management tool for DevTools.') {
@@ -49,20 +51,38 @@ class DevToolsCommandRunner extends CommandRunner {
4951
addCommand(UpdateFlutterSdkCommand());
5052
addCommand(UpdatePerfettoCommand());
5153

52-
argParser.addFlag(
53-
_flutterFromPathFlag,
54-
abbr: 'p',
55-
negatable: false,
56-
help:
57-
'Use the Flutter SDK on PATH for any `flutter`, `dart` and '
58-
'`dt` commands spawned by this process, instead of the '
59-
'Flutter SDK from tool/flutter-sdk which is used by default.',
60-
);
54+
argParser
55+
..addFlag(
56+
_flutterFromPathFlag,
57+
abbr: 'p',
58+
negatable: false,
59+
help:
60+
'Use the Flutter SDK on PATH for any `flutter`, `dart` and '
61+
'`dt` commands spawned by this process, instead of the '
62+
'Flutter SDK from tool/flutter-sdk which is used by default. '
63+
'This is incompatible with the `$_flutterSdkPathFlag` flag.',
64+
)
65+
..addOption(
66+
_flutterSdkPathFlag,
67+
help:
68+
'Use the Flutter SDK from the specified path for any `flutter`, '
69+
'`dart`, and `dt` commands spawned by this process, instead of the '
70+
'Flutter SDK from tool/flutter-sdk which is used by default. '
71+
'This is incompatible with the `$_flutterFromPathFlag` flag.',
72+
);
6173
}
6274

6375
@override
6476
Future<void> runCommand(ArgResults topLevelResults) {
65-
if (topLevelResults[_flutterFromPathFlag]) {
77+
if (topLevelResults.flag(_flutterFromPathFlag) &&
78+
topLevelResults.wasParsed(_flutterSdkPathFlag)) {
79+
throw ArgParserException(
80+
'Only one of `$_flutterFromPathFlag` and `$_flutterSdkPathFlag` may be passed',
81+
);
82+
}
83+
if (topLevelResults.wasParsed(_flutterSdkPathFlag)) {
84+
FlutterSdk.useFromPath(topLevelResults.option(_flutterSdkPathFlag)!);
85+
} else if (topLevelResults.flag(_flutterFromPathFlag)) {
6686
FlutterSdk.useFromPathEnvironmentVariable();
6787
} else {
6888
FlutterSdk.useFromCurrentVm();

tool/lib/model.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@ class FlutterSdk {
196196
_current = findFromPathEnvironmentVariable();
197197
}
198198

199+
/// Sets the active Flutter SDK to the one at [sdkPath].
200+
///
201+
/// Throws if an SDK is not found at [sdkPath].
202+
static void useFromPath(String sdkPath) {
203+
_current = findFromPath(sdkPath);
204+
}
205+
199206
/// Finds the Flutter SDK that contains the Dart VM being used to run this
200207
/// script.
201208
///
@@ -235,6 +242,14 @@ class FlutterSdk {
235242
);
236243
}
237244

245+
static FlutterSdk findFromPath(String sdkPath) {
246+
if (path.basename(path.dirname(sdkPath)) == 'bin') {
247+
return FlutterSdk._(path.dirname(path.dirname(sdkPath)));
248+
}
249+
250+
throw Exception('Unable to locate the Flutter SDK at "$sdkPath"');
251+
}
252+
238253
/// Finds a Flutter SDK in the `PATH` environment variable
239254
/// (by running which/where).
240255
///

0 commit comments

Comments
 (0)