From ffe5197e190cea3d7cdc77209e502188ce82c9f2 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Fri, 20 Nov 2020 13:13:04 -0800 Subject: [PATCH] refactor (#70959) --- .../lib/src/commands/create.dart | 11 ++---- .../lib/src/commands/create_base.dart | 39 ++++++++++++------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index e501104fbe..3b51c0493e 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -160,7 +160,7 @@ class CreateCommand extends CreateBase { // If the project directory exists and isn't empty, then try to determine the template // type from the project directory. if (projectDir.existsSync() && projectDir.listSync().isNotEmpty) { - detectedProjectType = determineTemplateType(projectDir); + detectedProjectType = determineTemplateType(); if (detectedProjectType == null && metadataExists) { // We can only be definitive that this is the wrong type if the .metadata file // exists and contains a type that we don't understand, or doesn't contain a type. @@ -189,10 +189,6 @@ class CreateCommand extends CreateBase { } validateOutputDirectoryArg(); - final String flutterRoot = getFlutterRoot(); - - 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) { @@ -223,11 +219,10 @@ class CreateCommand extends CreateBase { exitCode: 2); } - final String organization = await getOrganization(projectDir); + final String organization = await getOrganization(); final bool overwrite = boolArg('overwrite'); - validateProjectDir(projectDirPath, flutterRoot: flutterRoot, overwrite: overwrite); - final String projectName = getProjectName(projectDirPath); + validateProjectDir(overwrite: overwrite); if (boolArg('with-driver-test')) { globals.printError( diff --git a/packages/flutter_tools/lib/src/commands/create_base.dart b/packages/flutter_tools/lib/src/commands/create_base.dart index a0d5e31e2d..e8620d7d83 100644 --- a/packages/flutter_tools/lib/src/commands/create_base.dart +++ b/packages/flutter_tools/lib/src/commands/create_base.dart @@ -105,6 +105,18 @@ abstract class CreateBase extends FlutterCommand { ); } + /// The output directory of the command. + @protected + Directory get projectDir { + return globals.fs.directory(argResults.rest.first); + } + + /// The normalized absolute path of [projectDir]. + @protected + String get projectDirPath { + return globals.fs.path.normalize(projectDir.absolute.path); + } + /// Adds a `--platforms` argument. /// /// The help message of the argument is replaced with `customHelp` if `customHelp` is not null. @@ -140,7 +152,7 @@ abstract class CreateBase extends FlutterCommand { /// /// Throw with exit code 2 if the flutter sdk installed is invalid. @protected - String getFlutterRoot() { + String get flutterRoot { if (Cache.flutterRoot == null) { throwToolExit( 'The FLUTTER_ROOT environment variable was not specified. Unable to find package:flutter.', @@ -177,10 +189,10 @@ abstract class CreateBase extends FlutterCommand { /// Otherwise, we don't presume to know what type of project it could be, since /// many of the files could be missing, and we can't really tell definitively. /// - /// Throws assertion if projectDir does not exist or empty. + /// Throws assertion if [projectDir] does not exist or empty. /// Returns null if no project type can be determined. @protected - FlutterProjectType determineTemplateType(Directory projectDir) { + FlutterProjectType determineTemplateType() { assert(projectDir.existsSync() && projectDir.listSync().isNotEmpty); final File metadataFile = globals.fs .file(globals.fs.path.join(projectDir.absolute.path, '.metadata')); @@ -215,7 +227,7 @@ abstract class CreateBase extends FlutterCommand { /// If `--org` is specified in the command, returns that directly. /// If `--org` is not specified, returns the organization from the existing project. @protected - Future getOrganization(Directory projectDir) async { + Future getOrganization() async { String organization = stringArg('org'); if (!argResults.wasParsed('org')) { final FlutterProject project = FlutterProject.fromDirectory(projectDir); @@ -233,20 +245,19 @@ abstract class CreateBase extends FlutterCommand { /// Throws with exit 2 if the project directory is illegal. @protected - void validateProjectDir(String dirPath, - {String flutterRoot, bool overwrite = false}) { - if (globals.fs.path.isWithin(flutterRoot, dirPath)) { + void validateProjectDir({bool overwrite = false}) { + if (globals.fs.path.isWithin(flutterRoot, projectDirPath)) { throwToolExit( 'Cannot create a project within the Flutter SDK. ' - "Target directory '$dirPath' is within the Flutter SDK at '$flutterRoot'.", + "Target directory '$projectDirPath' is within the Flutter SDK at '$flutterRoot'.", exitCode: 2); } // 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 (globals.fs.isFileSync(dirPath)) { + if (globals.fs.isFileSync(projectDirPath)) { final String message = - "Invalid project name: '$dirPath' - refers to an existing file."; + "Invalid project name: '$projectDirPath' - refers to an existing file."; throwToolExit( overwrite ? '$message Refusing to overwrite a file with a directory.' @@ -258,17 +269,17 @@ abstract class CreateBase extends FlutterCommand { return; } - final FileSystemEntityType type = globals.fs.typeSync(dirPath); + final FileSystemEntityType type = globals.fs.typeSync(projectDirPath); switch (type) { case FileSystemEntityType.file: // Do not overwrite files. - throwToolExit("Invalid project name: '$dirPath' - file exists.", + throwToolExit("Invalid project name: '$projectDirPath' - file exists.", exitCode: 2); break; case FileSystemEntityType.link: // Do not overwrite links. - throwToolExit("Invalid project name: '$dirPath' - refers to a link.", + throwToolExit("Invalid project name: '$projectDirPath' - refers to a link.", exitCode: 2); break; default: @@ -279,7 +290,7 @@ abstract class CreateBase extends FlutterCommand { /// /// Use the current directory path name if the `--project-name` is not specified explicitly. @protected - String getProjectName(String projectDirPath) { + String get projectName { final String projectName = stringArg('project-name') ?? globals.fs.path.basename(projectDirPath); if (!boolArg('skip-name-checks')) {