From ee1c59d462fe4036bac56fd6209a6506d1814049 Mon Sep 17 00:00:00 2001 From: Andrew Kolos Date: Thu, 12 Jan 2023 13:42:02 -0800 Subject: [PATCH] reduce pub output from flutter create (#118285) * reduce pub output from flutter create * fix fake Pub implementations * fix tests * Update pub.dart * replace enum with simpler boolean * fix tests * Revert "fix tests" This reverts commit 8a3182d3b95d4f2bf337343cdb76e88c2f428ca8. * Revert "replace enum with simpler boolean" This reverts commit 445dbc443db4eb5ce284f76749f60e81208b8783. * go back to using an enum --- .../lib/src/commands/create.dart | 1 + .../lib/src/commands/update_packages.dart | 4 +- packages/flutter_tools/lib/src/dart/pub.dart | 49 ++++++++++++++----- .../hermetic/create_usage_test.dart | 2 +- .../commands.shard/hermetic/drive_test.dart | 2 +- .../commands.shard/hermetic/pub_get_test.dart | 2 +- .../hermetic/update_packages_test.dart | 2 +- .../test/general.shard/cache_test.dart | 1 + .../test/general.shard/dart/pub_get_test.dart | 2 +- .../runner/flutter_command_test.dart | 2 +- .../flutter_tools/test/src/throwing_pub.dart | 4 +- 11 files changed, 48 insertions(+), 23 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/create.dart b/packages/flutter_tools/lib/src/commands/create.dart index cbccb1fb9e65..5c46a766430d 100644 --- a/packages/flutter_tools/lib/src/commands/create.dart +++ b/packages/flutter_tools/lib/src/commands/create.dart @@ -414,6 +414,7 @@ class CreateCommand extends CreateBase { context: pubContext, project: project, offline: boolArgDeprecated('offline'), + outputMode: PubOutputMode.summaryOnly, ); await project.ensureReadyForPlatformSpecificTooling( androidPlatform: includeAndroid, diff --git a/packages/flutter_tools/lib/src/commands/update_packages.dart b/packages/flutter_tools/lib/src/commands/update_packages.dart index 553d158bfdf8..cd30ef022393 100644 --- a/packages/flutter_tools/lib/src/commands/update_packages.dart +++ b/packages/flutter_tools/lib/src/commands/update_packages.dart @@ -444,7 +444,7 @@ class UpdatePackagesCommand extends FlutterCommand { upgrade: doUpgrade, offline: boolArgDeprecated('offline'), flutterRootOverride: temporaryFlutterSdk?.path, - printProgress: false, + outputMode: PubOutputMode.none, ); if (doUpgrade) { @@ -538,7 +538,7 @@ class UpdatePackagesCommand extends FlutterCommand { // All dependencies should already have been downloaded by the fake // package, so the concurrent checks can all happen offline. offline: true, - printProgress: false, + outputMode: PubOutputMode.none, ); stopwatch.stop(); final double seconds = stopwatch.elapsedMilliseconds / 1000.0; diff --git a/packages/flutter_tools/lib/src/dart/pub.dart b/packages/flutter_tools/lib/src/dart/pub.dart index ffe9ca7cdfeb..a75efc1f752b 100644 --- a/packages/flutter_tools/lib/src/dart/pub.dart +++ b/packages/flutter_tools/lib/src/dart/pub.dart @@ -140,6 +140,17 @@ class PubContext { } } +/// Describes the amount of output that should get printed from a `pub` command. +/// [PubOutputMode.all] indicates that the complete output is printed. This is +/// typically the default. +/// [PubOutputMode.none] indicates that no output should be printed. +/// [PubOutputMode.summaryOnly] indicates that only summary information should be printed. +enum PubOutputMode { + none, + all, + summaryOnly, +} + /// A handle for interacting with the pub tool. abstract class Pub { /// Create a default [Pub] instance. @@ -172,6 +183,14 @@ abstract class Pub { /// If [shouldSkipThirdPartyGenerator] is true, the overall pub get will be /// skipped if the package config file has a "generator" other than "pub". /// Defaults to true. + /// + /// [outputMode] determines how verbose the output from `pub get` will be. + /// If [PubOutputMode.all] is used, `pub get` will print its typical output + /// which includes information about all changed dependencies. If + /// [PubOutputMode.summaryOnly] is used, only summary information will be printed. + /// This is useful for cases where the user is typically not interested in + /// what dependencies were changed, such as when running `flutter create`. + /// /// Will also resolve dependencies in the example folder if present. Future get({ required PubContext context, @@ -181,7 +200,7 @@ abstract class Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all }); /// Runs pub in 'batch' mode. @@ -221,7 +240,7 @@ abstract class Pub { required String command, bool touchesPackageConfig = false, bool generateSyntheticPackage = false, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all }); } @@ -286,7 +305,7 @@ class _DefaultPub implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all }) async { final String directory = project.directory.path; final File packageConfigFile = project.packageConfigFile; @@ -358,7 +377,7 @@ class _DefaultPub implements Pub { directory: directory, failureMessage: 'pub $command failed', flutterRootOverride: flutterRootOverride, - printProgress: printProgress + outputMode: outputMode, ); await _updateVersionAndPackageConfig(project); } @@ -375,7 +394,7 @@ class _DefaultPub implements Pub { Future _runWithStdioInherited( List arguments, { required String command, - required bool printProgress, + required PubOutputMode outputMode, required PubContext context, required String directory, String failureMessage = 'pub failed', @@ -384,10 +403,10 @@ class _DefaultPub implements Pub { int exitCode; final List pubCommand = _pubCommand(arguments); - final Map pubEnvironment = await _createPubEnvironment(context, flutterRootOverride); + final Map pubEnvironment = await _createPubEnvironment(context: context, flutterRootOverride: flutterRootOverride, summaryOnly: outputMode == PubOutputMode.summaryOnly); try { - if (printProgress) { + if (outputMode != PubOutputMode.none) { final io.Stdio? stdio = _stdio; if (stdio == null) { // Let pub inherit stdio and output directly to the tool's stdout and @@ -450,8 +469,7 @@ class _DefaultPub implements Pub { ? 'exists' : 'does not exist'; buffer.writeln('Working directory: "$directory" ($directoryExistsMessage)'); - final Map env = await _createPubEnvironment(context, flutterRootOverride); - buffer.write(_stringifyPubEnv(env)); + buffer.write(_stringifyPubEnv(pubEnvironment)); throw io.ProcessException( exception.executable, exception.arguments, @@ -517,7 +535,7 @@ class _DefaultPub implements Pub { if (showTraceForErrors) { arguments.insert(0, '--trace'); } - final Map pubEnvironment = await _createPubEnvironment(context, flutterRootOverride); + final Map pubEnvironment = await _createPubEnvironment(context: context, flutterRootOverride: flutterRootOverride); final List pubCommand = _pubCommand(arguments); final int code = await _processUtils.stream( pubCommand, @@ -557,14 +575,14 @@ class _DefaultPub implements Pub { required String command, bool touchesPackageConfig = false, bool generateSyntheticPackage = false, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all }) async { await _runWithStdioInherited( arguments, command: command, directory: _fileSystem.currentDirectory.path, context: context, - printProgress: printProgress, + outputMode: outputMode, ); if (touchesPackageConfig && project != null) { await _updateVersionAndPackageConfig(project); @@ -697,10 +715,15 @@ class _DefaultPub implements Pub { /// /// [context] provides extra information to package server requests to /// understand usage. - Future> _createPubEnvironment(PubContext context, [ String? flutterRootOverride ]) async { + Future> _createPubEnvironment({ + required PubContext context, + String? flutterRootOverride, + bool? summaryOnly = false, + }) async { final Map environment = { 'FLUTTER_ROOT': flutterRootOverride ?? Cache.flutterRoot!, _kPubEnvironmentKey: await _getPubEnvironmentValue(context), + if (summaryOnly ?? false) 'PUB_SUMMARY_ONLY': '1', }; final String? pubCache = _getPubCacheIfAvailable(); if (pubCache != null) { diff --git a/packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart index 4a617ff70e12..2731d287ad49 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart @@ -36,7 +36,7 @@ class FakePub extends Fake implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { project.directory.childFile('.packages').createSync(); if (offline == true) { diff --git a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart index a7445131369d..19f2eb2489ca 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart @@ -486,7 +486,7 @@ class FakePub extends Fake implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { } } diff --git a/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart index 4ab290199c6d..14a5ac034a99 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart @@ -190,7 +190,7 @@ class FakePub extends Fake implements Pub { required String command, bool touchesPackageConfig = false, bool generateSyntheticPackage = false, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { if (project != null) { fileSystem.directory(project.directory) diff --git a/packages/flutter_tools/test/commands.shard/hermetic/update_packages_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/update_packages_test.dart index 56b9fa0d7a12..29669bd5c0bb 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/update_packages_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/update_packages_test.dart @@ -282,7 +282,7 @@ class FakePub extends Fake implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { pubGetDirectories.add(project.directory.path); project.directory.childFile('pubspec.lock') diff --git a/packages/flutter_tools/test/general.shard/cache_test.dart b/packages/flutter_tools/test/general.shard/cache_test.dart index de4229c2d59c..381dfdfb93ef 100644 --- a/packages/flutter_tools/test/general.shard/cache_test.dart +++ b/packages/flutter_tools/test/general.shard/cache_test.dart @@ -1215,6 +1215,7 @@ class FakePub extends Fake implements Pub { bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { calledGet += 1; } diff --git a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart index 97f633ddcafa..de362611499b 100644 --- a/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart +++ b/packages/flutter_tools/test/general.shard/dart/pub_get_test.dart @@ -676,7 +676,7 @@ exit code: 66 await pub.get( project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), context: PubContext.flutterTests, - printProgress: false + outputMode: PubOutputMode.none, ); } on ToolExit { // Ignore. diff --git a/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart b/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart index 4f5d774a6d42..779a31a9b750 100644 --- a/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/flutter_command_test.dart @@ -956,7 +956,7 @@ class FakePub extends Fake implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) async { } } diff --git a/packages/flutter_tools/test/src/throwing_pub.dart b/packages/flutter_tools/test/src/throwing_pub.dart index a96d13375ab0..580722a80033 100644 --- a/packages/flutter_tools/test/src/throwing_pub.dart +++ b/packages/flutter_tools/test/src/throwing_pub.dart @@ -29,7 +29,7 @@ class ThrowingPub implements Pub { String? flutterRootOverride, bool checkUpToDate = false, bool shouldSkipThirdPartyGenerator = true, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) { throw UnsupportedError('Attempted to invoke pub during test.'); } @@ -42,7 +42,7 @@ class ThrowingPub implements Pub { required String command, bool touchesPackageConfig = false, bool generateSyntheticPackage = false, - bool printProgress = true, + PubOutputMode outputMode = PubOutputMode.all, }) { throw UnsupportedError('Attempted to invoke pub during test.'); }