diff --git a/gradle/plugins/src/main/kotlin/ChangePropertyTask.kt b/gradle/plugins/src/main/kotlin/ChangePropertyTask.kt new file mode 100644 index 00000000..7a400f08 --- /dev/null +++ b/gradle/plugins/src/main/kotlin/ChangePropertyTask.kt @@ -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() + + @get:Input + val newValue = objects.property() + + @get:Input + val propertyName = objects.property() + + @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", + ) + ) + } +} diff --git a/gradle/plugins/src/main/kotlin/PluginVersions.kt b/gradle/plugins/src/main/kotlin/PluginVersions.kt new file mode 100644 index 00000000..b81e35ff --- /dev/null +++ b/gradle/plugins/src/main/kotlin/PluginVersions.kt @@ -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") + } + } +} diff --git a/gradle/plugins/src/main/kotlin/local.bump-version.gradle.kts b/gradle/plugins/src/main/kotlin/local.bump-version.gradle.kts index 122ff1c1..4bca77f0 100644 --- a/gradle/plugins/src/main/kotlin/local.bump-version.gradle.kts +++ b/gradle/plugins/src/main/kotlin/local.bump-version.gradle.kts @@ -1,18 +1,11 @@ -import java.util.regex.Pattern - -val GRADLE_PROPERTIES = "gradle.properties" val VERSION_PROPERTY = "pluginVersion" -task("bumpVersion") { +tasks.register("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") { @@ -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) - } -}