@@ -104,7 +104,57 @@ class PublishCommand extends AmplifyCommand {
104104 : null ;
105105 }
106106
107+ /// Runs pre-publish operations for [package] , most importantly any necessary
108+ /// `build_runner` tasks.
109+ Future <void > _prePublish (PackageInfo package) async {
110+ if (! package.needsBuildRunner) {
111+ return ;
112+ }
113+ final progress =
114+ logger.progress ('Running pre-publish checks for ${package .name }...' );
115+ final buildRunnerCmd = await Process .start (
116+ package.flavor.entrypoint,
117+ [
118+ if (package.flavor == PackageFlavor .flutter) 'pub' ,
119+ 'run' ,
120+ 'build_runner' ,
121+ 'build' ,
122+ if (verbose) '--verbose' ,
123+ ],
124+ workingDirectory: package.path,
125+ );
126+ final output = StringBuffer ();
127+ buildRunnerCmd
128+ ..captureStdout (sink: output.writeln)
129+ ..captureStderr (sink: output.writeln);
130+ if (verbose) {
131+ buildRunnerCmd
132+ ..captureStdout ()
133+ ..captureStderr ();
134+ }
135+ if (await buildRunnerCmd.exitCode != 0 ) {
136+ progress.cancel ();
137+ logger.stderr ('Failed to run `build_runner` for ${package .name }: ' );
138+ if (! verbose) {
139+ logger.stderr (output.toString ());
140+ }
141+ exit (1 );
142+ }
143+ progress.finish (message: 'Success!' );
144+ }
145+
146+ static final _validationStartRegex = RegExp (
147+ r'Package validation found the following' ,
148+ );
149+ static final _validationConstraintRegex = RegExp (
150+ r'\* Your dependency on ".+" should allow more than one version.' ,
151+ );
152+ static final _validationErrorRegex = RegExp (r'^\s*\*' );
153+
154+ /// Publishes the package using `pub` .
107155 Future <void > _publish (PackageInfo package) async {
156+ final progress = logger
157+ .progress ('Publishing ${package .name }${dryRun ? ' (dry run)' : '' }...' );
108158 final publishCmd = await Process .start (
109159 package.flavor.entrypoint,
110160 [
@@ -114,18 +164,38 @@ class PublishCommand extends AmplifyCommand {
114164 ],
115165 workingDirectory: package.path,
116166 );
167+ final output = StringBuffer ();
168+ publishCmd
169+ ..captureStdout (sink: output.writeln)
170+ ..captureStderr (sink: output.writeln);
117171 if (verbose) {
118172 publishCmd
119173 ..captureStdout ()
120174 ..captureStderr ();
121175 }
122- if (force) {
123- publishCmd.stdin.writeln ('y' );
124- }
176+ publishCmd.stdin.writeln ('y' );
125177
126- if (await publishCmd.exitCode != 0 ) {
127- exitError ('Failed to publish package ${package .name }' );
178+ final exitCode = await publishCmd.exitCode;
179+ if (exitCode != 0 ) {
180+ // Find any error lines which are not dependency constraint-related.
181+ final failures = output
182+ .toString ()
183+ .split ('\n ' )
184+ .skipWhile ((line) => ! _validationStartRegex.hasMatch (line))
185+ .where (_validationErrorRegex.hasMatch)
186+ .where ((line) => ! _validationConstraintRegex.hasMatch (line));
187+ if (failures.isNotEmpty) {
188+ progress.cancel ();
189+ logger
190+ ..stderr (
191+ 'Failed to publish package ${package .name } '
192+ 'due to the following errors: ' ,
193+ )
194+ ..stderr (failures.join ('\n ' ));
195+ exit (1 );
196+ }
128197 }
198+ progress.finish (message: 'Success!' );
129199 }
130200
131201 @override
@@ -138,6 +208,11 @@ class PublishCommand extends AmplifyCommand {
138208 .whereType <PackageInfo >()
139209 .toList ();
140210
211+ if (publishablePackages.isEmpty) {
212+ logger.stdout ('No packages need publishing!' );
213+ return ;
214+ }
215+
141216 try {
142217 sortPackagesTopologically <PackageInfo >(
143218 publishablePackages,
@@ -150,7 +225,7 @@ class PublishCommand extends AmplifyCommand {
150225 }
151226
152227 stdout
153- ..writeln ('Preparing to publish: ' )
228+ ..writeln ('Preparing to publish${ dryRun ? ' (dry run)' : '' } : ' )
154229 ..writeln (
155230 publishablePackages
156231 .map ((pkg) => '${pkg .pubspecInfo .pubspec .version } ${pkg .name }' )
@@ -164,6 +239,14 @@ class PublishCommand extends AmplifyCommand {
164239 }
165240 }
166241
242+ // Run pre-publish checks before publishing any packages.
243+ if (! force) {
244+ for (final package in publishablePackages) {
245+ await _prePublish (package);
246+ }
247+ }
248+
249+ // Publish each package sequentially.
167250 for (final package in publishablePackages) {
168251 await _publish (package);
169252 }
0 commit comments