|
| 1 | +import com.badlogicgames.packr.Packr |
| 2 | +import com.badlogicgames.packr.PackrConfig |
| 3 | +import com.unciv.build.BuildConfig |
| 4 | + |
| 5 | +plugins { |
| 6 | + id("kotlin") |
| 7 | +} |
| 8 | + |
| 9 | +java { |
| 10 | + sourceCompatibility = JavaVersion.VERSION_1_8 |
| 11 | +} |
| 12 | + |
| 13 | +sourceSets { |
| 14 | + main { |
| 15 | + java.srcDir("src/") |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +val mainClassName = "com.unciv.app.server.UncivServer" |
| 20 | +val assetsDir = file("../android/assets") |
| 21 | +val deployFolder = file("../deploy") |
| 22 | + |
| 23 | +// See https://github.com/libgdx/libgdx/wiki/Starter-classes-and-configuration#common-issues |
| 24 | +// and https://github.com/yairm210/Unciv/issues/5679 |
| 25 | +val jvmArgsForMac = listOf("-XstartOnFirstThread", "-Djava.awt.headless=true") |
| 26 | +tasks.register<JavaExec>("run") { |
| 27 | + jvmArgs = mutableListOf<String>() |
| 28 | + if ("mac" in System.getProperty("os.name").toLowerCase()) |
| 29 | + (jvmArgs as MutableList<String>).addAll(jvmArgsForMac) |
| 30 | + // These are non-standard, only available/necessary on Mac. |
| 31 | + |
| 32 | + dependsOn(tasks.getByName("classes")) |
| 33 | + |
| 34 | + main = mainClassName |
| 35 | + classpath = sourceSets.main.get().runtimeClasspath |
| 36 | + standardInput = System.`in` |
| 37 | + workingDir = assetsDir |
| 38 | + isIgnoreExitValue = true |
| 39 | +} |
| 40 | + |
| 41 | +tasks.register<JavaExec>("debug") { |
| 42 | + jvmArgs = jvmArgsForMac |
| 43 | + dependsOn(tasks.getByName("classes")) |
| 44 | + main = mainClassName |
| 45 | + classpath = sourceSets.main.get().runtimeClasspath |
| 46 | + standardInput = System.`in` |
| 47 | + workingDir = assetsDir |
| 48 | + isIgnoreExitValue = true |
| 49 | + debug = true |
| 50 | +} |
| 51 | + |
| 52 | +tasks.register<Jar>("dist") { // Compiles the jar file |
| 53 | + dependsOn(tasks.getByName("classes")) |
| 54 | + |
| 55 | + // META-INF/INDEX.LIST and META-INF/io.netty.versions.properties are duplicated, but I don't know why |
| 56 | + duplicatesStrategy = DuplicatesStrategy.EXCLUDE |
| 57 | + |
| 58 | + from(files(sourceSets.main.get().output.resourcesDir)) |
| 59 | + from(files(sourceSets.main.get().output.classesDirs)) |
| 60 | + // see Laurent1967's comment on https://github.com/libgdx/libgdx/issues/5491 |
| 61 | + from({ configurations.compileClasspath.get().resolve().map { if (it.isDirectory) it else zipTree(it) } }) |
| 62 | + archiveFileName.set("UncivServer.jar") |
| 63 | + |
| 64 | + manifest { |
| 65 | + attributes(mapOf("Main-Class" to mainClassName, "Specification-Version" to BuildConfig.appVersion)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +for (platform in PackrConfig.Platform.values()) { |
| 70 | + val platformName = platform.toString() |
| 71 | + |
| 72 | + tasks.create("packr${platformName}") { |
| 73 | + dependsOn(tasks.getByName("dist")) |
| 74 | + |
| 75 | + // Needs to be here and not in doLast because the zip task depends on the outDir |
| 76 | + val jarFile = "$rootDir/server/build/libs/UncivServer.jar" |
| 77 | + val config = PackrConfig() |
| 78 | + config.platform = platform |
| 79 | + |
| 80 | + config.apply { |
| 81 | + executable = "UncivServer" |
| 82 | + classpath = listOf(jarFile) |
| 83 | + removePlatformLibs = config.classpath |
| 84 | + mainClass = mainClassName |
| 85 | + vmArgs = listOf("Xmx1G") |
| 86 | + minimizeJre = "server/packrConfig.json" |
| 87 | + outDir = file("packr") |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + doLast { |
| 92 | + // https://gist.github.com/seanf/58b76e278f4b7ec0a2920d8e5870eed6 |
| 93 | + fun String.runCommand(workingDir: File) { |
| 94 | + val process = ProcessBuilder(*split(" ").toTypedArray()) |
| 95 | + .directory(workingDir) |
| 96 | + .redirectOutput(ProcessBuilder.Redirect.PIPE) |
| 97 | + .redirectError(ProcessBuilder.Redirect.PIPE) |
| 98 | + .start() |
| 99 | + |
| 100 | + if (!process.waitFor(30, TimeUnit.SECONDS)) { |
| 101 | + process.destroy() |
| 102 | + throw RuntimeException("execution timed out: $this") |
| 103 | + } |
| 104 | + if (process.exitValue() != 0) { |
| 105 | + println("execution returned code ${process.exitValue()}: $this") |
| 106 | + } |
| 107 | + println(process.inputStream.bufferedReader().readText()) |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + if (config.outDir.exists()) delete(config.outDir) |
| 112 | + |
| 113 | + // Requires that both packr and the jre are downloaded, as per buildAndDeploy.yml, "Upload to itch.io" |
| 114 | + |
| 115 | + // Use old version of packr - newer versions aren't Windows32-compliant |
| 116 | + if (platform == PackrConfig.Platform.Windows32) { |
| 117 | + config.jdk = "jdk-windows-32.zip" |
| 118 | + Packr().pack(config) |
| 119 | + } else { |
| 120 | + val jdkFile = |
| 121 | + when (platform) { |
| 122 | + PackrConfig.Platform.Linux64 -> "jre-linux-64.tar.gz" |
| 123 | + PackrConfig.Platform.Windows64 -> "jdk-windows-64.zip" |
| 124 | + else -> "jre-macOS.tar.gz" |
| 125 | + } |
| 126 | + |
| 127 | + val platformNameForPackrCmd = |
| 128 | + if (platform == PackrConfig.Platform.MacOS) "mac" |
| 129 | + else platform.name.toLowerCase() |
| 130 | + |
| 131 | + val command = "java -jar $rootDir/packr-all-4.0.0.jar" + |
| 132 | + " --platform $platformNameForPackrCmd" + |
| 133 | + " --jdk $jdkFile" + |
| 134 | + " --executable UncivServer" + |
| 135 | + " --classpath $jarFile" + |
| 136 | + " --mainclass $mainClassName" + |
| 137 | + " --vmargs Xmx1G " + |
| 138 | + (if (platform == PackrConfig.Platform.MacOS) jvmArgsForMac.joinToString(" ") { |
| 139 | + it.removePrefix("-") |
| 140 | + } |
| 141 | + else "") + |
| 142 | + " --output ${config.outDir}" |
| 143 | + command.runCommand(rootDir) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + tasks.register<Zip>("zip${platformName}") { |
| 148 | + archiveFileName.set("UncivServer-${platformName}.zip") |
| 149 | + from(config.outDir) |
| 150 | + destinationDirectory.set(deployFolder) |
| 151 | + } |
| 152 | + |
| 153 | + finalizedBy("zip${platformName}") |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +tasks.register<Zip>("zipLinuxFilesForJar") { |
| 158 | + archiveFileName.set("linuxFilesForJar.zip") |
| 159 | + from(file("linuxFilesForJar")) |
| 160 | + destinationDirectory.set(deployFolder) |
| 161 | +} |
0 commit comments