Rewrite: Plugin creates Version-class

This commit is contained in:
Hartmut Bauer committed 2026-01-10 21:43:42 +01:00
1 parent 0ec2193cc7
commit e9e28d2ce7
9 files changed
+274 -130

No files matched your search

@@ -1,12 +1,134 @@
package net.keinekekse.version
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.TaskAction
import io.github.z4kn4fein.semver.Version
import io.github.z4kn4fein.semver.withoutSuffixes
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import java.util.*
abstract class AddVersionTask : Copy() {
abstract class AddVersionTask : DefaultTask() {
// 1. Define what triggers the task (Source Files)
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val sourceFiles: ConfigurableFileCollection
// 2. Define the output (The properties file)
@get:OutputFile
abstract val buildNumberFile: RegularFileProperty
@get:OutputFile
abstract val versionClassFile: RegularFileProperty
@get:Input
abstract val projectVersion: Property<String>
@get:Input
abstract val projectName: Property<String>
@get:Input
abstract val projectDescription: Property<String>
@get:Input
abstract val displayNameExtra: Property<String>
@get:Input
abstract val copyrightExtra: Property<String>
@get:Input
abstract val versionPackageName: Property<String>
@TaskAction
fun updateVersion() {
// action is defined in plugin
fun execute() {
println("execute task: incrementBuildNumber")
val buildNumber = incrementAndGetBuildNumber()
writeClassFile(buildNumber)
}
private fun writeClassFile(buildNumber: String) {
val ver = getVersionInfo(buildNumber)
val file = versionClassFile.get().asFile
file.parentFile.mkdirs()
file.writeText(
"""
package ${versionPackageName.get()}
object VERSION {
const val version = "${ver.version}"
const val fullVersion = "${ver.fullVersion}"
const val major = ${ver.major}
const val minor = ${ver.minor}
const val patch = ${ver.patch}
const val preRelease = "${ver.preRelease}"
const val buildMetadata = "${ver.buildMetadata}"
const val buildDate = "${ver.buildDate}"
const val displayName = "${ver.displayName}"
const val copyright = "${ver.copyright}"
const val name = "${ver.name}"
const val description = "${ver.description}"
}
""".trimIndent()
)
}
private fun getVersionInfo(buildNumber: String): VersionInfo {
val version =
if (Version.parse(projectVersion.get(), strict = false).buildMetadata.isNullOrEmpty())
Version.parse(projectVersion.get() + "+build.$buildNumber", strict = false)
else Version.parse(projectVersion.get(), strict = false)
println("Source files changed. Setting version to $version")
return VersionInfo(
major = version.major,
minor = version.minor,
patch = version.patch,
preRelease = version.preRelease ?: "",
buildMetadata = version.buildMetadata ?: "",
fullVersion = version.toString(),
version = if (version.isPreRelease) version.toString() else
version.withoutSuffixes().toString(),
buildDate = java.time.Instant.now().toString(),
displayName = displayNameExtra.get(),
copyright = copyrightExtra.get(),
name = projectName.get(),
description = projectDescription.get() ?: ""
)
}
private fun incrementAndGetBuildNumber(): String {
val file = buildNumberFile.get().asFile
val props = Properties()
if (file.exists()) {
file.inputStream().use { props.load(it) }
}
val current = props.getProperty("build.number")?.toLongOrNull() ?: 0
val actual = (current + 1).toString()
props.setProperty("build.number", actual)
file.outputStream().use {
props.store(it, "auto generated")
}
return actual
}
data class VersionInfo(
val version: String,
val fullVersion: String,
val major: Int,
val minor: Int,
val patch: Int,
val preRelease: String,
val buildMetadata: String,
val buildDate: String,
val displayName: String,
val copyright: String,
val name: String,
val description: String,
)
}