Skip to content

Commit

Permalink
[flutter_tools] migrate project-validate to analyze --suggestions (#1…
Browse files Browse the repository at this point in the history
…06149)
  • Loading branch information
Jasguerrero authored Jun 24, 2022
1 parent 47f54ac commit ddd493b
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 51 deletions.
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import 'src/globals.dart' as globals;
import 'src/isolated/mustache_template.dart';
import 'src/isolated/resident_web_runner.dart';
import 'src/pre_run_validator.dart';
import 'src/project_validator.dart';
import 'src/resident_runner.dart';
import 'src/runner/flutter_command.dart';
import 'src/web/web_runner.dart';
Expand Down Expand Up @@ -141,6 +142,7 @@ List<FlutterCommand> generateCommands({
logger: globals.logger,
terminal: globals.terminal,
artifacts: globals.artifacts!,
allProjectValidators: <ProjectValidator>[],
),
AssembleCommand(verboseHelp: verboseHelp, buildSystem: globals.buildSystem),
AttachCommand(verboseHelp: verboseHelp),
Expand Down
34 changes: 33 additions & 1 deletion packages/flutter_tools/lib/src/commands/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import 'package:process/process.dart';

import '../artifacts.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/terminal.dart';
import '../project_validator.dart';
import '../runner/flutter_command.dart';
import 'analyze_base.dart';
import 'analyze_continuously.dart';
import 'analyze_once.dart';
import 'validate_project.dart';

class AnalyzeCommand extends FlutterCommand {
AnalyzeCommand({
Expand All @@ -23,11 +27,13 @@ class AnalyzeCommand extends FlutterCommand {
required Logger logger,
required ProcessManager processManager,
required Artifacts artifacts,
required List<ProjectValidator> allProjectValidators,
}) : _artifacts = artifacts,
_fileSystem = fileSystem,
_processManager = processManager,
_logger = logger,
_terminal = terminal,
_allProjectValidators = allProjectValidators,
_platform = platform {
argParser.addFlag('flutter-repo',
negatable: false,
Expand Down Expand Up @@ -56,6 +62,9 @@ class AnalyzeCommand extends FlutterCommand {
help: 'The path to write the request and response protocol. This is '
'only intended to be used for debugging the tooling.',
hide: !verboseHelp);
argParser.addFlag('suggestions',
help: 'Show suggestions about the current flutter project.'
);

// Hidden option to enable a benchmarking mode.
argParser.addFlag('benchmark',
Expand Down Expand Up @@ -92,6 +101,7 @@ class AnalyzeCommand extends FlutterCommand {
final Terminal _terminal;
final ProcessManager _processManager;
final Platform _platform;
final List<ProjectValidator> _allProjectValidators;

@override
String get name => 'analyze';
Expand Down Expand Up @@ -119,7 +129,29 @@ class AnalyzeCommand extends FlutterCommand {

@override
Future<FlutterCommandResult> runCommand() async {
if (boolArgDeprecated('watch')) {
final bool? suggestionFlag = boolArg('suggestions');
if (suggestionFlag != null && suggestionFlag == true) {
final String directoryPath;
final bool? watchFlag = boolArg('watch');
if (watchFlag != null && watchFlag) {
throwToolExit('flag --watch is not compatible with --suggestions');
}
if (workingDirectory == null) {
final Set<String> items = findDirectories(argResults!, _fileSystem);
if (items.isEmpty || items.length > 1) {
throwToolExit('The suggestions flags needs one directory path');
}
directoryPath = items.first;
} else {
directoryPath = workingDirectory!.path;
}
return ValidateProject(
fileSystem: _fileSystem,
logger: _logger,
allProjectValidators: _allProjectValidators,
userPath: directoryPath,
).run();
} else if (boolArgDeprecated('watch')) {
await AnalyzeContinuously(
argResults!,
runner!.getRepoRoots(),
Expand Down
17 changes: 17 additions & 0 deletions packages/flutter_tools/lib/src/commands/analyze_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,20 @@ class PackageDependencyTracker {
return result;
}
}

/// Find directories or files from argResults.rest.
Set<String> findDirectories(ArgResults argResults, FileSystem fileSystem) {
final Set<String> items = Set<String>.of(argResults.rest
.map<String>((String path) => fileSystem.path.canonicalize(path)));
if (items.isNotEmpty) {
for (final String item in items) {
final FileSystemEntityType type = fileSystem.typeSync(item);

if (type == FileSystemEntityType.notFound) {
throwToolExit("You provided the path '$item', however it does not exist on disk");
}
}
}

return items;
}
14 changes: 1 addition & 13 deletions packages/flutter_tools/lib/src/commands/analyze_once.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,7 @@ class AnalyzeOnce extends AnalyzeBase {
Future<void> analyze() async {
final String currentDirectory =
(workingDirectory ?? fileSystem.currentDirectory).path;

// find directories or files from argResults.rest
final Set<String> items = Set<String>.of(argResults.rest
.map<String>((String path) => fileSystem.path.canonicalize(path)));
if (items.isNotEmpty) {
for (final String item in items) {
final FileSystemEntityType type = fileSystem.typeSync(item);

if (type == FileSystemEntityType.notFound) {
throwToolExit("'$item' does not exist");
}
}
}
final Set<String> items = findDirectories(argResults, fileSystem);

if (isFlutterRepo) {
// check for conflicting dependencies
Expand Down
23 changes: 5 additions & 18 deletions packages/flutter_tools/lib/src/commands/validate_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,22 @@ import '../project_validator.dart';
import '../project_validator_result.dart';
import '../runner/flutter_command.dart';

class ValidateProjectCommand extends FlutterCommand {
ValidateProjectCommand({
class ValidateProject {
ValidateProject({
required this.fileSystem,
required this.logger,
required this.allProjectValidators,
required this.userPath,
this.verbose = false
});

final FileSystem fileSystem;
final Logger logger;
final bool verbose;
final String userPath;
final List<ProjectValidator> allProjectValidators;

@override
final String name = 'validate-project';

@override
final String description = 'Show information about the current project.';

@override
final String category = FlutterCommandCategory.project;

@override
Future<FlutterCommandResult> runCommand() async {
final String userPath = getUserPath();
Future<FlutterCommandResult> run() async {
final Directory workingDirectory = userPath.isEmpty ? fileSystem.currentDirectory : fileSystem.directory(userPath);

final FlutterProject project = FlutterProject.fromDirectory(workingDirectory);
Expand Down Expand Up @@ -94,8 +85,4 @@ class ValidateProjectCommand extends FlutterCommand {

return '$icon $result';
}

String getUserPath(){
return (argResults == null || argResults!.rest.isEmpty) ? '' : argResults!.rest[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/dart/analysis.dart';
import 'package:flutter_tools/src/dart/pub.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project_validator.dart';
import 'package:process/process.dart';

import '../../src/common.dart';
Expand Down Expand Up @@ -199,6 +200,7 @@ void main() {
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
processManager: processManager,
allProjectValidators: <ProjectValidator>[],
);

final TestFlutterCommandRunner commandRunner = TestFlutterCommandRunner();
Expand Down Expand Up @@ -246,6 +248,7 @@ void main() {
platform: FakePlatform(),
fileSystem: fileSystem,
processManager: processManager,
allProjectValidators: <ProjectValidator>[],
);

await FakeAsync().run((FakeAsync time) async {
Expand Down Expand Up @@ -298,6 +301,7 @@ void main() {
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
processManager: processManager,
allProjectValidators: <ProjectValidator>[],
);

await FakeAsync().run((FakeAsync time) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

import 'package:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/validate_project.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/project_validator.dart';
import 'package:flutter_tools/src/project_validator_result.dart';

import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/test_flutter_command_runner.dart';

Expand Down Expand Up @@ -70,26 +74,36 @@ class ProjectValidatorCrash extends ProjectValidator {

void main() {
FileSystem fileSystem;
Terminal terminal;
ProcessManager processManager;
Platform platform;

group('analyze project command', () {
group('analyze --suggestions command', () {

setUp(() {
fileSystem = MemoryFileSystem.test();
terminal = Terminal.test();
processManager = FakeProcessManager.empty();
platform = FakePlatform();
});

testUsingContext('success, error and warning', () async {
final BufferLogger loggerTest = BufferLogger.test();
final ValidateProjectCommand command = ValidateProjectCommand(
fileSystem: fileSystem,
logger: loggerTest,
allProjectValidators: <ProjectValidator>[
ProjectValidatorDummy(),
ProjectValidatorSecondDummy()
]
final AnalyzeCommand command = AnalyzeCommand(
artifacts: Artifacts.test(),
fileSystem: fileSystem,
logger: loggerTest,
platform: platform,
terminal: terminal,
processManager: processManager,
allProjectValidators: <ProjectValidator>[
ProjectValidatorDummy(),
ProjectValidatorSecondDummy()
]
);
final CommandRunner<void> runner = createTestCommandRunner(command);

await runner.run(<String>['validate-project']);
await runner.run(<String>['analyze', '--suggestions', './']);

const String expected = '\n'
'┌──────────────────────────────────────────┐\n'
Expand All @@ -107,18 +121,41 @@ void main() {

testUsingContext('crash', () async {
final BufferLogger loggerTest = BufferLogger.test();
final ValidateProjectCommand command = ValidateProjectCommand(
final AnalyzeCommand command = AnalyzeCommand(
artifacts: Artifacts.test(),
fileSystem: fileSystem,
logger: loggerTest,
allProjectValidators: <ProjectValidator>[ProjectValidatorCrash()]
platform: platform,
terminal: terminal,
processManager: processManager,
allProjectValidators: <ProjectValidator>[
ProjectValidatorCrash(),
]
);
final CommandRunner<void> runner = createTestCommandRunner(command);

await runner.run(<String>['validate-project']);
await runner.run(<String>['analyze', '--suggestions', './']);

const String expected = '[☠] Exception: my exception: #0 ProjectValidatorCrash.start';

expect(loggerTest.statusText, contains(expected));
});

testUsingContext('--watch and --suggestions not compatible together', () async {
final BufferLogger loggerTest = BufferLogger.test();
final AnalyzeCommand command = AnalyzeCommand(
artifacts: Artifacts.test(),
fileSystem: fileSystem,
logger: loggerTest,
platform: platform,
terminal: terminal,
processManager: processManager,
allProjectValidators: <ProjectValidator>[]
);
final CommandRunner<void> runner = createTestCommandRunner(command);
Future<void> result () => runner.run(<String>['analyze', '--suggestions', '--watch']);

expect(result, throwsToolExit(message: 'flag --watch is not compatible with --suggestions'));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/commands/analyze_base.dart';
import 'package:flutter_tools/src/dart/analysis.dart';
import 'package:flutter_tools/src/project_validator.dart';

import '../../src/common.dart';
import '../../src/context.dart';
Expand Down Expand Up @@ -70,6 +71,7 @@ void main() {
platform: platform,
processManager: processManager,
terminal: terminal,
allProjectValidators: <ProjectValidator>[],
);
runner = createTestCommandRunner(command);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void main() {
testWithoutContext('file not found', () async {
await runCommand(
arguments: <String>['analyze', '--no-pub', 'not_found.abc'],
exitMessageContains: "not_found.abc' does not exist",
exitMessageContains: "not_found.abc', however it does not exist on disk",
exitCode: 1
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/commands/validate_project.dart';
import 'package:flutter_tools/src/commands/analyze.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project_validator.dart';

Expand All @@ -15,22 +15,32 @@ import '../src/test_flutter_command_runner.dart';
void main() {
late FileSystem fileSystem;

group('analyze project command', () {
group('analyze --suggestions command integration', () {

setUp(() {
fileSystem = globals.localFileSystem;
});

testUsingContext('General Info Project Validator', () async {
final BufferLogger loggerTest = BufferLogger.test();
final ValidateProjectCommand command = ValidateProjectCommand(
final AnalyzeCommand command = AnalyzeCommand(
artifacts: globals.artifacts!,
fileSystem: fileSystem,
logger: loggerTest,
allProjectValidators: <ProjectValidator>[GeneralInfoProjectValidator()]
platform: globals.platform,
terminal: globals.terminal,
processManager: globals.processManager,
allProjectValidators: <ProjectValidator>[GeneralInfoProjectValidator()],
);
final CommandRunner<void> runner = createTestCommandRunner(command);

await runner.run(<String>['validate-project', '../../dev/integration_tests/flutter_gallery']);
await runner.run(<String>[
'analyze',
'--no-pub',
'--no-current-package',
'--suggestions',
'../../dev/integration_tests/flutter_gallery',
]);

const String expected = '\n'
'┌────────────────────────────────────────────────────────────────────────────┐\n'
Expand Down

0 comments on commit ddd493b

Please sign in to comment.