@@ -120,7 +120,10 @@ class PubContext {
120
120
static final PubContext interactive = PubContext ._(< String > ['interactive' ]);
121
121
static final PubContext pubGet = PubContext ._(< String > ['get' ]);
122
122
static final PubContext pubUpgrade = PubContext ._(< String > ['upgrade' ]);
123
+ static final PubContext pubAdd = PubContext ._(< String > ['add' ]);
124
+ static final PubContext pubRemove = PubContext ._(< String > ['remove' ]);
123
125
static final PubContext pubForward = PubContext ._(< String > ['forward' ]);
126
+ static final PubContext pubPassThrough = PubContext ._(< String > ['passthrough' ]);
124
127
static final PubContext runTest = PubContext ._(< String > ['run_test' ]);
125
128
static final PubContext flutterTests = PubContext ._(< String > ['flutter_tests' ]);
126
129
static final PubContext updatePackages = PubContext ._(< String > ['update_packages' ]);
@@ -161,7 +164,7 @@ abstract class Pub {
161
164
required Stdio stdio,
162
165
}) = _DefaultPub .test;
163
166
164
- /// Runs `pub get` or `pub upgrade` for [project] .
167
+ /// Runs `pub get` for [project] .
165
168
///
166
169
/// [context] provides extra information to package server requests to
167
170
/// understand usage.
@@ -173,7 +176,6 @@ abstract class Pub {
173
176
Future <void > get ({
174
177
required PubContext context,
175
178
required FlutterProject project,
176
- bool skipIfAbsent = false ,
177
179
bool upgrade = false ,
178
180
bool offline = false ,
179
181
String ? flutterRootOverride,
@@ -203,14 +205,23 @@ abstract class Pub {
203
205
204
206
/// Runs pub in 'interactive' mode.
205
207
///
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.
208
217
Future <void > interactively (
209
218
List <String > arguments, {
210
- String ? directory,
211
- required io.Stdio stdio,
219
+ FlutterProject ? project,
220
+ required PubContext context,
221
+ required String command,
212
222
bool touchesPackageConfig = false ,
213
223
bool generateSyntheticPackage = false ,
224
+ bool printProgress = true ,
214
225
});
215
226
}
216
227
@@ -268,7 +279,6 @@ class _DefaultPub implements Pub {
268
279
Future <void > get ({
269
280
required PubContext context,
270
281
required FlutterProject project,
271
- bool skipIfAbsent = false ,
272
282
bool upgrade = false ,
273
283
bool offline = false ,
274
284
bool generateSyntheticPackage = false ,
@@ -280,8 +290,6 @@ class _DefaultPub implements Pub {
280
290
}) async {
281
291
final String directory = project.directory.path;
282
292
final File packageConfigFile = project.packageConfigFile;
283
- final Directory generatedDirectory = _fileSystem.directory (
284
- _fileSystem.path.join (directory, '.dart_tool' , 'flutter_gen' ));
285
293
final File lastVersion = _fileSystem.file (
286
294
_fileSystem.path.join (directory, '.dart_tool' , 'version' ));
287
295
final File currentVersion = _fileSystem.file (
@@ -352,25 +360,7 @@ class _DefaultPub implements Pub {
352
360
flutterRootOverride: flutterRootOverride,
353
361
printProgress: printProgress
354
362
);
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);
374
364
}
375
365
376
366
/// Runs pub with [arguments] and [ProcessStartMode.inheritStdio] mode.
@@ -392,9 +382,6 @@ class _DefaultPub implements Pub {
392
382
String ? flutterRootOverride,
393
383
}) async {
394
384
int exitCode;
395
- if (printProgress) {
396
- _logger.printStatus ('Running "flutter pub $command " in ${_fileSystem .path .basename (directory )}...' );
397
- }
398
385
399
386
final List <String > pubCommand = _pubCommand (arguments);
400
387
final Map <String , String > pubEnvironment = await _createPubEnvironment (context, flutterRootOverride);
@@ -567,64 +554,22 @@ class _DefaultPub implements Pub {
567
554
@override
568
555
Future <void > interactively (
569
556
List <String > arguments, {
570
- String ? directory,
571
- required io.Stdio stdio,
557
+ FlutterProject ? project,
558
+ required PubContext context,
559
+ required String command,
572
560
bool touchesPackageConfig = false ,
573
561
bool generateSyntheticPackage = false ,
562
+ bool printProgress = true ,
574
563
}) 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,
583
570
);
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);
628
573
}
629
574
}
630
575
@@ -766,23 +711,46 @@ class _DefaultPub implements Pub {
766
711
return environment;
767
712
}
768
713
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] .
770
740
///
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.
773
743
///
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
778
749
///
779
750
/// For more information, see:
780
751
/// * [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;
786
754
final PackageConfig packageConfig = await loadPackageConfigWithLogging (packageConfigFile, logger: _logger);
787
755
788
756
packageConfigFile.parent
@@ -792,7 +760,7 @@ class _DefaultPub implements Pub {
792
760
_fileSystem,
793
761
));
794
762
795
- if (! generateSyntheticPackage) {
763
+ if (! project.manifest. generateSyntheticPackage) {
796
764
return ;
797
765
}
798
766
if (packageConfig.packages.any ((Package package) => package.name == 'flutter_gen' )) {
0 commit comments