forked from intellij-rust/intellij-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
398 lines (341 loc) · 12.9 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
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
import org.jetbrains.intellij.tasks.PatchPluginXmlTask
import org.jetbrains.intellij.tasks.PublishTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import de.undercouch.gradle.tasks.download.Download
import org.gradle.api.internal.HasConvention
import org.gradle.api.tasks.SourceSet
import org.jetbrains.grammarkit.tasks.GenerateLexer
import org.jetbrains.grammarkit.tasks.GenerateParser
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.gradle.api.JavaVersion.VERSION_1_8
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.jvm.tasks.Jar
import java.io.Writer
import org.jetbrains.intellij.tasks.PrepareSandboxTask
import java.net.HttpURLConnection
import java.net.URL
import java.nio.file.Path
import kotlin.concurrent.thread
val CI = System.getenv("CI") != null
val channel = prop("publishChannel")
val platformVersion = prop("platformVersion")
val excludedJars = listOf(
"java-api.jar",
"java-impl.jar"
)
plugins {
idea
kotlin("jvm") version "1.3.0"
id("org.jetbrains.intellij") version "0.3.12"
id("org.jetbrains.grammarkit") version "2018.2.1"
id("de.undercouch.download") version "3.4.3"
id("net.saliman.properties") version "1.4.6"
}
idea {
module {
// https://github.com/gradle/kotlin-dsl/issues/537/
excludeDirs = excludeDirs + file("testData") + file("deps")
}
}
allprojects {
apply {
plugin("idea")
plugin("kotlin")
plugin("org.jetbrains.grammarkit")
plugin("org.jetbrains.intellij")
}
repositories {
mavenCentral()
}
idea {
module {
generatedSourceDirs.add(file("src/gen"))
}
}
intellij {
version = prop("ideaVersion")
downloadSources = !CI
updateSinceUntilBuild = true
instrumentCode = false
ideaDependencyCachePath = file("deps").absolutePath
tasks.withType<PatchPluginXmlTask> {
sinceBuild(prop("sinceBuild"))
untilBuild(prop("untilBuild"))
}
}
tasks.withType<PublishTask> {
username(prop("publishUsername"))
password(prop("publishPassword"))
channels(channel)
}
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.2"
apiVersion = "1.2"
}
}
configure<JavaPluginConvention> {
sourceCompatibility = VERSION_1_8
targetCompatibility = VERSION_1_8
}
sourceSets {
getByName("main").java.srcDirs("src/gen")
}
afterEvaluate {
val mainSourceSet = sourceSets.getByName("main")
val mainClassPath = mainSourceSet.compileClasspath
val exclusion = mainClassPath.filter { it.name in excludedJars }
mainSourceSet.compileClasspath = mainClassPath - exclusion
tasks.withType<AbstractTestTask> {
testLogging {
if (hasProp("showTestStatus") && prop("showTestStatus").toBoolean()) {
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
exceptionFormat = TestExceptionFormat.FULL
}
}
}
}
}
val channelSuffix = if (channel.isBlank()) "" else "-$channel"
project(":") {
val clionVersion = prop("clionVersion")
val versionSuffix = "-$platformVersion$channelSuffix"
version = "0.2.0.${prop("buildNumber")}$versionSuffix"
intellij {
pluginName = "intellij-rust"
// alternativeIdePath = "deps/clion-$clionVersion"
setPlugins(project(":intellij-toml"), "IntelliLang")
}
repositories {
maven { setUrl("https://dl.bintray.com/jetbrains/markdown") }
}
dependencies {
compile("org.jetbrains:markdown:0.1.30") {
exclude(module = "kotlin-runtime")
exclude(module = "kotlin-stdlib")
}
}
sourceSets {
getByName("main").kotlin.srcDirs("src/$platformVersion/kotlin")
create("debugger") {
kotlin.srcDirs("debugger/src/main/kotlin", "debugger/src/$platformVersion/kotlin")
compileClasspath += getByName("main").compileClasspath +
getByName("main").output +
files("deps/clion-$clionVersion/lib/clion.jar")
}
}
tasks.withType<Jar> {
from(sourceSets.getByName("debugger").output)
}
val generateRustLexer = task<GenerateLexer>("generateRustLexer") {
source = "src/main/grammars/RustLexer.flex"
targetDir = "src/gen/org/rust/lang/core/lexer"
targetClass = "_RustLexer"
purgeOldFiles = true
}
val generateRustDocHighlightingLexer = task<GenerateLexer>("generateRustDocHighlightingLexer") {
source = "src/main/grammars/RustDocHighlightingLexer.flex"
targetDir = "src/gen/org/rust/lang/doc/lexer"
targetClass = "_RustDocHighlightingLexer"
purgeOldFiles = true
}
val generateRustParser = task<GenerateParser>("generateRustParser") {
source = "src/main/grammars/RustParser.bnf"
targetRoot = "src/gen"
pathToParser = "/org/rust/lang/core/parser/RustParser.java"
pathToPsiRoot = "/org/rust/lang/core/psi"
purgeOldFiles = true
}
val downloadClion = task<Download>("downloadClion") {
onlyIf { !file("${project.projectDir}/deps/clion-$clionVersion.tar.gz").exists() }
src("https://download.jetbrains.com/cpp/CLion-$clionVersion.tar.gz")
dest(file("${project.projectDir}/deps/clion-$clionVersion.tar.gz"))
}
val unpackClion = task<Copy>("unpackClion") {
onlyIf { !file("${project.projectDir}/deps/clion-$clionVersion").exists() }
from(tarTree("deps/clion-$clionVersion.tar.gz"))
into(file("${project.projectDir}/deps"))
dependsOn(downloadClion)
}
tasks.withType<KotlinCompile> {
dependsOn(
generateRustLexer, generateRustDocHighlightingLexer,
generateRustParser, unpackClion
)
}
tasks.withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
}
task("resolveDependencies") {
dependsOn(unpackClion)
doLast {
rootProject.allprojects
.map { it.configurations }
.flatMap { listOf(it.compile, it.testCompile) }
.forEach { it.resolve() }
}
}
tasks.withType<PrepareSandboxTask> {
from("prettyPrinters") {
into("${intellij.pluginName}/prettyPrinters")
}
}
}
project(":intellij-toml") {
version = "0.2.0.${prop("buildNumber")}$channelSuffix"
val generateTomlLexer = task<GenerateLexer>("generateTomlLexer") {
source = "src/main/grammars/TomlLexer.flex"
targetDir = "src/gen/org/toml/lang/parse"
targetClass = "_TomlLexer"
purgeOldFiles = true
}
val generateTomlParser = task<GenerateParser>("generateTomlParser") {
source = "src/main/grammars/TomlParser.bnf"
targetRoot = "src/gen"
pathToParser = "/org/toml/lang/parse/TomlParser.java"
pathToPsiRoot = "/org/toml/lang/psi"
purgeOldFiles = true
}
tasks.withType<KotlinCompile> {
dependsOn(generateTomlLexer, generateTomlParser)
}
}
task("makeRelease") {
doLast {
val newChangelog = commitChangelog()
val newChangelogPath = newChangelog
.replace(".markdown", "")
.replaceFirst("-", "/").replaceFirst("-", "/").replaceFirst("-", "/")
val pluginXmlPath = "./src/main/resources/META-INF/plugin.xml"
val pluginXml = File(pluginXmlPath)
val oldText = pluginXml.readText()
val newText = oldText.replace(
"""https://intellij-rust\.github\.io/(.*)\.html""".toRegex(),
"https://intellij-rust.github.io/$newChangelogPath.html"
)
pluginXml.writeText(newText)
"git add $pluginXmlPath".execute()
"git commit -m Changelog".execute()
"git push".execute()
commitNightly()
}
}
fun commitChangelog(): String {
val website = "../intellij-rust.github.io"
val lastPost = File("$website/_posts").listFiles()
.map { it.name }
.sorted()
.last()
val postNumber = lastPost.substringAfterLast("-").substringBefore(".").toInt()
"python3 changelog.py -c".execute(website)
"git add _posts/$lastPost".execute(website)
listOf("git", "commit", "-m", "Changelog $postNumber").execute(website)
println()
"git show HEAD".execute(website)
println("Does ^^ look right? Answer `yes` to push changes\n")
val yes = readLine()!!.trim() == "yes"
if (!yes) error("Human says no")
"git push".execute(website)
return lastPost
}
fun commitNightly() {
// TODO: extract the latest versions of all supported platforms
val ideaArtifactName = "$platformVersion-EAP-SNAPSHOT"
val versionUrl = URL("https://www.jetbrains.com/intellij-repository/snapshots/com/jetbrains/intellij/idea/BUILD/$ideaArtifactName/BUILD-$ideaArtifactName.txt")
val ideaVersion = versionUrl.openStream().bufferedReader().readLine().trim()
println("\n NEW IDEA: $ideaVersion\n")
"rustup update nightly".execute()
val version = "rustup run nightly rustc --version".execute()
val date = """\d\d\d\d-\d\d-\d\d""".toRegex().find(version)!!.value
val rustVersion = "nightly-$date"
println("\n NEW RUST: $rustVersion\n")
val travisYml = File(rootProject.projectDir, ".travis.yml")
val updated = travisYml.readLines().joinToString("\n") { line ->
if ("modified by script" in line) {
line.replace("""RUST_VERSION=[\w\-\.]+""".toRegex(), "RUST_VERSION=$rustVersion")
.replace("""ORG_GRADLE_PROJECT_ideaVersion=[\w\-\.]+""".toRegex(), "ORG_GRADLE_PROJECT_ideaVersion=$ideaVersion")
} else {
line
}
}
travisYml.writeText(updated)
"git branch -Df nightly".execute(ignoreExitCode = true)
"git checkout -b nightly".execute()
"git add .travis.yml".execute()
listOf("git", "commit", "-m", ":arrow_up: nightly IDEA & rust").execute()
"git push origin nightly".execute()
}
task("updateCompilerFeatures") {
doLast {
val featureGateUrl = URL("https://raw.githubusercontent.com/rust-lang/rust/master/src/libsyntax/feature_gate.rs")
val text = featureGateUrl.openStream().bufferedReader().readText()
val file = File("src/main/kotlin/org/rust/lang/core/CompilerFeatures.kt")
file.bufferedWriter().use {
it.writeln("""
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
@file:Suppress("unused")
package org.rust.lang.core
import org.rust.lang.core.FeatureState.*
""".trimIndent())
it.writeFeatures("active", text)
it.writeln()
it.writeFeatures("accepted", text)
}
}
}
fun Writer.writeFeatures(featureSet: String, text: String) {
"""((\s*//.*\n)*)\s*\($featureSet, (\w+), (\"\d+\.\d+\.\d+\"), .*\),"""
.toRegex(RegexOption.MULTILINE)
.findAll(text)
.forEach { matcher ->
val (comments, _, featureName, version) = matcher.destructured
if (comments.isNotEmpty()) {
writeln(comments.trimIndent().trim())
}
writeln("""val ${featureName.toUpperCase()} = CompilerFeature("$featureName", ${featureSet.toUpperCase()}, $version)""")
}
}
fun Writer.writeln(str: String = "") {
write(str)
write("\n")
}
fun hasProp(name: String): Boolean = extra.has(name)
fun prop(name: String): String =
extra.properties[name] as? String
?: error("Property `$name` is not defined in gradle.properties")
inline operator fun <T : Task> T.invoke(a: T.() -> Unit): T = apply(a)
val SourceSet.kotlin: SourceDirectorySet
get() =
(this as HasConvention)
.convention
.getPlugin(KotlinSourceSet::class.java)
.kotlin
fun SourceSet.kotlin(action: SourceDirectorySet.() -> Unit) =
kotlin.action()
fun String.execute(wd: String? = null, ignoreExitCode: Boolean = false): String =
split(" ").execute(wd, ignoreExitCode)
fun List<String>.execute(wd: String? = null, ignoreExitCode: Boolean = false): String {
val process = ProcessBuilder(this)
.also { pb -> wd?.let { pb.directory(File(it)) } }
.start()
var result = ""
val errReader = thread { process.errorStream.bufferedReader().forEachLine { println(it) } }
val outReader = thread {
process.inputStream.bufferedReader().forEachLine { line ->
println(line)
result += line
}
}
process.waitFor()
outReader.join()
errReader.join()
if (process.exitValue() != 0 && !ignoreExitCode) error("Non-zero exit status for `$this`")
return result
}