Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest a potential valid name for the flutter project when using flutter create #130900

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/flutter_tools/lib/src/commands/create_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,38 @@ bool isValidPackageName(String name) {
!_keywords.contains(name);
}

/// Returns a potential valid name from the given [name].
///
/// If a valid name cannot be found, returns `null`.
@visibleForTesting
String? potentialValidPackageName(String name){
String newName = name.toLowerCase();
if (newName.startsWith(RegExp(r'[0-9]'))) {
newName = '_$newName';
}
newName = newName.replaceAll('-', '_');
if (isValidPackageName(newName)) {
return newName;
} else {
return null;
}
}

// Return null if the project name is legal. Return a validation message if
// we should disallow the project name.
String? _validateProjectName(String projectName) {
if (!isValidPackageName(projectName)) {
return '"$projectName" is not a valid Dart package name.\n\n'
'See https://dart.dev/tools/pub/pubspec#name for more information.';
final String? potentialValidName = potentialValidPackageName(projectName);

return <String>[
'"$projectName" is not a valid Dart package name.',
'\n\n',
'The name should be all lowercase, with underscores to separate words, "just_like_this".',
'Use only basic Latin letters and Arabic digits: [a-z0-9_].',
"Also, make sure the name is a valid Dart identifier—that it doesn't start with digits and isn't a reserved word.\n",
'See https://dart.dev/tools/pub/pubspec#name for more information.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also put the rules directly into the output so the user have to leave their terminal. I doubt these rules would ever change, so we don't have to worry about them becoming out of date.

However, I think just having a link is also sufficient 👍 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the instructions in doc: Add more description to the error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

If we do include the rules and the link, I think we should include the suggested name last. The suggested name is the least important information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I changed the order in refactor: Put suggested name at the end of the message

if (potentialValidName != null) '\nTry "$potentialValidName" instead.',
].join();
}
if (_packageDependencies.contains(projectName)) {
return "Invalid project name: '$projectName' - this will conflict with Flutter "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,13 @@ void main() {
testUsingContext('plugin project with invalid custom project name', () async {
expect(
() => _createProject(projectDir,
<String>['--no-pub', '--template=plugin', '--project-name', 'xyz.xyz', '--platforms', 'android,ios',],
<String>['--no-pub', '--template=plugin', '--project-name', 'xyz-xyz', '--platforms', 'android,ios',],
<String>[],
),
throwsToolExit(message: '"xyz.xyz" is not a valid Dart package name.'),
allOf(
throwsToolExit(message: '"xyz-xyz" is not a valid Dart package name.'),
throwsToolExit(message: 'Try "xyz_xyz" instead.'),
),
);
});

Expand Down
12 changes: 12 additions & 0 deletions packages/flutter_tools/test/general.shard/create_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ void main() {
expect(isValidPackageName('Foo_bar'), false);
});

test('Suggests a valid Pub package name', () {
expect(potentialValidPackageName('92'), '_92');
expect(potentialValidPackageName('a-b-c'), 'a_b_c');


expect(potentialValidPackageName('Foo_bar'), 'foo_bar');
expect(potentialValidPackageName('foo-_bar'), 'foo__bar');

expect(potentialValidPackageName('잘못된 이름'), isNull, reason: 'It should return null if it cannot find a valid name.');

});

test('kWindowsDrivePattern', () {
expect(CreateBase.kWindowsDrivePattern.hasMatch(r'D:\'), isFalse);
expect(CreateBase.kWindowsDrivePattern.hasMatch(r'z:\'), isFalse);
Expand Down