Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[flutter_tool] New command project #102118

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

Merged
merged 28 commits into from
Apr 30, 2022

Conversation

Jasguerrero
Copy link
Contributor

@Jasguerrero Jasguerrero commented Apr 18, 2022

#2885

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Apr 18, 2022
@Jasguerrero Jasguerrero marked this pull request as ready for review April 19, 2022 16:32
break;
}

buffer.write('$icon ${result.toString()}');
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. .toString() is unneeded, the default behavior of interpolating a non-string into a string is to call .toString() on it anyway

Suggested change
buffer.write('$icon ${result.toString()}');
buffer.write('$icon $result');


@override
Future<FlutterCommandResult> runCommand() async {
globals.flutterVersion.fetchTagsAndUpdate();
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need. will delete it next commit

Future<FlutterCommandResult> runCommand() async {
globals.flutterVersion.fetchTagsAndUpdate();

Directory workingDirectory;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. prefer final and you can replace the if/else with a ternary

}

void presentResults(final List<ProjectValidatorResult> results) {
StringBuffer buffer = StringBuffer();
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be final

for (ProjectValidatorResult result in results) {
addToBufferResult(result, buffer);
buffer.write('\n');
globals.logger.printBox(buffer.toString());
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't you be printing the buffer outside of the for loop?

If this is a bug, can you write a test that would have caught it (for example, hard-code the expected logger output, so that we can see what it should look like)? Add it to packages/flutter_tools/test/commands.shard/hermetic/analyze_project_test.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.

Yes. It should be outside, adding a test is a good idea

final String name;
final String value;
final String? warning;
final StatusProjectValidator _status;
Copy link
Contributor

Choose a reason for hiding this comment

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

might as well make this public and get rid of the getter.


class ProjectValidatorResult {

ProjectValidatorResult(this.name, this.value, this._status, {this.warning});
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. prefer named parameters when you have more than 2

Copy link
Contributor

Choose a reason for hiding this comment

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

also, this constructor can be made const

}

const List <ProjectValidator> allProjectValidators = [
// add validators
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// add validators
// TODO(jsguerrero): add validators

import 'analyze_project.dart';
import 'project.dart';

abstract class ProjectValidator {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add dartdoc's for all the new classes you add, and any non-obvious methods? (for example .supportsProject is self-explanatory)

Future<List<ProjectValidatorResult>> start(FlutterProject project);
}

const List <ProjectValidator> allProjectValidators = [
Copy link
Contributor

@christopherfujino christopherfujino Apr 20, 2022

Choose a reason for hiding this comment

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

nit. make this a static on the ProjectValidator class. Static's are essentially global like this; however, it's a little more organized.

import '../runner/flutter_command.dart';

class ValidateProjectCommand extends FlutterCommand {
ValidateProjectCommand(this.fileSystem, this.logger, {this.verbose = false});
Copy link
Contributor

Choose a reason for hiding this comment

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

nit use named parameters when you have more than 2

String icon;
switch(result.status) {
case StatusProjectValidator.error:
icon = '[X]';
Copy link
Contributor

Choose a reason for hiding this comment

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

We should be consistent with the doctor and use this unicode character: https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/doctor_validator.dart#L258

break;
}

buffer.write('$icon $result');
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 call buffer.writeln() here you don't need to print an extra newline in the above scope (line 53).

Suggested change
buffer.write('$icon $result');
buffer.writeln('$icon $result');


@override
String toString() {
if (_status == StatusProjectValidator.error) {
Copy link
Contributor

@christopherfujino christopherfujino Apr 20, 2022

Choose a reason for hiding this comment

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

if you are returning early, you can un-nest the conditionals to make reading this easier:

    if (_status == StatusProjectValidator.error) {
      return 'Error: $value';
    }
    if (warning != null) {
      return '$name: $value (warning: $warning)';
    }
    return '$name: $value';


for (final ProjectValidator validator in allProjectValidators) {
if (!ranValidators.contains(validator) && validator.supportsProject(project)) {
results.addAll(await validator.start(project));
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to await one before starting the next, or are these safe to run concurrently?


for (final ProjectValidator validator in allProjectValidators) {
if (!ranValidators.contains(validator) && validator.supportsProject(project)) {
results.addAll(await validator.start(project));
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, can we use a spinner like in the doctor, so that the user knows the tool is still running?

https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/doctor.dart#L341

Copy link
Contributor

@christopherfujino christopherfujino Apr 20, 2022

Choose a reason for hiding this comment

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

Also maybe we should catch exceptions and mark them as crashed, as we do in the doctor: https://github.com/flutter/flutter/blob/master/packages/flutter_tools/lib/src/doctor.dart#L350

}

void addToBufferResult(ProjectValidatorResult result, StringBuffer buffer) {
String icon;
Copy link
Contributor

Choose a reason for hiding this comment

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

i think you can mark this as final

Suggested change
String icon;
final String icon;

Copy link
Contributor

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

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

This is coming along great. Just a few nits.

FileSystem fileSystem;

group('analyze project command', () {
setUpAll(() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't re-use the same filesystem between tests

Suggested change
setUpAll(() {
setUp(() {

}

void main() {
final BufferLogger loggerTest = BufferLogger.test();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking you probably shouldn't re-use the logger between tests?

'│ [✓] pass: value │\n'
'│ [✗] Error: my error │\n'
'│ [!] pass two: pass (warning: my warning) │\n'
'│ │\n'
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get rid of this extra line?

/// Can return more than one result in case a file/command have a lot of info to share to the user
Future<List<ProjectValidatorResult>> start(FlutterProject project);
/// new ProjectValidators should be added here for the ValidateProjectCommand to run
static List <ProjectValidator> allProjectValidators = <ProjectValidator>[
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static List <ProjectValidator> allProjectValidators = <ProjectValidator>[
static const List <ProjectValidator> allProjectValidators = <ProjectValidator>[

@@ -146,6 +148,12 @@ List<FlutterCommand> generateCommands({
terminal: globals.terminal,
artifacts: globals.artifacts,
),
ValidateProjectCommand(
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should add this until it's ready to be used.

Copy link
Contributor

@christopherfujino christopherfujino left a comment

Choose a reason for hiding this comment

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

LGTM!

@fluttergithubbot
Copy link
Contributor

This pull request is not suitable for automatic merging in its current state.

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.

3 participants