forked from open-policy-agent/opa-idea-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
286 lines (246 loc) · 8.49 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
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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
import org.gradle.api.JavaVersion.VERSION_11
import org.gradle.api.internal.HasConvention
import org.intellij.markdown.ast.getTextInNode
import org.jetbrains.grammarkit.tasks.GenerateLexer
import org.jetbrains.grammarkit.tasks.GenerateParser
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 platformVersion = prop("platformVersion").toInt()
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.6.0"
id("org.jetbrains.intellij") version "1.2.1"
id("org.jetbrains.grammarkit") version "2021.1.3"
id("net.saliman.properties") version "1.5.1"
}
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.2.3"){
exclude("org.jetbrains.kotlin")
}
testImplementation("org.assertj:assertj-core:3.16.1")
}
idea {
module {
generatedSourceDirs.add(file("src/main/gen"))
}
}
intellij {
version.set(baseVersion)
sandboxDir.set("$buildDir/$baseIDE-sandbox-$platformVersion")
}
sourceSets {
main {
java.srcDirs("src/main/gen")
kotlin.srcDirs("src/$platformVersion/main/kotlin")
resources.srcDirs("src/$platformVersion/main/resources")
}
test {
kotlin.srcDirs("src/$platformVersion/test/kotlin")
resources.srcDirs("src/$platformVersion/test/resources")
}
}
configure<JavaPluginExtension> {
sourceCompatibility = VERSION_11
targetCompatibility = VERSION_11
}
tasks {
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "11"
freeCompilerArgs = listOf("-Xjvm-default=enable")
}
}
withType<org.jetbrains.intellij.tasks.PatchPluginXmlTask> {
sinceBuild.set(prop("sinceBuild"))
untilBuild.set(prop("untilBuild"))
changeNotes.set(provider {getLastReleaseNotes()}) // to check
}
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 versionSuffix = "-$platformVersion$channelSuffix"
val pluginVersion = prop("pluginVersion")
// module to build/run/publish opa-ida-plugin plugin
project(":plugin"){
version = "$pluginVersion$versionSuffix"
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))
}
runPluginVerifier {
ideVersions.set(prop("pluginVerifierIdeVersions").split(',').map(String::trim).filter(String::isNotEmpty))
}
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<GenerateLexer>("generateRegoLexer") {
source = "src/main/grammar/RegoLexer.flex"
targetDir = "src/main/gen/org/openpolicyagent/ideaplugin/lang/lexer"
targetClass = "_RegoLexer"
purgeOldFiles = true
}
val generateRegoParser = task<GenerateParser>("generateRegoParser") {
source = "src/main/grammar/Rego.bnf"
targetRoot = "src/main/gen"
pathToParser = "/org/openpolicyagent/ideaplugin/lang/parser/RegoParser.java"
pathToPsiRoot = "/org/openpolicyagent/ideaplugin/lang/psi"
purgeOldFiles = 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 as HasConvention)
.convention
.getPlugin(KotlinSourceSet::class.java)
.kotlin
fun SourceSet.kotlin(action: SourceDirectorySet.() -> Unit) =
kotlin.action()
fun getLastReleaseNotes(changLogPath: String = "CHANGELOG.md"): String {
val src = File(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()
}