Skip to content

Commit

Permalink
refactor(nextcloud_test): Remove version handling mess
Browse files Browse the repository at this point in the history
Signed-off-by: jld3103 <[email protected]>
  • Loading branch information
provokateurin committed Dec 18, 2023
1 parent d135010 commit 4aaf38b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 136 deletions.
2 changes: 1 addition & 1 deletion packages/nextcloud/test/dashboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main() {
final items = response.body.ocs.data['recommendations']!.items;
expect(items, hasLength(7));
},
skip: preset.version.getRaw() < Version(27, 1, 0),
skip: preset.version < Version(27, 1, 0),
);
});
},
Expand Down
33 changes: 15 additions & 18 deletions packages/nextcloud_test/bin/generate_presets.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/src/app.dart';
import 'package:nextcloud_test/src/extended_version.dart';
import 'package:universal_io/io.dart';
import 'package:version/version.dart';

Future<void> main() async {
const appIDs = [
Expand Down Expand Up @@ -43,7 +43,8 @@ Future<void> main() async {
}
}

File('${appPresetsDir.path}/${release.version.withoutPatch()}').writeAsStringSync(buffer.toString());
File('${appPresetsDir.path}/${release.version.major}.${release.version.minor}')
.writeAsStringSync(buffer.toString());
}
}

Expand All @@ -63,21 +64,21 @@ Future<void> main() async {
buffer.writeln('${app.id.toUpperCase()}_URL=${release.url}');
}

File('${serverPresetsDir.path}/${serverVersion.withoutPatch()}').writeAsStringSync(buffer.toString());
File('${serverPresetsDir.path}/${serverVersion.major}.${serverVersion.minor}').writeAsStringSync(buffer.toString());
}

final latestPresetLink = Link('docker/presets/latest');
if (latestPresetLink.existsSync()) {
latestPresetLink.updateSync('server/${serverVersions.first.withoutPatch()}');
latestPresetLink.updateSync('server/${serverVersions.first.major}.${serverVersions.first.minor}');
} else {
latestPresetLink.createSync('server/${serverVersions.first.withoutPatch()}');
latestPresetLink.createSync('server/${serverVersions.first.major}.${serverVersions.first.minor}');
}

httpClient.close(force: true);
}

