Skip to content

Commit 68c724f

Browse files
Copied and trimmed VersionNumber implementation from gradle (#452)
* Copied and trimmed VersionNumber implementation from gradle * Format kotlin code --------- Co-authored-by: Nelson Osacky <nelson@emergetools.com>
1 parent ebae83d commit 68c724f

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

fladle-plugin/src/main/java/com/osacky/flank/gradle/validation/ValidateOptions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.osacky.flank.gradle.validation
22

33
import com.osacky.flank.gradle.FladleConfig
44
import com.osacky.flank.gradle.FlankGradleExtension.Companion.FLANK_VERSION
5-
import org.gradle.util.VersionNumber
65
import kotlin.reflect.full.memberProperties
76

87
fun validateOptionsUsed(
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package com.osacky.flank.gradle.validation
2+
3+
/*
4+
* Copyright 2012 the original author or authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* Copied and trimmed from [org.gradle.util.VersionNumber].
21+
*/
22+
class VersionNumber private constructor(
23+
private val major: Int,
24+
private val minor: Int,
25+
private val micro: Int,
26+
private val patch: Int,
27+
private val qualifier: String?,
28+
private val scheme: AbstractScheme,
29+
) : Comparable<VersionNumber> {
30+
override fun compareTo(other: VersionNumber): Int {
31+
if (major != other.major) {
32+
return major - other.major
33+
}
34+
if (minor != other.minor) {
35+
return minor - other.minor
36+
}
37+
if (micro != other.micro) {
38+
return micro - other.micro
39+
}
40+
if (patch != other.patch) {
41+
return patch - other.patch
42+
}
43+
return qualifier.orEmpty().lowercase()
44+
.compareTo(other.qualifier.orEmpty().lowercase())
45+
}
46+
47+
override fun equals(other: Any?): Boolean {
48+
return other is VersionNumber && compareTo(other) == 0
49+
}
50+
51+
override fun hashCode(): Int {
52+
var result = major
53+
result = 31 * result + minor
54+
result = 31 * result + micro
55+
result = 31 * result + patch
56+
result = 31 * result + qualifier.hashCode()
57+
return result
58+
}
59+
60+
override fun toString(): String {
61+
return scheme.format(this)
62+
}
63+
64+
/**
65+
* Returns the version number scheme.
66+
*/
67+
interface Scheme {
68+
fun parse(versionString: String): VersionNumber
69+
70+
fun format(versionNumber: VersionNumber): String
71+
}
72+
73+
private abstract class AbstractScheme protected constructor(val depth: Int) : Scheme {
74+
override fun parse(versionString: String): VersionNumber {
75+
if (versionString.isEmpty()) {
76+
return UNKNOWN
77+
}
78+
val scanner = Scanner(versionString)
79+
80+
if (!scanner.hasDigit()) {
81+
return UNKNOWN
82+
}
83+
var minor = 0
84+
var micro = 0
85+
var patch = 0
86+
val major = scanner.scanDigit()
87+
if (scanner.isSeparatorAndDigit('.')) {
88+
scanner.skipSeparator()
89+
minor = scanner.scanDigit()
90+
if (scanner.isSeparatorAndDigit('.')) {
91+
scanner.skipSeparator()
92+
micro = scanner.scanDigit()
93+
if (depth > 3 && scanner.isSeparatorAndDigit('.', '_')) {
94+
scanner.skipSeparator()
95+
patch = scanner.scanDigit()
96+
}
97+
}
98+
}
99+
100+
if (scanner.isEnd) {
101+
return VersionNumber(major, minor, micro, patch, null, this)
102+
}
103+
104+
if (scanner.isQualifier) {
105+
scanner.skipSeparator()
106+
return VersionNumber(major, minor, micro, patch, scanner.remainder(), this)
107+
}
108+
109+
return UNKNOWN
110+
}
111+
112+
private class Scanner(val str: String) {
113+
var pos: Int = 0
114+
115+
fun hasDigit(): Boolean {
116+
return pos < str.length && Character.isDigit(str.get(pos))
117+
}
118+
119+
fun isSeparatorAndDigit(vararg separators: Char): Boolean {
120+
return pos < str.length - 1 && oneOf(*separators) && Character.isDigit(str.get(pos + 1))
121+
}
122+
123+
fun oneOf(vararg separators: Char): Boolean {
124+
val current = str.get(pos)
125+
for (separator in separators) {
126+
if (current == separator) {
127+
return true
128+
}
129+
}
130+
return false
131+
}
132+
133+
val isQualifier: Boolean
134+
get() = pos < str.length - 1 && oneOf('.', '-')
135+
136+
fun scanDigit(): Int {
137+
val start = pos
138+
while (hasDigit()) {
139+
pos++
140+
}
141+
return str.substring(start, pos).toInt()
142+
}
143+
144+
val isEnd: Boolean
145+
get() = pos == str.length
146+
147+
fun skipSeparator() {
148+
pos++
149+
}
150+
151+
fun remainder(): String? {
152+
return if (pos == str.length) null else str.substring(pos)
153+
}
154+
}
155+
}
156+
157+
private class DefaultScheme : AbstractScheme(3) {
158+
override fun format(versionNumber: VersionNumber): String {
159+
return String.format(
160+
VERSION_TEMPLATE,
161+
versionNumber.major,
162+
versionNumber.minor,
163+
versionNumber.micro,
164+
if (versionNumber.qualifier == null) "" else "-" + versionNumber.qualifier,
165+
)
166+
}
167+
168+
companion object {
169+
private const val VERSION_TEMPLATE = "%d.%d.%d%s"
170+
}
171+
}
172+
173+
companion object {
174+
private val DEFAULT_SCHEME = DefaultScheme()
175+
val UNKNOWN: VersionNumber = version(0)
176+
177+
@JvmOverloads
178+
fun version(
179+
major: Int,
180+
minor: Int = 0,
181+
): VersionNumber {
182+
return VersionNumber(
183+
major = major,
184+
minor = minor,
185+
micro = 0,
186+
patch = 0,
187+
qualifier = null,
188+
scheme = DEFAULT_SCHEME,
189+
)
190+
}
191+
192+
fun parse(versionString: String): VersionNumber {
193+
return DEFAULT_SCHEME.parse(versionString)
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)