From c87f98483a9dfbedc83f8b22a48059d2e1873a52 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Wed, 11 Dec 2024 16:17:06 +0100 Subject: [PATCH] Implement 'UpdateVersionsTask' --- build.gradle.kts | 40 +++--------- buildSrc/src/main/kotlin/UpdateVersionTask.kt | 65 +++++++++++++++++++ 2 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 buildSrc/src/main/kotlin/UpdateVersionTask.kt diff --git a/build.gradle.kts b/build.gradle.kts index fef55e86..1f62dcea 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,43 +1,21 @@ @file:Suppress("UnstableApiUsage") -import kotlin.text.replace - - tasks.maybeCreate("clean").apply { delete(layout.buildDirectory) } -val publishLocally by tasks.registering { - /* Update version used in samples */ - val sampleSettings = fileTree("samples") { +val updateVersions = tasks.register("updateVersions") { + sources = fileTree("samples") { include("**/settings.gradle.kts") - }.files - - val version = project.version.toString() - inputs.property("version", version) - - val fireworkVersion = deps.versions.firework.get() - inputs.property("fireworkVersion", fireworkVersion) + include("**/build.gradle.kts") + } - doLast { - logger.quiet("Found: $sampleSettings") - sampleSettings.forEach { settingsFile -> - val text = settingsFile.readText() - val declaration = """id("org.jetbrains.compose-hot-reload") version""" - val newText = text.lines().map { line -> - if (declaration !in line) return@map line - val indent = line.substringBefore(declaration) - indent + declaration + " \"${version}\"" - }.joinToString("\n") { line -> - val fireworkVersionRegex = Regex(""""2.*-firework\..*"""") - line.replace(fireworkVersionRegex, "\"$fireworkVersion\"") - } + projectVersion = project.version.toString() + kotlinFireworkVersion = deps.versions.firework.get() +} - if (newText != text) { - settingsFile.writeText(newText) - } - } - } +val publishLocally by tasks.registering { + dependsOn(updateVersions) } subprojects { diff --git a/buildSrc/src/main/kotlin/UpdateVersionTask.kt b/buildSrc/src/main/kotlin/UpdateVersionTask.kt new file mode 100644 index 00000000..952cbd39 --- /dev/null +++ b/buildSrc/src/main/kotlin/UpdateVersionTask.kt @@ -0,0 +1,65 @@ +import org.gradle.api.DefaultTask +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.TaskAction + +open class UpdateVersionTask : DefaultTask() { + @get:InputFiles + val sources = project.objects.fileCollection() + + @get:Input + val projectVersion = project.objects.property(String::class.java) + + @get:Input + val kotlinFireworkVersion = project.objects.property(String::class.java) + + @TaskAction + fun updateVersion() { + logger.info("Updating project versions to '${projectVersion.get()}'") + logger.info("Updating kotlin firework versions to '${kotlinFireworkVersion.get()}'") + + val projectVersionRegex = Regex("""hot-reload.*(?\d+\.\d+\.\d+-\w+\.\d+.\d+)""") + val kotlinFireworkRegex = Regex(""""\d+\.\d+\.\d+.*-firework\.\d+"""") + sources.forEach { sourceFile -> + logger.info("Processing ${sourceFile.toURI()}") + + val sourceFileText = sourceFile.readText() + val processedText = sourceFileText + .replaceAll(kotlinFireworkRegex) { "\"${kotlinFireworkVersion.get()}\"" } + .replaceAll(projectVersionRegex) { match -> + val versionGroup = match.groups["version"] ?: error("Missing 'version' group in $match") + val range = IntRange( + start = versionGroup.range.start - match.range.start, + endInclusive = versionGroup.range.endInclusive - match.range.start + ) + match.value.replaceRange(range, projectVersion.get()) + } + + if (sourceFileText != processedText) { + logger.info("Updating ${sourceFile.toURI()}") + sourceFile.writeText(processedText) + } + } + } +} + +internal fun String.replaceAll(regex: Regex, transform: (MatchResult) -> CharSequence): String { + if (this.isEmpty()) return this + val matches = regex.findAll(this) + if (matches.none()) return this + val source = this + + return buildString { + var nextIndex = 0 + + matches.forEach { match -> + appendRange(source, nextIndex, match.range.start) + append(transform(match)) + nextIndex = match.range.endInclusive + 1 + } + + if (nextIndex < source.length) { + appendRange(source, nextIndex, source.length) + } + } +}