Future<List<ExtendedVersion>> _getServerVersions(final HttpClient httpClient) async {
final versions = <ExtendedVersion, ExtendedVersion>{};
Future<List<Version>> _getServerVersions(final HttpClient httpClient) async {
final versions = <Version, Version>{};
String? next = 'https://hub.docker.com/v2/repositories/library/nextcloud/tags?page_size=1000';

while (next != null) {
Expand All @@ -96,14 +97,10 @@ Future<List<ExtendedVersion>> _getServerVersions(final HttpClient httpClient) as
try {
final tag = result as Map<String, dynamic>;

final version = ExtendedVersion.parse(tag['name'] as String);
if (version.minor == null || version.patch == null) {
continue;
}

final version = Version.parse(tag['name'] as String);
final normalizedVersion = version.withoutPatch();

if (version < coreMinVersion || version.major > core.maxMajor) {
if (version < core.minVersion || version.major > core.maxMajor) {
continue;
}

Expand Down Expand Up @@ -138,22 +135,22 @@ Future<List<App>> _getApps(final List<String> appIDs, final HttpClient httpClien
continue;
}

final releases = <ExtendedVersion, AppRelease>{};
final releases = <Version, AppRelease>{};

final releasesItems = app['releases'] as List;
for (final releaseItem in releasesItems) {
final release = releaseItem as Map<String, dynamic>;

final version = ExtendedVersion.parse(release['version'] as String);
final version = Version.parse(release['version'] as String);
final normalizedVersion = version.withoutPatch();

final rawPlatformVersionSpec = release['rawPlatformVersionSpec'] as String;
final minimumServerVersionRequirement =
ExtendedVersion.parse(rawPlatformVersionSpec.split(' ')[0].replaceFirst('>=', ''));
Version.parse(rawPlatformVersionSpec.split(' ')[0].replaceFirst('>=', ''));
final maximumServerVersionRequirement =
ExtendedVersion.parse(rawPlatformVersionSpec.split(' ')[1].replaceFirst('<=', ''));
Version.parse(rawPlatformVersionSpec.split(' ')[1].replaceFirst('<=', ''));

if (maximumServerVersionRequirement < coreMinVersion) {
if (maximumServerVersionRequirement < core.minVersion) {
continue;
}
if (minimumServerVersionRequirement.major > core.maxMajor) {
Expand Down
23 changes: 14 additions & 9 deletions packages/nextcloud_test/lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:meta/meta.dart';
import 'package:nextcloud_test/src/extended_version.dart';
import 'package:version/version.dart';

/// Describes a release of an [App] from https://apps.nextcloud.com
@internal
typedef AppRelease = ({
ExtendedVersion version,
Version version,
String url,
ExtendedVersion minimumServerVersion,
ExtendedVersion maximumServerVersion,
Version minimumServerVersion,
Version maximumServerVersion,
});

/// Describes an app from https://apps.nextcloud.com
Expand All @@ -17,15 +17,19 @@ typedef App = ({
List<AppRelease> releases,
});

extension VersionNormalization on Version {
Version withoutPatch() => Version(major, minor, 0);
}

@internal
extension AppFindLatestRelease on App {
AppRelease? findLatestCompatibleRelease(final ExtendedVersion serverVersion, {final bool allowUnstable = false}) {
AppRelease? findLatestCompatibleRelease(final Version serverVersion, {final bool allowUnstable = false}) {
final compatibleReleases = releases
.where(
(final release) =>
serverVersion >= release.minimumServerVersion &&
serverVersion <= release.maximumServerVersion &&
(allowUnstable || !release.version.getRaw().isPreRelease),
serverVersion < release.maximumServerVersion.incrementMajor() &&
(allowUnstable || !release.version.isPreRelease),
)
.toList()
..sort((final a, final b) => b.version.compareTo(a.version));
Expand All @@ -40,10 +44,11 @@ extension AppFindLatestRelease on App {

@internal
extension AppReleaseFindLatestServerVersion on AppRelease {
ExtendedVersion findLatestServerVersion(final List<ExtendedVersion> serverVersions) {
Version findLatestServerVersion(final List<Version> serverVersions) {
final compatibleReleases = serverVersions
.where(
(final serverVersion) => serverVersion >= minimumServerVersion && serverVersion <= maximumServerVersion,
(final serverVersion) =>
serverVersion >= minimumServerVersion && serverVersion < maximumServerVersion.incrementMajor(),
)
.toList()
..sort((final a, final b) => b.compareTo(a));
Expand Down
103 changes: 0 additions & 103 deletions packages/nextcloud_test/lib/src/extended_version.dart

This file was deleted.

10 changes: 5 additions & 5 deletions packages/nextcloud_test/lib/src/presets.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_test/nextcloud_test.dart';
import 'package:nextcloud_test/src/extended_version.dart';
import 'package:test/test.dart';
import 'package:universal_io/io.dart';
import 'package:version/version.dart';

/// Combination of preset `name` and preset `version`.
typedef Preset = ({String name, ExtendedVersion version});
typedef Preset = ({String name, Version version});

final Map<String, List<ExtendedVersion>> _presets = () {
final presets = <String, List<ExtendedVersion>>{};
final Map<String, List<Version>> _presets = () {
final presets = <String, List<Version>>{};

final presetGroups = Directory('../nextcloud_test/docker/presets')
.listSync(followLinks: false)
Expand All @@ -19,7 +19,7 @@ final Map<String, List<ExtendedVersion>> _presets = () {
final presetVersions = Directory('../nextcloud_test/docker/presets/$presetGroup')
.listSync(followLinks: false)
.whereType<File>()
.map((final f) => ExtendedVersion.parse(PathUri.parse(f.path).name));
.map((final f) => Version.parse(PathUri.parse(f.path).name));

presets[presetGroup] = presetVersions.toList();
}
Expand Down

0 comments on commit 4aaf38b

Please sign in to comment.