Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[flutter_plugin_tools] publish-plugin check against pub to determine if a release should happen #4068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- `xctest` now supports running macOS tests in addition to iOS
- **Breaking change**: it now requires an `--ios` and/or `--macos` flag.
- The tooling now runs in strong null-safe mode.
- `publish plugins` check against pub.dev to determine if a release should happen.
- Modified the output format of `pubspec-check` and `xctest`

## 0.2.0
Expand Down
42 changes: 27 additions & 15 deletions script/tool/lib/src/publish_plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io' as io;

import 'package:file/file.dart';
import 'package:git/git.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
Expand All @@ -18,6 +19,7 @@ import 'common/core.dart';
import 'common/git_version_finder.dart';
import 'common/plugin_command.dart';
import 'common/process_runner.dart';
import 'common/pub_version_finder.dart';

@immutable
class _RemoteInfo {
Expand Down Expand Up @@ -49,7 +51,10 @@ class PublishPluginCommand extends PluginCommand {
Print print = print,
io.Stdin? stdinput,
GitDir? gitDir,
}) : _print = print,
http.Client? httpClient,
}) : _pubVersionFinder =
PubVersionFinder(httpClient: httpClient ?? http.Client()),
_print = print,
_stdin = stdinput ?? io.stdin,
super(packagesDir, processRunner: processRunner, gitDir: gitDir) {
argParser.addOption(
Expand Down Expand Up @@ -131,6 +136,7 @@ class PublishPluginCommand extends PluginCommand {
final Print _print;
final io.Stdin _stdin;
StreamSubscription<String>? _stdinSubscription;
final PubVersionFinder _pubVersionFinder;

@override
Future<void> run() async {
Expand Down Expand Up @@ -182,6 +188,8 @@ class PublishPluginCommand extends PluginCommand {
remoteForTagPush: remote,
);
}

_pubVersionFinder.httpClient.close();
await _finish(successful);
}

Expand All @@ -196,6 +204,7 @@ class PublishPluginCommand extends PluginCommand {
_print('No version updates in this commit.');
return true;
}

_print('Getting existing tags...');
final io.ProcessResult existingTagsResult =
await baseGitDir.runCommand(<String>['tag', '--sort=-committerdate']);
Expand All @@ -212,7 +221,6 @@ class PublishPluginCommand extends PluginCommand {
.childFile(pubspecPath);
final _CheckNeedsReleaseResult result = await _checkNeedsRelease(
pubspecFile: pubspecFile,
gitVersionFinder: gitVersionFinder,
existingTags: existingTags,
);
switch (result) {
Expand Down Expand Up @@ -271,7 +279,6 @@ class PublishPluginCommand extends PluginCommand {
// Returns a [_CheckNeedsReleaseResult] that indicates the result.
Future<_CheckNeedsReleaseResult> _checkNeedsRelease({
required File pubspecFile,
required GitVersionFinder gitVersionFinder,
required List<String> existingTags,
}) async {
if (!pubspecFile.existsSync()) {
Expand All @@ -293,19 +300,24 @@ Safe to ignore if the package is deleted in this commit.
return _CheckNeedsReleaseResult.failure;
}

// Get latest tagged version and compare with the current version.
// TODO(cyanglaz): Check latest version of the package on pub instead of git
// https://github.com/flutter/flutter/issues/81047

final String latestTag = existingTags.firstWhere(
(String tag) => tag.split('-v').first == pubspec.name,
orElse: () => '');
if (latestTag.isNotEmpty) {
final String latestTaggedVersion = latestTag.split('-v').last;
final Version latestVersion = Version.parse(latestTaggedVersion);
if (pubspec.version! < latestVersion) {
// Check if the package named `packageName` with `version` has already published.
final Version version = pubspec.version!;
final PubVersionFinderResponse pubVersionFinderResponse =
await _pubVersionFinder.getPackageVersion(package: pubspec.name);
if (pubVersionFinderResponse.versions.contains(version)) {
final String tagsForPackageWithSameVersion = existingTags.firstWhere(
(String tag) =>
tag.split('-v').first == pubspec.name &&
tag.split('-v').last == version.toString(),
orElse: () => '');
_print(
'The version $version of ${pubspec.name} has already been published');
if (tagsForPackageWithSameVersion.isEmpty) {
_print(
'The new version (${pubspec.version}) is lower than the current version ($latestVersion) for ${pubspec.name}.\nThis git commit is a revert, no release is tagged.');
'However, the git release tag for this version (${pubspec.name}-v$version) is not found. Please manually fix the tag then run the command again.');
return _CheckNeedsReleaseResult.failure;
} else {
_print('skip.');
return _CheckNeedsReleaseResult.noRelease;
}
}
Expand Down
Loading