Skip to content

Commit 1daa716

Browse files
committed
Update BuildVersion plugin
1 parent c8d53c2 commit 1daa716

File tree

5 files changed

+143
-41
lines changed

5 files changed

+143
-41
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
1.0.1
1+
1.0.3
2+
-----
3+
*2020-01-31*
4+
- Update dependencies
5+
- Update `BuildVersion` plugin
6+
7+
1.0.2
28
-----
39
*2020-01-31*
410
- Fix `Proguard` rules
Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import java.io.File
22
import java.lang.IllegalArgumentException
33

4-
class BuildVersion(
5-
private val major: Int = 1,
6-
private val minor: Int = 0,
7-
private val patch: Int = 0,
8-
private val preRelease: String? = null,
9-
private val buildMetadata: String? = null
10-
) {
4+
class BuildVersion(private val versionFile: File) {
115
companion object {
12-
136
private val VERSION_PATTERN = Regex(
147
"""(0|[1-9]\d*)?(?:\.)?(0|[1-9]\d*)?(?:\.)?(0|[1-9]\d*)?(?:-([\dA-z\-]+(?:\.[\dA-z\-]+)*))?(?:\+([\dA-z\-]+(?:\.[\dA-z\-]+)*))?"""
158
)
@@ -21,30 +14,33 @@ class BuildVersion(
2114
private val BUILD_METADATA_PATTERN = Regex(
2215
"""[\dA-z\-]+(?:\.[\dA-z\-]+)*"""
2316
)
17+
}
18+
19+
private var major: Int
20+
21+
private var minor: Int
2422

25-
@JvmStatic
26-
fun parse(versionText: String): BuildVersion {
23+
private var patch: Int
24+
25+
private var preRelease: String?
26+
27+
private var buildMetadata: String?
28+
29+
init {
30+
if (versionFile.exists() && versionFile.canRead()) {
31+
val versionText = versionFile.readText()
2732
val result = VERSION_PATTERN.matchEntire(versionText)
2833
?: throw IllegalArgumentException("Unable to parse build version: $versionText")
29-
return BuildVersion(
30-
major = result.groupValues[1].toInt(),
31-
minor = result.groupValues[2].toInt(),
32-
patch = result.groupValues[3].toInt(),
33-
preRelease = if (result.groupValues[4].isEmpty()) null else result.groupValues[4],
34-
buildMetadata = if (result.groupValues[5].isEmpty()) null else result.groupValues[5]
35-
)
36-
}
3734

38-
@JvmStatic
39-
fun parse(versionFile: File): BuildVersion =
40-
if (versionFile.exists() && versionFile.canRead()) {
41-
parse(versionFile.readText())
42-
} else {
43-
throw IllegalArgumentException("Unable to read version file: ${versionFile.path}")
44-
}
45-
}
35+
major = result.groupValues[1].toInt()
36+
minor = result.groupValues[2].toInt()
37+
patch = result.groupValues[3].toInt()
38+
preRelease = if (result.groupValues[4].isEmpty()) null else result.groupValues[4]
39+
buildMetadata = if (result.groupValues[5].isEmpty()) null else result.groupValues[5]
40+
} else {
41+
throw IllegalArgumentException("Unable to read version file: ${versionFile.path}")
42+
}
4643

47-
init {
4844
require(major >= 0) { "Major version must be a positive number" }
4945
require(minor >= 0) { "Minor version must be a positive number" }
5046
require(patch >= 0) { "Patch version must be a positive number" }
@@ -55,21 +51,60 @@ class BuildVersion(
5551
}
5652

5753
buildMetadata?.let {
58-
require(buildMetadata.matches(BUILD_METADATA_PATTERN)) { "Build metadata is not valid" }
54+
require(it.matches(BUILD_METADATA_PATTERN)) { "Build metadata is not valid" }
5955
}
6056
}
6157

62-
val versionCode: Int = major * 10000 + minor * 1000 + patch
58+
val versionCode: Int
59+
get() = major * 10000 + minor * 1000 + patch
6360

64-
val versionName: String = buildString {
65-
append("$major.$minor.$patch")
61+
val versionName: String
62+
get() = buildString {
63+
append("$major.$minor.$patch")
6664

67-
preRelease?.let {
68-
append("-$it")
65+
preRelease?.let {
66+
append("-$it")
67+
}
68+
69+
buildMetadata?.let {
70+
append("+$it")
71+
}
6972
}
7073

71-
buildMetadata?.let {
72-
append("+$it")
74+
// Trim pre-release and build metadata
75+
fun prepareProdRelease() {
76+
preRelease = null
77+
buildMetadata = null
78+
}
79+
80+
fun incrementMajor() {
81+
major++
82+
minor = 0
83+
patch = 0
84+
85+
versionFile.writeText(versionName)
86+
}
87+
88+
fun incrementMinor() {
89+
minor++
90+
patch = 0
91+
92+
if (minor >= 1000) {
93+
major++
94+
minor = 0
7395
}
96+
97+
versionFile.writeText(versionName)
98+
}
99+
100+
fun incrementPatch() {
101+
patch++
102+
103+
if (patch >= 1000) {
104+
minor++
105+
patch = 0
106+
}
107+
108+
versionFile.writeText(versionName)
74109
}
75110
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.gradle.api.DefaultTask
2+
import org.gradle.api.tasks.Input
3+
import org.gradle.api.tasks.TaskAction
4+
5+
enum class Increment {
6+
MAJOR,
7+
MINOR,
8+
PATCH
9+
}
10+
11+
abstract class IncrementVersion : DefaultTask() {
12+
@get:Input
13+
var prodRelease: Boolean = false
14+
15+
@get:Input
16+
abstract var increment: Increment
17+
18+
@get:Input
19+
abstract var version: BuildVersion
20+
21+
@TaskAction
22+
fun increment() {
23+
println("Incrementing ${increment.name} version...")
24+
25+
val prevVersionCode = version.versionCode
26+
val prevVersionName = version.versionName
27+
28+
if (prodRelease) {
29+
version.prepareProdRelease()
30+
}
31+
32+
when (increment) {
33+
Increment.MAJOR -> {
34+
version.incrementMajor()
35+
}
36+
Increment.MINOR -> {
37+
version.incrementMinor()
38+
}
39+
Increment.PATCH -> {
40+
version.incrementPatch()
41+
}
42+
}
43+
44+
println("$prevVersionName ($prevVersionCode) -> ${version.versionName} (${version.versionCode})")
45+
}
46+
}

logger/build.gradle.kts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313
}
1414

1515
val archivesBaseName = "archivarius-logger"
16-
val buildVersion = BuildVersion.parse(rootProject.file("version"))
16+
val buildVersion = BuildVersion(rootProject.file("version"))
1717

1818
group = "com.github.asherepenko"
1919
version = buildVersion.versionName
@@ -70,14 +70,14 @@ ktlint {
7070
}
7171
}
7272

73-
val archivariusVersion = "1.0.4"
74-
val loggerVersion = "1.0.2"
73+
val archivariusVersion = "1.0.5"
74+
val loggerVersion = "1.0.3"
7575

7676
dependencies {
7777
api("com.github.asherepenko:android-archivarius:$archivariusVersion")
7878
api("com.github.asherepenko:android-logger:$loggerVersion")
7979
implementation(kotlin("stdlib-jdk8", KotlinCompilerVersion.VERSION))
80-
testImplementation("junit:junit:4.12")
80+
testImplementation("junit:junit:4.13")
8181
testImplementation("androidx.test:core:1.2.0")
8282
testImplementation("androidx.test:runner:1.2.0")
8383
testImplementation("androidx.test.ext:junit:1.1.1")
@@ -108,6 +108,21 @@ tasks {
108108
from(android.sourceSets.getByName("main").java.srcDirs)
109109
}
110110

111+
val incrementMajor by registering(IncrementVersion::class) {
112+
increment = Increment.MAJOR
113+
version = buildVersion
114+
}
115+
116+
val incrementMinor by registering(IncrementVersion::class) {
117+
increment = Increment.MINOR
118+
version = buildVersion
119+
}
120+
121+
val incrementPatch by registering(IncrementVersion::class) {
122+
increment = Increment.PATCH
123+
version = buildVersion
124+
}
125+
111126
artifacts {
112127
archives(javadocJar)
113128
archives(sourcesJar)

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.2
1+
1.0.3

0 commit comments

Comments
 (0)