Skip to content

flutter_tools: refactor CreateCommand.#70874

Merged
fluttergithubbot merged 7 commits intoflutter:masterfrom
cyanglaz:create_refactor
Nov 20, 2020
Merged

flutter_tools: refactor CreateCommand.#70874
fluttergithubbot merged 7 commits intoflutter:masterfrom
cyanglaz:create_refactor

Conversation

@cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Nov 19, 2020

Description

This PR refactors some methods form CreateCommand to a mixin. This is to get ready for making flutter create plugin a separate command. (#59493)

This refactoring does the bare minimum in order for the new flutter create plugin to reuse the code in CreateCommand.
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.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

@flutter-dashboard
Copy link

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.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 19, 2020
@google-cla google-cla bot added the cla: yes label Nov 19, 2020
Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

output

!_keywords.contains(name);
}

/// Generate application project in the `directory` using `templateCnotext`.
Copy link
Contributor

Choose a reason for hiding this comment

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

templateContext

import '../template.dart';

/// Mixin contains methods that are shared with `flutter create` commands.
mixin CreateCommandMixin on FlutterCommand {
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

String getFlutterRoot() {
if (Cache.flutterRoot == null) {
throwToolExit(
'Neither the --flutter-root command line flag nor the FLUTTER_ROOT environment '
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the section about --flutter-root

Suggested change
'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);
Copy link
Contributor

Choose a reason for hiding this comment

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

asserts only run during tests, do these need to be error checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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() {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you make CreateBase an abstract class, then you should move the shared argParser options to that class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@cyanglaz cyanglaz left a comment

Choose a reason for hiding this comment

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

@jonahwilliams Updated. PTAL

import '../template.dart';

/// Mixin contains methods that are shared with `flutter create` commands.
mixin CreateCommandMixin on FlutterCommand {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

/// 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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

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. '
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: make this a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

Also please fix the tests

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

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 {
Copy link
Member

Choose a reason for hiding this comment

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

Make a getter?

/// Gets the project name based.
///
/// Use the current directory path name if the `--project-name` is not specified explicitly.
String getProjectName(String projectDirPath) {
Copy link
Member

Choose a reason for hiding this comment

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

Make a getter?

return camelizedName[0].toUpperCase() + camelizedName.substring(1);
}

String createUTIIdentifier(String organization, String name) {
Copy link
Member

Choose a reason for hiding this comment

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

Can these be made @protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jmagman
Copy link
Member

jmagman commented Nov 20, 2020

Overall LGTM.

@jonahwilliams can you approve?

Copy link
Contributor

@jonahwilliams jonahwilliams left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor the CreateCommand

4 participants