Skip to content

Commit fad3d52

Browse files
committed
Add release gradle task
1 parent 283d110 commit fad3d52

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ To run with a specific version you can supply the app version as system properti
1717
Releases are created by CI and are published to GitHub releases. There are executables for Windows, Mac and Linux
1818
created. To create a new release just push a tag in the form of `vmajor.minor.patch` (e.g. `v1.0.0`) to GitHub. This
1919
will start the CI. After a few minutes the artifacts can be downloaded
20-
from [here](https://datepollsystems.github.io/WaiterRobot-Desktop/download.html) (or
21-
[GitHub Releases](https://github.com/DatepollSystems/WaiterRobot-Desktop/releases)).
20+
from [here](https://datepollsystems.github.io/WaiterRobot-Desktop/download.html) (
21+
or[GitHub Releases](https://github.com/DatepollSystems/WaiterRobot-Desktop/releases)).
22+
23+
For convenience there is also a gradle task to start a release.
24+
25+
```sh
26+
./gradlew release
27+
```
28+
29+
You will be asked if you want to increase `major`, `minor` or `patch`.
30+
You can also supply a specific version by using the `v` parameter `./gradlew release -Pv=1.2.3`.
2231

2332
## Recommendations
2433

build.gradle.kts

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io.gitlab.arturbosch.detekt.Detekt
22
import io.gitlab.arturbosch.detekt.report.ReportMergeTask
33
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4+
import java.io.ByteArrayOutputStream
45

56
plugins {
67
val kotlinVersion = "1.9.21"
@@ -40,8 +41,8 @@ dependencies {
4041
implementation("io.ktor:ktor-client-websockets:$ktorVersion")
4142

4243
val mokoMvvmVersion = "0.16.1"
43-
implementation("dev.icerock.moko:mvvm-core:${mokoMvvmVersion}")
44-
implementation("dev.icerock.moko:mvvm-compose:${mokoMvvmVersion}")
44+
implementation("dev.icerock.moko:mvvm-core:$mokoMvvmVersion")
45+
implementation("dev.icerock.moko:mvvm-compose:$mokoMvvmVersion")
4546

4647
implementation("io.insert-koin:koin-core:3.5.3")
4748
implementation("io.insert-koin:koin-compose:1.1.2")
@@ -112,3 +113,106 @@ configurations.all {
112113
attribute(Attribute.of("ui", String::class.java), "awt")
113114
}
114115
}
116+
117+
tasks.register("release") {
118+
val versionParam = findProperty("v")?.toString()
119+
120+
doLast {
121+
val versionTag = "v" + getNewVersion(versionParam)
122+
123+
println()
124+
println("Creating git tag $versionTag")
125+
exec {
126+
commandLine("git", "tag", versionTag)
127+
}
128+
129+
println("Push git tag $versionTag to origin")
130+
exec {
131+
commandLine("git", "push", "origin", versionTag)
132+
}
133+
}
134+
}
135+
136+
fun getNewVersion(version: String?): VersionNumber {
137+
val lastTagVersion = getLastTag()
138+
139+
val newVersion = if (version != null) {
140+
VersionNumber.fromString(version)
141+
} else {
142+
println(
143+
"""
144+
The latest version tag is: $lastTagVersion
145+
What do you want to increase?
146+
1): Major
147+
2): Minor
148+
3): Patch (default)
149+
""".trimIndent()
150+
)
151+
152+
when (readln()) {
153+
"1" -> lastTagVersion.nextMajor()
154+
"2" -> lastTagVersion.nextMinor()
155+
else -> lastTagVersion.nextPatch()
156+
}
157+
}
158+
159+
require(newVersion > lastTagVersion) {
160+
"New version ($newVersion) must be grater than the latest version ($lastTagVersion)."
161+
}
162+
163+
return newVersion
164+
}
165+
166+
fun getLastTag(): VersionNumber {
167+
// Fetch all the remote tags
168+
exec {
169+
commandLine("git", "fetch", "--tags")
170+
}
171+
172+
// Capture the names of all tags
173+
val osAllTags = ByteArrayOutputStream()
174+
exec {
175+
commandLine("git", "tag", "-l")
176+
standardOutput = osAllTags
177+
}
178+
val allTags: List<String> = osAllTags.toString(Charsets.UTF_8)
179+
.trim()
180+
.split("\n")
181+
.filter { it.matches(Regex("""v\d+\.\d+\.\d+""")) }
182+
.map { it.removePrefix("v") }
183+
184+
return VersionNumber.fromString(allTags.last())
185+
}
186+
187+
data class VersionNumber(val major: Int, val minor: Int, val patch: Int) {
188+
override fun toString(): String = "$major.$minor.$patch"
189+
190+
fun nextMajor() = VersionNumber(major + 1, 0, 0)
191+
192+
fun nextMinor() = VersionNumber(major, minor + 1, 0)
193+
194+
fun nextPatch() = VersionNumber(major, minor, patch + 1)
195+
196+
operator fun compareTo(other: VersionNumber): Int {
197+
return when {
198+
this.major != other.major -> this.major.compareTo(other.major)
199+
this.minor != other.minor -> this.minor.compareTo(other.minor)
200+
else -> this.patch.compareTo(other.patch)
201+
}
202+
}
203+
204+
companion object {
205+
fun fromString(version: String): VersionNumber {
206+
val split = version.removePrefix("v").split('.')
207+
require(split.count() == 3) {
208+
"The provided version '$version' is not valid. It must follow the pattern of x.y.z (e.g. 1.2.3)"
209+
}
210+
211+
return VersionNumber(
212+
major = split[0].toInt(),
213+
minor = split[1].toInt(),
214+
patch = split[2].toInt()
215+
)
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)