Skip to content

Commit

Permalink
sort by updated at
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jan 17, 2025
1 parent 8f53cbd commit b010f6b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 59 deletions.
21 changes: 7 additions & 14 deletions packages/shorebird_cli/lib/src/commands/patch/patch_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';
import 'package:collection/collection.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:meta/meta.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:scoped_deps/scoped_deps.dart';
import 'package:shorebird_cli/src/artifact_manager.dart';
import 'package:shorebird_cli/src/cache.dart';
Expand Down Expand Up @@ -79,9 +78,8 @@ class PatchCommand extends ShorebirdCommand {
'release-version',
help: '''
The version of the associated release (e.g. "1.0.0").
To target the latest release (highest released version) use --release-version=latest.
If you are building an xcframework or aar, this number needs to match the host app's release version.''',
If you are building an xcframework or aar, this number needs to match the host app's release version.
To target the latest release (e.g. the release that was most recently updated) use --release-version=latest.''',
)
..addFlag(
'allow-native-diffs',
Expand Down Expand Up @@ -299,14 +297,15 @@ NOTE: this is ${styleBold.wrap('not')} recommended. Asset changes cannot be incl
..removeWhere(
(release) => !release.platformStatuses.keys.contains(releasePlatform),
)
..sortByVersion();
..sortByUpdatedAt();
if (releases.isEmpty) {
logger.warn(
'''No ${releasePlatform.displayName} releases found for app $appId. You must first create a release before you can create a patch.''',
);
throw ProcessExit(ExitCode.usage.code);
}
release = releases.first;
// Use the most recently updated release for the specified platform.
release = releases.last;
} else if (results.wasParsed('release-version')) {
final releaseVersion = results['release-version'] as String;
release = await codePushClientWrapper.getRelease(
Expand Down Expand Up @@ -584,12 +583,6 @@ ${summary.join('\n')}

/// Extension on list of releases for sorting the releases.
extension SortReleases on List<Release> {
/// Sort the list of releases by version (descending).
void sortByVersion() {
return sort(
(a, b) => Version.parse(b.version).compareTo(
Version.parse(a.version),
),
);
}
/// Sort the list of releases by when they were last updated ascending.
void sortByUpdatedAt() => sort((a, b) => a.updatedAt.compareTo(b.updatedAt));
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import '../../matchers.dart';
import '../../mocks.dart';

class _FakeRelease extends Fake with EquatableMixin implements Release {
_FakeRelease({required this.version});
_FakeRelease({required this.updatedAt});

@override
final String version;
final DateTime updatedAt;

@override
List<Object?> get props => [version];
List<Object?> get props => [updatedAt];
}

void main() {
Expand Down Expand Up @@ -1316,51 +1316,20 @@ Please re-run the release command for this version or create a new release.''',
});
});

group('sortByVersion', () {
test('sorts versions by semver', () {
// Sorts by major version
group('sortByUpdatedAt', () {
test('sorts versions correctly', () {
expect(
[
_FakeRelease(version: '1.0.0+1'),
_FakeRelease(version: '3.0.0+1'),
_FakeRelease(version: '2.0.0+1'),
_FakeRelease(version: '4.0.0+1'),
]..sortByVersion(),
_FakeRelease(updatedAt: DateTime(2025, 05, 15)),
_FakeRelease(updatedAt: DateTime(2025, 04, 15)),
_FakeRelease(updatedAt: DateTime(2021, 09, 25)),
_FakeRelease(updatedAt: DateTime(2024)),
]..sortByUpdatedAt(),
equals([
_FakeRelease(version: '4.0.0+1'),
_FakeRelease(version: '3.0.0+1'),
_FakeRelease(version: '2.0.0+1'),
_FakeRelease(version: '1.0.0+1'),
]),
);

// Sorts by build number
expect(
[
_FakeRelease(version: '1.0.0+1'),
_FakeRelease(version: '1.0.0+4'),
_FakeRelease(version: '1.0.0+2'),
_FakeRelease(version: '1.0.0+6'),
]..sortByVersion(),
equals([
_FakeRelease(version: '1.0.0+6'),
_FakeRelease(version: '1.0.0+4'),
_FakeRelease(version: '1.0.0+2'),
_FakeRelease(version: '1.0.0+1'),
]),
);

// Sorts by pre-release, handles missing build numbers, etc.
expect(
[
_FakeRelease(version: '1.0.0'),
_FakeRelease(version: '1.0.0+1.0.0'),
_FakeRelease(version: '1.0.0-dev'),
]..sortByVersion(),
equals([
_FakeRelease(version: '1.0.0+1.0.0'),
_FakeRelease(version: '1.0.0'),
_FakeRelease(version: '1.0.0-dev'),
_FakeRelease(updatedAt: DateTime(2021, 09, 25)),
_FakeRelease(updatedAt: DateTime(2024)),
_FakeRelease(updatedAt: DateTime(2025, 04, 15)),
_FakeRelease(updatedAt: DateTime(2025, 05, 15)),
]),
);
});
Expand Down

0 comments on commit b010f6b

Please sign in to comment.