Skip to content

Commit b7e1e46

Browse files
author
Kosh
committed
create versioning plugin
1 parent fa049e9 commit b7e1e46

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ buildscript {
1515
plugins {
1616
id(AppPlugins.DOKKA) version DependenciesVersion.DOKKA_VERSION
1717
id(AppPlugins.PUBLISH) version DependenciesVersion.PUBLISH_VERSION
18+
id(AppPlugins.GITHUB_RELEASE) version DependenciesVersion.GITHUB_RELEASE_VERSION
19+
}
20+
21+
apply {
22+
plugin<com.github.k0shk0sh.plugins.LibVersionPlugin>()
1823
}
1924

2025
subprojects {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import groovy.json.JsonOutput
2+
import groovy.json.JsonSlurper
3+
import java.io.File
4+
5+
data class LibVersion(
6+
val major: Int,
7+
val minor: Int,
8+
val patch: Int,
9+
)
10+
11+
fun LibVersion.increaseMajor() = this.copy(major = major + 1, minor = 0, patch = 0)
12+
fun LibVersion.increaseMinor() = this.copy(minor = minor + 1)
13+
fun LibVersion.increasePatch() = this.copy(minor = patch + 1)
14+
fun LibVersion.toMap(): Map<String, Int> = mapOf(
15+
"major" to major, "minor" to minor, "patch" to patch,
16+
)
17+
18+
object LibVersionProvider {
19+
private const val versionFile = "version.json"
20+
21+
@Suppress("unchecked_cast")
22+
fun versionFile(): LibVersion {
23+
val json = JsonSlurper()
24+
val map = json.parse(File(versionFile)) as Map<String, Any>
25+
return LibVersion(
26+
major = map["major"] as Int,
27+
minor = map["minor"] as Int,
28+
patch = map["patch"] as Int,
29+
)
30+
}
31+
32+
fun saveVersionFile(version: LibVersion) {
33+
val jsonData = JsonOutput.toJson(version.toMap())
34+
File(versionFile).writeText(JsonOutput.prettyPrint(jsonData))
35+
}
36+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.k0shk0sh.plugins
2+
3+
import org.gradle.api.*
4+
import LibVersion
5+
import LibVersionProvider
6+
import increaseMajor
7+
import increaseMinor
8+
import increasePatch
9+
10+
class LibVersionPlugin : Plugin<Project> {
11+
override fun apply(target: Project) {
12+
target.afterEvaluate {
13+
val libFile = LibVersionProvider.versionFile()
14+
setProjectVersion(target, libFile)
15+
tasks.create("nextMajor", nextMajor())
16+
tasks.create("nextMinor", nextMinor())
17+
tasks.create("nextPatch", nextPatch())
18+
}
19+
}
20+
21+
private fun nextMajor(): (Task).() -> Unit = {
22+
group = "libVersion"
23+
description = "updating major version"
24+
actions = listOf(
25+
object : Action<Task> {
26+
override fun execute(t: Task) {
27+
val nextMajor = LibVersionProvider.versionFile().increaseMajor()
28+
LibVersionProvider.saveVersionFile(nextMajor)
29+
setProjectVersion(t.project, nextMajor)
30+
}
31+
},
32+
)
33+
}
34+
35+
private fun nextMinor(): (Task).() -> Unit = {
36+
group = "libVersion"
37+
description = "updating minor version"
38+
actions = listOf(
39+
object : Action<Task> {
40+
override fun execute(t: Task) {
41+
val nextMinor = LibVersionProvider.versionFile().increaseMinor()
42+
LibVersionProvider.saveVersionFile(nextMinor)
43+
setProjectVersion(t.project, nextMinor)
44+
}
45+
},
46+
)
47+
}
48+
49+
private fun nextPatch(): (Task).() -> Unit = {
50+
group = "libVersion"
51+
description = "updating patch version"
52+
actions = listOf(
53+
object : Action<Task> {
54+
override fun execute(t: Task) {
55+
val nextPatch = LibVersionProvider.versionFile().increasePatch()
56+
LibVersionProvider.saveVersionFile(nextPatch)
57+
setProjectVersion(t.project, nextPatch)
58+
}
59+
},
60+
)
61+
}
62+
63+
private fun setProjectVersion(
64+
project: Project,
65+
libFile: LibVersion,
66+
) {
67+
project.version = "${libFile.major}.${libFile.minor}.${libFile.patch}"
68+
}
69+
70+
}

buildSrc/src/main/kotlin/configs.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ object AppPlugins {
1616
const val KOTLIN_PARCELIZE = "kotlin-parcelize"
1717
const val DOKKA = "org.jetbrains.dokka"
1818
const val PUBLISH = "com.vanniktech.maven.publish"
19+
const val GITHUB_RELEASE = "com.github.breadmoirai.github-release"
1920
const val AGP = "com.android.tools.build:gradle:${DependenciesVersion.AGP_VERSION}"
2021
const val KGP = "org.jetbrains.kotlin:kotlin-gradle-plugin:${DependenciesVersion.KGP_VERSION}"
2122
}
@@ -35,6 +36,7 @@ object DependenciesVersion {
3536
const val MOCKITO_VERSION = "3.2.0"
3637
const val DOKKA_VERSION = "1.5.0"
3738
const val PUBLISH_VERSION = "0.18.0"
39+
const val GITHUB_RELEASE_VERSION = "2.2.12"
3840
}
3941

4042
object ComposeDependencies {

version.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"major": 0,
3+
"minor": 0,
4+
"patch": 0
5+
}

0 commit comments

Comments
 (0)