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:
parent
c512a79b2f
commit
ab8bc00e2c
@ -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) {
|
||||
|
@ -824,6 +824,30 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
testUsingContext('recreating project uses pubspec name as project name fallback', () async {
|
||||
final Directory outputDirectory = tempDir.childDirectory('invalid-name');
|
||||
|
||||
// Create the new project with a valid project name,
|
||||
// but with a directory name that would be an invalid project name.
|
||||
await _createProject(
|
||||
outputDirectory,
|
||||
<String>['--no-pub', '--template=app', '--project-name', 'valid_name', '--platforms' , 'android'],
|
||||
<String>[]
|
||||
);
|
||||
|
||||
// Now amend a new platform to the project, but omit the project name, so the fallback project name is used.
|
||||
await _createProject(
|
||||
outputDirectory,
|
||||
<String>['--no-pub', '--template=app', '--platforms', 'web'],
|
||||
<String>[]
|
||||
);
|
||||
|
||||
// Verify that the pubspec name was used as project name for the web project.
|
||||
final File webOutputFile = outputDirectory.childDirectory('web').childFile('index.html');
|
||||
|
||||
expect(webOutputFile.readAsStringSync(), contains('<title>valid_name</title>'));
|
||||
});
|
||||
|
||||
testUsingContext('module project with pub', () async {
|
||||
return _createProject(projectDir, <String>[
|
||||
'--template=module',
|
||||
|
Loading…
x
Reference in New Issue
Block a user