|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package util |
| 6 | + |
| 7 | +import org.gradle.api.DefaultTask |
| 8 | +import org.gradle.api.GradleException |
| 9 | +import org.gradle.api.Project |
| 10 | +import org.gradle.api.provider.Property |
| 11 | +import org.gradle.api.tasks.InputFile |
| 12 | +import org.gradle.api.tasks.OutputFile |
| 13 | +import org.gradle.api.tasks.TaskAction |
| 14 | +import org.gradle.kotlin.dsl.register |
| 15 | +import java.io.File |
| 16 | +import kotlin.io.path.createDirectories |
| 17 | +import kotlin.io.path.createFile |
| 18 | +import kotlin.io.path.exists |
| 19 | +import kotlin.io.path.readLines |
| 20 | +import kotlin.io.path.writeLines |
| 21 | + |
| 22 | +private val ROOT_CHANGELOG_PATH = File("CHANGELOG.md") |
| 23 | +private val DOCS_CHANGELOG_PATH = File("docs/pages/kotlinx-rpc/topics/changelog.md") |
| 24 | + |
| 25 | +private val PULL_REGEX = "https://github.com/Kotlin/kotlinx-rpc/pull/(\\d+)".toRegex() |
| 26 | +private val COMPARE_REGEX = "https://github.com/Kotlin/kotlinx-rpc/compare/([+.\\w_-]+)".toRegex() |
| 27 | +private val USERNAME_REGEX = "@([\\w_-]+)".toRegex() |
| 28 | +private val WHITESPACE = "\\s+".toRegex() |
| 29 | + |
| 30 | +abstract class UpdateDocsChangelog : DefaultTask() { |
| 31 | + @get:InputFile |
| 32 | + abstract val input: Property<File> |
| 33 | + |
| 34 | + @get:OutputFile |
| 35 | + abstract val output: Property<File> |
| 36 | + |
| 37 | + @TaskAction |
| 38 | + fun update() { |
| 39 | + val inputPath = input.get().toPath() |
| 40 | + val outputPath = output.get().toPath() |
| 41 | + |
| 42 | + if (!inputPath.exists()) { |
| 43 | + throw GradleException("fatal error: input file $inputPath does not exist") |
| 44 | + } |
| 45 | + |
| 46 | + var currentRelease = "" |
| 47 | + val fullChangelogLines = mutableListOf<String>() |
| 48 | + val lines = inputPath.readLines(Charsets.UTF_8).flatMap { line -> |
| 49 | + val updated = line |
| 50 | + .replace(PULL_REGEX) { |
| 51 | + "[#${it.groupValues[1]}](${it.groupValues[0]})" |
| 52 | + } |
| 53 | + .replace(COMPARE_REGEX) { |
| 54 | + "[${it.groupValues[1]}](${it.groupValues[0]})" |
| 55 | + } |
| 56 | + .replace(USERNAME_REGEX) { |
| 57 | + "[${it.groupValues[0]}](https://github.com/${it.groupValues[1]})" |
| 58 | + }.let { |
| 59 | + if (it.startsWith("#")) { |
| 60 | + "#$it" |
| 61 | + } else { |
| 62 | + it |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (updated.startsWith("## ")) { |
| 67 | + currentRelease = updated |
| 68 | + .drop(3) |
| 69 | + .replace(".", "_") |
| 70 | + } |
| 71 | + |
| 72 | + when { |
| 73 | + updated.startsWith("###") -> { |
| 74 | + val name = updated |
| 75 | + .dropWhile { it == '#' } |
| 76 | + .dropLastWhile { !it.isDigit() && !it.isLetter() } |
| 77 | + .trim() |
| 78 | + .replace(WHITESPACE, "_") |
| 79 | + |
| 80 | + listOf("$updated {id=${name}_$currentRelease}") |
| 81 | + } |
| 82 | + |
| 83 | + updated.startsWith("**Full Changelog**:") -> { |
| 84 | + fullChangelogLines.add(updated) |
| 85 | + emptyList() |
| 86 | + } |
| 87 | + |
| 88 | + else -> listOf(updated) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + val result = mutableListOf<String>() |
| 93 | + |
| 94 | + var i = 0 |
| 95 | + var fci = 0 |
| 96 | + while (i < lines.size) { |
| 97 | + val line = lines[i] |
| 98 | + result.add(line) |
| 99 | + |
| 100 | + if (line.startsWith("## ")) { |
| 101 | + result.add(lines[i + 1]) |
| 102 | + result.add("") |
| 103 | + result.add(fullChangelogLines[fci++]) |
| 104 | + i++ |
| 105 | + } |
| 106 | + |
| 107 | + i++ |
| 108 | + } |
| 109 | + |
| 110 | + if (!outputPath.exists()) { |
| 111 | + outputPath.parent.createDirectories() |
| 112 | + outputPath.createFile() |
| 113 | + } |
| 114 | + |
| 115 | + val header = listOf( |
| 116 | + "# Changelog", |
| 117 | + "", |
| 118 | + "This page contains all changes throughout releases of the library.", |
| 119 | + "", |
| 120 | + ) |
| 121 | + |
| 122 | + outputPath.writeLines(header + result) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +fun Project.registerChangelogTask() { |
| 127 | + tasks.register<UpdateDocsChangelog>("updateDocsChangelog") { |
| 128 | + input.set(ROOT_CHANGELOG_PATH) |
| 129 | + output.set(DOCS_CHANGELOG_PATH) |
| 130 | + } |
| 131 | +} |
0 commit comments