Skip to content

Commit

Permalink
Implement 'UpdateVersionsTask'
Browse files Browse the repository at this point in the history
  • Loading branch information
sellmair committed Dec 11, 2024
1 parent 75316a2 commit c87f984
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
40 changes: 9 additions & 31 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
@file:Suppress("UnstableApiUsage")

import kotlin.text.replace


tasks.maybeCreate<Delete>("clean").apply {
delete(layout.buildDirectory)
}

val publishLocally by tasks.registering {
/* Update version used in samples */
val sampleSettings = fileTree("samples") {
val updateVersions = tasks.register<UpdateVersionTask>("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 {
Expand Down
65 changes: 65 additions & 0 deletions buildSrc/src/main/kotlin/UpdateVersionTask.kt
Original file line number Diff line number Diff line change
@@ -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.*(?<version>\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)
}
}
}

0 comments on commit c87f984

Please sign in to comment.