Skip to content

Commit 5a852e6

Browse files
committed
feat: timing, version resource file
1 parent 31c3170 commit 5a852e6

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

build.gradle.kts

+62-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import java.io.FileOutputStream
2+
import java.util.jar.*
3+
14
plugins {
25
kotlin("jvm") version "2.0.21"
36
kotlin("plugin.serialization") version "2.0.21"
47
id("com.gradleup.shadow") version "8.3.5"
58
}
69

7-
group = "io.github.sploonmc.builder"
8-
version = "1.0.0-SNAPSHOT"
10+
group = "io.github.sploonmc.bundler"
11+
version = "0.0.0"
912

1013
dependencies {
1114
implementation("com.github.codemonstur:simplexml:3.2.0")
@@ -14,16 +17,63 @@ dependencies {
1417
implementation("org.ow2.asm:asm:9.7.1")
1518
}
1619

17-
tasks.shadowJar {
18-
archiveClassifier = ""
19-
}
20+
tasks {
21+
shadowJar {
22+
archiveClassifier = ""
23+
version = ""
24+
minimize()
25+
}
2026

21-
tasks.build {
22-
dependsOn(tasks.shadowJar)
23-
}
27+
build {
28+
dependsOn(shadowJar)
29+
}
30+
31+
jar {
32+
manifest {
33+
attributes["Main-Class"] = "io.github.sploonmc.bundler.MainKt"
34+
}
35+
}
36+
37+
abstract class VersionTask : DefaultTask() {
38+
@get:Input
39+
abstract val version: Property<String>
40+
41+
init {
42+
group = "bundler-versioning"
43+
description = "Creates a jar for a specific Minecraft version: $version"
44+
}
2445

25-
tasks.jar {
26-
manifest {
27-
attributes["Main-Class"] = "io.github.sploonmc.builder.MainKt"
46+
@TaskAction
47+
fun createVersionedJar() {
48+
val version = version.get()
49+
50+
val jarFile = project.layout.buildDirectory.file("libs/${project.name}.jar").get().asFile
51+
val outputFile = jarFile.parentFile.resolve("${project.name}-$version.jar")
52+
outputFile.delete()
53+
54+
logger.lifecycle("Creating versioned jar: ${outputFile.absolutePath}")
55+
56+
JarFile(jarFile).use { jar ->
57+
JarOutputStream(FileOutputStream(outputFile)).use { output ->
58+
jar.entries().asSequence().forEach { entry ->
59+
output.putNextEntry(JarEntry(entry.name))
60+
jar.getInputStream(entry).use { it.copyTo(output) }
61+
}
62+
63+
output.putNextEntry(JarEntry("META-INF/sploon.version"))
64+
version.byteInputStream().use { it.copyTo(output) }
65+
}
66+
}
67+
68+
logger.lifecycle("Versioned jar created at: ${outputFile.absolutePath}")
69+
}
2870
}
29-
}
71+
72+
fun versionTask(version: String) = register<VersionTask>(version.replace(".", "_")) {
73+
dependsOn(shadowJar)
74+
this.version = version
75+
}
76+
77+
versionTask("1.21.3")
78+
versionTask("1.21.1")
79+
}

src/main/kotlin/io/github/sploonmc/bundler/Main.kt

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import kotlin.io.path.Path
44
import kotlin.system.exitProcess
55

66
fun main(args: Array<String>) {
7-
if (args.isEmpty()) {
7+
val versionResource = readResource("/META-INF/sploon.version")
8+
9+
if (args.isEmpty() && versionResource == null) {
810
println("Version argument not passed")
911
exitProcess(1)
1012
}
1113

14+
val version = versionResource ?: args[0]
15+
1216
val serverArgs = args.drop(1).toMutableList()
1317
if ("nogui" !in args) serverArgs.addFirst("nogui")
1418

15-
SploonBundler(args[0], Path(""), serverArgs.toTypedArray()).start()
16-
}
19+
SploonBundler(version, Path(""), serverArgs.toTypedArray()).start()
20+
}

src/main/kotlin/io/github/sploonmc/bundler/SploonBundler.kt

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import java.nio.file.FileVisitResult
1616
import java.nio.file.Path
1717
import java.nio.file.SimpleFileVisitor
1818
import java.nio.file.attribute.BasicFileAttributes
19+
import java.time.Duration
20+
import java.time.Instant
1921
import java.util.jar.JarFile
2022
import kotlin.io.path.ExperimentalPathApi
2123
import kotlin.io.path.copyTo
@@ -33,6 +35,7 @@ private const val SPLOON_PATCHES_REPO_BASE_URL = "https://raw.githubusercontent.
3335
class SploonBundler(val minecraftVersion: String, workDir: Path, val serverArgs: Array<String>) {
3436
private val pistonVersions = PistonAPI.getPistonVersions()
3537
private val versionMeta = pistonVersions.getVersionMeta(minecraftVersion)
38+
val startTime = Instant.now()
3639

3740
val bundlerDir = workDir.resolve("bundler")
3841

@@ -141,6 +144,8 @@ class SploonBundler(val minecraftVersion: String, workDir: Path, val serverArgs:
141144
}, "Server thread")
142145

143146
serverThread.contextClassLoader = classLoader
147+
println("Took " + Duration.between(startTime, Instant.now()).toSeconds().toString() + " seconds")
148+
144149
serverThread.start()
145150
}
146151

src/main/kotlin/io/github/sploonmc/bundler/utils.kt

+4
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ fun Path.sha1() = readBytes()
4848
.use {
4949
MessageDigest.getInstance("SHA-1").digest(it.readBytes())
5050
}.joinToString("") { "%02x".format(it) }
51+
52+
fun readResource(resourcePath: String): String? {
53+
return object {}.javaClass.getResourceAsStream(resourcePath)?.bufferedReader()?.use { it.readText() }
54+
}

0 commit comments

Comments
 (0)