forked from open-policy-agent/opa-idea-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
260 lines (222 loc) · 7.66 KB
/
build.gradle.kts
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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
import org.intellij.markdown.ast.getTextInNode
import org.jetbrains.grammarkit.tasks.GenerateLexerTask
import org.jetbrains.grammarkit.tasks.GenerateParserTask
import org.jetbrains.intellij.tasks.RunIdeTask
import org.jetbrains.intellij.tasks.PublishPluginTask
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val baseIDE = prop("baseIDE")
val ideaVersion = prop("ideaVersion")
val pycharmCommunityVersion = prop("pycharmCommunityVersion")
val baseVersion = when (baseIDE) {
"idea" -> ideaVersion
"pycharmCommunity" -> pycharmCommunityVersion
else -> error("Unexpected IDE name: `$baseIDE`")
}
val psiViewerPluginVersion = prop("psiViewerPluginVersion")
val channel = prop("publishChannel")
buildscript {
repositories {
mavenCentral()
}
dependencies {
// needed to extract the last release notes
classpath("org.jetbrains:markdown:0.2.0")
}
}
idea {
module {
// https://github.com/gradle/kotlin-dsl/issues/537/
excludeDirs = excludeDirs + file("testData")
}
}
plugins {
idea
kotlin("jvm") version "1.9.21"
id("org.jetbrains.intellij") version "1.17.0"
id("org.jetbrains.grammarkit") version "2022.3.2"
}
allprojects {
apply {
plugin("idea")
plugin("kotlin")
plugin("org.jetbrains.grammarkit")
plugin("org.jetbrains.intellij")
}
repositories {
mavenCentral()
maven("https://cache-redirector.jetbrains.com/intellij-dependencies")
}
configurations {
all {
resolutionStrategy.sortArtifacts(ResolutionStrategy.SortOrder.DEPENDENCY_FIRST)
}
}
dependencies {
implementation("com.github.kittinunf.fuel", "fuel", "2.3.1") {
exclude("org.jetbrains.kotlin")
}
testImplementation("org.assertj:assertj-core:3.24.2")
}
idea {
module {
generatedSourceDirs.add(file("src/main/gen"))
}
}
intellij {
version.set(baseVersion)
sandboxDir.set("$buildDir/$baseIDE-sandbox-$baseVersion")
}
// Set the JVM language level used to build project. Use Java 11 for 2020.3+, and Java 17 for 2022.2+.
kotlin {
jvmToolchain(17)
}
sourceSets {
main {
java.srcDirs("src/main/gen")
}
}
tasks {
withType<org.jetbrains.intellij.tasks.PatchPluginXmlTask> {
sinceBuild.set(prop("sinceBuild"))
untilBuild.set(prop("untilBuild"))
changeNotes.set(provider { getLastReleaseNotes() })
}
withType<RunIdeTask> {
jvmArgs("--add-exports", "java.base/jdk.internal.vm=ALL-UNNAMED")
}
buildSearchableOptions {
// buildSearchableOptions task doesn't make sense for non-root subprojects
enabled = false
}
}
afterEvaluate {
tasks.withType<Test>().configureEach {
// We need to prevent the platform-specific shared JNA library to loading from the system library paths,
// because otherwise it can lead to compatibility issues.
// Also note that IDEA does the same thing at startup, and not only for tests.
systemProperty("jna.nosys", "true")
}
}
}
val channelSuffix = if (channel.isBlank() || channel == "stable") "" else "-$channel"
val pluginVersion = prop("pluginVersion")
// module to build/run/publish opa-ida-plugin plugin
project(":plugin") {
version = "$pluginVersion$channelSuffix"
intellij {
pluginName.set("opa-idea-plugin")
val pluginList = mutableListOf(
"PsiViewer:$psiViewerPluginVersion"
)
if (baseIDE == "idea") {
pluginList += listOf(
"java"
)
}
plugins.set(pluginList)
}
dependencies {
implementation(project(":"))
implementation(project(":idea"))
}
tasks {
buildPlugin {
// Set proper name for final plugin zip.
// Otherwise, base name is the same as gradle module name
archiveBaseName.set("opa-idea-plugin")
}
withType<RunIdeTask> {
jvmArgs("--add-exports", "java.base/jdk.internal.vm=ALL-UNNAMED")
}
withType<PublishPluginTask> {
token.set(prop("publishToken"))
channels.set(listOf(channel))
}
buildSearchableOptions {
// buildSearchableOptions task doesn't make sense for non-root subprojects
enabled = prop("enableBuildSearchableOptions").toBoolean()
}
}
}
project(":") {
val testOutput = configurations.create("testOutput")
dependencies {
testOutput(sourceSets.getByName("test").output.classesDirs)
}
val generateRegoLexer = task<GenerateLexerTask>("generateRegoLexer") {
sourceFile.set(file("src/main/grammar/RegoLexer.flex"))
targetDir.set("src/main/gen/org/openpolicyagent/ideaplugin/lang/lexer")
targetClass.set("_RegoLexer")
purgeOldFiles.set(true)
}
val generateRegoParser = task<GenerateParserTask>("generateRegoParser") {
sourceFile.set(file("src/main/grammar/Rego.bnf"))
targetRoot.set("src/main/gen")
pathToParser.set("/org/openpolicyagent/ideaplugin/lang/parser/RegoParser.java")
pathToPsiRoot.set("/org/openpolicyagent/ideaplugin/lang/psi")
purgeOldFiles.set(true)
}
tasks.withType<KotlinCompile> {
dependsOn(
generateRegoLexer,
generateRegoParser
)
}
task("resolveDependencies") {
doLast {
rootProject.allprojects
.map { it.configurations }
.flatMap { it.filter { c -> c.isCanBeResolved } }
.forEach { it.resolve() }
}
}
}
project(":idea") {
dependencies {
implementation(project(":"))
testImplementation(project(":", "testOutput"))
}
}
fun prop(name: String): String =
extra.properties[name] as? String
?: error("Property `$name` is not defined in gradle.properties")
val SourceSet.kotlin: SourceDirectorySet
get() = this.extensions.getByName<KotlinSourceSet>("kotlin").kotlin
fun SourceSet.kotlin(action: SourceDirectorySet.() -> Unit) =
kotlin.action()
fun getLastReleaseNotes(changLogPath: String = "CHANGELOG.md"): String {
val src = File(project.projectDir, changLogPath).readText()
val flavour = org.intellij.markdown.flavours.commonmark.CommonMarkFlavourDescriptor()
val parsedTree = org.intellij.markdown.parser.MarkdownParser(flavour).buildMarkdownTreeFromString(src)
var found = false
val releaseNotesChildren: MutableList<org.intellij.markdown.ast.ASTNode> = mutableListOf()
for (child in parsedTree.children) {
if (child.type == org.intellij.markdown.MarkdownElementTypes.ATX_1) {
if (found) {
// collect finished. exit
break
}
if (child.getTextInNode(src).startsWith("# Release notes for v")) {
releaseNotesChildren.add(child)
found = true
}
} else {
if (found) { // collect child related to this release note
releaseNotesChildren.add(child)
}
}
}
if (!found) {
throw Exception("Can not find releases notes in '${changLogPath}'")
}
val root = org.intellij.markdown.ast.CompositeASTNode(
org.intellij.markdown.MarkdownElementTypes.MARKDOWN_FILE,
releaseNotesChildren
)
return org.intellij.markdown.html.HtmlGenerator(src, root, flavour).generateHtml()
}