Skip to content

Commit

Permalink
reduce pub output from flutter create (#118285)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andrewkolos authored Jan 12, 2023
1 parent c7a3f0f commit ee1c59d
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/commands/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ class CreateCommand extends CreateBase {
context: pubContext,
project: project,
offline: boolArgDeprecated('offline'),
outputMode: PubOutputMode.summaryOnly,
);
await project.ensureReadyForPlatformSpecificTooling(
androidPlatform: includeAndroid,
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/lib/src/commands/update_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class UpdatePackagesCommand extends FlutterCommand {
upgrade: doUpgrade,
offline: boolArgDeprecated('offline'),
flutterRootOverride: temporaryFlutterSdk?.path,
printProgress: false,
outputMode: PubOutputMode.none,
);

if (doUpgrade) {
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 36 additions & 13 deletions packages/flutter_tools/lib/src/dart/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<void> get({
required PubContext context,
Expand All @@ -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.
Expand Down Expand Up @@ -221,7 +240,7 @@ abstract class Pub {
required String command,
bool touchesPackageConfig = false,
bool generateSyntheticPackage = false,
bool printProgress = true,
PubOutputMode outputMode = PubOutputMode.all
});
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -358,7 +377,7 @@ class _DefaultPub implements Pub {
directory: directory,
failureMessage: 'pub $command failed',
flutterRootOverride: flutterRootOverride,
printProgress: printProgress
outputMode: outputMode,
);
await _updateVersionAndPackageConfig(project);
}
Expand All @@ -375,7 +394,7 @@ class _DefaultPub implements Pub {
Future<void> _runWithStdioInherited(
List<String> arguments, {
required String command,
required bool printProgress,
required PubOutputMode outputMode,
required PubContext context,
required String directory,
String failureMessage = 'pub failed',
Expand All @@ -384,10 +403,10 @@ class _DefaultPub implements Pub {
int exitCode;

final List<String> pubCommand = _pubCommand(arguments);
final Map<String, String> pubEnvironment = await _createPubEnvironment(context, flutterRootOverride);
final Map<String, String> 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
Expand Down Expand Up @@ -450,8 +469,7 @@ class _DefaultPub implements Pub {
? 'exists'
: 'does not exist';
buffer.writeln('Working directory: "$directory" ($directoryExistsMessage)');
final Map<String, String> env = await _createPubEnvironment(context, flutterRootOverride);
buffer.write(_stringifyPubEnv(env));
buffer.write(_stringifyPubEnv(pubEnvironment));
throw io.ProcessException(
exception.executable,
exception.arguments,
Expand Down Expand Up @@ -517,7 +535,7 @@ class _DefaultPub implements Pub {
if (showTraceForErrors) {
arguments.insert(0, '--trace');
}
final Map<String, String> pubEnvironment = await _createPubEnvironment(context, flutterRootOverride);
final Map<String, String> pubEnvironment = await _createPubEnvironment(context: context, flutterRootOverride: flutterRootOverride);
final List<String> pubCommand = _pubCommand(arguments);
final int code = await _processUtils.stream(
pubCommand,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -697,10 +715,15 @@ class _DefaultPub implements Pub {
///
/// [context] provides extra information to package server requests to
/// understand usage.
Future<Map<String, String>> _createPubEnvironment(PubContext context, [ String? flutterRootOverride ]) async {
Future<Map<String, String>> _createPubEnvironment({
required PubContext context,
String? flutterRootOverride,
bool? summaryOnly = false,
}) async {
final Map<String, String> environment = <String, String>{
'FLUTTER_ROOT': flutterRootOverride ?? Cache.flutterRoot!,
_kPubEnvironmentKey: await _getPubEnvironmentValue(context),
if (summaryOnly ?? false) 'PUB_SUMMARY_ONLY': '1',
};
final String? pubCache = _getPubCacheIfAvailable();
if (pubCache != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions packages/flutter_tools/test/general.shard/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
}

Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/test/src/throwing_pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand All @@ -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.');
}
Expand Down

0 comments on commit ee1c59d

Please sign in to comment.