Files
HMCL/HMCL/build.gradle

140 lines
4.6 KiB
Groovy
Raw Normal View History

plugins {
id 'com.github.johnrengelman.shadow' version '2.0.4'
}
2018-07-31 19:39:21 +08:00
import java.nio.file.FileSystems
import java.security.KeyFactory
2017-08-10 19:34:19 +08:00
import java.security.MessageDigest
2018-07-31 19:39:21 +08:00
import java.security.Signature
import java.security.spec.PKCS8EncodedKeySpec
2018-08-04 22:12:02 +08:00
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.jar.Pack200
2018-08-05 09:37:55 +08:00
import java.util.zip.GZIPOutputStream
2018-07-31 19:39:21 +08:00
import java.util.zip.ZipFile
2018-08-04 22:12:02 +08:00
import java.nio.file.Files
2017-08-10 19:34:19 +08:00
2018-08-05 01:02:42 +08:00
import org.tukaani.xz.LZMA2Options
import org.tukaani.xz.XZOutputStream
2018-07-31 13:46:31 +08:00
def buildnumber = System.getenv("BUILD_NUMBER") ?: "SNAPSHOT"
if (System.getenv("BUILD_NUMBER") != null && System.getenv("BUILD_NUMBER_OFFSET") != null)
buildnumber = (Integer.parseInt(System.getenv("BUILD_NUMBER")) - Integer.parseInt(System.getenv("BUILD_NUMBER_OFFSET"))).toString()
2018-09-08 22:14:06 +08:00
def versionroot = System.getenv("VERSION_ROOT") ?: "3.2"
2018-07-31 13:46:31 +08:00
version = versionroot + '.' + buildnumber
2017-08-10 19:34:19 +08:00
2015-06-22 16:47:05 +08:00
dependencies {
2017-02-15 20:28:01 +08:00
compile project(":HMCLCore")
2017-08-01 13:51:48 +08:00
compile rootProject.files("lib/JFoenix.jar")
2017-08-10 19:34:19 +08:00
}
2018-07-31 19:39:21 +08:00
def digest(String algorithm, byte[] bytes) {
return MessageDigest.getInstance(algorithm).digest(bytes)
}
2018-07-31 15:48:29 +08:00
def createChecksum(File file) {
def algorithm = "SHA-1"
def suffix = "sha1"
2018-07-31 19:39:21 +08:00
new File(file.parentFile, file.name + "." + suffix).text = digest(algorithm, file.bytes).encodeHex().toString() + "\n"
}
2018-08-04 22:12:02 +08:00
def attachSignature(File jar) {
2018-07-31 19:39:21 +08:00
def keyLocation = System.getenv("HMCL_SIGNATURE_KEY");
2018-08-04 22:12:02 +08:00
if (keyLocation == null)
return
2018-07-31 19:39:21 +08:00
def privatekey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(new File(keyLocation).bytes))
def signer = Signature.getInstance("SHA512withRSA")
signer.initSign(privatekey)
2018-08-04 22:12:02 +08:00
new ZipFile(jar).withCloseable { zip ->
2018-07-31 19:39:21 +08:00
zip.stream()
2018-08-04 22:12:02 +08:00
.sorted(Comparator.comparing { it.name })
.filter { it.name != "META-INF/hmcl_signature" }
.forEach {
2018-08-05 00:42:05 +08:00
signer.update(digest("SHA-512", it.name.getBytes("UTF-8")))
signer.update(digest("SHA-512", zip.getInputStream(it).bytes))
}
2018-07-31 19:39:21 +08:00
}
def signature = signer.sign()
2018-08-04 22:12:02 +08:00
FileSystems.newFileSystem(URI.create("jar:" + jar.toURI()), [:]).withCloseable { zipfs ->
2018-08-05 09:37:55 +08:00
Files.newOutputStream(zipfs.getPath("META-INF/hmcl_signature")).withCloseable { it << signature }
2018-07-31 19:39:21 +08:00
}
2018-07-31 15:48:29 +08:00
}
2018-08-05 00:42:05 +08:00
ext.packer = Pack200.newPacker()
packer.properties()["pack.effort"] = "9"
ext.unpacker = Pack200.newUnpacker()
def repack(File file) {
def packed = new ByteArrayOutputStream()
new JarFile(file).withCloseable { packer.pack(it, packed) }
new JarOutputStream(file.newOutputStream()).withCloseable { unpacker.unpack(new ByteArrayInputStream(packed.toByteArray()), it) }
}
2017-08-10 19:34:19 +08:00
jar {
manifest {
2018-07-29 16:33:14 +08:00
attributes 'Created-By': 'Copyright(c) 2013-2018 huangyuhui.',
2018-08-04 22:12:02 +08:00
'Main-Class': 'org.jackhuang.hmcl.Main',
'Multi-Release': 'true',
'Implementation-Version': version
2017-08-10 19:34:19 +08:00
}
finalizedBy shadowJar
}
shadowJar {
classifier = null
exclude 'META-INF/maven/**'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/LICENSE.txt'
dependencies {
exclude(dependency('org.jetbrains:annotations'))
}
2017-08-10 19:34:19 +08:00
doLast {
repack(jar.archivePath)
attachSignature(jar.archivePath)
createChecksum(jar.archivePath)
2017-08-10 19:34:19 +08:00
}
}
2018-07-31 15:48:29 +08:00
def createExecutable(String suffix, String header) {
def output = new File(jar.archivePath.parentFile, jar.archivePath.name[0..-4] + suffix)
output.bytes = new File(project.projectDir, header).bytes
output << jar.archivePath.bytes
createChecksum(output)
}
2018-07-19 00:54:48 +08:00
2018-08-05 09:37:55 +08:00
task makePack(dependsOn: jar) {
ext.outputPath = new File(jar.archivePath.parentFile, jar.archivePath.name[0..-4] + "pack")
doLast {
outputPath.newOutputStream().withCloseable { out ->
new JarFile(jar.archivePath).withCloseable { jarFile -> packer.pack(jarFile, out) }
}
createChecksum(outputPath)
2018-08-04 22:12:02 +08:00
}
2018-08-05 09:37:55 +08:00
}
task makePackXz(dependsOn: makePack) doLast {
def packXz = new File(makePack.outputPath.parentFile, makePack.outputPath.name + ".xz")
new XZOutputStream(packXz.newOutputStream(), new LZMA2Options(6)).withCloseable { it << makePack.outputPath.bytes }
2018-08-05 09:37:55 +08:00
createChecksum(packXz)
}
2018-08-04 22:12:02 +08:00
2018-08-05 09:37:55 +08:00
task makePackGz(dependsOn: makePack) doLast {
def packGz = new File(makePack.outputPath.parentFile, makePack.outputPath.name + ".gz")
new GZIPOutputStream(packGz.newOutputStream()).withCloseable { it << makePack.outputPath.bytes }
createChecksum(packGz)
2018-08-04 22:12:02 +08:00
}
2018-07-31 15:48:29 +08:00
task makeExecutables(dependsOn: jar) doLast {
createExecutable("exe", "src/main/resources/assets/HMCLauncher.exe")
2017-08-27 12:49:56 +08:00
}
2018-08-05 01:02:42 +08:00
build.dependsOn makePackXz
2018-08-05 09:37:55 +08:00
build.dependsOn makePackGz
2018-07-31 15:48:29 +08:00
build.dependsOn makeExecutables