Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jan 17, 2025
1 parent 9cd1175 commit 8f53cbd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/shorebird_cli/lib/src/commands/patch/patch_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ 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.''',
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.''',
)
..addFlag(
'allow-native-diffs',
Expand Down Expand Up @@ -297,11 +299,7 @@ NOTE: this is ${styleBold.wrap('not')} recommended. Asset changes cannot be incl
..removeWhere(
(release) => !release.platformStatuses.keys.contains(releasePlatform),
)
..sort(
(a, b) => Version.parse(b.version).compareTo(
Version.parse(a.version),
),
);
..sortByVersion();
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.''',
Expand Down Expand Up @@ -583,3 +581,15 @@ ${summary.join('\n')}
return artifactFile;
}
}

/// 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),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:args/args.dart';
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:scoped_deps/scoped_deps.dart';
Expand Down Expand Up @@ -31,6 +32,16 @@ import '../../helpers.dart';
import '../../matchers.dart';
import '../../mocks.dart';

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

@override
final String version;

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

void main() {
group(PatchCommand, () {
const appId = 'test-app-id';
Expand Down Expand Up @@ -1304,4 +1315,54 @@ 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
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(),
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'),
]),
);
});
});
}

0 comments on commit 8f53cbd

Please sign in to comment.