import io.github.aloussase.changelog.config.ChangelogPluginExtension
import io.github.aloussase.changelog.config.Config
-import io.github.aloussase.changelog.formatter.ChangelogFormatterFactory
-import io.github.aloussase.changelog.parser.ChangelogParserFactory
import org.gradle.api.Plugin
import org.gradle.api.Project
-import java.io.File
class ChangelogPlugin : Plugin<Project> {
override fun apply(project: Project) {
extension.format.convention("markdown")
extension.fileName.convention("CHANGELOG.md")
- project.tasks.register("changelog") {
- val config = Config.from(extension)
- val parser = ChangelogParserFactory.createParser(config.documentFormat)
- val changelogFile = File(config.fileName)
- if (!changelogFile.exists()) {
- changelogFile.createNewFile()
- }
- val document = changelogFile.readText()
- val changelog = parser.parse(document).getOrThrow()
-
- // TODO: Add commits from current branch here.
-
- val formatter = ChangelogFormatterFactory.create(config.documentFormat)
- changelogFile.writeText(formatter.format(changelog))
+ project.tasks.register("changelog", ChangelogTask::class.java) {
+ it.config = Config.configure(extension)
}
}
}
--- /dev/null
+package io.github.aloussase.changelog
+
+import io.github.aloussase.changelog.config.Config
+import io.github.aloussase.changelog.formatter.ChangelogFormatterFactory
+import io.github.aloussase.changelog.git.GetCurrentBranchCommits
+import io.github.aloussase.changelog.parser.ChangelogParserFactory
+import org.gradle.api.DefaultTask
+import org.gradle.api.tasks.Input
+import org.gradle.api.tasks.TaskAction
+import java.io.File
+
+abstract class ChangelogTask : DefaultTask() {
+
+ @Input
+ lateinit var config: Config
+
+ @TaskAction
+ fun run() {
+ val parser = ChangelogParserFactory.createParser(config.documentFormat)
+ val changelogFile = File(config.fileName)
+ if (!changelogFile.exists()) {
+ changelogFile.createNewFile()
+ }
+ val document = changelogFile.readText()
+ val changelog = parser.parse(document).getOrThrow()
+
+ // TODO: Add commits from current branch here.
+ println(GetCurrentBranchCommits().execute())
+
+ val formatter = ChangelogFormatterFactory.create(config.documentFormat)
+ changelogFile.writeText(formatter.format(changelog))
+ }
+
+}
import io.github.aloussase.changelog.formats.DocumentFormat
import org.gradle.api.GradleException
-data class Config(
+class Config private constructor(
val fileName: String,
val gitBranch: String,
val documentFormat: DocumentFormat,
) {
+
companion object {
- fun from(extension: ChangelogPluginExtension): Config {
+ fun configure(extension: ChangelogPluginExtension): Config {
val format = DocumentFormat.parse(extension.format.get())
if (format == null) {
throw GradleException("Invalid document format: cannot make a changelog from ${extension.format.get()}")
package io.github.aloussase.changelog.git
-abstract class AbstractGitCommand : GitCommand {
+abstract class AbstractGitCommand<T> : GitCommand<T> {
- override fun execute(): String {
+ abstract fun transform(rawOutput: String): T
+
+ override fun execute(): T {
val runtime = Runtime.getRuntime()
val ary = arrayOf("/bin/sh", "-c", commandLine)
val process = runtime.exec(ary)
process.waitFor()
return process.inputReader().use { br ->
- br.readText().trim()
+ transform(br.readText().trim())
}
}
--- /dev/null
+package io.github.aloussase.changelog.git
+
+class GetCurrentBranchCommits : AbstractGitCommand<List<Pair<String, String>>>() {
+ override val commandLine: String
+ get() = "git log main..HEAD --no-merges --oneline --pretty=format:\"%an|%s\""
+
+ override fun transform(rawOutput: String): List<Pair<String, String>> =
+ rawOutput
+ .split("\n")
+ .map { it.split("|") }
+ .map { Pair(it[0], it[1]) }
+}
package io.github.aloussase.changelog.git
-class GetCurrentReleaseCommand : AbstractGitCommand() {
+class GetCurrentReleaseCommand : AbstractGitCommand<String>() {
override val commandLine: String
get() = "git tag | tail -n1"
+
+ override fun transform(rawOutput: String): String = rawOutput
}
package io.github.aloussase.changelog.git
-interface GitCommand {
+interface GitCommand<T> {
val commandLine: String
- fun execute(): String
+ fun execute(): T
}
val entries = arrayListOf<ChangelogEntry>()
- for (blk in doc.split("\n\n")) {
+ for (blk in doc.trim().split("\n\n")) {
if (DOC_TITLE_REGEX.matches(blk)) continue
val lines = blk.split("\n")