-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
98 lines (85 loc) · 2.96 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
plugins {
id("io.github.tree-sitter.ktreesitter-plugin") version "0.23.0"
// kotlin("jvm")
kotlin("multiplatform")
id("sunnychung.publication")
}
group = "io.github.sunny-chung"
version = "1.0.0.0"
grammar {
/* Default options */
// The base directory of the grammar
baseDir.set(file("tree-sitter-graphql"))
// // The name of the C interop def file
// interopName.set("grammar")
// // The name of the JNI library
// libraryName.set("ktreesitter-${grammarName.get()}")
libraryName.set("tree-sitter-graphql")
/* Required options */
// The name of the grammar
grammarName.set("graphql")
// The name of the class
className.set("TreeSitterGraphql")
// The name of the package
packageName.set("io.github.dralletje.ktreesitter.graphql")
// The source files of the grammar
files.set(arrayOf(
baseDir.get().resolve("src/parser.c"),
// baseDir.get().resolve("src/scanner.c")
))
}
repositories {
mavenCentral()
}
val generateTask = tasks.generateGrammarFiles.get()
kotlin {
jvm {}
jvmToolchain(17)
sourceSets {
val generatedSrc = generateTask.generatedSrc.get()
configureEach {
kotlin.srcDir(generatedSrc.dir(name).dir("kotlin"))
println("Configure: $name")
}
commonMain {
resources.srcDir(generatedSrc.dir("resources"))
}
}
}
/**
* The purpose of this task is to disable default `System.loadLibrary()` calls,
* and fix the CMakeLists.txt for Windows.
*/
val enhancedGenerateTask = task("generateGrammarFilesEnhanced") {
dependsOn(generateTask)
doLast {
val generatedSrc = generateTask.generatedSrc.get()
val jvmSrc = generatedSrc.dir("jvmMain").dir("kotlin")
jvmSrc.asFileTree.filter { it.isFile && it.name.endsWith(".kt") }
.forEach { file ->
println("Check src ${file.name}")
val sourceCode = file.readText()
if (sourceCode.contains("^\\s*System\\.loadLibrary\\(".toRegex(RegexOption.MULTILINE))) {
println("Replacing ${file.name}")
val enhancedCode = sourceCode.replace("^(\\s*)(System\\.loadLibrary\\()".toRegex(RegexOption.MULTILINE)) {
"${it.groups[1]!!.value}// ${it.groups[2]!!.value}"
}
println(enhancedCode)
file.writeText(enhancedCode)
}
}
if (isWindowsOs()) {
val file = layout.buildDirectory.get().dir("generated").file("CMakeLists.txt").asFile
val sourceCode = file.readText()
if (sourceCode.contains("\\")) {
println("Replacing ${file.name}")
val enhancedCode = sourceCode.replace("\\", "/")
println(enhancedCode)
file.writeText(enhancedCode)
}
}
}
}
fun isWindowsOs(): Boolean {
return System.getProperty("os.name").startsWith("Win")
}