[flutter_tools] document flutter root initialization (#67884)

Moves the flutter root initialization to a static method on the cache. This is a small step towards making this functionality non-static and instead injected like normal members - however, completely removing all of the static-ness at once was too large of a change.

Instead document and add unit tests and change existing code as little as possible.

#47161
This commit is contained in:
Jonah Williams
2020-10-27 15:20:07 -07:00
committed by GitHub
parent f5ce638959
commit 9e5e763ebe
5 changed files with 185 additions and 42 deletions

View File

@@ -156,43 +156,6 @@ class FlutterCommandRunner extends CommandRunner<void> {
return '$prefix\n\n$usageWithoutDescription';
}
static String get defaultFlutterRoot {
if (globals.platform.environment.containsKey(kFlutterRootEnvironmentVariableName)) {
return globals.platform.environment[kFlutterRootEnvironmentVariableName];
}
try {
if (globals.platform.script.scheme == 'data') {
return '../..'; // we're running as a test
}
if (globals.platform.script.scheme == 'package') {
final String packageConfigPath = Uri.parse(globals.platform.packageConfig).toFilePath();
return globals.fs.path.dirname(globals.fs.path.dirname(globals.fs.path.dirname(packageConfigPath)));
}
final String script = globals.platform.script.toFilePath();
if (globals.fs.path.basename(script) == kSnapshotFileName) {
return globals.fs.path.dirname(globals.fs.path.dirname(globals.fs.path.dirname(script)));
}
if (globals.fs.path.basename(script) == kFlutterToolsScriptFileName) {
return globals.fs.path.dirname(globals.fs.path.dirname(globals.fs.path.dirname(globals.fs.path.dirname(script))));
}
// If run from a bare script within the repo.
if (script.contains('flutter/packages/')) {
return script.substring(0, script.indexOf('flutter/packages/') + 8);
}
if (script.contains('flutter/examples/')) {
return script.substring(0, script.indexOf('flutter/examples/') + 8);
}
} on Exception catch (error) {
// we don't have a logger at the time this is run
// (which is why we don't use printTrace here)
print(userMessages.runnerNoRoot('$error'));
}
return '.';
}
@override
ArgResults parse(Iterable<String> args) {
try {
@@ -264,7 +227,11 @@ class FlutterCommandRunner extends CommandRunner<void> {
// We must set Cache.flutterRoot early because other features use it (e.g.
// enginePath's initializer uses it).
final String flutterRoot = topLevelResults['flutter-root'] as String ?? defaultFlutterRoot;
final String flutterRoot = topLevelResults['flutter-root'] as String ?? Cache.defaultFlutterRoot(
platform: globals.platform,
fileSystem: globals.fs,
userMessages: globals.userMessages,
);
Cache.flutterRoot = globals.fs.path.normalize(globals.fs.path.absolute(flutterRoot));
// Set up the tooling configuration.
@@ -339,7 +306,11 @@ class FlutterCommandRunner extends CommandRunner<void> {
@visibleForTesting
static void initFlutterRoot() {
Cache.flutterRoot ??= defaultFlutterRoot;
Cache.flutterRoot ??= Cache.defaultFlutterRoot(
platform: globals.platform,
fileSystem: globals.fs,
userMessages: globals.userMessages,
);
}
/// Get the root directories of the repo - the directories containing Dart packages.