Initial
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package io.izzel.taboolib.gradle
|
||||
|
||||
import groovy.transform.ToString
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.InputFile
|
||||
import org.gradle.api.tasks.Optional
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.objectweb.asm.ClassReader
|
||||
import org.objectweb.asm.ClassWriter
|
||||
import org.objectweb.asm.commons.ClassRemapper
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.nio.file.StandardCopyOption
|
||||
import java.util.jar.JarEntry
|
||||
import java.util.jar.JarFile
|
||||
import java.util.jar.JarOutputStream
|
||||
|
||||
@ToString
|
||||
class RelocateJar extends DefaultTask {
|
||||
|
||||
@InputFile
|
||||
File inJar
|
||||
|
||||
@Input
|
||||
Map<String, String> relocations
|
||||
|
||||
@Optional
|
||||
@Input
|
||||
String classifier
|
||||
|
||||
@TaskAction
|
||||
def relocate() {
|
||||
def mapping = relocations.collectEntries { [(it.key.replace('.', '/')), it.value.replace('.', '/')] }
|
||||
def remapper = new RelocateRemapper(relocations, mapping as Map<String, String>)
|
||||
def index = inJar.name.lastIndexOf('.')
|
||||
def name = inJar.name.substring(0, index) + (classifier == null ? "" : "-" + classifier) + inJar.name.substring(index)
|
||||
def outJar = new File(inJar.getParentFile(), name)
|
||||
def tmpOut = File.createTempFile(name, ".jar")
|
||||
new JarOutputStream(new FileOutputStream(tmpOut)).withCloseable { out ->
|
||||
project.logger.info(outJar.getAbsolutePath())
|
||||
int n
|
||||
def buf = new byte[32768]
|
||||
new JarFile(inJar).withCloseable {jarFile ->
|
||||
for (def jarEntry : jarFile.entries()) {
|
||||
jarFile.getInputStream(jarEntry).withCloseable {
|
||||
if (jarEntry.name.endsWith(".class")) {
|
||||
project.logger.info("Relocating " + jarEntry.name)
|
||||
def reader = new ClassReader(it)
|
||||
def writer = new ClassWriter(0)
|
||||
reader.accept(new ClassRemapper(writer, remapper), 0)
|
||||
out.putNextEntry(new JarEntry(remapper.map(jarEntry.name)))
|
||||
out.write(writer.toByteArray())
|
||||
} else {
|
||||
out.putNextEntry(new JarEntry(remapper.map(jarEntry.name)))
|
||||
while ((n = it.read(buf)) != -1) {
|
||||
out.write(buf, 0, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Files.copy(tmpOut.toPath(), outJar.toPath(), StandardCopyOption.REPLACE_EXISTING)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.izzel.taboolib.gradle
|
||||
|
||||
import groovy.transform.Canonical
|
||||
import org.objectweb.asm.commons.Remapper
|
||||
|
||||
@Canonical
|
||||
class RelocateRemapper extends Remapper {
|
||||
|
||||
Map<String, String> dot
|
||||
Map<String, String> slash
|
||||
|
||||
@Override
|
||||
Object mapValue(Object value) {
|
||||
if (value instanceof String) {
|
||||
def match = dot.find { (value as String).startsWith(it.key) }
|
||||
if (match) {
|
||||
return (match.value + (value as String).substring(match.key.length())).toString()
|
||||
}
|
||||
}
|
||||
return super.mapValue(value)
|
||||
}
|
||||
|
||||
@Override
|
||||
String map(String internalName) {
|
||||
def match = slash.find { internalName.startsWith(it.key) }
|
||||
if (match) {
|
||||
return match.value + internalName.substring(match.key.length())
|
||||
} else {
|
||||
return internalName
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.izzel.taboolib.gradle
|
||||
|
||||
import groovy.transform.Canonical
|
||||
|
||||
@Canonical
|
||||
class TabooLibExtension {
|
||||
|
||||
String tabooLibVersion = '5.+'
|
||||
|
||||
String loaderVersion = '2.+'
|
||||
|
||||
String classifier = 'all'
|
||||
|
||||
Map<String, String> relocation = new LinkedHashMap<>()
|
||||
|
||||
def relocate(String pre, String post) {
|
||||
this.relocation[pre] = post
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package io.izzel.taboolib.gradle
|
||||
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.jvm.tasks.Jar
|
||||
|
||||
class TabooLibPlugin implements Plugin<Project> {
|
||||
|
||||
@Override
|
||||
void apply(Project project) {
|
||||
project.repositories.maven { url "http://repo.ptms.ink/repository/codemc-nms/" }
|
||||
project.repositories.maven { url "http://repo.ptms.ink/repository/maven-releases/" }
|
||||
def tabooExt = project.extensions.create('taboolib', TabooLibExtension)
|
||||
def taboo = project.configurations.maybeCreate('taboo')
|
||||
project.configurations.compileClasspath.extendsFrom(taboo)
|
||||
def tabooTask = project.tasks.create('tabooRelocateJar', RelocateJar)
|
||||
|
||||
project.afterEvaluate {
|
||||
println(project.group.toString())
|
||||
project.configurations.compileClasspath.dependencies.add(project.dependencies.create("io.izzel.taboolib:TabooLib:${tabooExt.tabooLibVersion}:all"))
|
||||
taboo.dependencies.add(project.dependencies.create("io.izzel.taboolib:TabooLibLoader:${tabooExt.loaderVersion}:all"))
|
||||
|
||||
def shadowPresent = project.plugins.hasPlugin('com.github.johnrengelman.shadow')
|
||||
if (!shadowPresent) {
|
||||
project.tasks.jar.finalizedBy(tabooTask)
|
||||
project.tasks.jar.configure { Jar task ->
|
||||
task.from(taboo.collect { it.isDirectory() ? it : project.zipTree(it) })
|
||||
}
|
||||
def jarTask = project.tasks.jar as Jar
|
||||
tabooTask.configure { RelocateJar task ->
|
||||
task.inJar = task.inJar ?: jarTask.archivePath
|
||||
task.relocations = tabooExt.relocation
|
||||
task.classifier = tabooExt.classifier
|
||||
task.relocations['io.izzel.taboolib.loader'] = task.relocations['io.izzel.taboolib.loader'] ?: project.group.toString() + '.boot'
|
||||
task.relocations['org.bstats.bukkit'] = task.relocations['org.bstats.bukkit'] ?: project.group.toString() + '.metrics'
|
||||
}
|
||||
} else {
|
||||
def shadowJar = project.tasks.getByName('shadowJar')
|
||||
if (shadowJar) {
|
||||
shadowJar.configurations.add(taboo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
implementation-class=io.izzel.taboolib.gradle.TabooLibPlugin
|
||||
Reference in New Issue
Block a user