From a5d0e524cb0091f152a6bfd3b2e46ff4f37adc85 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Mon, 27 Mar 2023 08:50:27 +0200 Subject: [PATCH] chore: deprecated Gradle internal VersionNumber not used * Do not use deprecated gradle internal VersionNumber This fixes caused deprecation warnings and incompatibility with Gradle => 8.1 * Tweak comment * Simplify VersionNumber to usecases of this plugin * Minor test polishing --- .../dockercompose/ComposeExecutor.groovy | 3 +- .../tasks/ComposeDownForced.groovy | 2 +- .../dockercompose/util/VersionNumber.java | 119 ++++++++++++++++++ .../util/VersionNumberTest.groovy | 30 +++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java create mode 100644 src/test/groovy/com/avast/gradle/dockercompose/util/VersionNumberTest.groovy diff --git a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy index 5fcd552..802d5ca 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/ComposeExecutor.groovy @@ -14,7 +14,6 @@ import org.gradle.api.services.BuildServiceParameters import org.gradle.internal.UncheckedException import org.gradle.process.ExecOperations import org.gradle.process.ExecSpec -import org.gradle.util.VersionNumber import org.yaml.snakeyaml.Yaml import javax.inject.Inject @@ -22,6 +21,8 @@ import java.lang.ref.WeakReference import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executors +import com.avast.gradle.dockercompose.util.VersionNumber + abstract class ComposeExecutor implements BuildService, AutoCloseable { static interface Parameters extends BuildServiceParameters { abstract DirectoryProperty getProjectDirectory() diff --git a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeDownForced.groovy b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeDownForced.groovy index cb1eb0c..20f64fd 100644 --- a/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeDownForced.groovy +++ b/src/main/groovy/com/avast/gradle/dockercompose/tasks/ComposeDownForced.groovy @@ -3,13 +3,13 @@ package com.avast.gradle.dockercompose.tasks import com.avast.gradle.dockercompose.ComposeExecutor import com.avast.gradle.dockercompose.RemoveImages import com.avast.gradle.dockercompose.ServiceInfoCache +import com.avast.gradle.dockercompose.util.VersionNumber import org.gradle.api.DefaultTask import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction -import org.gradle.util.VersionNumber import java.time.Duration diff --git a/src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java b/src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java new file mode 100644 index 0000000..cdc3f8f --- /dev/null +++ b/src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java @@ -0,0 +1,119 @@ +package com.avast.gradle.dockercompose.util; + +import javax.annotation.Nullable; +import java.util.Objects; + +/** + * This class is a simplified version of the deprecated org.gradle.util.VersionNumber class + * See https://github.com/gradle/gradle/blob/7d8cacafe70e5c4cc06173550cb13511cfbf3749/subprojects/core/src/main/java/org/gradle/util/VersionNumber.java + * causing compatibility issues with Gradle 8.1 onwards. + */ +public class VersionNumber implements Comparable { + public static final VersionNumber UNKNOWN = new VersionNumber(0, 0, 0); + + private final int major; + private final int minor; + private final int micro; + + private VersionNumber(int major, int minor, int micro) { + this.major = major; + this.minor = minor; + this.micro = micro; + } + + @Override + public int compareTo(VersionNumber other) { + if (major != other.major) { + return major - other.major; + } + if (minor != other.minor) { + return minor - other.minor; + } + if (micro != other.micro) { + return micro - other.micro; + } + return 0; + } + + @Override + public boolean equals(@Nullable Object other) { + return other instanceof VersionNumber && compareTo((VersionNumber) other) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(major, minor, micro); + } + + @Override + public String toString() { + return String.format("%d.%d.%d", major, minor, micro); + } + + public static VersionNumber parse(String versionString) { + if (versionString == null || versionString.length() == 0) { + return UNKNOWN; + } + Scanner scanner = new Scanner(versionString); + + int major = 0; + int minor = 0; + int micro = 0; + + if (!scanner.hasDigit()) { + return UNKNOWN; + } + major = scanner.scanDigit(); + if (scanner.isSeparatorAndDigit()) { + scanner.skipSeparator(); + minor = scanner.scanDigit(); + if (scanner.isSeparatorAndDigit()) { + scanner.skipSeparator(); + micro = scanner.scanDigit(); + } + } + + if (scanner.isEnd()) { + return new VersionNumber(major, minor, micro); + } + + return UNKNOWN; + } + + private static class Scanner { + int pos; + final String str; + + private Scanner(String string) { + this.str = string; + } + + boolean hasDigit() { + return pos < str.length() && Character.isDigit(str.charAt(pos)); + } + + boolean isSeparatorAndDigit() { + return pos < str.length() - 1 && isSeparator() && Character.isDigit(str.charAt(pos + 1)); + } + + private boolean isSeparator() { + return str.charAt(pos) == '.'; + } + + int scanDigit() { + int start = pos; + while (hasDigit()) { + pos++; + } + return Integer.parseInt(str.substring(start, pos)); + } + + public boolean isEnd() { + return pos == str.length(); + } + + public void skipSeparator() { + pos++; + } + } +} diff --git a/src/test/groovy/com/avast/gradle/dockercompose/util/VersionNumberTest.groovy b/src/test/groovy/com/avast/gradle/dockercompose/util/VersionNumberTest.groovy new file mode 100644 index 0000000..2d996cd --- /dev/null +++ b/src/test/groovy/com/avast/gradle/dockercompose/util/VersionNumberTest.groovy @@ -0,0 +1,30 @@ +package com.avast.gradle.dockercompose.util + +import spock.lang.Specification +import spock.lang.Unroll + +import static com.avast.gradle.dockercompose.util.VersionNumber.parse + +class VersionNumberTest extends Specification { + + @Unroll + def "can compare version #a and #b"() { + expect: + parse(a) <=> parse(b) == expected + where: + a | b | expected + "0.0.1" | "0.0.1" | 0 + "0.0.1" | "0.0.2" | -1 + "0.0.2" | "0.0.1" | 1 + "0.1.0" | "0.1.0" | 0 + "0.1.0" | "0.1.0" | 0 + "1.1.1" | "1.1.1" | 0 + "1.1.0" | "1.2.0" | -1 + "1.28.0" | "1.16.0" | 1 + } + + def "handles non parseable versions as UNKNOWN"() { + expect: + parse("SomeInvalid") == VersionNumber.UNKNOWN + } +}