flutter_tools: refactor CreateCommand.#70874
flutter_tools: refactor CreateCommand.#70874fluttergithubbot merged 7 commits intoflutter:masterfrom
CreateCommand.#70874Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat. Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
997ee43 to
71d3abd
Compare
jonahwilliams
left a comment
There was a problem hiding this comment.
I would start with a base class over a mixin
| /// Mixin contains methods that are shared with `flutter create` commands. | ||
| mixin CreateCommandMixin on FlutterCommand { | ||
| /// Throw with exit code 2 if the output directory is invalid. | ||
| void validateOutoutDirectoryArg() { |
| !_keywords.contains(name); | ||
| } | ||
|
|
||
| /// Generate application project in the `directory` using `templateCnotext`. |
| import '../template.dart'; | ||
|
|
||
| /// Mixin contains methods that are shared with `flutter create` commands. | ||
| mixin CreateCommandMixin on FlutterCommand { |
There was a problem hiding this comment.
Doesn't seem like there is a good reason for this to be a mixin. I would instead make this CreateBase and an abstract class, and then rename the library to create_base.dart.
| String getFlutterRoot() { | ||
| if (Cache.flutterRoot == null) { | ||
| throwToolExit( | ||
| 'Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment ' |
There was a problem hiding this comment.
Remove the section about --flutter-root
| 'Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment ' | |
| 'Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment ' |
| /// Throws assertion if projectDir does not exist or empty. | ||
| /// Returns null if no project type can be determined. | ||
| FlutterProjectType determineTemplateType(Directory projectDir) { | ||
| assert(projectDir.existsSync() && projectDir.listSync().isNotEmpty); |
There was a problem hiding this comment.
asserts only run during tests, do these need to be error checks?
There was a problem hiding this comment.
This method only makes sense when projectDir is not empty. So I think it makes sense to have an assertion here so bugs can be easier determined in the future. (And yes, it would only help debugging under tests)
|
|
||
| class CreateCommand extends FlutterCommand { | ||
| class CreateCommand extends FlutterCommand with CreateCommandMixin { | ||
| CreateCommand() { |
There was a problem hiding this comment.
If you make CreateBase an abstract class, then you should move the shared argParser options to that class
cyanglaz
left a comment
There was a problem hiding this comment.
@jonahwilliams Updated. PTAL
| import '../template.dart'; | ||
|
|
||
| /// Mixin contains methods that are shared with `flutter create` commands. | ||
| mixin CreateCommandMixin on FlutterCommand { |
| /// Throws assertion if projectDir does not exist or empty. | ||
| /// Returns null if no project type can be determined. | ||
| FlutterProjectType determineTemplateType(Directory projectDir) { | ||
| assert(projectDir.existsSync() && projectDir.listSync().isNotEmpty); |
There was a problem hiding this comment.
This method only makes sense when projectDir is not empty. So I think it makes sense to have an assertion here so bugs can be easier determined in the future. (And yes, it would only help debugging under tests)
|
|
||
| class CreateCommand extends FlutterCommand { | ||
| class CreateCommand extends FlutterCommand with CreateCommandMixin { | ||
| CreateCommand() { |
jonahwilliams
left a comment
There was a problem hiding this comment.
Overall LGTM.
I'd let @jmagman take a look too
| void addPlatformsOptions({String customHelp}) { | ||
| argParser.addMultiOption('platforms', | ||
| help: customHelp ?? | ||
| 'Required: The platforms supported by this project. ' |
There was a problem hiding this comment.
nit: make this a constant
jonahwilliams
left a comment
There was a problem hiding this comment.
Also please fix the tests
jmagman
left a comment
There was a problem hiding this comment.
LGTM with nits once the tests are fixed.
| /// | ||
| /// If `--org` is specified in the command, returns that directly. | ||
| /// If `--org` is not specified, returns the organization from the existing project. | ||
| Future<String> getOrganization(Directory projectDir) async { |
| /// Gets the project name based. | ||
| /// | ||
| /// Use the current directory path name if the `--project-name` is not specified explicitly. | ||
| String getProjectName(String projectDirPath) { |
| return camelizedName[0].toUpperCase() + camelizedName.substring(1); | ||
| } | ||
|
|
||
| String createUTIIdentifier(String organization, String name) { |
@jonahwilliams can you approve? |
Description
This PR refactors some methods form
CreateCommandto a mixin. This is to get ready for makingflutter create plugina separate command. (#59493)This refactoring does the bare minimum in order for the new
flutter create pluginto reuse the code inCreateCommand.So it is basically a copy paste to make the private method reusable by a separate class.
Related Issues
#59493
Fixes #59494
Tests
I added the following tests:
This is a pure refactoring, nothing new was added.
Checklist
Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes (
[x]). This will ensure a smooth and quick review process.///).flutter analyze --flutter-repo) does not report any problems on my PR.Breaking Change
Did any tests fail when you ran them? Please read Handling breaking changes.