1+ import 'dart:io' ;
2+
13import 'package:args/command_runner.dart' ;
4+ import 'package:dart_diff_cli/src/utils/index.dart' ;
25import 'package:mason_logger/mason_logger.dart' ;
36
47class ExecCommand extends Command <int > {
@@ -7,15 +10,15 @@ class ExecCommand extends Command<int> {
710 }) : _logger = logger {
811 argParser
912 ..addOption (
10- 'branch' ,
11- abbr: 'b' ,
13+ Options .branch.name,
14+ abbr: Options .branch.abbr,
15+ defaultsTo: Options .branch.defaultVal,
1216 help: 'Specify the base branch to use for git diff' ,
13- defaultsTo: 'main' ,
1417 )
1518 ..addOption (
16- ' remote' ,
17- abbr: 'r' ,
18- defaultsTo: 'origin' ,
19+ Options . remote.name ,
20+ abbr: Options .remote.abbr ,
21+ defaultsTo: Options .remote.defaultVal ,
1922 help: 'Specify the remote repository to use for git diff' ,
2023 )
2124 ..addFlag (
@@ -35,11 +38,78 @@ class ExecCommand extends Command<int> {
3538
3639 @override
3740 Future <int > run () async {
38- //TODO(droyder7) implement run
39- _logger.info (
40- 'Why did the scarecrow win an award? Because he was outstanding in his field!' );
41- _logger.info ('This is a joke, but the command is working' );
42- _logger.info ('args: ${argResults ?.arguments .join (', ' )}' );
41+ if (! isFlutterProjectRoot ()) {
42+ print (
43+ 'Error: No pubspec.yaml file found. '
44+ 'This command should be run from the root of your Flutter project.' ,
45+ );
46+ return 1 ;
47+ }
48+
49+ if (argResults == null ) {
50+ _logger.info ('No arguments.\n $usage ' );
51+ return 1 ;
52+ }
53+
54+ final args = argResults! ;
55+ _logger.info ('args: ${args .arguments .join (', ' )}' );
56+
57+ final branch = Options .branch.parsedValue (args);
58+ final remote = Options .remote.parsedValue (args);
59+
60+ final extraArgs = args.rest;
61+ if (extraArgs.isEmpty) {
62+ _logger.info ('No extra args.\n $usage ' );
63+ return 1 ;
64+ }
65+
66+ final bool isTest = extraArgs.any ((e) => e == 'test' );
67+
68+ print ('Running: ${extraArgs .join (' ' )}' );
69+
70+ final relativeBasePath = getRelativeBasePath (_logger);
71+
72+ final modifiedFiles = getModifiedFiles (remote, branch)
73+ .where (
74+ (file) => file.endsWith ('.dart' ) && file.startsWith (relativeBasePath),
75+ )
76+ .toList ();
77+
78+ if (modifiedFiles.isEmpty) {
79+ print ('No modified Dart files detected.' );
80+ return 0 ;
81+ }
82+
83+ print ('Modified Dart files:\n ${modifiedFiles .join ('\n ' )}' );
84+
85+ final files = < String > [];
86+ final testFiles = < String > {};
87+
88+ for (final file in modifiedFiles) {
89+ final relativePath = file.withoutBasePath (relativeBasePath);
90+ if (File (relativePath.withPlatformPath ()).existsSync ()) {
91+ files.add (relativePath);
92+ if (! isTest) {
93+ continue ;
94+ }
95+ if (relativePath.endsWith ('_test.dart' )) {
96+ testFiles.add (relativePath);
97+ } else {
98+ final testFile = calculateTestFile (relativePath);
99+ if (File (testFile).existsSync ()) {
100+ testFiles.add (testFile);
101+ } else {
102+ print ('No test file found for $relativePath . Skipping...' );
103+ }
104+ }
105+ } else {
106+ print ('File with path: $relativePath does not exist. Skipping...' );
107+ }
108+ }
109+
110+ final fileList = isTest ? testFiles : files;
111+ runCommand ([...extraArgs, ...fileList]);
112+
43113 return 0 ;
44114 }
45115}
0 commit comments