-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
127 lines (113 loc) · 4.14 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
import java.util.zip.ZipOutputStream
import java.util.zip.ZipEntry
plugins {
java
application
kotlin("jvm") version "1.4.0"
}
group = "com.mivik"
version = "0.1.0"
object Config {
const val llvmVersion = "10.0.0"
const val javacppVersion = "1.5.3"
}
application {
mainClassName = "com.mivik.kamet.CLIMainKt"
}
repositories {
mavenCentral()
maven("https://jitpack.io")
maven("https://oss.sonatype.org/content/repositories/snapshots/")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("com.github.KiotLand.kiot-lexer:kiot-lexer:1.0.6.2")
implementation("org.bytedeco:llvm-platform:${Config.llvmVersion}-${Config.javacppVersion}")
implementation("com.xenomachina:kotlin-argparser:2.0.7")
testImplementation(kotlin("test-junit"))
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = freeCompilerArgs + "-Xopt-in=kotlin.RequiresOptIn"
}
}
abstract class PlatformDistTask : DefaultTask() {
@get:Incremental
@get:PathSensitive(PathSensitivity.NAME_ONLY)
@get:InputDirectory
val inputDir: File
get() = File("${project.buildDir}/install/${project.name}")
@get:OutputDirectory
val outputDir: File = File("${project.buildDir}/distributions/${project.name}")
@TaskAction
fun execute(inputChanges: InputChanges) {
outputDir.mkdirs()
val distDir = File(project.buildDir, "install/${project.name}")
val binDir = File(distDir, "bin")
val libDir = File(distDir, "lib")
val javacppPrefix = "javacpp-${Config.javacppVersion}-"
val llvmPrefix = "llvm-${Config.llvmVersion}-${Config.javacppVersion}-"
fun platformOf(name: String) =
when {
name.startsWith(javacppPrefix) -> name.substring(javacppPrefix.length, name.lastIndexOf('.'))
name.startsWith(llvmPrefix) -> name.substring(llvmPrefix.length, name.lastIndexOf('.'))
else -> null
}
fun splitLine(content: String, pattern: String, fileType: String): Pair<String, String> {
val beginIndex =
(content.indexOf(pattern).takeIf { it != -1 } ?: error("Corrupted $fileType file")) + pattern.length
val endIndex = content.indexOf('\n', beginIndex)
return Pair(
content.substring(0, beginIndex),
content.substring(endIndex)
)
}
val (shellBeginPart, shellEndPart) =
splitLine(File(binDir, project.name).readText(), "\nCLASSPATH=", "shell")
val (batBeginPart, batEndPart) =
splitLine(File(binDir, "${project.name}.bat").readText(), "set CLASSPATH=", "bat")
val libFiles = (libDir.listFiles() ?: error("Could not found javacpp libs in distribution"))
val platforms = libFiles.mapNotNull {
// javacpp has distribution for some platforms while llvm hasn't
if (it.name.startsWith(llvmPrefix)) platformOf(it.name)
else null
}.toSet().toList()
for (platform in platforms) {
println("Distributing for $platform")
val outputFile = File(outputDir, "${project.name}-${project.version}-$platform.zip")
ZipOutputStream(outputFile.outputStream().buffered()).use { zip ->
zip.putNextEntry(ZipEntry("lib/"))
val shellClassPath = StringBuilder()
val batClassPath = StringBuilder()
for (lib in libFiles)
if (platformOf(lib.name).let {
it == null || it == platform
}) {
shellClassPath.append("\$APP_HOME/lib/${lib.name}:")
batClassPath.append("%APP_HOME%\\lib\\${lib.name};")
zip.putNextEntry(ZipEntry("lib/${lib.name}"))
lib.inputStream().copyTo(zip)
zip.closeEntry()
}
if (shellClassPath.isNotEmpty()) shellClassPath.deleteCharAt(shellClassPath.lastIndex)
if (batClassPath.isNotEmpty()) batClassPath.deleteCharAt(batClassPath.lastIndex)
zip.putNextEntry(ZipEntry("bin/"))
zip.putNextEntry(ZipEntry("bin/${project.name}"))
zip.write(shellBeginPart.toByteArray())
zip.write(shellClassPath.toString().toByteArray())
zip.write(shellEndPart.toByteArray())
zip.closeEntry()
zip.putNextEntry(ZipEntry("bin/${project.name}.bat"))
zip.write(batBeginPart.toByteArray())
zip.write(batClassPath.toString().toByteArray())
zip.write(batEndPart.toByteArray())
zip.closeEntry()
}
}
}
}
tasks.register<PlatformDistTask>("platformDist") {
group = "distribution"
dependsOn += tasks["installDist"]
}