From 20cba48be157d9744d0a9dd9739c097d5b9a1a7a Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 8 Aug 2024 18:59:09 -0600 Subject: [PATCH] Move changelog task to standalone file --- .../wbrawner/releasehelper/ChangelogTask.kt | 39 +++++++++++++++++++ .../releasehelper/ReleaseHelperPlugin.kt | 37 ++---------------- 2 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ChangelogTask.kt diff --git a/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ChangelogTask.kt b/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ChangelogTask.kt new file mode 100644 index 0000000..3f31e56 --- /dev/null +++ b/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ChangelogTask.kt @@ -0,0 +1,39 @@ +package com.wbrawner.releasehelper + +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.model.ObjectFactory +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction +import javax.inject.Inject + +private const val CHANGELOG_PATH = "src/play/play/release-notes/en-US/default.txt" + +abstract class ChangelogTask @Inject constructor(objectFactory: ObjectFactory) : DefaultTask() { + @get:OutputFile + val changelogFile: RegularFileProperty = objectFactory.fileProperty() + + init { + changelogFile.set(project.layout.projectDirectory.file(CHANGELOG_PATH)) + } + + @TaskAction + fun execute() { + val latestTag = "git describe --tags --abbrev=0".execute() + val changelog = "git log --format=\"%B\" ${latestTag.first().trim()}..".execute() + logger.info("Latest tag: $latestTag") + logger.info("Changelog: ${changelog.joinToString("\n")}") + changelogFile.get().asFile.writer().use { writer -> + writer.write( + changelog.joinToString("\n") { it.trim('"') } + ) + } + } + + private fun String.execute(): List = ProcessBuilder() + .command(this.split(" ")) + .redirectOutput(ProcessBuilder.Redirect.PIPE) + .start() + .inputReader() + .readLines() +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ReleaseHelperPlugin.kt b/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ReleaseHelperPlugin.kt index d275b79..82e1a3b 100644 --- a/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ReleaseHelperPlugin.kt +++ b/buildSrc/src/main/kotlin/com/wbrawner/releasehelper/ReleaseHelperPlugin.kt @@ -2,46 +2,15 @@ package com.wbrawner.releasehelper import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.tasks.Exec import org.gradle.kotlin.dsl.extra import org.gradle.kotlin.dsl.provideDelegate -import java.io.ByteArrayOutputStream +import org.gradle.kotlin.dsl.register import java.io.File + class ReleaseHelperPlugin : Plugin { override fun apply(target: Project) { - target.tasks.register("getLatestTag", Exec::class.java) { - val latestTag = ByteArrayOutputStream() - standardOutput = latestTag - commandLine("git describe --tags --abbrev=0".split(" ")) - doLast { - target.project.extra["latestTag"] = latestTag.toString().trim() - logger.info("Latest tag: ${target.project.extra["latestTag"]}") - } - } - - target.tasks.register("changelog") { - val changelogFile = File(target.projectDir, "src/play/play/release-notes/en-US/default.txt") - inputs.property("tag", target.provider { - target.project.extra["latestTag"] - }) - outputs.file(changelogFile) - dependsOn("getLatestTag") - doLast { - val latestTag: String by target.project.extra - val changelog = ByteArrayOutputStream() - target.exec { - standardOutput = changelog - commandLine = "git log --format=\"%B\" ${latestTag.trim()}..".split(" ") - } - changelogFile.writeText( - changelog.toString() - .split("\n") - .mapNotNull { it.trim('"').ifBlank { null } } - .joinToString("\n") { "- $it" } - ) - } - } + target.tasks.register("changelog") target.tasks.register("majorRelease") { dependsOn("changelog", "getLatestTag")