Fix project name fallback (#150614)
This PR changes the project name logic for `flutter create` to look for the name in the pubspec.yaml `name` field, before falling back to the directory name. Fixes https://github.com/flutter/flutter/issues/53106 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
import '../android/android.dart' as android_common;
|
||||
import '../android/android_workflow.dart';
|
||||
@@ -319,13 +320,45 @@ abstract class CreateBase extends FlutterCommand {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the project name based.
|
||||
/// Gets the project name.
|
||||
///
|
||||
/// Use the current directory path name if the `--project-name` is not specified explicitly.
|
||||
/// If the `--project-name` is not specified explicitly,
|
||||
/// the `name` field from the pubspec.yaml file is used.
|
||||
///
|
||||
/// If the pubspec.yaml file does not exist,
|
||||
/// the current directory path name is used.
|
||||
@protected
|
||||
String get projectName {
|
||||
final String projectName =
|
||||
stringArg('project-name') ?? globals.fs.path.basename(projectDirPath);
|
||||
String? projectName = stringArg('project-name');
|
||||
|
||||
if (projectName == null) {
|
||||
final File pubspec = globals.fs
|
||||
.directory(projectDirPath)
|
||||
.childFile('pubspec.yaml');
|
||||
|
||||
if (pubspec.existsSync()) {
|
||||
final String pubspecContents = pubspec.readAsStringSync();
|
||||
|
||||
try {
|
||||
final Object? pubspecYaml = loadYaml(pubspecContents);
|
||||
|
||||
if (pubspecYaml is YamlMap) {
|
||||
final Object? pubspecName = pubspecYaml['name'];
|
||||
|
||||
if (pubspecName is String) {
|
||||
projectName = pubspecName;
|
||||
}
|
||||
}
|
||||
} on YamlException {
|
||||
// If the pubspec is malformed, fallback to using the directory name.
|
||||
}
|
||||
}
|
||||
|
||||
final String projectDirName = globals.fs.path.basename(projectDirPath);
|
||||
|
||||
projectName ??= projectDirName;
|
||||
}
|
||||
|
||||
if (!boolArg('skip-name-checks')) {
|
||||
final String? error = _validateProjectName(projectName);
|
||||
if (error != null) {
|
||||
|
||||
Reference in New Issue
Block a user