-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.gradle.kts
242 lines (214 loc) · 7.11 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
import de.undercouch.gradle.tasks.download.Download
import org.jetbrains.grammarkit.tasks.GenerateLexer
import org.jetbrains.grammarkit.tasks.GenerateParser
import org.jetbrains.intellij.tasks.PatchPluginXmlTask
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.*
import java.nio.file.Paths
val isCI = !System.getenv("CI").isNullOrBlank()
val commitHash = Runtime.getRuntime().exec("git rev-parse --short HEAD").run {
waitFor()
val output = inputStream.use { inputStream.use { it.readBytes().let(::String) } }
destroy()
output.trim()
}
val pluginComingVersion = "0.4.0"
val pluginVersion = if (isCI) "$pluginComingVersion-$commitHash" else pluginComingVersion
val packageName = "rs.pest"
val asmble = "asmble"
val rustTarget = projectDir.resolve("rust").resolve("target")
group = packageName
version = pluginVersion
plugins {
java
// Kotlin support
kotlin("jvm") version "1.9.22"
// https://github.com/JetBrains/gradle-intellij-plugin
id("org.jetbrains.intellij") version "1.17.1"
// https://github.com/JetBrains/gradle-changelog-plugin
id("org.jetbrains.changelog") version "2.2.0"
// https://github.com/JetBrains/gradle-grammar-kit-plugin
id("org.jetbrains.grammarkit") version "2022.3.2.1"
}
allprojects { apply { plugin("org.jetbrains.grammarkit") } }
fun fromToolbox(root: String, ide: String) = file(root)
.resolve(ide)
.takeIf { it.exists() }
?.resolve("ch-0")
?.listFiles()
.orEmpty()
.filterNotNull()
.filter { it.isDirectory }
.filterNot { it.name.endsWith(".plugins") }
.maxBy {
val (major, minor, patch) = it.name.split('.')
String.format("%5s%5s%5s", major, minor, patch)
}
?.also { println("Picked: $it") }
intellij {
updateSinceUntilBuild = false
instrumentCode = true
val user = System.getProperty("user.name")
val os = System.getProperty("os.name")
val root = when {
os.startsWith("Windows") -> "C:\\Users\\$user\\AppData\\Local\\JetBrains\\Toolbox\\apps"
os == "Linux" -> "/home/$user/.local/share/JetBrains/Toolbox/apps"
else -> return@intellij
}
val intellijPath = sequenceOf("IDEA-C", "IDEA-U")
.mapNotNull { fromToolbox(root, it) }.firstOrNull()
intellijPath?.absolutePath?.let { localPath = it }
val pycharmPath = sequenceOf("IDEA-C", "PyCharm-C", "IDEA-U")
.mapNotNull { fromToolbox(root, it) }.firstOrNull()
pycharmPath?.absolutePath?.let { alternativeIdePath = it }
if (!isCI) setPlugins("PsiViewer:201.6251.22-EAP-SNAPSHOT.3")
else version = "2020.1"
setPlugins("org.rust.lang:0.2.120.2202-201", "org.toml.lang:0.2.120.37-193", "java")
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
tasks.withType<PatchPluginXmlTask> {
changeNotes(file("res/META-INF/change-notes.html").readText())
pluginDescription(file("res/META-INF/description.html").readText())
version(pluginVersion)
pluginId(packageName)
}
sourceSets {
main {
withConvention(KotlinSourceSet::class) {
listOf(java, kotlin).forEach { it.srcDirs("src", "gen") }
}
resources.srcDirs("res", rustTarget.resolve("java").absolutePath)
}
test {
withConvention(KotlinSourceSet::class) {
listOf(java, kotlin).forEach { it.srcDirs("test") }
}
resources.srcDirs("testData")
}
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.eclipse.mylyn.github", "org.eclipse.egit.github.core", "2.1.5") {
exclude(module = "gson")
}
implementation("org.jetbrains.kotlinx", "kotlinx-html-jvm", "0.7.1") {
exclude(module = "kotlin-stdlib")
}
implementation(files("$projectDir/rust/target/java"))
testImplementation(kotlin("test-junit"))
testImplementation("junit", "junit", "4.12")
}
task("displayCommitHash") {
group = "help"
description = "Display the newest commit hash"
doFirst { println("Commit hash: $commitHash") }
}
task("isCI") {
group = "help"
description = "Check if it's running in a continuous-integration"
doFirst { println(if (isCI) "Yes, I'm on a CI." else "No, I'm not on CI.") }
}
val downloadAsmble = task<Download>("downloadAsmble") {
group = asmble
src("https://github.com/pest-parser/intellij-pest/files/3592625/asmble.zip")
dest(buildDir.absolutePath)
overwrite(false)
}
val unzipAsmble = task<Copy>("unzipAsmble") {
group = asmble
dependsOn(downloadAsmble)
from(zipTree(buildDir.resolve("asmble.zip")))
into(buildDir)
}
val wasmFile = rustTarget
.resolve("wasm32-unknown-unknown")
.resolve("release")
.resolve("pest_ide.wasm")
val wasmGcFile = wasmFile.resolveSibling("pest_ide_gc.wasm")
val asmbleExe by lazy {
buildDir
.resolve("asmble")
.resolve("bin")
.resolve(if (System.getProperty("os.name").startsWith("Windows")) "asmble.bat" else "asmble")
}
val compileRust = task<Exec>("compileRust") {
val rustDir = projectDir.resolve("rust")
workingDir(rustDir)
inputs.dir(rustDir.resolve("src"))
outputs.dir(rustDir.resolve("target"))
commandLine("rustup", "run", "nightly", "cargo", "build", "--release")
}
val gcWasm = task/*<Exec>*/("gcWasm") {
dependsOn(compileRust)
/* Asmble is broken for GCed wasm.
workingDir(projectDir)
inputs.file(wasmFile)
outputs.file(wasmGcFile)
commandLine("wasm-gc", wasmFile.absolutePath, wasmGcFile.absolutePath)
*/
doFirst { wasmFile.copyTo(wasmGcFile, overwrite = true) }
}
val translateWasm = task<Exec>("translateWasm") {
group = asmble
dependsOn(unzipAsmble, gcWasm)
workingDir(projectDir)
val path = wasmGcFile.also { inputs.file(it) }.absolutePath
val outPath = "${path.dropLast(1)}t".also { outputs.file(it) }
commandLine(asmbleExe, "translate", path, outPath, "-out", "wast")
doFirst { println("Output file: $outPath") }
}
val compileWasm = task<Exec>("compileWasm") {
group = asmble
dependsOn(unzipAsmble, gcWasm)
workingDir(projectDir.absolutePath)
val classRelativePath = listOf("rs", "pest", "vm", "PestUtil.class")
val outFile = classRelativePath
.fold(rustTarget.resolve("java"), File::resolve)
.apply { parentFile.mkdirs() }
.also { outputs.file(it) }
.absolutePath
val wasmGcFile = wasmGcFile.also { inputs.file(it) }.absolutePath
val cls = classRelativePath.joinToString(separator = ".").removeSuffix(".class")
commandLine(asmbleExe, "compile", wasmGcFile, cls, "-out", outFile)
doFirst {
println("Input file: $wasmGcFile")
println("Output file: $outFile")
}
}
fun path(more: Iterable<*>) = more.joinToString(File.separator)
val genParser = task<GenerateParser>("genParser") {
group = tasks["init"].group!!
description = "Generate the Parser and PsiElement classes"
source = "grammar/pest.bnf"
targetRoot = "gen/"
val parserRoot = Paths.get("rs", "pest")
pathToParser = path(parserRoot + "PestParser.java")
pathToPsiRoot = path(parserRoot + "psi")
purgeOldFiles = true
}
val genLexer = task<GenerateLexer>("genLexer") {
group = genParser.group
description = "Generate the Lexer"
source = "grammar/pest.flex"
targetDir = path(Paths.get("gen", "rs", "pest", "psi"))
targetClass = "PestLexer"
purgeOldFiles = true
dependsOn(genParser)
}
tasks.withType<KotlinCompile> {
dependsOn(genParser, genLexer, compileWasm)
kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.3"
apiVersion = "1.3"
freeCompilerArgs = listOf("-Xjvm-default=enable")
}
}