-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdeploy.gradle
More file actions
246 lines (196 loc) · 8.67 KB
/
Copy pathdeploy.gradle
File metadata and controls
246 lines (196 loc) · 8.67 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import org.gradle.internal.os.OperatingSystem
// ----------------------- Publishing (kept from your original) -----------------------
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact tasks.shadowJar
groupId = project.group ?: 'de.peeeq'
artifactId = 'wurstscript'
version = project.version
}
}
repositories {
mavenLocal()
// or: maven { url = uri("${buildDir}/repo") }
}
}
def toolJavaHome = {
def svc = project.extensions.getByType(JavaToolchainService)
def launcher = svc.launcherFor(project.java.toolchain)
launcher.get().metadata.installationPath.asFile
}
def toolExecutable = { String toolName ->
def javaHome = System.getenv('JAVA_HOME') ? new File(System.getenv('JAVA_HOME')) : toolJavaHome()
def ext = OperatingSystem.current().isWindows() ? ".exe" : ""
new File(javaHome, "bin/${toolName}${ext}").absolutePath
}
// ----------------------- Common providers/locations ---------------------------------
def fatJar = tasks.named('shadowJar').flatMap { it.archiveFile }
// Allow adding modules if reflection/ServiceLoader pulls them in:
// ./gradlew jlinkRuntime25 -PextraJdkModules=java.desktop,jdk.crypto.ec
def extraJdkModules = (project.findProperty("extraJdkModules") ?: "").toString().trim()
def jlinkWorkDir = layout.buildDirectory.dir("jlink")
def modulesTxt = jlinkWorkDir.map { it.file("modules.txt") }
def jreImageDir = layout.buildDirectory.dir("jre-wurst-25")
def os = OperatingSystem.current()
def arch = System.getProperty("os.arch") // "amd64"/"x86_64"/"aarch64"
def plat = os.isWindows() ? "win-x64"
: os.isMacOsX() ? (arch == "aarch64" ? "macos-arm64" : "macos-x64")
: "linux-x64"
def distRoot = layout.buildDirectory.dir("dist/slim-${plat}")
def releasesDir = layout.buildDirectory.dir("releases")
// ----------------------- Tasks: jdeps → jlink → assemble → package ------------------
// 1) Detect JDK modules via jdeps (from the fat jar)
tasks.register("jdepsModules") {
description = "Detects required JDK modules for the fat compiler.jar via jdeps"
group = "distribution"
inputs.file(fatJar)
outputs.file(modulesTxt)
doLast {
ExecOperations execOps = project.services.get(ExecOperations)
def jdeps = toolExecutable("jdeps")
def outBuf = new ByteArrayOutputStream()
modulesTxt.get().asFile.parentFile.mkdirs()
execOps.exec {
commandLine jdeps,
"--multi-release", "25",
"--ignore-missing-deps",
"--print-module-deps",
fatJar.get().asFile.absolutePath
standardOutput = outBuf
}
def detected = outBuf.toString().trim()
if (detected.isEmpty()) detected = "java.base"
if (!extraJdkModules.isEmpty()) detected = detected + "," + extraJdkModules
modulesTxt.get().asFile.text = detected
logger.lifecycle("[jdeps] Using modules: ${detected}")
}
}
// 2) Build slim runtime with jlink (overwrite if exists)
// 2) Build slim runtime with jlink (overwrite if exists)
tasks.register("jlinkRuntime25") {
description = "Builds a slim Java 25 runtime image containing only the needed modules"
group = "distribution"
dependsOn("shadowJar", "jdepsModules")
inputs.file(modulesTxt)
outputs.dir(jreImageDir)
doLast {
ExecOperations execOps = project.services.get(ExecOperations)
// Resolve Java home from toolchain (fallback to JAVA_HOME if set)
def svc = project.extensions.getByType(JavaToolchainService)
def launcher = svc.launcherFor(project.java.toolchain)
File javaHome = System.getenv('JAVA_HOME') ? new File(System.getenv('JAVA_HOME')) : launcher.get().metadata.installationPath.asFile
def jlink = new File(javaHome, "bin/${OperatingSystem.current().isWindows() ? 'jlink.exe' : 'jlink'}").absolutePath
def jmodsDir = new File(javaHome, "jmods").absolutePath
def mods = modulesTxt.get().asFile.text.trim()
def outDir = jreImageDir.get().asFile
if (!mods) throw new GradleException("No modules detected for jlink.")
// jlink requires the output dir to NOT exist
if (outDir.exists()) {
project.delete(outDir)
}
outDir.parentFile.mkdirs()
logger.lifecycle("[jlink] Using: ${jlink}")
logger.lifecycle("[jlink] JAVA_HOME: ${javaHome}")
logger.lifecycle("[jlink] jmods: ${jmodsDir}")
logger.lifecycle("[jlink] Modules: ${mods}")
def errBuf = new ByteArrayOutputStream()
def outBuf = new ByteArrayOutputStream()
def result = execOps.exec {
commandLine jlink,
"--verbose",
"--module-path", jmodsDir,
"--add-modules", mods,
"--no-header-files",
"--no-man-pages",
"--strip-debug",
"--compress=zip-6",
"--output", outDir.absolutePath
errorOutput = errBuf
standardOutput = outBuf
ignoreExitValue = true
}
if (result.exitValue != 0) {
logger.lifecycle("[jlink][stdout]\n${outBuf.toString()}")
logger.error("[jlink][stderr]\n${errBuf.toString()}")
throw new GradleException("jlink failed with exit ${result.exitValue}")
}
logger.lifecycle("[jlink] Runtime created at: ${outDir}")
}
}
// 3) Assemble folder layout: jre + compiler.jar (no manifest)
tasks.register("assembleSlimCompilerDist", Copy) {
description = "Assembles dist folder with slim JRE and compiler.jar (no manifest)."
group = "distribution"
dependsOn("jlinkRuntime25", "shadowJar")
from(jreImageDir) { into("wurst-runtime") }
from(fatJar) { into("wurst-compiler") }
into(distRoot)
doLast {
logger.lifecycle("[dist] Folder ready at: ${distRoot.get().asFile}")
}
}
tasks.named("assembleSlimCompilerDist", Copy) { t ->
if (os.isWindows()) {
from("../Wurstpack/wurstscript/wurstscript.cmd") { into(".") }
} else {
from("../Wurstpack/wurstscript/wurstscript") {
into(".")
}
}
}
// 4a) Package ZIP on Windows
tasks.register("packageSlimCompilerDistZip", Zip) {
description = "Packages slim dist as a ZIP archive (Windows)."
group = "distribution"
enabled = os.isWindows()
dependsOn("assembleSlimCompilerDist")
from(distRoot)
destinationDirectory.set(releasesDir)
archiveFileName.set("wurst-compiler-${project.version}-${plat}.zip")
}
// 4b) Package tar.gz on Linux/macOS
tasks.register("packageSlimCompilerDistTar", Tar) {
description = "Packages slim dist as a tar.gz archive (Linux/macOS)."
group = "distribution"
enabled = !os.isWindows()
dependsOn("assembleSlimCompilerDist")
from(distRoot)
destinationDirectory.set(releasesDir)
compression = Compression.GZIP
archiveExtension.set("tar.gz")
archiveFileName.set("wurst-compiler-${project.version}-${plat}.tar.gz")
}
// 4c) OS-aware convenience wrapper
tasks.register("packageSlimCompilerDist") {
description = "Packages slim dist for the current platform (zip on Windows, tar.gz elsewhere)."
group = "distribution"
dependsOn(os.isWindows() ? "packageSlimCompilerDistZip" : "packageSlimCompilerDistTar")
}
// 5) SHA-256 checksum for the packaged archive
tasks.register("checksumSlimCompilerDist") {
description = "Writes SHA-256 alongside the packaged archive."
group = "distribution"
dependsOn("packageSlimCompilerDist")
outputs.upToDateWhen { false }
doLast {
def outDir = releasesDir.get().asFile
def expectedZip = new File(outDir, "wurst-compiler-${project.version}-${plat}.zip")
def expectedTarGz = new File(outDir, "wurst-compiler-${project.version}-${plat}.tar.gz")
def archFile = expectedZip.exists() ? expectedZip : (expectedTarGz.exists() ? expectedTarGz : null)
if (archFile == null) throw new GradleException("Archive not found in ${outDir}")
def md = java.security.MessageDigest.getInstance("SHA-256")
archFile.withInputStream { is ->
byte[] buf = new byte[8192]
for (int r = is.read(buf); r != -1; r = is.read(buf)) {
md.update(buf, 0, r)
}
}
def hex = md.digest().collect { String.format("%02x", it) }.join()
def sumFile = new File(outDir, archFile.name + ".sha256")
sumFile.text = "${hex} ${archFile.name}\n"
logger.lifecycle("[sha256] ${sumFile.name} -> ${hex}")
logger.lifecycle("[release] Upload: ${archFile.absolutePath}")
}
}