Skip to content

Commit f60a440

Browse files
committed
feat: Added an easier way to cofigure some parts of the change log (#62)
1 parent ba9c07e commit f60a440

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

src/functionalTest/kotlin/git/semver/plugin/gradle/GitSemverPluginFunctionalTest.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class GitSemverPluginFunctionalTest {
2121
@JvmStatic
2222
fun gradleVersions(): List<Arguments> {
2323
return listOf(
24-
Arguments.of("8.6"),
24+
// Arguments.of("8.6"),
2525
Arguments.of("8.7"),
2626
Arguments.of("7.6.4"),
2727
// Arguments.of("6.9.4"),
@@ -32,7 +32,8 @@ class GitSemverPluginFunctionalTest {
3232

3333
@ParameterizedTest
3434
@MethodSource("gradleVersions")
35-
fun `can run release task`(version: String) {
35+
@NullSource
36+
fun `can run release task`(version: String?) {
3637
val projectDir = setupTestProject()
3738

3839
val releaseResult = run(
@@ -50,7 +51,8 @@ class GitSemverPluginFunctionalTest {
5051

5152
@ParameterizedTest
5253
@MethodSource("gradleVersions")
53-
fun `can run release task with --no-tag and --no-commit`(version: String) {
54+
@NullSource
55+
fun `can run release task with --no-tag and --no-commit`(version: String?) {
5456
val projectDir = setupTestProject()
5557

5658
val releaseResult2 = run(
@@ -71,7 +73,7 @@ class GitSemverPluginFunctionalTest {
7173
fun `can run testTask task`(version: String?) {
7274
val projectDir = setupTestProject()
7375

74-
val result = run(projectDir, version, "testTask", "--configuration-cache")
76+
val result = run(projectDir, version, "testTask", "--configuration-cache", "--stacktrace")
7577

7678
assertThat(result.output).containsPattern("\\d+\\.\\d+\\.\\d+")
7779
assertThat(result.output).containsPattern("ProjVer: \\d+\\.\\d+\\.\\d+")
@@ -138,6 +140,9 @@ class GitSemverPluginFunctionalTest {
138140
semver {
139141
changeLogTexts {
140142
header = "# Test changelog"
143+
hashFormat = { commitInfo ->
144+
commitInfo.commits.joinToString(" <<<", "", ">>> ") { it.sha }
145+
}
141146
}
142147
// changeLogFormat = git.semver.plugin.changelog.ChangeLogFormat.defaultChangeLog
143148
// changeLogFormat = git.semver.plugin.changelog.ChangeLogFormat.simpleChangeLog
@@ -147,7 +152,7 @@ class GitSemverPluginFunctionalTest {
147152
withType("test") {
148153
appendLine("## Test")
149154
formatChanges {
150-
appendLine("- ${'$'}{scope()}${'$'}{header()}")
155+
appendLine("- ${'$'}{scope()}${'$'}{header()} ${'$'}{hash()}")
151156
}
152157
appendLine()
153158
}

src/main/kotlin/git/semver/plugin/changelog/ChangeLogBuilder.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ChangeLogBuilder(
1212

1313
fun formatChanges(block: ChangeLogTextFormatter.() -> Unit) {
1414
for (commitInfo in remainingCommitInfos()) {
15-
val formatter = ChangeLogTextFormatter(commitInfo)
15+
val formatter = ChangeLogTextFormatter(commitInfo, constants)
1616
formatter.block()
1717
append(formatter.build())
1818
context.flagCommit(commitInfo)
@@ -138,7 +138,8 @@ class ChangeLogBuilder(
138138
}
139139

140140
class ChangeLogTextFormatter(
141-
val commitInfo: ChangeLogFormatter.CommitInfo
141+
val commitInfo: ChangeLogFormatter.CommitInfo,
142+
val constants: ChangeLogTexts
142143
) : DocumentBuilder() {
143144
companion object {
144145
fun sanitizeHtml(str: String): String {
@@ -174,25 +175,26 @@ class ChangeLogTextFormatter(
174175
}
175176
}
176177

177-
fun header() = (sanitizeHtml(commitInfo.message ?: commitInfo.text)).lineSequence().first()
178+
fun header() = sanitizeHtml(constants.headerFormat(commitInfo))
178179

179-
fun body() = sanitizeHtml(commitInfo.text).lineSequence()
180-
.drop(1)
181-
.dropWhile { it.isEmpty() }
182-
.takeWhile { it.isNotEmpty() }
180+
fun fullHeader() = sanitizeHtml(constants.fullHeaderFormat(commitInfo))
183181

184-
fun fullHeader() = sanitizeHtml(commitInfo.text).lineSequence().first()
182+
fun scope() = constants.scopeFormat(commitInfo)
185183

186-
fun scope(format: String = "%s: ") = commitInfo.scope?.let { format.format(it) }.orEmpty()
184+
fun type() = constants.typeFormat(commitInfo)
187185

188-
fun type(format: String = "%s: ") = commitInfo.type?.let { format.format(it) }.orEmpty()
189-
fun hash(format: String = "%s", len: Int = 40) =
190-
commitInfo.commits.joinToString(" ", "", " ") { format.format(it.sha.take(len)) }
186+
fun hash() = constants.hashFormat(commitInfo)
191187

192188
fun authorName(format: String = "%s") =
193189
commitInfo.commits.map{ format.format(it.authorName) }.distinct().joinToString(" ", "", "")
194-
fun authorNameAndEmail(format: String = "%s <%s>") =
190+
191+
fun authorNameAndEmail(format: String = "%s [%s]") =
195192
commitInfo.commits.map { format.format(it.authorName, it.authorEmail)}.distinct().joinToString(" ", "", "")
193+
194+
fun body() = sanitizeHtml(commitInfo.text).lineSequence()
195+
.drop(1)
196+
.dropWhile { it.isEmpty() }
197+
.takeWhile { it.isNotEmpty() }
196198
}
197199

198200
open class DocumentBuilder {

src/main/kotlin/git/semver/plugin/changelog/ChangeLogTexts.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package git.semver.plugin.changelog
22

33
/**
4-
* The texts used to build the change log.
4+
* The settings and texts used to build the change log.
55
*/
66
open class ChangeLogTexts(
77
/**
@@ -65,4 +65,20 @@ open class ChangeLogTexts(
6565

6666
var changeLogPattern: String = "\\A(?<Type>\\w+)(?:\\((?<Scope>[^()]+)\\))?!?:\\s*(?<Message>(?:.|\n)*)"
6767

68+
69+
var headerFormat: (ChangeLogFormatter.CommitInfo) -> String = { commitInfo ->
70+
(commitInfo.message ?: commitInfo.text).lineSequence().first()
71+
}
72+
var fullHeaderFormat: (ChangeLogFormatter.CommitInfo) -> String = { commitInfo ->
73+
commitInfo.text.lineSequence().first()
74+
}
75+
var scopeFormat: (ChangeLogFormatter.CommitInfo) -> String = { commitInfo ->
76+
commitInfo.scope?.let { "$it: " }.orEmpty()
77+
}
78+
var typeFormat: (ChangeLogFormatter.CommitInfo) -> String = { commitInfo ->
79+
commitInfo.type?.let { "$it: " }.orEmpty()
80+
}
81+
var hashFormat: (ChangeLogFormatter.CommitInfo) -> String = { commitInfo ->
82+
commitInfo.commits.joinToString(" ", "", " ") { it.sha }
83+
}
6884
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package git.semver.plugin.changelog
2+
3+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.BREAKING_CHANGES
4+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.BUG_FIXES
5+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.BUILD
6+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.CHORES
7+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.CI
8+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.DEPENDENCY_UPDATES
9+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.DOCUMENTATION
10+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.NEW_FEATURE
11+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.OTHER_CHANGES
12+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.PERFORMANCE_ENHANCEMENTS
13+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.TESTS
14+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.TOP_HEADER
15+
import git.semver.plugin.changelog.DefaultHeaderTextsEmojisLast.REFACTOR
16+
17+
internal object DefaultHeaderTextsEmojisLast {
18+
const val TOP_HEADER = "## What's Changed"
19+
const val BREAKING_CHANGES = "### Breaking Changes 🛠"
20+
const val OTHER_CHANGES = "### Other Changes \uD83D\uDCA1"
21+
const val BUG_FIXES = "### Bug Fixes \uD83D\uDC1E"
22+
const val NEW_FEATURE = "### New Features \uD83C\uDF89"
23+
const val TESTS = "### Tests ✅"
24+
const val DOCUMENTATION = "### Documentation \uD83D\uDCD6"
25+
const val DEPENDENCY_UPDATES = "### Dependency Updates \uD83D\uDE80"
26+
const val BUILD = "### Build \uD83D\uDC18 & CI ⚙\uFE0F"
27+
const val CI = BUILD
28+
const val CHORES = "### Chores \uD83D\uDD27"
29+
const val PERFORMANCE_ENHANCEMENTS = "### Performance Enhancements ⚡"
30+
const val REFACTOR = "### Refactorings \uD83D\uDE9C"
31+
}
32+
33+
object DefaultChangeLogTextsEmojisLast : ChangeLogTexts(mutableMapOf(
34+
HEADER to TOP_HEADER,
35+
BREAKING_CHANGE to BREAKING_CHANGES,
36+
OTHER_CHANGE to OTHER_CHANGES,
37+
"fix" to BUG_FIXES,
38+
"feat" to NEW_FEATURE,
39+
"test" to TESTS,
40+
"docs" to DOCUMENTATION,
41+
"deps" to DEPENDENCY_UPDATES,
42+
"build" to BUILD,
43+
"ci" to CI,
44+
"chore" to CHORES,
45+
"perf" to PERFORMANCE_ENHANCEMENTS,
46+
"refactor" to REFACTOR
47+
))

0 commit comments

Comments
 (0)