-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
185 additions
and
162 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/git/semver/plugin/changelog/ChangeLogContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package git.semver.plugin.changelog | ||
|
||
class ChangeLogContext { | ||
private val flaggedCommits = mutableSetOf<ChangeLogFormatter.CommitInfo>() | ||
|
||
fun flagCommit(commit: ChangeLogFormatter.CommitInfo) { | ||
flaggedCommits.add(commit) | ||
} | ||
|
||
fun isCommitFlagged(commit: ChangeLogFormatter.CommitInfo): Boolean { | ||
return commit in flaggedCommits | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
src/main/kotlin/git/semver/plugin/changelog/ChangeLogDocumentBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package git.semver.plugin.changelog | ||
|
||
import java.util.* | ||
|
||
open class MarkDownDocumentBuilder { | ||
val out = StringBuilder() | ||
|
||
fun build(): String { | ||
return out.toString() | ||
} | ||
|
||
fun heading1(text: String?) { | ||
out.append("# ").appendLine(text) | ||
} | ||
|
||
fun heading2(text: String?) { | ||
out.append("## ").appendLine(text) | ||
} | ||
|
||
fun heading3(text: String?) { | ||
out.append("### ").appendLine(text) | ||
} | ||
|
||
fun heading4(text: String?) { | ||
out.append("#### ").appendLine(text) | ||
} | ||
|
||
fun append(t: String?): MarkDownDocumentBuilder { | ||
out.append(t) | ||
return this | ||
} | ||
|
||
fun appendLine(t: String? = ""): MarkDownDocumentBuilder { | ||
out.appendLine(t) | ||
return this | ||
} | ||
} | ||
|
||
open class ChangeLogDocumentBuilder( | ||
private val commitInfos: List<ChangeLogFormatter.CommitInfo>, | ||
private val context: ChangeLogContext | ||
) : MarkDownDocumentBuilder() { | ||
fun breakingChanges(block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block) { it.isBreaking } | ||
} | ||
|
||
fun withScopes(vararg scopes: String, block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block) { it.scope in scopes } | ||
} | ||
|
||
fun withoutScope(block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block) { it.scope == null } | ||
} | ||
|
||
fun withTypes(vararg types: String, block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block) { it.type in types } | ||
} | ||
|
||
fun withoutType(block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block) { it.type == null } | ||
} | ||
|
||
fun with(filter: (ChangeLogFormatter.CommitInfo) -> Boolean, block: ChangeLogDocumentBuilder.() -> Unit) { | ||
filter(block, filter) | ||
} | ||
|
||
private fun filter(block: ChangeLogDocumentBuilder.() -> Unit, filter: (ChangeLogFormatter.CommitInfo) -> Boolean) { | ||
val filteredCommits = commitInfos().filter(filter) | ||
if (filteredCommits.isNotEmpty()) { | ||
val builder = ChangeLogDocumentBuilder(filteredCommits, context) | ||
builder.block() | ||
out.appendLine(builder.build()) | ||
} | ||
} | ||
|
||
fun groupByScope(block: ChangeLogGroupBuilder.() -> Unit) { | ||
groupBy({ it.scope ?: "" }, block) | ||
} | ||
|
||
fun groupByType(block: ChangeLogGroupBuilder.() -> Unit) { | ||
groupBy({ it.type ?: "" }, block) | ||
} | ||
|
||
fun groupBy(group: (ChangeLogFormatter.CommitInfo) -> String, block: ChangeLogGroupBuilder.() -> Unit) { | ||
processGroups(commitInfos().groupBy(group), block) | ||
} | ||
|
||
fun groupBySorted(group: (ChangeLogFormatter.CommitInfo) -> String, block: ChangeLogGroupBuilder.() -> Unit) { | ||
processGroups(commitInfos().groupByTo(TreeMap(), group), block) | ||
} | ||
|
||
private fun processGroups( | ||
groupedByScope: Map<String, List<ChangeLogFormatter.CommitInfo>>, | ||
block: ChangeLogGroupBuilder.() -> Unit | ||
) { | ||
for ((key, scopeCommits) in groupedByScope.filterKeys { it.isNotEmpty() }) { | ||
processGroup(scopeCommits, key, block) | ||
} | ||
} | ||
|
||
private fun processGroup( | ||
scopeCommits: List<ChangeLogFormatter.CommitInfo>, | ||
group: String, | ||
block: ChangeLogGroupBuilder.() -> Unit | ||
) { | ||
val builder = ChangeLogGroupBuilder(scopeCommits, context, group) | ||
block(builder) | ||
out.appendLine(builder.build()) | ||
} | ||
|
||
fun formatEach(block: ChangeLogTextFormatter.() -> String) { | ||
for (commitInfo in commitInfos()) { | ||
context.flagCommit(commitInfo) | ||
val builder = ChangeLogTextFormatter(commitInfo) | ||
out.appendLine(builder.block()) | ||
} | ||
} | ||
|
||
fun skip() { | ||
for (commitInfo in commitInfos()) { | ||
context.flagCommit(commitInfo) | ||
} | ||
} | ||
|
||
private fun commitInfos() = commitInfos.filter { !context.isCommitFlagged(it) } | ||
} | ||
|
||
class ChangeLogGroupBuilder( | ||
commitInfos: List<ChangeLogFormatter.CommitInfo>, | ||
flagManager: ChangeLogContext, | ||
val group: String | ||
) : ChangeLogDocumentBuilder(commitInfos, flagManager) |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/git/semver/plugin/changelog/ChangeLogTextFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package git.semver.plugin.changelog | ||
|
||
class ChangeLogTextFormatter( | ||
private val commitInfo: ChangeLogFormatter.CommitInfo | ||
) { | ||
|
||
fun header() = (commitInfo.message ?: commitInfo.text).lineSequence().first() | ||
|
||
fun body() = commitInfo.text.lineSequence() | ||
.drop(1) | ||
.dropWhile { it.isEmpty() } | ||
.takeWhile { it.isNotEmpty() } | ||
|
||
fun fullHeader() = commitInfo.text.lineSequence().first() | ||
|
||
fun scope(format: String = "%s: ") = commitInfo.scope?.let { format.format(it) }.orEmpty() | ||
|
||
fun type(format: String = "%s") = commitInfo.type?.let { format.format(it) }.orEmpty() | ||
|
||
fun hash(format: String = "%s ", len: Int = 40) = | ||
commitInfo.commits.joinToString("") { format.format(it.sha.take(len)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters