Skip to content

Commit

Permalink
chore: deprecated Gradle internal VersionNumber not used
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
breskeby authored Mar 27, 2023
1 parent 542e72d commit a5d0e52
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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
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<Parameters>, AutoCloseable {
static interface Parameters extends BuildServiceParameters {
abstract DirectoryProperty getProjectDirectory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
119 changes: 119 additions & 0 deletions src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java
Original file line number Diff line number Diff line change
@@ -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<VersionNumber> {
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++;
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit a5d0e52

Please sign in to comment.