Refactor build.gradle

This commit is contained in:
yushijinhun 2018-08-05 00:42:05 +08:00
parent 3c2f232acb
commit ace830bced
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -1,5 +1,4 @@
import java.nio.file.FileSystems import java.nio.file.FileSystems
import java.nio.file.StandardOpenOption
import java.security.KeyFactory import java.security.KeyFactory
import java.security.MessageDigest import java.security.MessageDigest
import java.security.Signature import java.security.Signature
@ -43,17 +42,27 @@ def attachSignature(File jar) {
.sorted(Comparator.comparing { it.name }) .sorted(Comparator.comparing { it.name })
.filter { it.name != "META-INF/hmcl_signature" } .filter { it.name != "META-INF/hmcl_signature" }
.forEach { .forEach {
signer.update(digest("SHA-512", it.name.getBytes("UTF-8"))) signer.update(digest("SHA-512", it.name.getBytes("UTF-8")))
signer.update(digest("SHA-512", zip.getInputStream(it).bytes)) signer.update(digest("SHA-512", zip.getInputStream(it).bytes))
} }
} }
def signature = signer.sign() def signature = signer.sign()
FileSystems.newFileSystem(URI.create("jar:" + jar.toURI()), [:]).withCloseable { zipfs -> FileSystems.newFileSystem(URI.create("jar:" + jar.toURI()), [:]).withCloseable { zipfs ->
Files.newOutputStream(zipfs.getPath("META-INF/hmcl_signature"), StandardOpenOption.CREATE, StandardOpenOption.WRITE).bytes = signature Files.newOutputStream(zipfs.getPath("META-INF/hmcl_signature")).withCloseable { it.bytes = signature }
} }
} }
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) }
}
jar { jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
@ -65,6 +74,7 @@ jar {
} }
doLast { doLast {
repack(archivePath)
attachSignature(archivePath) attachSignature(archivePath)
createChecksum(archivePath) createChecksum(archivePath)
} }
@ -78,22 +88,12 @@ def createExecutable(String suffix, String header) {
} }
task makePackGz(dependsOn: jar) doLast { task makePackGz(dependsOn: jar) doLast {
def tmp = new File(project.buildDir, "tmp") def outputPath = new File(jar.archivePath.parentFile, jar.archivePath.name[0..-4] + "pack.gz")
def unpackedJar = new File(tmp, jar.archivePath.name) new GZIPOutputStream(outputPath.newOutputStream()).withCloseable { out ->
def packGz = new File(jar.archivePath.parentFile, jar.archivePath.name[0..-4] + "pack.gz") new JarFile(jar.archivePath).withCloseable { jarFile -> packer.pack(jarFile, out) }
def originalStream = new ByteArrayOutputStream()
def unpackedJarStream = new JarOutputStream(new FileOutputStream(unpackedJar))
Pack200.newPacker().pack(new JarFile(jar.archivePath), originalStream)
Pack200.newUnpacker().unpack(new ByteArrayInputStream(originalStream.toByteArray()), unpackedJarStream)
unpackedJarStream.close()
attachSignature(unpackedJar)
new GZIPOutputStream(new FileOutputStream(packGz)).withCloseable { stream ->
Pack200.newPacker().pack(new JarFile(unpackedJar), stream)
} }
createChecksum(packGz) createChecksum(outputPath)
} }