Skip to content

Commit

Permalink
fix: Improved change log
Browse files Browse the repository at this point in the history
  • Loading branch information
jmongard committed Sep 13, 2023
1 parent 790a053 commit 12409bc
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/main/kotlin/git/semver/plugin/gradle/ChangeLogFormatter.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package git.semver.plugin.gradle

import git.semver.plugin.semver.SemverSettings

import java.util.TreeMap

class ChangeLogFormatter(private val settings: SemverSettings) {
companion object {
Expand All @@ -23,12 +23,13 @@ class ChangeLogFormatter(private val settings: SemverSettings) {
private val otherType = settings.changeLogTexts[OTHER_TYPE]

internal fun formatLog(changeLog: List<String>): String {
val groupedByHeading = TreeMap<String, MutableSet<String>>()
changeLog.forEach { addChange(it, groupedByHeading) }

val builder = StringBuilder()
addText(builder, HEADER)

val groupedByHeading = mutableMapOf<String, MutableSet<String>>()
changeLog.forEach { addChange(it, groupedByHeading) }
groupedByHeading.toSortedMap().forEach { (prefix, items) ->
groupedByHeading.forEach { (prefix, items) ->
builder.appendLine(prefix)
items.sorted().forEach { item ->
builder.appendLine(item.trim().lines().joinToString(separator, this.prefix, postfix))
Expand All @@ -50,40 +51,40 @@ class ChangeLogFormatter(private val settings: SemverSettings) {
text: String,
resultMap: MutableMap<String, MutableSet<String>>
) {
fun getValue(match: MatchResult, id: String) = match.groups[id]?.value
fun add(category: String?, message: String) {
fun addChange(category: String?, message: String) {
if (!category.isNullOrEmpty()) {
resultMap.computeIfAbsent(category) { mutableSetOf() }.add(message)
}
}
fun getValue(match: MatchResult, id: String) = match.groups[id]?.value

if (settings.majorRegex.containsMatchIn(text)) {
add(breakingChange, text)
addChange(breakingChange, text)
return
}

val match = settings.changeLogRegex.find(text);
if (match == null) {
add(missingType, text)
addChange(missingType, text)
return
}

val scope = getValue(match, "Scope")
val scopeHeading = scope?.let { settings.changeLogTexts[it] }
if (scopeHeading != null) {
add(scopeHeading, text)
addChange(scopeHeading, text)
return
}

val typeHeading = getValue(match, "Type")?.let { settings.changeLogTexts[it] }
if (typeHeading != null) {
val messageText = getValue(match, "Message")
val scopeText = scope?.let { s -> "$s: " }.orEmpty()
add(typeHeading, scopeText + (messageText ?: text).trim())
addChange(typeHeading, scopeText + (messageText ?: text).trim())
return
}

add(otherType, text)
addChange(otherType, text)
}

}

0 comments on commit 12409bc

Please sign in to comment.