Skip to content

Commit b7881e5

Browse files
authored
Align flutter pub get/upgrade/add/remove/downgrade (#117896)
* Align `flutter pub get/upgrade/add/remove/downgrade` * Add final . to command description * Remove trailing whitespace * Don't print message that command is being run * Update expectations * Use relative path * Remove duplicated line * Improve function dartdoc
1 parent cf529ec commit b7881e5

File tree

12 files changed

+255
-304
lines changed

12 files changed

+255
-304
lines changed

packages/flutter_tools/lib/src/commands/packages.dart

+161-162
Large diffs are not rendered by default.

packages/flutter_tools/lib/src/dart/pub.dart

+66-98
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ class PubContext {
120120
static final PubContext interactive = PubContext._(<String>['interactive']);
121121
static final PubContext pubGet = PubContext._(<String>['get']);
122122
static final PubContext pubUpgrade = PubContext._(<String>['upgrade']);
123+
static final PubContext pubAdd = PubContext._(<String>['add']);
124+
static final PubContext pubRemove = PubContext._(<String>['remove']);
123125
static final PubContext pubForward = PubContext._(<String>['forward']);
126+
static final PubContext pubPassThrough = PubContext._(<String>['passthrough']);
124127
static final PubContext runTest = PubContext._(<String>['run_test']);
125128
static final PubContext flutterTests = PubContext._(<String>['flutter_tests']);
126129
static final PubContext updatePackages = PubContext._(<String>['update_packages']);
@@ -161,7 +164,7 @@ abstract class Pub {
161164
required Stdio stdio,
162165
}) = _DefaultPub.test;
163166

164-
/// Runs `pub get` or `pub upgrade` for [project].
167+
/// Runs `pub get` for [project].
165168
///
166169
/// [context] provides extra information to package server requests to
167170
/// understand usage.
@@ -173,7 +176,6 @@ abstract class Pub {
173176
Future<void> get({
174177
required PubContext context,
175178
required FlutterProject project,
176-
bool skipIfAbsent = false,
177179
bool upgrade = false,
178180
bool offline = false,
179181
String? flutterRootOverride,
@@ -203,14 +205,23 @@ abstract class Pub {
203205

204206
/// Runs pub in 'interactive' mode.
205207
///
206-
/// directly piping the stdin stream of this process to that of pub, and the
207-
/// stdout/stderr stream of pub to the corresponding streams of this process.
208+
/// This will run the pub process with StdioInherited (unless [_stdio] is set
209+
/// for testing).
210+
///
211+
/// The pub process will be run in current working directory, so `--directory`
212+
/// should be passed appropriately in [arguments]. This ensures output from
213+
/// pub will refer to relative paths correctly.
214+
///
215+
/// [touchesPackageConfig] should be true if this is a command expexted to
216+
/// create a new `.dart_tool/package_config.json` file.
208217
Future<void> interactively(
209218
List<String> arguments, {
210-
String? directory,
211-
required io.Stdio stdio,
219+
FlutterProject? project,
220+
required PubContext context,
221+
required String command,
212222
bool touchesPackageConfig = false,
213223
bool generateSyntheticPackage = false,
224+
bool printProgress = true,
214225
});
215226
}
216227

@@ -268,7 +279,6 @@ class _DefaultPub implements Pub {
268279
Future<void> get({
269280
required PubContext context,
270281
required FlutterProject project,
271-
bool skipIfAbsent = false,
272282
bool upgrade = false,
273283
bool offline = false,
274284
bool generateSyntheticPackage = false,
@@ -280,8 +290,6 @@ class _DefaultPub implements Pub {
280290
}) async {
281291
final String directory = project.directory.path;
282292
final File packageConfigFile = project.packageConfigFile;
283-
final Directory generatedDirectory = _fileSystem.directory(
284-
_fileSystem.path.join(directory, '.dart_tool', 'flutter_gen'));
285293
final File lastVersion = _fileSystem.file(
286294
_fileSystem.path.join(directory, '.dart_tool', 'version'));
287295
final File currentVersion = _fileSystem.file(
@@ -352,25 +360,7 @@ class _DefaultPub implements Pub {
352360
flutterRootOverride: flutterRootOverride,
353361
printProgress: printProgress
354362
);
355-
356-
if (!packageConfigFile.existsSync()) {
357-
throwToolExit('$directory: pub did not create .dart_tools/package_config.json file.');
358-
}
359-
lastVersion.writeAsStringSync(currentVersion.readAsStringSync());
360-
await _updatePackageConfig(
361-
packageConfigFile,
362-
generatedDirectory,
363-
project.manifest.generateSyntheticPackage,
364-
);
365-
if (project.hasExampleApp && project.example.pubspecFile.existsSync()) {
366-
final Directory exampleGeneratedDirectory = _fileSystem.directory(
367-
_fileSystem.path.join(project.example.directory.path, '.dart_tool', 'flutter_gen'));
368-
await _updatePackageConfig(
369-
project.example.packageConfigFile,
370-
exampleGeneratedDirectory,
371-
project.example.manifest.generateSyntheticPackage,
372-
);
373-
}
363+
await _updateVersionAndPackageConfig(project);
374364
}
375365

376366
/// Runs pub with [arguments] and [ProcessStartMode.inheritStdio] mode.
@@ -392,9 +382,6 @@ class _DefaultPub implements Pub {
392382
String? flutterRootOverride,
393383
}) async {
394384
int exitCode;
395-
if (printProgress) {
396-
_logger.printStatus('Running "flutter pub $command" in ${_fileSystem.path.basename(directory)}...');
397-
}
398385

399386
final List<String> pubCommand = _pubCommand(arguments);
400387
final Map<String, String> pubEnvironment = await _createPubEnvironment(context, flutterRootOverride);
@@ -567,64 +554,22 @@ class _DefaultPub implements Pub {
567554
@override
568555
Future<void> interactively(
569556
List<String> arguments, {
570-
String? directory,
571-
required io.Stdio stdio,
557+
FlutterProject? project,
558+
required PubContext context,
559+
required String command,
572560
bool touchesPackageConfig = false,
573561
bool generateSyntheticPackage = false,
562+
bool printProgress = true,
574563
}) async {
575-
// Fully resolved pub or pub.bat is calculated based on current platform.
576-
final io.Process process = await _processUtils.start(
577-
_pubCommand(<String>[
578-
if (_logger.supportsColor) '--color',
579-
...arguments,
580-
]),
581-
workingDirectory: directory,
582-
environment: await _createPubEnvironment(PubContext.interactive),
564+
await _runWithStdioInherited(
565+
arguments,
566+
command: command,
567+
directory: _fileSystem.currentDirectory.path,
568+
context: context,
569+
printProgress: printProgress,
583570
);
584-
585-
// Pipe the Flutter tool stdin to the pub stdin.
586-
unawaited(process.stdin.addStream(stdio.stdin)
587-
// If pub exits unexpectedly with an error, that will be reported below
588-
// by the tool exit after the exit code check.
589-
.catchError((dynamic err, StackTrace stack) {
590-
_logger.printTrace('Echoing stdin to the pub subprocess failed:');
591-
_logger.printTrace('$err\n$stack');
592-
}
593-
));
594-
595-
// Pipe the pub stdout and stderr to the tool stdout and stderr.
596-
try {
597-
await Future.wait<dynamic>(<Future<dynamic>>[
598-
stdio.addStdoutStream(process.stdout),
599-
stdio.addStderrStream(process.stderr),
600-
]);
601-
} on Exception catch (err, stack) {
602-
_logger.printTrace('Echoing stdout or stderr from the pub subprocess failed:');
603-
_logger.printTrace('$err\n$stack');
604-
}
605-
606-
// Wait for pub to exit.
607-
final int code = await process.exitCode;
608-
if (code != 0) {
609-
throwToolExit('pub finished with exit code $code', exitCode: code);
610-
}
611-
612-
if (touchesPackageConfig) {
613-
final String targetDirectory = directory ?? _fileSystem.currentDirectory.path;
614-
final File packageConfigFile = _fileSystem.file(
615-
_fileSystem.path.join(targetDirectory, '.dart_tool', 'package_config.json'));
616-
final Directory generatedDirectory = _fileSystem.directory(
617-
_fileSystem.path.join(targetDirectory, '.dart_tool', 'flutter_gen'));
618-
final File lastVersion = _fileSystem.file(
619-
_fileSystem.path.join(targetDirectory, '.dart_tool', 'version'));
620-
final File currentVersion = _fileSystem.file(
621-
_fileSystem.path.join(Cache.flutterRoot!, 'version'));
622-
lastVersion.writeAsStringSync(currentVersion.readAsStringSync());
623-
await _updatePackageConfig(
624-
packageConfigFile,
625-
generatedDirectory,
626-
generateSyntheticPackage,
627-
);
571+
if (touchesPackageConfig && project != null) {
572+
await _updateVersionAndPackageConfig(project);
628573
}
629574
}
630575

@@ -766,23 +711,46 @@ class _DefaultPub implements Pub {
766711
return environment;
767712
}
768713

769-
/// Update the package configuration file.
714+
/// Updates the .dart_tool/version file to be equal to current Flutter
715+
/// version.
716+
///
717+
/// Calls [_updatePackageConfig] for [project] and [project.example] (if it
718+
/// exists).
719+
///
720+
/// This should be called after pub invocations that are expected to update
721+
/// the packageConfig.
722+
Future<void> _updateVersionAndPackageConfig(FlutterProject project) async {
723+
if (!project.packageConfigFile.existsSync()) {
724+
throwToolExit('${project.directory}: pub did not create .dart_tools/package_config.json file.');
725+
}
726+
final File lastVersion = _fileSystem.file(
727+
_fileSystem.path.join(project.directory.path, '.dart_tool', 'version'),
728+
);
729+
final File currentVersion = _fileSystem.file(
730+
_fileSystem.path.join(Cache.flutterRoot!, 'version'));
731+
lastVersion.writeAsStringSync(currentVersion.readAsStringSync());
732+
733+
await _updatePackageConfig(project);
734+
if (project.hasExampleApp && project.example.pubspecFile.existsSync()) {
735+
await _updatePackageConfig(project.example);
736+
}
737+
}
738+
739+
/// Update the package configuration file in [project].
770740
///
771-
/// Creates a corresponding `package_config_subset` file that is used by the build
772-
/// system to avoid rebuilds caused by an updated pub timestamp.
741+
/// Creates a corresponding `package_config_subset` file that is used by the
742+
/// build system to avoid rebuilds caused by an updated pub timestamp.
773743
///
774-
/// if [generateSyntheticPackage] is true then insert flutter_gen synthetic
775-
/// package into the package configuration. This is used by the l10n localization
776-
/// tooling to insert a new reference into the package_config file, allowing the import
777-
/// of a package URI that is not specified in the pubspec.yaml
744+
/// if `project.generateSyntheticPackage` is `true` then insert flutter_gen
745+
/// synthetic package into the package configuration. This is used by the l10n
746+
/// localization tooling to insert a new reference into the package_config
747+
/// file, allowing the import of a package URI that is not specified in the
748+
/// pubspec.yaml
778749
///
779750
/// For more information, see:
780751
/// * [generateLocalizations], `in lib/src/localizations/gen_l10n.dart`
781-
Future<void> _updatePackageConfig(
782-
File packageConfigFile,
783-
Directory generatedDirectory,
784-
bool generateSyntheticPackage,
785-
) async {
752+
Future<void> _updatePackageConfig(FlutterProject project) async {
753+
final File packageConfigFile = project.packageConfigFile;
786754
final PackageConfig packageConfig = await loadPackageConfigWithLogging(packageConfigFile, logger: _logger);
787755

788756
packageConfigFile.parent
@@ -792,7 +760,7 @@ class _DefaultPub implements Pub {
792760
_fileSystem,
793761
));
794762

795-
if (!generateSyntheticPackage) {
763+
if (!project.manifest.generateSyntheticPackage) {
796764
return;
797765
}
798766
if (packageConfig.packages.any((Package package) => package.name == 'flutter_gen')) {

packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class FakePub extends Fake implements Pub {
2929
Future<void> get({
3030
PubContext? context,
3131
required FlutterProject project,
32-
bool skipIfAbsent = false,
3332
bool upgrade = false,
3433
bool offline = false,
3534
bool generateSyntheticPackage = false,

packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ class FakePub extends Fake implements Pub {
480480
Future<void> get({
481481
PubContext? context,
482482
required FlutterProject project,
483-
bool skipIfAbsent = false,
484483
bool upgrade = false,
485484
bool offline = false,
486485
bool generateSyntheticPackage = false,

packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart

+18-20
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main() {
3535
fileSystem.currentDirectory.childFile('.flutter-plugins').createSync();
3636
fileSystem.currentDirectory.childFile('.flutter-plugins-dependencies').createSync();
3737

38-
final PackagesGetCommand command = PackagesGetCommand('get', false);
38+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
3939
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
4040

4141
await commandRunner.run(<String>['get']);
@@ -60,7 +60,7 @@ void main() {
6060
..createSync(recursive: true)
6161
..writeAsBytesSync(<int>[0]);
6262

63-
final PackagesGetCommand command = PackagesGetCommand('get', false);
63+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
6464
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
6565

6666
await commandRunner.run(<String>['get']);
@@ -81,7 +81,7 @@ void main() {
8181
final Directory targetDirectory = fileSystem.currentDirectory.childDirectory('target');
8282
targetDirectory.childFile('pubspec.yaml').createSync();
8383

84-
final PackagesGetCommand command = PackagesGetCommand('get', false);
84+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
8585
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
8686

8787
await commandRunner.run(<String>['get', targetDirectory.path]);
@@ -98,7 +98,7 @@ void main() {
9898
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
9999
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
100100

101-
final PackagesGetCommand command = PackagesGetCommand('get', false);
101+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
102102
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
103103

104104
await commandRunner.run(<String>['get']);
@@ -115,7 +115,7 @@ void main() {
115115
});
116116

117117
testUsingContext('pub get throws error on missing directory', () async {
118-
final PackagesGetCommand command = PackagesGetCommand('get', false);
118+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
119119
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
120120

121121
try {
@@ -159,7 +159,7 @@ void main() {
159159
'''
160160
);
161161

162-
final PackagesGetCommand command = PackagesGetCommand('get', false);
162+
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
163163
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
164164

165165
await commandRunner.run(<String>['get']);
@@ -183,23 +183,21 @@ class FakePub extends Fake implements Pub {
183183
final FileSystem fileSystem;
184184

185185
@override
186-
Future<void> get({
186+
Future<void> interactively(
187+
List<String> arguments, {
188+
FlutterProject? project,
187189
required PubContext context,
188-
required FlutterProject project,
189-
bool skipIfAbsent = false,
190-
bool upgrade = false,
191-
bool offline = false,
190+
required String command,
191+
bool touchesPackageConfig = false,
192192
bool generateSyntheticPackage = false,
193-
bool generateSyntheticPackageForExample = false,
194-
String? flutterRootOverride,
195-
bool checkUpToDate = false,
196-
bool shouldSkipThirdPartyGenerator = true,
197193
bool printProgress = true,
198194
}) async {
199-
fileSystem.directory(project.directory)
200-
.childDirectory('.dart_tool')
201-
.childFile('package_config.json')
202-
..createSync(recursive: true)
203-
..writeAsStringSync('{"configVersion":2,"packages":[]}');
195+
if (project != null) {
196+
fileSystem.directory(project.directory)
197+
.childDirectory('.dart_tool')
198+
.childFile('package_config.json')
199+
..createSync(recursive: true)
200+
..writeAsStringSync('{"configVersion":2,"packages":[]}');
201+
}
204202
}
205203
}

packages/flutter_tools/test/commands.shard/hermetic/update_packages_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ class FakePub extends Fake implements Pub {
275275
Future<void> get({
276276
required PubContext context,
277277
required FlutterProject project,
278-
bool skipIfAbsent = false,
279278
bool upgrade = false,
280279
bool offline = false,
281280
bool generateSyntheticPackage = false,

packages/flutter_tools/test/commands.shard/permeable/packages_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void main() {
7272
'packages',
7373
verb,
7474
...?args,
75+
'--directory',
7576
projectPath,
7677
]);
7778
return command;

packages/flutter_tools/test/general.shard/cache_test.dart

-1
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,6 @@ class FakePub extends Fake implements Pub {
12081208
Future<void> get({
12091209
PubContext? context,
12101210
required FlutterProject project,
1211-
bool skipIfAbsent = false,
12121211
bool upgrade = false,
12131212
bool offline = false,
12141213
bool generateSyntheticPackage = false,

0 commit comments

Comments
 (0)