@@ -11,38 +11,39 @@ import 'package:collection/collection.dart';
11
11
import 'package:graphs/graphs.dart' ;
12
12
import 'package:path/path.dart' as p;
13
13
14
- /// Command to publish all Dart/Flutter packages in the repo.
15
- class PublishCommand extends AmplifyCommand with GlobOptions {
16
- PublishCommand () {
17
- argParser
18
- ..addFlag (
19
- 'force' ,
20
- abbr: 'f' ,
21
- help: 'Ignores errors in pre-publishing commands and publishes '
22
- 'without prompt' ,
23
- negatable: false ,
24
- )
25
- ..addFlag (
26
- 'dry-run' ,
27
- help: 'Passes `--dry-run` flag to `dart` or `flutter` publish command' ,
28
- negatable: false ,
29
- );
30
- }
14
+ /// Helpers for commands which publish packages to `pub.dev` .
15
+ mixin PublishHelpers on AmplifyCommand {
16
+ bool get dryRun;
17
+ bool get force;
31
18
32
- late final bool force = argResults! ['force' ] as bool ;
33
- late final bool dryRun = argResults! ['dry-run' ] as bool ;
19
+ /// Gathers the subset of packages which are publishable and whose latest
20
+ /// version is not already available on `pub.dev` .
21
+ Future <List <PackageInfo >> unpublishedPackages (
22
+ List <PackageInfo > publishablePackages,
23
+ ) async {
24
+ final unpublishedPackages = (await Future .wait ([
25
+ for (final package in publishablePackages) checkPublishable (package),
26
+ ]))
27
+ .whereType <PackageInfo >()
28
+ .toList ();
34
29
35
- @override
36
- String get description =>
37
- 'Publishes all packages in the Amplify Flutter repo which '
38
- 'need publishing.' ;
30
+ try {
31
+ sortPackagesTopologically <PackageInfo >(
32
+ unpublishedPackages,
33
+ (pkg) => pkg.pubspecInfo.pubspec,
34
+ );
35
+ } on CycleException <dynamic > {
36
+ if (! force) {
37
+ exitError ('Cannot sort packages with inter-dependencies.' );
38
+ }
39
+ }
39
40
40
- @override
41
- String get name => 'publish' ;
41
+ return unpublishedPackages;
42
+ }
42
43
43
44
/// Checks if [package] can be published based on whether the local version
44
45
/// is newer than the one published to `pub.dev` .
45
- Future <PackageInfo ?> _checkPublishable (PackageInfo package) async {
46
+ Future <PackageInfo ?> checkPublishable (PackageInfo package) async {
46
47
final publishTo = package.pubspecInfo.pubspec.publishTo;
47
48
if (publishTo == 'none' ) {
48
49
return null ;
@@ -61,8 +62,8 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
61
62
final publishedVersion = maxBy (
62
63
[
63
64
if (versionInfo? .latestPrerelease != null )
64
- versionInfo? .latestPrerelease! ,
65
- if (versionInfo? .latestVersion != null ) versionInfo? .latestVersion! ,
65
+ versionInfo! .latestPrerelease! ,
66
+ if (versionInfo? .latestVersion != null ) versionInfo! .latestVersion! ,
66
67
],
67
68
(v) => v,
68
69
);
@@ -85,7 +86,7 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
85
86
86
87
/// Runs pre-publish operations for [package] , most importantly any necessary
87
88
/// `build_runner` tasks.
88
- Future <void > _prePublish (PackageInfo package) async {
89
+ Future <void > prePublish (PackageInfo package) async {
89
90
logger.info ('Running pre-publish checks for ${package .name }...' );
90
91
if (! dryRun) {
91
92
// Remove any overrides so that `pub` commands resolve against
@@ -136,7 +137,7 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
136
137
static final _validationErrorRegex = RegExp (r'^\s*\*' );
137
138
138
139
/// Publishes the package using `pub` .
139
- Future <void > _publish (PackageInfo package) async {
140
+ Future <void > publish (PackageInfo package) async {
140
141
logger.info ('Publishing ${package .name }${dryRun ? ' (dry run)' : '' }...' );
141
142
final publishCmd = await Process .start (
142
143
package.flavor.entrypoint,
@@ -181,21 +182,49 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
181
182
}
182
183
}
183
184
}
185
+ }
186
+
187
+ /// Command to publish all Dart/Flutter packages in the repo.
188
+ class PublishCommand extends AmplifyCommand with GlobOptions , PublishHelpers {
189
+ PublishCommand () {
190
+ argParser
191
+ ..addFlag (
192
+ 'force' ,
193
+ abbr: 'f' ,
194
+ help: 'Ignores errors in pre-publishing commands and publishes '
195
+ 'without prompt' ,
196
+ negatable: false ,
197
+ )
198
+ ..addFlag (
199
+ 'dry-run' ,
200
+ help: 'Passes `--dry-run` flag to `dart` or `flutter` publish command' ,
201
+ negatable: false ,
202
+ );
203
+ }
204
+
205
+ @override
206
+ late final bool force = argResults! ['force' ] as bool ;
207
+
208
+ @override
209
+ late final bool dryRun = argResults! ['dry-run' ] as bool ;
210
+
211
+ @override
212
+ String get description =>
213
+ 'Publishes all packages in the Amplify Flutter repo which '
214
+ 'need publishing.' ;
215
+
216
+ @override
217
+ String get name => 'publish' ;
184
218
185
219
@override
186
220
Future <void > run () async {
187
221
await super .run ();
188
222
// Gather packages which can be published.
189
- final publishablePackages = repo
190
- .publishablePackages (commandPackages)
191
- .where ((pkg) => pkg.pubspecInfo.pubspec.publishTo != 'none' );
223
+ final publishablePackages = repo.publishablePackages (commandPackages);
192
224
193
225
// Gather packages which need to be published.
194
- final packagesNeedingPublish = (await Future .wait ([
195
- for (final package in publishablePackages) _checkPublishable (package),
196
- ]))
197
- .whereType <PackageInfo >()
198
- .toList ();
226
+ final packagesNeedingPublish =
227
+ await unpublishedPackages (publishablePackages);
199
228
200
229
// Publishable packages which are being held back.
201
230
final unpublishablePackages = publishablePackages.where (
@@ -207,17 +236,6 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
207
236
return ;
208
237
}
209
238
210
- try {
211
- sortPackagesTopologically <PackageInfo >(
212
- packagesNeedingPublish,
213
- (pkg) => pkg.pubspecInfo.pubspec,
214
- );
215
- } on CycleException <dynamic > {
216
- if (! force) {
217
- exitError ('Cannot sort packages with inter-dependencies.' );
218
- }
219
- }
220
-
221
239
stdout
222
240
..writeln ('Preparing to publish${dryRun ? ' (dry run)' : '' }: ' )
223
241
..writeln (
@@ -247,8 +265,8 @@ class PublishCommand extends AmplifyCommand with GlobOptions {
247
265
// some packages will not be published, it also means that the command
248
266
// can be re-run to pick up where it left off.
249
267
for (final package in packagesNeedingPublish) {
250
- await _prePublish (package);
251
- await _publish (package);
268
+ await prePublish (package);
269
+ await publish (package);
252
270
}
253
271
254
272
stdout.writeln (
0 commit comments