Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions kotlin-preprocessors/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import org.ajoberstar.grgit.Commit
import org.ajoberstar.grgit.Grgit
import versioning.GitTagValueSource
import versioning.GitHashValueSource

plugins {
kotlin("jvm")
`kotlin-dsl`
id("org.ajoberstar.grgit") version "5.3.2"
id("com.utopia-rise.maven-central-publish")
id("com.gradle.plugin-publish") version "1.2.0"
`java-gradle-plugin`
}

val baseVersion = "0.1.4"
val baseVersion = "0.2.1"

val grgit = Grgit.open(mapOf("currentDir" to project.rootDir))
val exactTag = providers.of(GitTagValueSource::class) {
parameters.workDir.set(layout.projectDirectory)
}

val shortHash = providers.of(GitHashValueSource::class) {
parameters.workDir.set(layout.projectDirectory)
parameters.abbrev.set(7)
}

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

version = if (!releaseMode) {
"$baseVersion-${currentCommit.abbreviatedId}-SNAPSHOT"
} else {
requireNotNull(tagOnCurrentCommit).name
val tagValue = exactTag.get()
version = tagValue.ifBlank {
"$baseVersion-${shortHash.get()}-SNAPSHOT"
}

group = "com.utopia-rise"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package versioning

import org.gradle.api.GradleException
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity

abstract class GitHashValueSource : GitValueSource<String, GitHashValueSource.Params>() {
interface Params : ValueSourceParameters {
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
val workDir: DirectoryProperty

@get:Input
val abbrev: Property<Int>
}

override fun obtain(): String {
val workDir = parameters.workDir.asFile.get()
val abbrev = parameters.abbrev.orNull ?: 7

val shortHead = runGitCommand(workDir, "rev-parse", "--short=$abbrev", "HEAD")
if (shortHead.exitCode == 0) {
val hash = shortHead.output.trim()
if (hash.matches(Regex("[0-9a-fA-F]{4,40}"))) return hash
}
throw GradleException("Failed to obtain short git hash (exit=${shortHead.exitCode}, out=${shortHead.output.trim()})")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package versioning

import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity

abstract class GitTagValueSource : GitValueSource<String, GitTagValueSource.Params>() {
interface Params : ValueSourceParameters {
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
val workDir: DirectoryProperty
}

override fun obtain(): String {
val workDir = parameters.workDir.asFile.get()

val exactTag = runGitCommand(workDir, "describe", "--tags", "--exact-match")
return if (exactTag.exitCode == 0) {
exactTag.output.trim()
} else {
""
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package versioning

import org.gradle.api.provider.ValueSource
import org.gradle.api.provider.ValueSourceParameters
import java.io.File

abstract class GitValueSource<T, P : ValueSourceParameters> : ValueSource<T, P> {
protected fun runGitCommand(workingDir: File, vararg args: String): Result {
return try {
val pb = ProcessBuilder(listOf("git") + args.toList())
pb.directory(workingDir)
pb.redirectErrorStream(true)
val proc = pb.start()
val output = proc.inputStream.bufferedReader().readText()
val exit = proc.waitFor()
Result(exit, output)
} catch (e: Exception) {
Result(1, e.message ?: e::class.java.name)
}
}

protected data class Result(val exitCode: Int, val output: String)
}
4 changes: 4 additions & 0 deletions kotlin-preprocessors/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

rootProject.name = "kotlin-preprocessors"

plugins {
// downloads missing jdk for `jvmToolchain` see: https://docs.gradle.org/current/userguide/toolchains.html#sub:download_repositories
id("org.gradle.toolchains.foojay-resolver-convention") version("1.0.0") // https://github.com/gradle/foojay-toolchains/tags
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PreProcessorPlugin : Plugin<Project> {
description = "Generate definitions for kotlin as constants."
}

val generatedKotlinDefinitionsDirectory = project.buildDir.resolve("definitions")
val generatedKotlinDefinitionsDirectory = project.layout.buildDirectory.asFile.get().resolve("definitions")

project
.kotlinExtension
Expand Down
Loading