forked from yairm210/Unciv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
171 lines (141 loc) · 5.83 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
import com.unciv.build.BuildConfig
plugins {
id("kotlin")
}
sourceSets {
main {
java.srcDir("src/")
}
}
val mainClassName = "com.unciv.app.server.UncivServer"
val assetsDir = file("../android/assets")
val deployFolder = file("../deploy")
// See https://github.com/libgdx/libgdx/wiki/Starter-classes-and-configuration#common-issues
// and https://github.com/yairm210/Unciv/issues/5679
val jvmArgsForMac = listOf("-XstartOnFirstThread", "-Djava.awt.headless=true")
tasks.register<JavaExec>("run") {
jvmArgs = mutableListOf<String>()
if ("mac" in System.getProperty("os.name").lowercase())
(jvmArgs as MutableList<String>).addAll(jvmArgsForMac)
// These are non-standard, only available/necessary on Mac.
dependsOn(tasks.getByName("classes"))
mainClass.set(mainClassName)
classpath = sourceSets.main.get().runtimeClasspath
standardInput = System.`in`
workingDir = assetsDir
isIgnoreExitValue = true
}
tasks.register<JavaExec>("debug") {
jvmArgs = jvmArgsForMac
dependsOn(tasks.getByName("classes"))
mainClass.set(mainClassName)
classpath = sourceSets.main.get().runtimeClasspath
standardInput = System.`in`
workingDir = assetsDir
isIgnoreExitValue = true
debug = true
}
tasks.register<Jar>("dist") { // Compiles the jar file
dependsOn(tasks.getByName("classes"))
// META-INF/INDEX.LIST and META-INF/io.netty.versions.properties are duplicated, but I don't know why
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from(files(sourceSets.main.get().output.resourcesDir))
from(files(sourceSets.main.get().output.classesDirs))
// see Laurent1967's comment on https://github.com/libgdx/libgdx/issues/5491
from({ configurations.compileClasspath.get().resolve().map { if (it.isDirectory) it else zipTree(it) } })
archiveFileName.set("UncivServer.jar")
manifest {
attributes(mapOf("Main-Class" to mainClassName, "Specification-Version" to BuildConfig.appVersion))
}
}
enum class Platform(val desc: String) {
Windows32("windows32"), Windows64("windows64"), Linux32("linux32"), Linux64("linux64"), MacOS("mac");
}
class PackrConfig(
var platform: Platform? = null,
var jdk: String? = null,
var executable: String? = null,
var classpath: List<String>? = null,
var removePlatformLibs: List<String>? = null,
var mainClass: String? = null,
var vmArgs: List<String>? = null,
var minimizeJre: String? = null,
var cacheJre: File? = null,
var resources: List<File>? = null,
var outDir: File? = null,
var platformLibsOutDir: File? = null,
var iconResource: File? = null,
var bundleIdentifier: String? = null
)
for (platform in Platform.values()) {
val platformName = platform.toString()
tasks.create("packr${platformName}") {
dependsOn(tasks.getByName("dist"))
// Needs to be here and not in doLast because the zip task depends on the outDir
val jarFile = "$rootDir/server/build/libs/UncivServer.jar"
val config = PackrConfig()
config.platform = platform
config.apply {
executable = "UncivServer"
classpath = listOf(jarFile)
removePlatformLibs = config.classpath
mainClass = mainClassName
vmArgs = listOf("Xmx1G")
minimizeJre = "server/packrConfig.json"
outDir = file("packr")
}
doLast {
// https://gist.github.com/seanf/58b76e278f4b7ec0a2920d8e5870eed6
fun String.runCommand(workingDir: File) {
val process = ProcessBuilder(*split(" ").toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
if (!process.waitFor(30, TimeUnit.SECONDS)) {
process.destroy()
throw RuntimeException("execution timed out: $this")
}
if (process.exitValue() != 0) {
println("execution returned code ${process.exitValue()}: $this")
}
println(process.inputStream.bufferedReader().readText())
}
if (config.outDir!!.exists()) delete(config.outDir)
// Requires that both packr and the jre are downloaded, as per buildAndDeploy.yml, "Upload to itch.io"
val jdkFile =
when (platform) {
Platform.Linux64 -> "jre-linux-64.tar.gz"
Platform.Windows64 -> "jdk-windows-64.zip"
else -> "jre-macOS.tar.gz"
}
val platformNameForPackrCmd =
if (platform == Platform.MacOS) "mac"
else platform.name.lowercase()
val command = "java -jar $rootDir/packr-all-4.0.0.jar" +
" --platform $platformNameForPackrCmd" +
" --jdk $jdkFile" +
" --executable UncivServer" +
" --classpath $jarFile" +
" --mainclass $mainClassName" +
" --vmargs Xmx1G " +
(if (platform == Platform.MacOS) jvmArgsForMac.joinToString(" ") {
it.removePrefix("-")
}
else "") +
" --output ${config.outDir}"
command.runCommand(rootDir)
}
tasks.register<Zip>("zip${platformName}") {
archiveFileName.set("UncivServer-${platformName}.zip")
from(config.outDir)
destinationDirectory.set(deployFolder)
}
finalizedBy("zip${platformName}")
}
}
tasks.register<Zip>("zipLinuxFilesForJar") {
archiveFileName.set("linuxFilesForJar.zip")
from(file("linuxFilesForJar"))
destinationDirectory.set(deployFolder)
}