-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/groovy/com/avast/gradle/dockercompose/util/VersionNumber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/groovy/com/avast/gradle/dockercompose/util/VersionNumberTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |