Skip to content

Commit

Permalink
Move build-update-utils to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
octylFractal committed Aug 1, 2024
1 parent 78c0fbe commit 66080bb
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PluginProject(private val buildAndTest: BuildType) : Project({
steps {
step(configuredGradle {
name = "Change to Release Version"
tasks = ":unified-plugin:build-update-utils:changeSnapshotToReleaseVersion"
tasks = ":unified-plugin:changeSnapshotToReleaseVersion"
workingDir = "unified-prototype"
})
step(configuredGradle {
Expand All @@ -64,7 +64,7 @@ class PluginProject(private val buildAndTest: BuildType) : Project({
})
step(configuredGradle {
name = "Change to next Snapshot Version"
tasks = ":unified-plugin:build-update-utils:changeReleaseToNextSnapshotVersion"
tasks = ":unified-plugin:changeReleaseToNextSnapshotVersion"
workingDir = "unified-prototype"
})
script {
Expand Down
9 changes: 9 additions & 0 deletions unified-prototype/unified-plugin/build-logic/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ plugins {
dependencies {
implementation("com.gradle.publish:plugin-publish-plugin:1.2.1")
}

gradlePlugin {
plugins {
create("build-logic.build-update-utils") {
id = "build-logic.build-update-utils"
implementationClass = "buildlogic.BuildUpdateUtilsPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package buildlogic

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.process.ExecOperations
import javax.inject.Inject

open class BuildUpdateUtilsPlugin @Inject constructor(
private val execOperations: ExecOperations
) : Plugin<Project> {

companion object {
private const val CI_UPDATE_TASKS = "CI Update Tasks"
}

override fun apply(target: Project) {
target.run {
val versionFile = rootProject.file("version.txt")

fun getVersionFromFile() = versionFile.readText().trim()

fun writeNewVersion(newVersion: String) = versionFile.writeText(newVersion)

tasks.register("changeSnapshotToReleaseVersion") {
description = "Change the version of the plugin to a release version"
group = CI_UPDATE_TASKS
doLast {
val version = getVersionFromFile()
val newVersion = version.removeSuffix("-SNAPSHOT")
if (version == newVersion) {
throw IllegalStateException("Version is not a snapshot version: $version")
}
writeNewVersion(newVersion)
execOperations.exec {
commandLine("git", "commit", "-m", "Release version $newVersion", versionFile.absolutePath)
}
execOperations.exec {
commandLine("git", "tag", "v${newVersion}", "-m", "Release version $newVersion")
}
}
}

tasks.register("changeReleaseToNextSnapshotVersion") {
description = "Change the version of the plugin to a snapshot version"
group = CI_UPDATE_TASKS
doLast {
val version = getVersionFromFile()
if (!version.matches(Regex("\\d+\\.\\d+\\.\\d+"))) {
throw IllegalStateException("Version is not a release version: $version")
}
val versionParts = version.split(".")
val newVersion = "${versionParts[0]}.${versionParts[1]}.${versionParts[2].toInt() + 1}-SNAPSHOT"
writeNewVersion(newVersion)
execOperations.exec {
commandLine(
"git",
"commit",
"-m",
"Switch to next snapshot version $newVersion",
versionFile.absolutePath
)
}
}
}
}
}
}

This file was deleted.

1 change: 1 addition & 0 deletions unified-prototype/unified-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
kotlin("jvm").version(libs.versions.kotlin).apply(false)
id("build-logic.build-update-utils")
}

version = file("version.txt").readText().trim()
Expand Down

0 comments on commit 66080bb

Please sign in to comment.