-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
executable file
·296 lines (247 loc) · 11.3 KB
/
build.gradle.kts
File metadata and controls
executable file
·296 lines (247 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import org.hildan.github.changelog.builder.DEFAULT_TIMEZONE
import org.hildan.github.changelog.builder.SectionDefinition
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.concurrent.TimeUnit
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
}
plugins {
id("org.jetbrains.intellij") version "1.13.3"
kotlin("jvm") version "1.8.20"
id("org.hildan.github.changelog") version "1.6.0"
java
}
group = "wu.seal"
// Determine version based on Git tags or environment variable
version = System.getenv("TAG") ?: run {
// Execute git command to check if current commit has a tag
val gitTagCommand = "git describe --exact-match --tags HEAD".execute()
val gitLatestTagCommand = "git describe --tags --abbrev=0".execute()
if (gitTagCommand.exitValue() == 0) {
// Current commit is exactly at a tag - use the tag name
gitTagCommand.text.trim()
} else {
// Current commit is not at a tag - use "Unreleased"
"Unreleased"
}
}
intellij {
version.set("2023.3.5")
type.set("IU")
pluginName.set("JsonToKotlinClass")
}
// Add a function to convert Markdown to HTML for our specific changelog format
fun formatChangelogToHtml(markdown: String): String {
// Extract the version information
val versionPattern = """## \[(.*?)\](?:\((.*?)\))?(?: \((.*?)\))?""".toRegex()
val versionMatch = versionPattern.find(markdown)
var html = "<h2>Version ${versionMatch?.groupValues?.get(1) ?: "Latest"}</h2>"
// Add release date if available
if (versionMatch != null && versionMatch.groupValues.size > 3 && versionMatch.groupValues[3].isNotEmpty()) {
html += "<p><b>Released:</b> ${versionMatch.groupValues[3]}</p>"
}
// Add link to commits if available
val commitsPattern = """\[View commits\]\((.*?)\)""".toRegex()
val commitsMatch = commitsPattern.find(markdown)
if (commitsMatch != null) {
html += "<p><a href=\"${commitsMatch.groupValues[1]}\">View commits</a></p>"
}
// Process each section (Bugfix, Enhancement, Features)
val sections = listOf("Bugfix", "Enhancement", "Features")
for (section in sections) {
val sectionPattern = """(?:\*\*$section\*\*)(.*?)(?:\*\*|$)""".toRegex(RegexOption.DOT_MATCHES_ALL)
val sectionMatch = sectionPattern.find(markdown)
if (sectionMatch != null) {
html += "<h3>$section</h3><ul>"
// Extract list items (lines starting with "- ")
val content = sectionMatch.groupValues[1]
val lines = content.split("\n")
var currentItem = ""
for (line in lines) {
val trimmed = line.trim()
if (trimmed.startsWith("-")) {
// If we have accumulated an item before, add it
if (currentItem.isNotEmpty()) {
var itemText = currentItem.trim()
// Convert Markdown links to HTML links
val linkPattern = """\[(.*?)\]\((.*?)\)""".toRegex()
itemText = itemText.replace(linkPattern) { matchResult ->
val text = matchResult.groupValues[1]
val url = matchResult.groupValues[2]
"<a href=\"$url\">$text</a>"
}
// Convert escaped characters
itemText = itemText.replace("\\[", "[").replace("\\]", "]").replace("\\(", "(").replace("\\)", ")")
html += "<li>$itemText</li>"
}
// Start a new item with the current line (minus the leading dash)
currentItem = trimmed.substring(1).trim()
} else if (trimmed.isNotEmpty() && currentItem.isNotEmpty()) {
// Continue accumulating the current item with additional line
currentItem += " " + trimmed
}
}
// Don't forget to add the last item if there is one
if (currentItem.isNotEmpty()) {
var itemText = currentItem.trim()
// Convert Markdown links to HTML links
val linkPattern = """\[(.*?)\]\((.*?)\)""".toRegex()
itemText = itemText.replace(linkPattern) { matchResult ->
val text = matchResult.groupValues[1]
val url = matchResult.groupValues[2]
"<a href=\"$url\">$text</a>"
}
// Convert escaped characters
itemText = itemText.replace("\\[", "[").replace("\\]", "]").replace("\\(", "(").replace("\\)", ")")
html += "<li>$itemText</li>"
}
html += "</ul>"
}
}
// Add a link to the full changelog
html += "<p><a href=\"https://github.com/wuseal/JsonToKotlinClass/blob/master/doc/CHANGELOG.md\">View full changelog</a></p>"
return html
}
// Update the patchPluginXml task to use our improved HTML conversion
tasks.patchPluginXml {
untilBuild.set("")
// Extract appropriate version's changes and convert to HTML
changeNotes.set(
try {
val changelogFile = File("${project.projectDir}/doc/CHANGELOG.md")
val changelogText = changelogFile.readText()
val currentVersion = project.version.toString()
val isReleaseVersion = currentVersion != "Unreleased"
// First find the section that matches our current version
val sectionHeader = if (isReleaseVersion) "## [$currentVersion]" else "## [Unreleased]"
val sectionStart = changelogText.indexOf(sectionHeader)
val markdownNotes = if (sectionStart != -1) {
// Find the start of the next section
val nextSectionStart = changelogText.indexOf("## ", sectionStart + sectionHeader.length)
// Extract just this version's section
if (nextSectionStart != -1) {
changelogText.substring(sectionStart, nextSectionStart).trim()
} else {
// This is the only/last section
changelogText.substring(sectionStart).trim()
}
} else {
// Section not found - show just a simple message
if (isReleaseVersion) "## [$currentVersion] (2023-09-01)" else "## [Unreleased]"
}
// Convert to HTML using our improved function
formatChangelogToHtml(markdownNotes)
} catch (e: Exception) {
"<p>See <a href=\"https://github.com/wuseal/JsonToKotlinClass/blob/master/doc/CHANGELOG.md\">GitHub</a> for changelog</p>"
}
)
}
tasks.publishPlugin {
token.set(System.getenv("token") ?: "")
channels.set(listOf(System.getProperty("channels", "")))
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
// Mark Kotlin stdlib as "compileOnly" to avoid duplication with the version provided by the IntelliJ Platform
compileOnly(kotlin("stdlib"))
testImplementation("com.winterbe:expekt:0.5.0") {
exclude(group = "org.jetbrains.kotlin")
}
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType(KotlinCompile::class.java).configureEach {
kotlinOptions {
jvmTarget = "17"
}
}
tasks.buildSearchableOptions {
enabled = false
}
// Add back the GitHub changelog configuration
changelog {
githubUser = "wuseal"
githubRepository = rootProject.name
githubToken = findProperty("githubToken")?.toString() ?: (System.getenv("GH_TOKEN")?.toString())
title = "Change Log"
showUnreleased = true
unreleasedVersionTitle = "Unreleased"
val currentVersion = project.version.toString()
if (currentVersion != "Unreleased" && !System.getenv("TAG").isNullOrEmpty()) {
println("TAG is ${System.getenv("TAG")}, Set future version to $currentVersion")
futureVersionTag = currentVersion
}
sections = listOf(
SectionDefinition("Features", "feature request"),
SectionDefinition("Bugfix", listOf("bug", "bug fix")),
SectionDefinition("Enhancement", "enhancement")
)
includeLabels = listOf("feature request", "bug", "bug fix", "enhancement")
excludeLabels = listOf("duplicate", "invalid", "question", "wontfix")
sinceTag = "V3.0.0"
skipTags = listOf()
useMilestoneAsTag = true
timezone = DEFAULT_TIMEZONE
outputFile = file("${projectDir}/doc/CHANGELOG.md")
}
// Custom task to generate GitHub release notes
task("createGithubReleaseNotes") {
doLast {
val githubReleaseNoteFile = file("./githubReleaseNote.md")
val content = try {
"**" + file("${projectDir}/doc/CHANGELOG.md").readText()
.substringAfter("**").substringBefore("##").trim()
} catch (e: Exception) {
"No changelog available"
}
githubReleaseNoteFile.writeText(content)
}
}
// Create a custom task to ensure the changelog is generated before building the plugin
tasks.register("ensureChangelogGenerated") {
doLast {
// Get the current version
val currentVersion = project.version.toString()
val isReleaseVersion = currentVersion != "Unreleased"
if (!file("${projectDir}/doc/CHANGELOG.md").exists() || gradle.startParameter.taskNames.contains("generateChangelog")) {
// If the file doesn't exist or a regeneration is explicitly requested, generate it
tasks.named("generateChangelog").get().actions.forEach { it.execute(tasks.named("generateChangelog").get()) }
}
// If we're building an "Unreleased" version and have changes since the last tag,
// make sure the Unreleased section exists in the changelog
if (!isReleaseVersion) {
val changelogFile = file("${projectDir}/doc/CHANGELOG.md")
val changelogContent = if (changelogFile.exists()) changelogFile.readText() else ""
if (!changelogContent.contains("## [Unreleased]")) {
// If there's no Unreleased section, add it at the top of the changelog
val updatedContent = "# Change Log\n\n## [Unreleased]\n\n" +
(if (changelogContent.contains("# Change Log"))
changelogContent.substringAfter("# Change Log").trim()
else changelogContent.trim())
changelogFile.writeText(updatedContent)
}
}
}
}
tasks.getByName("buildPlugin").dependsOn("ensureChangelogGenerated")
// Extension function to execute shell commands
fun String.execute(): Process {
val parts = this.split("\\s".toRegex())
val process = ProcessBuilder(*parts.toTypedArray())
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
process.waitFor(10, TimeUnit.SECONDS)
return process
}
// Extension property to get process output as text
val Process.text: String
get() = inputStream.bufferedReader().readText().trim()