Skip to content

Commit be5d129

Browse files
authored
Replace grgit and bump version (#5)
* Bump version and add settings plugin for missing toolchain download * Replace grgit with own implementation
1 parent d9c7f44 commit be5d129

File tree

6 files changed

+103
-14
lines changed

6 files changed

+103
-14
lines changed

kotlin-preprocessors/build.gradle.kts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
import org.ajoberstar.grgit.Commit
2-
import org.ajoberstar.grgit.Grgit
1+
import versioning.GitTagValueSource
2+
import versioning.GitHashValueSource
33

44
plugins {
55
kotlin("jvm")
66
`kotlin-dsl`
7-
id("org.ajoberstar.grgit") version "5.3.2"
87
id("com.utopia-rise.maven-central-publish")
98
id("com.gradle.plugin-publish") version "1.2.0"
109
`java-gradle-plugin`
1110
}
1211

13-
val baseVersion = "0.1.4"
12+
val baseVersion = "0.2.1"
1413

15-
val grgit = Grgit.open(mapOf("currentDir" to project.rootDir))
14+
val exactTag = providers.of(GitTagValueSource::class) {
15+
parameters.workDir.set(layout.projectDirectory)
16+
}
17+
18+
val shortHash = providers.of(GitHashValueSource::class) {
19+
parameters.workDir.set(layout.projectDirectory)
20+
parameters.abbrev.set(7)
21+
}
1622

17-
val currentCommit: Commit = grgit.head()
18-
// check if the current commit is tagged
19-
var tagOnCurrentCommit = grgit.tag.list().firstOrNull { tag -> tag.commit.id == currentCommit.id }
20-
var releaseMode = tagOnCurrentCommit != null
23+
logger.lifecycle("Current tag is: ${exactTag.orNull}")
24+
logger.lifecycle("Current commit hash is: ${shortHash.orNull}")
2125

22-
version = if (!releaseMode) {
23-
"$baseVersion-${currentCommit.abbreviatedId}-SNAPSHOT"
24-
} else {
25-
requireNotNull(tagOnCurrentCommit).name
26+
val tagValue = exactTag.get()
27+
version = tagValue.ifBlank {
28+
"$baseVersion-${shortHash.get()}-SNAPSHOT"
2629
}
2730

2831
group = "com.utopia-rise"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package versioning
2+
3+
import org.gradle.api.GradleException
4+
import org.gradle.api.file.DirectoryProperty
5+
import org.gradle.api.provider.Property
6+
import org.gradle.api.provider.ValueSourceParameters
7+
import org.gradle.api.tasks.Input
8+
import org.gradle.api.tasks.InputDirectory
9+
import org.gradle.api.tasks.PathSensitive
10+
import org.gradle.api.tasks.PathSensitivity
11+
12+
abstract class GitHashValueSource : GitValueSource<String, GitHashValueSource.Params>() {
13+
interface Params : ValueSourceParameters {
14+
@get:InputDirectory
15+
@get:PathSensitive(PathSensitivity.RELATIVE)
16+
val workDir: DirectoryProperty
17+
18+
@get:Input
19+
val abbrev: Property<Int>
20+
}
21+
22+
override fun obtain(): String {
23+
val workDir = parameters.workDir.asFile.get()
24+
val abbrev = parameters.abbrev.orNull ?: 7
25+
26+
val shortHead = runGitCommand(workDir, "rev-parse", "--short=$abbrev", "HEAD")
27+
if (shortHead.exitCode == 0) {
28+
val hash = shortHead.output.trim()
29+
if (hash.matches(Regex("[0-9a-fA-F]{4,40}"))) return hash
30+
}
31+
throw GradleException("Failed to obtain short git hash (exit=${shortHead.exitCode}, out=${shortHead.output.trim()})")
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package versioning
2+
3+
import org.gradle.api.file.DirectoryProperty
4+
import org.gradle.api.provider.ValueSourceParameters
5+
import org.gradle.api.tasks.InputDirectory
6+
import org.gradle.api.tasks.PathSensitive
7+
import org.gradle.api.tasks.PathSensitivity
8+
9+
abstract class GitTagValueSource : GitValueSource<String, GitTagValueSource.Params>() {
10+
interface Params : ValueSourceParameters {
11+
@get:InputDirectory
12+
@get:PathSensitive(PathSensitivity.RELATIVE)
13+
val workDir: DirectoryProperty
14+
}
15+
16+
override fun obtain(): String {
17+
val workDir = parameters.workDir.asFile.get()
18+
19+
val exactTag = runGitCommand(workDir, "describe", "--tags", "--exact-match")
20+
return if (exactTag.exitCode == 0) {
21+
exactTag.output.trim()
22+
} else {
23+
""
24+
}
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package versioning
2+
3+
import org.gradle.api.provider.ValueSource
4+
import org.gradle.api.provider.ValueSourceParameters
5+
import java.io.File
6+
7+
abstract class GitValueSource<T, P : ValueSourceParameters> : ValueSource<T, P> {
8+
protected fun runGitCommand(workingDir: File, vararg args: String): Result {
9+
return try {
10+
val pb = ProcessBuilder(listOf("git") + args.toList())
11+
pb.directory(workingDir)
12+
pb.redirectErrorStream(true)
13+
val proc = pb.start()
14+
val output = proc.inputStream.bufferedReader().readText()
15+
val exit = proc.waitFor()
16+
Result(exit, output)
17+
} catch (e: Exception) {
18+
Result(1, e.message ?: e::class.java.name)
19+
}
20+
}
21+
22+
protected data class Result(val exitCode: Int, val output: String)
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11

22
rootProject.name = "kotlin-preprocessors"
33

4+
plugins {
5+
// downloads missing jdk for `jvmToolchain` see: https://docs.gradle.org/current/userguide/toolchains.html#sub:download_repositories
6+
id("org.gradle.toolchains.foojay-resolver-convention") version("1.0.0") // https://github.com/gradle/foojay-toolchains/tags
7+
}

kotlin-preprocessors/src/main/kotlin/com/utopiarise/kotlin/preprocessors/gradle/PreProcessorPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PreProcessorPlugin : Plugin<Project> {
7575
description = "Generate definitions for kotlin as constants."
7676
}
7777

78-
val generatedKotlinDefinitionsDirectory = project.buildDir.resolve("definitions")
78+
val generatedKotlinDefinitionsDirectory = project.layout.buildDirectory.asFile.get().resolve("definitions")
7979

8080
project
8181
.kotlinExtension

0 commit comments

Comments
 (0)