-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
413 lines (326 loc) · 12 KB
/
build.gradle.kts
File metadata and controls
413 lines (326 loc) · 12 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import org.jetbrains.grammarkit.tasks.GenerateLexerTask
import org.jetbrains.grammarkit.tasks.GenerateParserTask
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDate
import java.time.format.DateTimeFormatter.ofPattern
fun properties(key: String) = project.findProperty(key).toString()
val JavaVersion = 21
fun getVersionNumber() : String {
val major = properties("pluginMajorVersion")
val minor = LocalDate.now().format(ofPattern("yyMMdd"))
val build = System.getenv("BUILD_NUMBER") ?: 3
val branchName = System.getenv("BRANCH_NAME") ?: "undefined"
if (branchName.matches(Regex("""[0-9]{3}\.x"""))) {
return "${major}.${minor}.${build}"
} else {
return "${major}.${minor}.${build}-$branchName"
}
}
plugins {
id("java")
id("org.jetbrains.intellij.platform") version "2.16.0"
id("org.jetbrains.grammarkit") version "2023.3.0.3"
id("checkstyle")
id("com.avast.gradle.docker-compose") version "0.17.21"
id("org.jetbrains.kotlin.jvm") version "2.3.21"
id("idea")
}
dockerCompose {
dockerComposeWorkingDirectory.set(file("./systemd-build/"))
setProjectName("systemdbuild")
}
group = "net.sjrx.intellij.plugins"
version = getVersionNumber()
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(JavaVersion))
}
}
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(JavaVersion))
}
compilerOptions {
jvmTarget.set(JvmTarget.JVM_21)
}
}
repositories {
mavenCentral()
intellijPlatform {
defaultRepositories()
}
}
dependencies {
implementation("commons-io:commons-io:2.21.0")
implementation("com.google.guava:guava:33.6.0-jre")
testImplementation("junit:junit:4.13.2")
testImplementation("org.opentest4j:opentest4j:1.3.0")
intellijPlatform {
val type = providers.gradleProperty("platformType")
val version = providers.gradleProperty("intellijVersion")
create(type, version)
pluginVerifier()
testFramework(TestFrameworkType.Platform)
}
}
val relativePath = "CHANGELOG"
val filePath = Paths.get(project.layout.buildDirectory.get().asFile.path, relativePath)
// Check if the file exists and read its content or use a default string
val changeLogContents: String = if (Files.exists(filePath)) {
filePath.toFile().readText()
} else {
"Development Build"
}
tasks {
patchPluginXml {
changeNotes.set(changeLogContents)
sinceBuild.set(properties("sinceVersion"))
untilBuild.set(properties("untilVersion"))
}
}
// Add generated sources root
sourceSets {
main {
java {
srcDirs("src/main/gen")
}
}
}
idea {
module {
generatedSourceDirs.add(file("src/main/gen"))
setDownloadJavadoc(true)
setDownloadSources(true)
}
}
/*
* CI Tasks
*/
checkstyle {
// Exclude the generated sources
toolVersion = "8.11"
}
tasks {
checkstyleMain {
source = fileTree("src/main/java")
}
}
tasks {
test {
testLogging.showExceptions = true
testLogging.setExceptionFormat("full")
}
}
tasks {
buildSearchableOptions {
enabled = false
}
}
/*
* Build Tasks
*/
tasks.register<GenerateDataFromManPages>("generateDataFromManPages") {
description = "Regenerate semantic data (used for documentation and inspections) by parsing the documentation from systemd git repository"
group = "generation"
systemdSourceCodeRoot = file("./systemd-build/build/")
generatedJsonFileLocation =
file(sourceSets["main"].output.resourcesDir?.getAbsolutePath() + "/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata")
}
/*
* Lexing / Parsing and Grammar Tasks
*/
// import is optional to make task creation easier
tasks.register<GenerateLexerTask>("generateLexerTask") {
description = "Generate the lexer necessary for parsing unit files using JFlex"
group = "generation"
sourceFile.set(file("src/main/resources/net/sjrx/intellij/plugins/systemdunitfiles/lexer/SystemdUnitFile.flex"))
targetOutputDir.set(file("src/main/gen/net/sjrx/intellij/plugins/systemdunitfiles/generated/"))
purgeOldFiles.set(true)
mustRunAfter(tasks.compileJava)
}
tasks.register<GenerateParserTask>("generateParserTask") {
description = "Generate the grammar necessary for parsing unit files using GrammarKit"
group = "generation"
sourceFile.set(file("src/main/resources/net/sjrx/intellij/plugins/systemdunitfiles/grammar/SystemdUnitFile.bnf"))
targetRootOutputDir.set(file("src/main/gen/"))
// path to a parser file, relative to the targetRoot
pathToParser.set("net/sjrx/intellij/plugins/systemdunitfiles/generated/UnitFileParser.java")
// path to a directory with generated psi files, relative to the targetRoot
pathToPsiRoot.set("net/sjrx/intellij/plugins/systemdunitfiles/psi/")
purgeOldFiles.set(true)
dependsOn("generateLexerTask")
}
tasks.register<Copy>("generateOptionValidator") {
listOf(
"journald-gperf.gperf",
"link-config-gperf.gperf",
"load-fragment-gperf.gperf",
"logind-gperf.gperf",
"netdev-gperf.gperf",
"networkd-gperf.gperf",
"networkd-network-gperf.gperf",
"nspawn-gperf.gperf",
"resolved-dnssd-gperf.gperf",
"resolved-gperf.gperf",
"timesyncd-gperf.gperf").forEach {
fileName -> from("./systemd-build/build/${fileName}")
}
into("${sourceSets["main"].output.resourcesDir?.getAbsolutePath()}/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/")
}
tasks.register("mergePodmanDocumentation") {
description = "Merge podman quadlet documentation JSON into the generated sectionToKeywordMapFromDoc.json"
group = "generation"
val semanticDataDir = file("${sourceSets["main"].output.resourcesDir?.getAbsolutePath()}/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata")
val podmanJsonFile = file("./src/main/resources/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/podman/podman-sectionToKeywordMapFromDoc.json")
val targetJsonFile = file("${semanticDataDir}/sectionToKeywordMapFromDoc.json")
inputs.file(podmanJsonFile)
dependsOn("generateDataFromManPages")
mustRunAfter("processResources", "generateOptionValidator", "generateUnitAutoCompleteData")
doLast {
val slurper = groovy.json.JsonSlurper()
val sharedSections = listOf("Unit", "Install", "Service")
// Merge documented keywords
@Suppress("UNCHECKED_CAST")
val mainData = slurper.parse(targetJsonFile) as MutableMap<String, Any>
@Suppress("UNCHECKED_CAST")
val podmanData = slurper.parse(podmanJsonFile) as MutableMap<String, Any>
@Suppress("UNCHECKED_CAST")
val unitFileClassData = mainData["unit"] as? Map<String, Any>
@Suppress("UNCHECKED_CAST")
val podmanNetworkData = podmanData.getOrDefault("podman_network", mutableMapOf<String, Any>()) as MutableMap<String, Any>
if (unitFileClassData != null) {
for (section in sharedSections) {
val sectionData = unitFileClassData[section]
if (sectionData != null) {
podmanNetworkData[section] = sectionData
}
}
}
podmanData["podman_network"] = podmanNetworkData
mainData.putAll(podmanData)
val output = groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(mainData))
targetJsonFile.writeText(output)
// Merge undocumented keywords (deprecated/moved options)
val undocumentedJsonFile = file("${semanticDataDir}/undocumentedSectionToKeywordMap.json")
@Suppress("UNCHECKED_CAST")
val undocData = slurper.parse(undocumentedJsonFile) as MutableMap<String, Any>
@Suppress("UNCHECKED_CAST")
val unitUndocData = undocData["unit"] as? Map<String, Any>
if (unitUndocData != null) {
val podmanUndocData = mutableMapOf<String, Any>()
for (section in sharedSections) {
val sectionData = unitUndocData[section]
if (sectionData != null) {
podmanUndocData[section] = sectionData
}
}
undocData["podman_network"] = podmanUndocData
}
val undocOutput = groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(undocData))
undocumentedJsonFile.writeText(undocOutput)
}
}
tasks.register("generatePodmanNetworkGperf") {
description = "Generate podman-network-gperf.gperf by merging systemd unit sections with podman quadlet network keys"
group = "generation"
val loadFragmentFile = file("./systemd-build/build/load-fragment-gperf.gperf")
val podmanNetworkFile = file("./src/main/resources/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/podman/podman-network.gperf")
val outputDir = file("${sourceSets["main"].output.resourcesDir?.getAbsolutePath()}/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/")
val outputFile = file("${outputDir}/podman-network-gperf.gperf")
inputs.file(loadFragmentFile)
inputs.file(podmanNetworkFile)
outputs.file(outputFile)
dependsOn("generateOptionValidator")
doLast {
val unitSections = setOf("Unit", "Install", "Service")
val unitLines = loadFragmentFile.readLines().filter { line ->
val trimmed = line.trim()
trimmed.isNotEmpty() && unitSections.any { section -> trimmed.startsWith("$section.") }
}
val podmanLines = podmanNetworkFile.readLines()
outputFile.parentFile.mkdirs()
outputFile.writeText((unitLines + podmanLines).joinToString("\n") + "\n")
}
}
tasks {
runIde {
jvmArgs("-XX:+UnlockDiagnosticVMOptions")
}
}
//classes.dependsOn += generateParserTask
tasks {
classes {
dependsOn("generateOptionValidator")
dependsOn("generatePodmanNetworkGperf")
}
}
if (!(project.file("./systemd-build/build/load-fragment-gperf.gperf").exists())) {
println("Could not find metadata file")
tasks.named("generateOptionValidator") {
dependsOn("composeUp")
}
}
if (!(project.file("./systemd-build/build/man").exists())) {
println("Could not find man pages")
tasks.named("generateDataFromManPages") {
dependsOn("composeUp")
}
}
tasks.register<Copy>("generateUnitAutoCompleteData") {
from("./systemd-build/build/ubuntu-units.txt")
into("${sourceSets["main"].output.resourcesDir?.getAbsolutePath()}/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/")
}
if (!(project.file("./systemd-build/build/ubuntu-units.txt").exists())) {
println("Could not find ubuntu units")
tasks.named("generateUnitAutoCompleteData") {
dependsOn("composeUp")
}
}
tasks {
jar {
dependsOn("generateDataFromManPages")
dependsOn("mergePodmanDocumentation")
dependsOn("generateOptionValidator")
dependsOn("generatePodmanNetworkGperf")
dependsOn("generateUnitAutoCompleteData")
}
checkstyleMain {
dependsOn("generateDataFromManPages")
dependsOn("generateUnitAutoCompleteData")
}
instrumentedJar {
dependsOn("generateDataFromManPages")
dependsOn("mergePodmanDocumentation")
dependsOn("generatePodmanNetworkGperf")
dependsOn("generateUnitAutoCompleteData")
}
compileTestKotlin {
dependsOn("generateUnitAutoCompleteData")
dependsOn("generateDataFromManPages")
dependsOn("mergePodmanDocumentation")
}
compileTestJava {
dependsOn("generateUnitAutoCompleteData")
dependsOn("generateDataFromManPages")
dependsOn("mergePodmanDocumentation")
}
}
if (hasProperty("buildScan")) {
extensions.findByName("buildScan")?.withGroovyBuilder {
setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
setProperty("termsOfServiceAgree", "yes")
}
}
tasks.register<org.jetbrains.intellij.platform.gradle.tasks.PublishPluginTask>("publishPluginStandalone") {
token.set(System.getenv("PUBLISH_TOKEN"))
// pluginVersion is based on the SemVer (https://semver.org) and supports pre-release labels, like 2.1.7-alpha.3
// Specify pre-release label to publish the plugin in a custom Release Channel automatically. Read more:
// https://jetbrains.org/intellij/sdk/docs/tutorials/build_system/deployment.html#specifying-a-release-channel
channels.set(listOf(System.getenv("RELEASE_CHANNEL")?:"dev"))
host.set("https://plugins.jetbrains.com")
// Set the distribution file in gradle build to the archive file of the buildPlugin task
archiveFile.set(project.file("build/distributions/${project.name}-${project.version}.zip"))
}