-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
206 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import org.gradle.api.tasks.InputFile | ||
import org.gradle.jvm.tasks.Jar | ||
import java.io.File | ||
import java.util.jar.JarEntry | ||
import java.util.jar.JarOutputStream | ||
import java.util.zip.Deflater | ||
|
||
typealias FileProcessor = (File) -> Unit | ||
|
||
open class ProcessJar : Jar() { | ||
val input = project.objects.fileProperty() | ||
@InputFile get | ||
|
||
private val processors = mutableListOf<FileProcessor>() | ||
|
||
init { | ||
group = "build" | ||
} | ||
|
||
fun addFileProcessor(regex: Regex, processor: FileProcessor) { | ||
processors.add { | ||
it.walkTopDown().forEach { file -> | ||
if (file.extension.matches(regex)) | ||
processor(file) | ||
} | ||
} | ||
} | ||
|
||
fun addFileProcessor(vararg extensions: String, processor: FileProcessor) { | ||
processors.add { | ||
it.walkTopDown().forEach { file -> | ||
if (file.extension in extensions) | ||
processor(file) | ||
} | ||
} | ||
} | ||
|
||
fun addDirProcessor(processor: FileProcessor) { | ||
processors.add(processor) | ||
} | ||
|
||
override fun copy() { | ||
val inputJar = input.get().asFile | ||
|
||
if (!inputJar.exists()) | ||
error("Input jar does not exist: $inputJar") | ||
|
||
val dir = temporaryDir.resolve("unpack") | ||
|
||
if (dir.exists()) | ||
dir.deleteRecursively() | ||
dir.mkdirs() | ||
|
||
// unpack jar to temp dir | ||
project.copy { | ||
from(project.zipTree(inputJar)) | ||
into(dir) | ||
} | ||
|
||
processors.forEach { it(dir) } | ||
|
||
// merge manifests | ||
val manifestFile = dir.resolve("META-INF/MANIFEST.MF") | ||
if (manifestFile.exists()) { | ||
manifest.from(manifestFile) | ||
manifestFile.delete() | ||
manifestFile.createNewFile() | ||
manifest.effectiveManifest.writeTo(manifestFile) | ||
} | ||
|
||
// repack jar | ||
JarOutputStream(archiveFile.get().asFile.outputStream()).use { jos -> | ||
jos.setLevel(Deflater.BEST_COMPRESSION) | ||
for (it in dir.walkTopDown()) { | ||
if(it.isDirectory) continue | ||
val entry = JarEntry(it.relativeTo(dir).path) | ||
jos.putNextEntry(entry) | ||
it.inputStream().copyTo(jos) | ||
jos.closeEntry() | ||
} | ||
|
||
jos.finish() | ||
jos.flush() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import org.gradle.api.Task | ||
import org.gradle.api.internal.file.copy.DefaultCopySpec | ||
import org.gradle.api.tasks.AbstractCopyTask | ||
import org.gradle.api.tasks.TaskContainer | ||
import org.gradle.api.tasks.bundling.AbstractArchiveTask | ||
import org.gradle.jvm.tasks.Jar | ||
|
||
fun Jar.clearSourcePaths() { | ||
AbstractCopyTask::class.java.getDeclaredField("mainSpec").let { | ||
it.isAccessible = true | ||
val thing = it.get(this) as DefaultCopySpec | ||
thing.sourcePaths.clear() | ||
it.isAccessible = false | ||
} | ||
} | ||
|
||
fun AbstractArchiveTask.putInDevlibs() { | ||
destinationDirectory.set(project.layout.buildDirectory.dir("devlibs")) | ||
} | ||
|
||
inline fun <reified T : Task> TaskContainer.get(name: String): T { | ||
return this.withType(T::class.java).getByName(name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters