refactor (#70959)
This commit is contained in:
@@ -160,7 +160,7 @@ class CreateCommand extends CreateBase {
|
|||||||
// If the project directory exists and isn't empty, then try to determine the template
|
// If the project directory exists and isn't empty, then try to determine the template
|
||||||
// type from the project directory.
|
// type from the project directory.
|
||||||
if (projectDir.existsSync() && projectDir.listSync().isNotEmpty) {
|
if (projectDir.existsSync() && projectDir.listSync().isNotEmpty) {
|
||||||
detectedProjectType = determineTemplateType(projectDir);
|
detectedProjectType = determineTemplateType();
|
||||||
if (detectedProjectType == null && metadataExists) {
|
if (detectedProjectType == null && metadataExists) {
|
||||||
// We can only be definitive that this is the wrong type if the .metadata file
|
// 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.
|
// 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();
|
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;
|
String sampleCode;
|
||||||
if (argResults['sample'] != null) {
|
if (argResults['sample'] != null) {
|
||||||
@@ -223,11 +219,10 @@ class CreateCommand extends CreateBase {
|
|||||||
exitCode: 2);
|
exitCode: 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
final String organization = await getOrganization(projectDir);
|
final String organization = await getOrganization();
|
||||||
|
|
||||||
final bool overwrite = boolArg('overwrite');
|
final bool overwrite = boolArg('overwrite');
|
||||||
validateProjectDir(projectDirPath, flutterRoot: flutterRoot, overwrite: overwrite);
|
validateProjectDir(overwrite: overwrite);
|
||||||
final String projectName = getProjectName(projectDirPath);
|
|
||||||
|
|
||||||
if (boolArg('with-driver-test')) {
|
if (boolArg('with-driver-test')) {
|
||||||
globals.printError(
|
globals.printError(
|
||||||
|
|||||||
@@ -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.
|
/// Adds a `--platforms` argument.
|
||||||
///
|
///
|
||||||
/// The help message of the argument is replaced with `customHelp` if `customHelp` is not null.
|
/// 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.
|
/// Throw with exit code 2 if the flutter sdk installed is invalid.
|
||||||
@protected
|
@protected
|
||||||
String getFlutterRoot() {
|
String get flutterRoot {
|
||||||
if (Cache.flutterRoot == null) {
|
if (Cache.flutterRoot == null) {
|
||||||
throwToolExit(
|
throwToolExit(
|
||||||
'The FLUTTER_ROOT environment variable was not specified. Unable to find package:flutter.',
|
'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
|
/// 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.
|
/// 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.
|
/// Returns null if no project type can be determined.
|
||||||
@protected
|
@protected
|
||||||
FlutterProjectType determineTemplateType(Directory projectDir) {
|
FlutterProjectType determineTemplateType() {
|
||||||
assert(projectDir.existsSync() && projectDir.listSync().isNotEmpty);
|
assert(projectDir.existsSync() && projectDir.listSync().isNotEmpty);
|
||||||
final File metadataFile = globals.fs
|
final File metadataFile = globals.fs
|
||||||
.file(globals.fs.path.join(projectDir.absolute.path, '.metadata'));
|
.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 specified in the command, returns that directly.
|
||||||
/// If `--org` is not specified, returns the organization from the existing project.
|
/// If `--org` is not specified, returns the organization from the existing project.
|
||||||
@protected
|
@protected
|
||||||
Future<String> getOrganization(Directory projectDir) async {
|
Future<String> getOrganization() async {
|
||||||
String organization = stringArg('org');
|
String organization = stringArg('org');
|
||||||
if (!argResults.wasParsed('org')) {
|
if (!argResults.wasParsed('org')) {
|
||||||
final FlutterProject project = FlutterProject.fromDirectory(projectDir);
|
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.
|
/// Throws with exit 2 if the project directory is illegal.
|
||||||
@protected
|
@protected
|
||||||
void validateProjectDir(String dirPath,
|
void validateProjectDir({bool overwrite = false}) {
|
||||||
{String flutterRoot, bool overwrite = false}) {
|
if (globals.fs.path.isWithin(flutterRoot, projectDirPath)) {
|
||||||
if (globals.fs.path.isWithin(flutterRoot, dirPath)) {
|
|
||||||
throwToolExit(
|
throwToolExit(
|
||||||
'Cannot create a project within the Flutter SDK. '
|
'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);
|
exitCode: 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the destination directory is actually a file, then we refuse to
|
// 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.
|
// 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 =
|
final String message =
|
||||||
"Invalid project name: '$dirPath' - refers to an existing file.";
|
"Invalid project name: '$projectDirPath' - refers to an existing file.";
|
||||||
throwToolExit(
|
throwToolExit(
|
||||||
overwrite
|
overwrite
|
||||||
? '$message Refusing to overwrite a file with a directory.'
|
? '$message Refusing to overwrite a file with a directory.'
|
||||||
@@ -258,17 +269,17 @@ abstract class CreateBase extends FlutterCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final FileSystemEntityType type = globals.fs.typeSync(dirPath);
|
final FileSystemEntityType type = globals.fs.typeSync(projectDirPath);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case FileSystemEntityType.file:
|
case FileSystemEntityType.file:
|
||||||
// Do not overwrite files.
|
// Do not overwrite files.
|
||||||
throwToolExit("Invalid project name: '$dirPath' - file exists.",
|
throwToolExit("Invalid project name: '$projectDirPath' - file exists.",
|
||||||
exitCode: 2);
|
exitCode: 2);
|
||||||
break;
|
break;
|
||||||
case FileSystemEntityType.link:
|
case FileSystemEntityType.link:
|
||||||
// Do not overwrite links.
|
// Do not overwrite links.
|
||||||
throwToolExit("Invalid project name: '$dirPath' - refers to a link.",
|
throwToolExit("Invalid project name: '$projectDirPath' - refers to a link.",
|
||||||
exitCode: 2);
|
exitCode: 2);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -279,7 +290,7 @@ abstract class CreateBase extends FlutterCommand {
|
|||||||
///
|
///
|
||||||
/// Use the current directory path name if the `--project-name` is not specified explicitly.
|
/// Use the current directory path name if the `--project-name` is not specified explicitly.
|
||||||
@protected
|
@protected
|
||||||
String getProjectName(String projectDirPath) {
|
String get projectName {
|
||||||
final String projectName =
|
final String projectName =
|
||||||
stringArg('project-name') ?? globals.fs.path.basename(projectDirPath);
|
stringArg('project-name') ?? globals.fs.path.basename(projectDirPath);
|
||||||
if (!boolArg('skip-name-checks')) {
|
if (!boolArg('skip-name-checks')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user