Skip to content

Commit

Permalink
[tools] Fix publish flag calculation (flutter#5694)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartmorgan authored and mauricioluz committed Jan 26, 2023
1 parent 3889092 commit af9b859
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT

- Fixes changelog validation when reverting to a `NEXT` state.
- Fixes multiplication of `--force` flag when publishing multiple packages.

## 0.8.5

Expand Down
4 changes: 3 additions & 1 deletion script/tool/lib/src/common/plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ abstract class PluginCommand extends Command<void> {

/// Convenience accessor for List<String> arguments.
List<String> getStringListArg(String key) {
return (argResults![key] as List<String>?) ?? <String>[];
// Clone the list so that if a caller modifies the result it won't change
// the actual arguments list for future queries.
return List<String>.from(argResults![key] as List<String>? ?? <String>[]);
}

/// If true, commands should log timing information that might be useful in
Expand Down
17 changes: 10 additions & 7 deletions script/tool/lib/src/publish_plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class PublishPluginCommand extends PackageLoopingCommand {
List<String> _existingGitTags = <String>[];
// The remote to push tags to.
late _RemoteInfo _remote;
// Flags to pass to `pub publish`.
late List<String> _publishFlags;

@override
String get successSummaryMessage => 'published';
Expand Down Expand Up @@ -149,6 +151,11 @@ class PublishPluginCommand extends PackageLoopingCommand {
_existingGitTags = (existingTagsResult.stdout as String).split('\n')
..removeWhere((String element) => element.isEmpty);

_publishFlags = <String>[
...getStringListArg(_pubFlagsOption),
if (getBoolArg(_skipConfirmationFlag)) '--force',
];

if (getBoolArg(_dryRunFlag)) {
print('=============== DRY RUN ===============');
}
Expand Down Expand Up @@ -333,22 +340,18 @@ Safe to ignore if the package is deleted in this commit.

Future<bool> _publish(RepositoryPackage package) async {
print('Publishing...');
final List<String> publishFlags = getStringListArg(_pubFlagsOption);
print('Running `pub publish ${publishFlags.join(' ')}` in '
print('Running `pub publish ${_publishFlags.join(' ')}` in '
'${package.directory.absolute.path}...\n');
if (getBoolArg(_dryRunFlag)) {
return true;
}

if (getBoolArg(_skipConfirmationFlag)) {
publishFlags.add('--force');
}
if (publishFlags.contains('--force')) {
if (_publishFlags.contains('--force')) {
_ensureValidPubCredential();
}

final io.Process publish = await processRunner.start(
flutterCommand, <String>['pub', 'publish'] + publishFlags,
flutterCommand, <String>['pub', 'publish', ..._publishFlags],
workingDirectory: package.directory);
publish.stdout.transform(utf8.decoder).listen((String data) => print(data));
publish.stderr.transform(utf8.decoder).listen((String data) => print(data));
Expand Down
29 changes: 29 additions & 0 deletions script/tool/test/publish_plugin_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,35 @@ void main() {
plugin.path)));
});

test('--force is only added once, regardless of plugin count', () async {
_createMockCredentialFile();
final RepositoryPackage plugin1 =
createFakePlugin('plugin_a', packagesDir, examples: <String>[]);
final RepositoryPackage plugin2 =
createFakePlugin('plugin_b', packagesDir, examples: <String>[]);

await runCapturingPrint(commandRunner, <String>[
'publish-plugin',
'--packages=plugin_a,plugin_b',
'--skip-confirmation',
'--pub-publish-flags',
'--server=bar'
]);

expect(
processRunner.recordedCalls,
containsAllInOrder(<ProcessCall>[
ProcessCall(
flutterCommand,
const <String>['pub', 'publish', '--server=bar', '--force'],
plugin1.path),
ProcessCall(
flutterCommand,
const <String>['pub', 'publish', '--server=bar', '--force'],
plugin2.path),
]));
});

test('throws if pub publish fails', () async {
createFakePlugin('foo', packagesDir, examples: <String>[]);

Expand Down

0 comments on commit af9b859

Please sign in to comment.