From 90a8fc181442765feaa7f4da252e9d56f8f7a424 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 3 Jun 2021 21:12:40 -0700 Subject: [PATCH] pub command: allow any subcommand+args, prints a summary while executing --- mono_repo/CHANGELOG.md | 9 ++- mono_repo/README.md | 2 +- mono_repo/lib/src/commands/pub.dart | 103 +++++++++------------------- mono_repo/test/mono_repo_test.dart | 2 +- 4 files changed, 41 insertions(+), 75 deletions(-) diff --git a/mono_repo/CHANGELOG.md b/mono_repo/CHANGELOG.md index 592346b2..518d3578 100644 --- a/mono_repo/CHANGELOG.md +++ b/mono_repo/CHANGELOG.md @@ -1,10 +1,13 @@ ## 4.1.0-dev - Improve the stability of ordering in Github workflow definitions. -- Drop `--no-precompile` as the default for `pub` `get` and `upgrade` commands. - It's now the default across Dart tools. - Use latest `actions/cache@v2.1.6`. -- Migrate to new command pattern supported in Dart 2.10+ +- Migrate to new command pattern supported in Dart 2.10 +- `pub` command + - Now allows any `pub` command and argument combination to be provided. + - Dropped `--no-precompile` since it is now the default. + - Now prints a summary of execution progress. Useful for repositories with + many packages. ## 4.0.0 diff --git a/mono_repo/README.md b/mono_repo/README.md index 00156e87..821e920e 100644 --- a/mono_repo/README.md +++ b/mono_repo/README.md @@ -35,7 +35,7 @@ Available commands: check Check the state of the repository. generate Generates the CI configuration for child packages. presubmit Run the CI presubmits locally. - pub Run `dart pub get` or `dart pub upgrade` against all packages. + pub Runs the `pub` command with the provided arguments across all packages. travis (Deprecated, use `generate`) Configure Travis-CI for child packages. Run "mono_repo help " for more information about a command. diff --git a/mono_repo/lib/src/commands/pub.dart b/mono_repo/lib/src/commands/pub.dart index f0cbafb4..f85656f4 100644 --- a/mono_repo/lib/src/commands/pub.dart +++ b/mono_repo/lib/src/commands/pub.dart @@ -5,90 +5,34 @@ import 'dart:async'; import 'dart:io'; -import 'package:args/command_runner.dart'; +import 'package:args/args.dart'; import 'package:io/ansi.dart'; import 'package:path/path.dart' as p; import '../root_config.dart'; import 'mono_repo_command.dart'; -class PubCommand extends Command { - PubCommand() { - addSubcommand(_PubSubCommand('get')); - addSubcommand(_PubSubCommand('upgrade')); - } +class PubCommand extends MonoRepoCommand { + @override + final ArgParser argParser = ArgParser.allowAnything(); @override String get name => 'pub'; @override String get description => - 'Run `dart pub get` or `dart pub upgrade` against all packages.'; -} - -const _offline = 'offline'; -const _dryRun = 'dry-run'; -const _precompile = 'precompile'; - -class _PubSubCommand extends MonoRepoCommand { - @override - final String name; - - _PubSubCommand(this.name) { - argParser - ..addFlag( - _offline, - help: 'Use cached packages instead of accessing the network.', - ) - ..addFlag( - _dryRun, - abbr: 'n', - negatable: false, - help: "Report what dependencies would change but don't change any.", - ) - ..addFlag( - _precompile, - help: 'Precompile executables and transformed dependencies.', - ); - } - - @override - String get description => 'Run `dart pub $name` against all packages.'; + 'Runs the `pub` command with the provided arguments across all packages.'; @override Future run() => pub( rootConfig(), - name, - offline: argResults![_offline] as bool, - dryRun: argResults![_dryRun] as bool, - preCompile: argResults![_precompile] as bool, + argResults?.rest ?? const [], ); } -Future pub( - RootConfig rootConfig, - String pubCommand, { - required bool offline, - required bool dryRun, - required bool preCompile, -}) async { +Future pub(RootConfig rootConfig, List args) async { final pkgDirs = rootConfig.map((pc) => pc.relativePath).toList(); - // TODO(kevmoo): use UI-as-code features when min SDK is >= 2.3.0 - final args = [pubCommand]; - if (offline) { - args.add('--$_offline'); - } - - if (dryRun) { - args.add('--$_dryRun'); - } - - // Note: the default is `false` - if (preCompile) { - args.add('--$_precompile'); - } - print( lightBlue.wrap( 'Running `dart pub ${args.join(' ')}` across ${pkgDirs.length} ' @@ -96,23 +40,25 @@ Future pub( ), ); + final packageArgs = ['pub', ...args]; + + var successCount = 0; + final failSet = {}; + for (var config in rootConfig) { final dir = config.relativePath; - List packageArgs; String executable; if (config.hasFlutterDependency) { executable = _flutterPath; - packageArgs = ['packages', ...args]; } else { - executable = _pubPath; - packageArgs = args; + executable = _dartPath; } print(''); print( wrapWith( - 'Starting `$executable ${packageArgs.join(' ')}` in `$dir`...', + '`$dir`: Starting `$executable ${packageArgs.join(' ')}`', [styleBold, lightBlue], ), ); @@ -124,10 +70,27 @@ Future pub( final exit = await proc.exitCode; if (exit == 0) { + successCount++; print(wrapWith('`$dir`: success!', [styleBold, green])); } else { + failSet.add(dir); print(wrapWith('`$dir`: failed!', [styleBold, red])); } + + if (rootConfig.length > 1) { + print(''); + print('Successes: $successCount'); + if (failSet.isNotEmpty) { + print( + 'Failures: ${failSet.length}\n' + '${failSet.map((e) => ' $e').join('\n')}', + ); + } + final remaining = rootConfig.length - (successCount + failSet.length); + if (remaining > 0) { + print('Remaining: $remaining'); + } + } } } @@ -140,8 +103,8 @@ final String _sdkDir = (() { return aboveExecutable; })(); -final String _pubPath = - p.join(_sdkDir, 'bin', Platform.isWindows ? 'pub.bat' : 'pub'); +final String _dartPath = + p.join(_sdkDir, 'bin', Platform.isWindows ? 'dart.bat' : 'dart'); /// The "flutter[.bat]" command. final String _flutterPath = Platform.isWindows ? 'flutter.bat' : 'flutter'; diff --git a/mono_repo/test/mono_repo_test.dart b/mono_repo/test/mono_repo_test.dart index 4f4d4f08..972b97d2 100644 --- a/mono_repo/test/mono_repo_test.dart +++ b/mono_repo/test/mono_repo_test.dart @@ -44,7 +44,7 @@ Available commands: check Check the state of the repository. generate Generates the CI configuration for child packages. presubmit Run the CI presubmits locally. - pub Run `dart pub get` or `dart pub upgrade` against all packages. + pub Runs the `pub` command with the provided arguments across all packages. travis (Deprecated, use `generate`) Configure Travis-CI for child packages. Run "mono_repo help " for more information about a command.''';