[flutter_tools] Ensure that global variables are easily identifiable (#47398)
diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index 071d4a7..823da6a 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart
@@ -20,7 +20,7 @@ import '../dart/pub.dart'; import '../doctor.dart'; import '../features.dart'; -import '../globals.dart'; +import '../globals.dart' as globals; import '../project.dart'; import '../reporting/reporting.dart'; import '../runner/flutter_command.dart'; @@ -176,7 +176,7 @@ if (!projectDir.existsSync()) { return null; } - final File metadataFile = fs.file(fs.path.join(projectDir.absolute.path, '.metadata')); + final File metadataFile = globals.fs.file(globals.fs.path.join(projectDir.absolute.path, '.metadata')); if (!metadataFile.existsSync()) { return null; } @@ -190,7 +190,7 @@ } bool exists(List<String> path) { - return fs.directory(fs.path.joinAll(<String>[projectDir.absolute.path, ...path])).existsSync(); + return globals.fs.directory(globals.fs.path.joinAll(<String>[projectDir.absolute.path, ...path])).existsSync(); } // If it exists, the project type in the metadata is definitive. @@ -243,7 +243,7 @@ /// [outputFilePath]. Future<void> _writeSamplesJson(String outputFilePath) async { try { - final File outputFile = fs.file(outputFilePath); + final File outputFile = globals.fs.file(outputFilePath); if (outputFile.existsSync()) { throwToolExit('File "$outputFilePath" already exists', exitCode: 1); } @@ -253,7 +253,7 @@ } else { outputFile.writeAsStringSync(samplesJson); - printStatus('Wrote samples JSON to "$outputFilePath"'); + globals.printStatus('Wrote samples JSON to "$outputFilePath"'); } } catch (e) { throwToolExit('Failed to write samples JSON to "$outputFilePath": $e', exitCode: 2); @@ -320,21 +320,21 @@ 'variable was specified. Unable to find package:flutter.', exitCode: 2); } - final String flutterRoot = fs.path.absolute(Cache.flutterRoot); + final String flutterRoot = globals.fs.path.absolute(Cache.flutterRoot); - final String flutterPackagesDirectory = fs.path.join(flutterRoot, 'packages'); - final String flutterPackagePath = fs.path.join(flutterPackagesDirectory, 'flutter'); - if (!fs.isFileSync(fs.path.join(flutterPackagePath, 'pubspec.yaml'))) { + final String flutterPackagesDirectory = globals.fs.path.join(flutterRoot, 'packages'); + final String flutterPackagePath = globals.fs.path.join(flutterPackagesDirectory, 'flutter'); + if (!globals.fs.isFileSync(globals.fs.path.join(flutterPackagePath, 'pubspec.yaml'))) { throwToolExit('Unable to find package:flutter in $flutterPackagePath', exitCode: 2); } - final String flutterDriverPackagePath = fs.path.join(flutterRoot, 'packages', 'flutter_driver'); - if (!fs.isFileSync(fs.path.join(flutterDriverPackagePath, 'pubspec.yaml'))) { + final String flutterDriverPackagePath = globals.fs.path.join(flutterRoot, 'packages', 'flutter_driver'); + if (!globals.fs.isFileSync(globals.fs.path.join(flutterDriverPackagePath, 'pubspec.yaml'))) { throwToolExit('Unable to find package:flutter_driver in $flutterDriverPackagePath', exitCode: 2); } - final Directory projectDir = fs.directory(argResults.rest.first); - final String projectDirPath = fs.path.normalize(projectDir.absolute.path); + final Directory projectDir = globals.fs.directory(argResults.rest.first); + final String projectDirPath = globals.fs.path.normalize(projectDir.absolute.path); String sampleCode; if (argResults['sample'] != null) { @@ -372,7 +372,7 @@ throwToolExit(error); } - final String projectName = stringArg('project-name') ?? fs.path.basename(projectDirPath); + final String projectName = stringArg('project-name') ?? globals.fs.path.basename(projectDirPath); error = _validateProjectName(projectName); if (error != null) { throwToolExit(error); @@ -392,18 +392,18 @@ macos: featureFlags.isMacOSEnabled, ); - final String relativeDirPath = fs.path.relative(projectDirPath); + final String relativeDirPath = globals.fs.path.relative(projectDirPath); if (!projectDir.existsSync() || projectDir.listSync().isEmpty) { - printStatus('Creating project $relativeDirPath... androidx: ${boolArg('androidx')}'); + globals.printStatus('Creating project $relativeDirPath... androidx: ${boolArg('androidx')}'); } else { if (sampleCode != null && !overwrite) { throwToolExit('Will not overwrite existing project in $relativeDirPath: ' 'must specify --overwrite for samples to overwrite.'); } - printStatus('Recreating project $relativeDirPath...'); + globals.printStatus('Recreating project $relativeDirPath...'); } - final Directory relativeDir = fs.directory(projectDirPath); + final Directory relativeDir = globals.fs.directory(projectDirPath); int generatedFileCount = 0; switch (template) { case _ProjectType.app: @@ -422,36 +422,36 @@ if (sampleCode != null) { generatedFileCount += _applySample(relativeDir, sampleCode); } - printStatus('Wrote $generatedFileCount files.'); - printStatus('\nAll done!'); + globals.printStatus('Wrote $generatedFileCount files.'); + globals.printStatus('\nAll done!'); final String application = sampleCode != null ? 'sample application' : 'application'; if (generatePackage) { - final String relativeMainPath = fs.path.normalize(fs.path.join( + final String relativeMainPath = globals.fs.path.normalize(globals.fs.path.join( relativeDirPath, 'lib', '${templateContext['projectName']}.dart', )); - printStatus('Your package code is in $relativeMainPath'); + globals.printStatus('Your package code is in $relativeMainPath'); } else if (generateModule) { - final String relativeMainPath = fs.path.normalize(fs.path.join( + final String relativeMainPath = globals.fs.path.normalize(globals.fs.path.join( relativeDirPath, 'lib', 'main.dart', )); - printStatus('Your module code is in $relativeMainPath.'); + globals.printStatus('Your module code is in $relativeMainPath.'); } else { // Run doctor; tell the user the next steps. final FlutterProject project = FlutterProject.fromPath(projectDirPath); final FlutterProject app = project.hasExampleApp ? project.example : project; - final String relativeAppPath = fs.path.normalize(fs.path.relative(app.directory.path)); - final String relativeAppMain = fs.path.join(relativeAppPath, 'lib', 'main.dart'); - final String relativePluginPath = fs.path.normalize(fs.path.relative(projectDirPath)); - final String relativePluginMain = fs.path.join(relativePluginPath, 'lib', '$projectName.dart'); + final String relativeAppPath = globals.fs.path.normalize(globals.fs.path.relative(app.directory.path)); + final String relativeAppMain = globals.fs.path.join(relativeAppPath, 'lib', 'main.dart'); + final String relativePluginPath = globals.fs.path.normalize(globals.fs.path.relative(projectDirPath)); + final String relativePluginMain = globals.fs.path.join(relativePluginPath, 'lib', '$projectName.dart'); if (doctor.canLaunchAnything) { // Let them know a summary of the state of their tooling. await doctor.summary(); - printStatus(''' + globals.printStatus(''' In order to run your $application, type: \$ cd $relativeAppPath @@ -460,7 +460,7 @@ Your $application code is in $relativeAppMain. '''); if (generatePlugin) { - printStatus(''' + globals.printStatus(''' Your plugin code is in $relativePluginMain. Host platform code is in the "android" and "ios" directories under $relativePluginPath. @@ -468,18 +468,18 @@ '''); } } else { - printStatus("You'll need to install additional components before you can run " + globals.printStatus("You'll need to install additional components before you can run " 'your Flutter app:'); - printStatus(''); + globals.printStatus(''); // Give the user more detailed analysis. await doctor.diagnose(); - printStatus(''); - printStatus("After installing components, run 'flutter doctor' in order to " + globals.printStatus(''); + globals.printStatus("After installing components, run 'flutter doctor' in order to " 're-validate your setup.'); - printStatus("When complete, type 'flutter run' from the '$relativeAppPath' " + globals.printStatus("When complete, type 'flutter run' from the '$relativeAppPath' " 'directory in order to launch your app.'); - printStatus('Your $application code is in $relativeAppMain'); + globals.printStatus('Your $application code is in $relativeAppMain'); } } @@ -492,7 +492,7 @@ ? stringArg('description') : 'A new flutter module project.'; templateContext['description'] = description; - generatedCount += _renderTemplate(fs.path.join('module', 'common'), directory, templateContext, overwrite: overwrite); + generatedCount += _renderTemplate(globals.fs.path.join('module', 'common'), directory, templateContext, overwrite: overwrite); if (boolArg('pub')) { await pub.get( context: PubContext.create, @@ -602,7 +602,7 @@ bool web = false, bool macos = false, }) { - flutterRoot = fs.path.normalize(flutterRoot); + flutterRoot = globals.fs.path.normalize(flutterRoot); final String pluginDartClass = _createPluginClassName(projectName); final String pluginClass = pluginDartClass.endsWith('Plugin') @@ -652,7 +652,7 @@ int _injectGradleWrapper(FlutterProject project) { int filesCreated = 0; copyDirectorySync( - cache.getArtifactDirectory('gradle_wrapper'), + globals.cache.getArtifactDirectory('gradle_wrapper'), project.android.hostAppGradleRoot, onFileCopied: (File sourceFile, File destinationFile) { filesCreated++; @@ -850,14 +850,14 @@ /// Return null if the project directory is legal. Return a validation message /// if we should disallow the directory name. String _validateProjectDir(String dirPath, { String flutterRoot, bool overwrite = false }) { - if (fs.path.isWithin(flutterRoot, dirPath)) { + if (globals.fs.path.isWithin(flutterRoot, dirPath)) { return 'Cannot create a project within the Flutter SDK. ' "Target directory '$dirPath' is within the Flutter SDK at '$flutterRoot'."; } // If the destination directory is actually a file, then we refuse to // overwrite, on the theory that the user probably didn't expect it to exist. - if (fs.isFileSync(dirPath)) { + if (globals.fs.isFileSync(dirPath)) { return "Invalid project name: '$dirPath' - refers to an existing file." '${overwrite ? ' Refusing to overwrite a file with a directory.' : ''}'; } @@ -866,7 +866,7 @@ return null; } - final FileSystemEntityType type = fs.typeSync(dirPath); + final FileSystemEntityType type = globals.fs.typeSync(dirPath); if (type != FileSystemEntityType.notFound) { switch (type) {