diff --git a/packages/nextcloud/test/dashboard_test.dart b/packages/nextcloud/test/dashboard_test.dart index 020fc043dae..22e0188d864 100644 --- a/packages/nextcloud/test/dashboard_test.dart +++ b/packages/nextcloud/test/dashboard_test.dart @@ -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), ); }); }, diff --git a/packages/nextcloud_test/bin/generate_presets.dart b/packages/nextcloud_test/bin/generate_presets.dart index 77be1b41f89..b3c1cc7a7b9 100644 --- a/packages/nextcloud_test/bin/generate_presets.dart +++ b/packages/nextcloud_test/bin/generate_presets.dart @@ -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 main() async { const appIDs = [ @@ -43,7 +43,8 @@ Future main() async { } } - File('${appPresetsDir.path}/${release.version.withoutPatch()}').writeAsStringSync(buffer.toString()); + File('${appPresetsDir.path}/${release.version.major}.${release.version.minor}') + .writeAsStringSync(buffer.toString()); } } @@ -63,21 +64,21 @@ Future 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> _getServerVersions(final HttpClient httpClient) async { - final versions = {}; +Future> _getServerVersions(final HttpClient httpClient) async { + final versions = {}; String? next = 'https://hub.docker.com/v2/repositories/library/nextcloud/tags?page_size=1000'; while (next != null) { @@ -96,14 +97,10 @@ Future> _getServerVersions(final HttpClient httpClient) as try { final tag = result as Map; - 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; } @@ -138,22 +135,22 @@ Future> _getApps(final List appIDs, final HttpClient httpClien continue; } - final releases = {}; + final releases = {}; final releasesItems = app['releases'] as List; for (final releaseItem in releasesItems) { final release = releaseItem as Map; - 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) { diff --git a/packages/nextcloud_test/lib/src/app.dart b/packages/nextcloud_test/lib/src/app.dart index cd7d41f172b..e5a32182d03 100644 --- a/packages/nextcloud_test/lib/src/app.dart +++ b/packages/nextcloud_test/lib/src/app.dart @@ -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 @@ -17,15 +17,19 @@ typedef App = ({ List 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)); @@ -40,10 +44,11 @@ extension AppFindLatestRelease on App { @internal extension AppReleaseFindLatestServerVersion on AppRelease { - ExtendedVersion findLatestServerVersion(final List serverVersions) { + Version findLatestServerVersion(final List 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)); diff --git a/packages/nextcloud_test/lib/src/extended_version.dart b/packages/nextcloud_test/lib/src/extended_version.dart deleted file mode 100644 index 1a749dfacea..00000000000 --- a/packages/nextcloud_test/lib/src/extended_version.dart +++ /dev/null @@ -1,103 +0,0 @@ -import 'package:meta/meta.dart'; -import 'package:nextcloud/core.dart' as core; -import 'package:version/version.dart'; - -/// An [ExtendedVersion] for [core.minVersion]. -@internal -final coreMinVersion = ExtendedVersion.fromVersion(core.minVersion); - -/// A replacement for [Version] that correctly compares [minor] and [patch] versions. -@immutable -@internal -class ExtendedVersion implements Comparable { - const ExtendedVersion( - final Version raw, - this.major, - this.minor, - this.patch, - ) : _raw = raw; - - factory ExtendedVersion.parse(final String versionString) { - final version = Version.parse(versionString); - final dotCount = versionString.split('').where((final char) => char == '.').length; - - return ExtendedVersion( - version, - version.major, - dotCount >= 1 ? version.minor : null, - dotCount >= 2 ? version.patch : null, - ); - } - - factory ExtendedVersion.fromVersion(final Version version) => ExtendedVersion( - version, - version.major, - version.minor, - version.patch, - ); - - final Version _raw; - - final int major; - - final int? minor; - - final int? patch; - - ExtendedVersion withoutPatch() => ExtendedVersion( - _raw, - major, - minor, - null, - ); - - Version getRaw() => _raw; - - @override - String toString() { - final buffer = StringBuffer('$major'); - - if (minor != null) { - buffer.write('.$minor'); - } - if (patch != null) { - buffer.write('.$patch'); - } - - return buffer.toString(); - } - - @override - int compareTo(final ExtendedVersion other) => _compare(this, other); - - bool operator <(final dynamic other) => other is ExtendedVersion && _compare(this, other) < 0; - - bool operator <=(final dynamic other) => other is ExtendedVersion && _compare(this, other) <= 0; - - @override - bool operator ==(final dynamic other) => other is ExtendedVersion && _compare(this, other) == 0; - - bool operator >=(final dynamic other) => other is ExtendedVersion && _compare(this, other) >= 0; - - bool operator >(final dynamic other) => other is ExtendedVersion && _compare(this, other) > 0; - - static int _compare(final ExtendedVersion a, final ExtendedVersion b) { - final compareMinors = a.minor != null && b.minor != null; - final comparePatches = a.patch != null && b.patch != null; - - return Version( - a.major, - compareMinors ? a.minor! : 0, - comparePatches ? a.patch! : 0, - ).compareTo( - Version( - b.major, - compareMinors ? b.minor! : 0, - comparePatches ? b.patch! : 0, - ), - ); - } - - @override - int get hashCode => Object.hashAll([major, minor, patch]); -} diff --git a/packages/nextcloud_test/lib/src/presets.dart b/packages/nextcloud_test/lib/src/presets.dart index f32a6ba8dea..8648c3874a9 100644 --- a/packages/nextcloud_test/lib/src/presets.dart +++ b/packages/nextcloud_test/lib/src/presets.dart @@ -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> _presets = () { - final presets = >{}; +final Map> _presets = () { + final presets = >{}; final presetGroups = Directory('../nextcloud_test/docker/presets') .listSync(followLinks: false) @@ -19,7 +19,7 @@ final Map> _presets = () { final presetVersions = Directory('../nextcloud_test/docker/presets/$presetGroup') .listSync(followLinks: false) .whereType() - .map((final f) => ExtendedVersion.parse(PathUri.parse(f.path).name)); + .map((final f) => Version.parse(PathUri.parse(f.path).name)); presets[presetGroup] = presetVersions.toList(); }