Skip to content

Commit d2a62fa

Browse files
Jochum van der Ploegfelangel
andauthored
feat: add --verbose flag (#465)
Co-authored-by: Felix Angelov <felangelov@gmail.com>
1 parent ca66c60 commit d2a62fa

18 files changed

+246
-73
lines changed

lib/src/cli/cli.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,19 @@ class _Cmd {
7878
List<String> args, {
7979
bool throwOnError = true,
8080
String? workingDirectory,
81+
required Logger logger,
8182
}) async {
83+
logger.detail('Running: $cmd with $args');
8284
final runProcess = ProcessOverrides.current?.runProcess ?? Process.run;
8385
final result = await runProcess(
8486
cmd,
8587
args,
8688
workingDirectory: workingDirectory,
8789
runInShell: true,
8890
);
91+
logger
92+
..detail('stdout:\n${result.stdout}')
93+
..detail('stderr:\n${result.stderr}');
8994

9095
if (throwOnError) {
9196
_throwIfProcessFailed(result, cmd, args);

lib/src/cli/dart_cli.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ part of 'cli.dart';
33
/// Dart CLI
44
class Dart {
55
/// Determine whether dart is installed.
6-
static Future<bool> installed() async {
6+
static Future<bool> installed({
7+
required Logger logger,
8+
}) async {
79
try {
8-
await _Cmd.run('dart', ['--version']);
10+
await _Cmd.run('dart', ['--version'], logger: logger);
911
return true;
1012
} catch (_) {
1113
return false;
@@ -16,12 +18,18 @@ class Dart {
1618
static Future<void> applyFixes({
1719
String cwd = '.',
1820
bool recursive = false,
21+
required Logger logger,
1922
}) async {
2023
if (!recursive) {
2124
final pubspec = File(p.join(cwd, 'pubspec.yaml'));
2225
if (!pubspec.existsSync()) throw PubspecNotFound();
2326

24-
await _Cmd.run('dart', ['fix', '--apply'], workingDirectory: cwd);
27+
await _Cmd.run(
28+
'dart',
29+
['fix', '--apply'],
30+
workingDirectory: cwd,
31+
logger: logger,
32+
);
2533
return;
2634
}
2735

@@ -30,6 +38,7 @@ class Dart {
3038
'dart',
3139
['fix', '--apply'],
3240
workingDirectory: entity.parent.path,
41+
logger: logger,
3342
),
3443
where: _isPubspec,
3544
cwd: cwd,

lib/src/cli/flutter_cli.dart

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ typedef GeneratorBuilder = Future<MasonGenerator> Function(MasonBundle);
6464
/// Flutter CLI
6565
class Flutter {
6666
/// Determine whether flutter is installed.
67-
static Future<bool> installed() async {
67+
static Future<bool> installed({
68+
required Logger logger,
69+
}) async {
6870
try {
69-
await _Cmd.run('flutter', ['--version']);
71+
await _Cmd.run('flutter', ['--version'], logger: logger);
7072
return true;
7173
} catch (_) {
7274
return false;
@@ -77,18 +79,18 @@ class Flutter {
7779
static Future<void> packagesGet({
7880
String cwd = '.',
7981
bool recursive = false,
80-
Logger? logger,
82+
required Logger logger,
8183
}) async {
8284
await _runCommand(
8385
cmd: (cwd) async {
84-
final installProgress = logger?.progress(
86+
final installProgress = logger.progress(
8587
'Running "flutter packages get" in $cwd',
8688
);
8789

8890
try {
89-
await _verifyGitDependencies(cwd);
91+
await _verifyGitDependencies(cwd, logger: logger);
9092
} catch (_) {
91-
installProgress?.fail();
93+
installProgress.fail();
9294
rethrow;
9395
}
9496

@@ -97,9 +99,10 @@ class Flutter {
9799
'flutter',
98100
['packages', 'get'],
99101
workingDirectory: cwd,
102+
logger: logger,
100103
);
101104
} finally {
102-
installProgress?.complete();
105+
installProgress.complete();
103106
}
104107
},
105108
cwd: cwd,
@@ -111,12 +114,14 @@ class Flutter {
111114
static Future<void> pubGet({
112115
String cwd = '.',
113116
bool recursive = false,
117+
required Logger logger,
114118
}) async {
115119
await _runCommand(
116120
cmd: (cwd) => _Cmd.run(
117121
'flutter',
118122
['pub', 'get'],
119123
workingDirectory: cwd,
124+
logger: logger,
120125
),
121126
cwd: cwd,
122127
recursive: recursive,
@@ -134,7 +139,7 @@ class Flutter {
134139
String? excludeFromCoverage,
135140
String? randomSeed,
136141
List<String>? arguments,
137-
Logger? logger,
142+
required Logger logger,
138143
void Function(String)? stdout,
139144
void Function(String)? stderr,
140145
FlutterTestRunner testRunner = flutterTest,
@@ -171,7 +176,7 @@ class Flutter {
171176
}
172177

173178
if (optimizePerformance) {
174-
final optimizationProgress = logger?.progress('Optimizing tests');
179+
final optimizationProgress = logger.progress('Optimizing tests');
175180
try {
176181
final generator = await buildGenerator(testRunnerBundle);
177182
var vars = <String, dynamic>{'package-root': workingDirectory};
@@ -186,7 +191,7 @@ class Flutter {
186191
fileConflictResolution: FileConflictResolution.overwrite,
187192
);
188193
} finally {
189-
optimizationProgress?.complete();
194+
optimizationProgress.complete();
190195
}
191196
}
192197

@@ -235,7 +240,10 @@ class Flutter {
235240
///
236241
/// If any git dependencies are unreachable,
237242
/// an [UnreachableGitDependency] is thrown.
238-
Future<void> _verifyGitDependencies(String cwd) async {
243+
Future<void> _verifyGitDependencies(
244+
String cwd, {
245+
required Logger logger,
246+
}) async {
239247
final pubspec = Pubspec.parse(
240248
await File(p.join(cwd, 'pubspec.yaml')).readAsString(),
241249
);
@@ -254,7 +262,12 @@ Future<void> _verifyGitDependencies(String cwd) async {
254262
.toList();
255263

256264
await Future.wait(
257-
gitDependencies.map((dependency) => Git.reachable(dependency.url)),
265+
gitDependencies.map(
266+
(dependency) => Git.reachable(
267+
dependency.url,
268+
logger: logger,
269+
),
270+
),
258271
);
259272
}
260273

lib/src/cli/git_cli.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ Make sure the remote exists and you have the correct access rights.''';
2222
/// Git CLI
2323
class Git {
2424
/// Determine whether the [remote] is reachable.
25-
static Future<void> reachable(Uri remote) async {
25+
static Future<void> reachable(
26+
Uri remote, {
27+
required Logger logger,
28+
}) async {
2629
try {
27-
await _Cmd.run('git', ['ls-remote', '$remote', '--exit-code']);
30+
await _Cmd.run(
31+
'git',
32+
['ls-remote', '$remote', '--exit-code'],
33+
logger: logger,
34+
);
2835
} catch (_) {
2936
throw UnreachableGitDependency(remote: remote);
3037
}

lib/src/command_runner.dart

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
4343
'true': 'Enable anonymous usage statistics',
4444
'false': 'Disable anonymous usage statistics',
4545
},
46+
)
47+
..addFlag(
48+
'verbose',
49+
help: 'Noisy logging, including all shell commands executed.',
4650
);
47-
addCommand(CreateCommand(analytics: _analytics, logger: logger));
48-
addCommand(PackagesCommand(logger: logger));
49-
addCommand(TestCommand(logger: logger));
50-
addCommand(UpdateCommand(logger: logger, pubUpdater: pubUpdater));
51+
addCommand(CreateCommand(analytics: _analytics, logger: _logger));
52+
addCommand(PackagesCommand(logger: _logger));
53+
addCommand(TestCommand(logger: _logger));
54+
addCommand(UpdateCommand(logger: _logger, pubUpdater: pubUpdater));
5155
}
5256

5357
/// Standard timeout duration for the CLI.
@@ -78,6 +82,9 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
7882
normalizedResponse == 'y' || normalizedResponse == 'yes';
7983
}
8084
final _argResults = parse(args);
85+
if (_argResults['verbose'] == true) {
86+
_logger.level = Level.verbose;
87+
}
8188
return await runCommand(_argResults) ?? ExitCode.success.code;
8289
} on FormatException catch (e, stackTrace) {
8390
_logger
@@ -97,6 +104,30 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
97104

98105
@override
99106
Future<int?> runCommand(ArgResults topLevelResults) async {
107+
_logger
108+
..detail('Argument information:')
109+
..detail(' Top level options:');
110+
for (final option in topLevelResults.options) {
111+
if (topLevelResults.wasParsed(option)) {
112+
_logger.detail(' - $option: ${topLevelResults[option]}');
113+
}
114+
}
115+
if (topLevelResults.command != null) {
116+
final commandResult = topLevelResults.command!;
117+
_logger
118+
..detail(' Command: ${commandResult.name}')
119+
..detail(' Command options:');
120+
for (final option in commandResult.options) {
121+
if (commandResult.wasParsed(option)) {
122+
_logger.detail(' - $option: ${commandResult[option]}');
123+
}
124+
}
125+
}
126+
127+
if (_analytics.enabled) {
128+
_logger.detail('Running with analytics enabled.');
129+
}
130+
100131
int? exitCode = ExitCode.unavailable.code;
101132
if (topLevelResults['version'] == true) {
102133
_logger.info(packageVersion);

lib/src/commands/create/create.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class CreateCommand extends Command<int> {
3838
/// {@macro create_command}
3939
CreateCommand({
4040
required Analytics analytics,
41-
Logger? logger,
41+
required Logger logger,
4242
GeneratorBuilder? generator,
4343
}) : _analytics = analytics,
44-
_logger = logger ?? Logger(),
44+
_logger = logger,
4545
_generator = generator ?? MasonGenerator.fromBundle {
4646
argParser
4747
..addOption(
@@ -216,6 +216,7 @@ class CreateCommand extends Command<int> {
216216
}
217217

218218
void _validateOrgName(String name) {
219+
_logger.detail('Validating org name; $name');
219220
final isValidOrgName = _isValidOrgName(name);
220221
if (!isValidOrgName) {
221222
usageException(
@@ -230,6 +231,7 @@ class CreateCommand extends Command<int> {
230231
}
231232

232233
void _validateProjectName(String name) {
234+
_logger.detail('Validating project name; $name');
233235
final isValidProjectName = _isValidPackageName(name);
234236
if (!isValidProjectName) {
235237
usageException(
@@ -255,6 +257,7 @@ class CreateCommand extends Command<int> {
255257
}
256258

257259
void _validateOutputDirectoryArg(List<String> args) {
260+
_logger.detail('Validating output directory args: $args');
258261
if (args.isEmpty) {
259262
usageException('No option specified for the output directory.');
260263
}

lib/src/commands/create/templates/post_generate_actions.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Future<void> installDartPackages(
77
Logger logger,
88
Directory outputDir,
99
) async {
10-
final isFlutterInstalled = await Flutter.installed();
10+
final isFlutterInstalled = await Flutter.installed(logger: logger);
1111
if (isFlutterInstalled) {
1212
final installDependenciesProgress = logger.progress(
1313
'Running "flutter pub get" in ${outputDir.path}',
1414
);
15-
await Flutter.pubGet(cwd: outputDir.path);
15+
await Flutter.pubGet(cwd: outputDir.path, logger: logger);
1616
installDependenciesProgress.complete();
1717
}
1818
}
@@ -23,13 +23,13 @@ Future<void> installFlutterPackages(
2323
Directory outputDir, {
2424
bool recursive = false,
2525
}) async {
26-
final isFlutterInstalled = await Flutter.installed();
26+
final isFlutterInstalled = await Flutter.installed(logger: logger);
2727
if (isFlutterInstalled) {
28-
final installDependenciesProgress = logger.progress(
29-
'Running "flutter packages get" in ${outputDir.path}',
28+
await Flutter.packagesGet(
29+
cwd: outputDir.path,
30+
recursive: recursive,
31+
logger: logger,
3032
);
31-
await Flutter.packagesGet(cwd: outputDir.path, recursive: recursive);
32-
installDependenciesProgress.complete();
3333
}
3434
}
3535

@@ -39,12 +39,16 @@ Future<void> applyDartFixes(
3939
Directory outputDir, {
4040
bool recursive = false,
4141
}) async {
42-
final isDartInstalled = await Dart.installed();
42+
final isDartInstalled = await Dart.installed(logger: logger);
4343
if (isDartInstalled) {
4444
final applyFixesProgress = logger.progress(
4545
'Running "dart fix --apply" in ${outputDir.path}',
4646
);
47-
await Dart.applyFixes(cwd: outputDir.path, recursive: recursive);
47+
await Dart.applyFixes(
48+
cwd: outputDir.path,
49+
recursive: recursive,
50+
logger: logger,
51+
);
4852
applyFixesProgress.complete();
4953
}
5054
}

lib/src/commands/packages.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class PackagesGetCommand extends Command<int> {
5959
final recursive = _argResults['recursive'] as bool;
6060
final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.';
6161
final targetPath = path.normalize(Directory(target).absolute.path);
62-
final isFlutterInstalled = await Flutter.installed();
62+
final isFlutterInstalled = await Flutter.installed(logger: _logger);
6363
if (isFlutterInstalled) {
6464
try {
6565
await Flutter.packagesGet(

lib/src/commands/test/test.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import 'package:universal_io/io.dart';
99
import 'package:very_good_cli/src/cli/cli.dart';
1010

1111
/// Signature for the [Flutter.installed] method.
12-
typedef FlutterInstalledCommand = Future<bool> Function();
12+
typedef FlutterInstalledCommand = Future<bool> Function({
13+
required Logger logger,
14+
});
1315

1416
/// Signature for the [Flutter.test] method.
1517
typedef FlutterTestCommand = Future<List<int>> Function({
@@ -21,7 +23,7 @@ typedef FlutterTestCommand = Future<List<int>> Function({
2123
String? excludeFromCoverage,
2224
String? randomSeed,
2325
List<String>? arguments,
24-
Logger? logger,
26+
required Logger logger,
2527
void Function(String)? stdout,
2628
void Function(String)? stderr,
2729
});
@@ -32,10 +34,10 @@ typedef FlutterTestCommand = Future<List<int>> Function({
3234
class TestCommand extends Command<int> {
3335
/// {@macro test_command}
3436
TestCommand({
35-
Logger? logger,
37+
required Logger logger,
3638
FlutterInstalledCommand? flutterInstalled,
3739
FlutterTestCommand? flutterTest,
38-
}) : _logger = logger ?? Logger(),
40+
}) : _logger = logger,
3941
_flutterInstalled = flutterInstalled ?? Flutter.installed,
4042
_flutterTest = flutterTest ?? Flutter.test {
4143
argParser
@@ -131,7 +133,7 @@ This command should be run from the root of your Flutter project.''',
131133
);
132134
final excludeTags = _argResults['exclude-tags'] as String?;
133135
final tags = _argResults['tags'] as String?;
134-
final isFlutterInstalled = await _flutterInstalled();
136+
final isFlutterInstalled = await _flutterInstalled(logger: _logger);
135137
final excludeFromCoverage = _argResults['exclude-coverage'] as String?;
136138
final randomOrderingSeed =
137139
_argResults['test-randomize-ordering-seed'] as String?;

0 commit comments

Comments
 (0)