Skip to content

Commit

Permalink
Merge pull request #76 from NixOS/fix-version-bump
Browse files Browse the repository at this point in the history
Fix :bumpVersion with Gradle Configuration Cache
  • Loading branch information
JojOatXGME authored Mar 29, 2024
2 parents d99c181 + 2d841c4 commit 0b49c52
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
51 changes: 51 additions & 0 deletions gradle/plugins/src/main/kotlin/ChangePropertyTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import org.gradle.api.DefaultTask
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.UntrackedTask
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

/**
* Replaces the value of [propertyName] in the given [propertiesFile].
* The old value given by [oldValue] is replaced by the value given by [newValue].
*/
@UntrackedTask(because = "Changes project files in-place")
abstract class ChangePropertyTask @Inject constructor(
objects: ObjectFactory,
layout: ProjectLayout,
) : DefaultTask() {

@get:Input
val oldValue = objects.property<String>()

@get:Input
val newValue = objects.property<String>()

@get:Input
val propertyName = objects.property<String>()

@get:OutputFile
val propertiesFile = objects.fileProperty()
.convention(layout.projectDirectory.file("gradle.properties"))

@TaskAction
fun run() {
val escapedName = Regex.escape(propertyName.get())
val escapedOldValue = Regex.escape(oldValue.get())
val escapedReplacement = Regex.escapeReplacement(newValue.get())

val file = propertiesFile.get().asFile
file.writeText(
file.readText().replace(
Regex(
"^(\\s*$escapedName\\s*=\\s*)$escapedOldValue(\\s*)$",
RegexOption.MULTILINE,
),
"$1${escapedReplacement}$2",
)
)
}
}
18 changes: 18 additions & 0 deletions gradle/plugins/src/main/kotlin/PluginVersions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.gradle.api.GradleException
import java.util.regex.Pattern

object PluginVersions {
/**
* Increments the last integer within the given string.
*/
@JvmStatic
fun increment(previous: String): String {
val matcher = Pattern.compile("(.*\\D)(\\d+)(\\D*)").matcher(previous)
if (matcher.matches()) {
val incrementedNumber = Integer.parseInt(matcher.group(2)) + 1
return matcher.group(1) + incrementedNumber + matcher.group(3)
} else {
throw GradleException("Unsupported version: $previous")
}
}
}
44 changes: 4 additions & 40 deletions gradle/plugins/src/main/kotlin/local.bump-version.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import java.util.regex.Pattern

val GRADLE_PROPERTIES = "gradle.properties"
val VERSION_PROPERTY = "pluginVersion"

task("bumpVersion") {
tasks.register<ChangePropertyTask>("bumpVersion") {
description = "Bumps the version of the project"
oldValue = providers.gradleProperty(VERSION_PROPERTY)
newValue = oldValue.map { PluginVersions.increment(it) }
propertyName = VERSION_PROPERTY
dependsOn("patchChangelog")
outputs.upToDateWhen { false }

doLast {
val prevVersion = project.property(VERSION_PROPERTY) as String
val nextVersion = incrementVersion(prevVersion)
replaceInProperties(prevVersion, nextVersion)
}
}

tasks.named("patchChangelog") {
Expand All @@ -25,32 +18,3 @@ tasks.named("patchChangelog") {
}
}
}

/**
* Replaces the [prevVersion] with [nextVersion] for the `pluginVersion` property in `gradle.properties`.
*/
fun replaceInProperties(prevVersion: String, nextVersion: String): Unit {
val pProperty = Regex.escape(VERSION_PROPERTY)
val pVersion = Regex.escape(prevVersion)
val rVersion = Regex.escapeReplacement(nextVersion)

val file = file(GRADLE_PROPERTIES)
val previousContent = file.readText()
file.writeText(previousContent.replace(
Regex("^(\\s*$pProperty\\s*=\\s*)$pVersion(\\s*)$", RegexOption.MULTILINE),
"$1${rVersion}$2"))
}

/**
* Increments the last integer within the given string.
*/
fun incrementVersion(previous: String): String {
val matcher = Pattern.compile("(.*[^\\d])(\\d+)([^\\d]*)").matcher(previous)
if (matcher.matches()) {
val incrementedNumber = Integer.parseInt(matcher.group(2)) + 1
return matcher.group(1) + incrementedNumber + matcher.group(3)
}
else {
throw GradleException("Unsupported version: " + previous)
}
}

0 comments on commit 0b49c52

Please sign in to comment.