Skip to content

Commit

Permalink
refactor downgradeFiles, rename ShadeAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 2, 2024
1 parent cb5cb3d commit dee674b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 32 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,35 @@ task customDowngrade(type: xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar) {
archiveClassifier = "downgraded-8"
}
task customShadeDowngradedApi(type: xyz.wagyourtail.jvmdg.gradle.task.ShadeApi) {
task customShadeDowngradedApi(type: xyz.wagyourtail.jvmdg.gradle.task.ShadeJar) {
inputFile = customDowngrade.archiveFile
archiveClassifier = "downgraded-8-shaded"
}
```

you can also downgrade a `FileCollection`:
you can also downgrade/shade a `FileCollection`:

```gradle
task downgradeFileCollection(type: xyz.wagyourtail.jvmdg.gradle.task.DowngradeFiles) {
toDowngrade = files("file1.jar", "file2.jar")
task downgradeFileCollection(type: xyz.wagyourtail.jvmdg.gradle.task.files.DowngradeFiles) {
inputCollection = files("file1.jar", "file2.jar")
downgradeTo = JavaVersion.VERSION_1_8 // default
classpath = sourceSets.main.runtimeClasspath // default
}
// get the output with
downgradeFileCollection.outputCollection
task shadeFileCollection(type: xyz.wagyourtail.jvmdg.gradle.task.files.ShadeFiles) {
inputCollection = downgradeFileCollection.outputCollection
downgradeTo = JavaVersion.VERSION_1_8 // default
}
// get the output with
shadeFileCollection.outputCollection
```

Make sure the task is configured before trying to use the outputCollection, it's computed from the `toDowngrade` files.
Make sure the task is configured before trying to use the outputCollection,
it's computed from the `toDowngrade` files.

you can downgrade a configuration:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import xyz.wagyourtail.jvmdg.gradle.flags.DowngradeFlags
import xyz.wagyourtail.jvmdg.gradle.flags.ShadeFlags
import xyz.wagyourtail.jvmdg.gradle.flags.toFlags
import xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar
import xyz.wagyourtail.jvmdg.gradle.task.ShadeAPI
import xyz.wagyourtail.jvmdg.gradle.task.ShadeJar
import xyz.wagyourtail.jvmdg.gradle.transform.DowngradeTransform
import xyz.wagyourtail.jvmdg.gradle.transform.ShadeTransform
import xyz.wagyourtail.jvmdg.util.FinalizeOnRead
Expand All @@ -34,7 +34,7 @@ abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val proj
}

@get:Internal
val defaultShadeTask = project.tasks.register("shadeDowngradedApi", ShadeAPI::class.java).apply {
val defaultShadeTask = project.tasks.register("shadeDowngradedApi", ShadeJar::class.java).apply {
configure {
it.inputFile.set(defaultTask.get().archiveFile)
it.archiveClassifier.set("downgraded-shaded")
Expand All @@ -59,7 +59,7 @@ abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val proj
quiet.convention(false).finalizeValueOnRead()
debug.convention(false).finalizeValueOnRead()
debugSkipStubs.convention(emptySet()).finalizeValueOnRead()
shadePath.convention { it.substringBefore(".").substringBeforeLast("-").replace(Regex("[.;\\[/]"), "-") }
shadePath.convention { it.substringBefore(".").substringBeforeLast("-").replace(Regex("[.;\\[/]"), "-") + "/" }
}

@get:Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import xyz.wagyourtail.jvmdg.util.readZipInputStreamFor
import java.nio.file.StandardOpenOption
import kotlin.io.path.outputStream

abstract class ShadeAPI : Jar(), ShadeFlags {
abstract class ShadeJar : Jar(), ShadeFlags {

private val jvmdg by lazy {
project.extensions.getByType(JVMDowngraderExtension::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.gradle.task
package xyz.wagyourtail.jvmdg.gradle.task.files

import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
Expand All @@ -24,7 +24,7 @@ abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {
}

@get:InputFiles
open var toDowngrade: FileCollection by FinalizeOnRead(MustSet())
open var inputCollection: FileCollection by FinalizeOnRead(MustSet())

@get:InputFiles
var classpath: FileCollection by FinalizeOnRead(LazyMutable {
Expand All @@ -33,25 +33,21 @@ abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {

@get:Internal
val outputMap: Map<File, File>
get() = toDowngrade.associateWith { temporaryDir.resolve(it.name) }
get() = inputCollection.associateWith { temporaryDir.resolve(it.name) }

/**
* this is the true output, gradle just doesn't have a
* \@OutputDirectoriesAndFiles
*/
@get:Internal
val outputCollection: FileCollection
get() = project.files(toDowngrade.map { temporaryDir.resolve(it.name) })
val outputCollection: FileCollection by lazy {
val fd = inputCollection.map { it to temporaryDir.resolve(it.name) }

@get:OutputFiles
@get:ApiStatus.Internal
val outputFiles: FileCollection
get() = outputCollection.filter { it.isFile }
outputs.dirs(*fd.filter { it.first.isDirectory }.map { it.second }.toTypedArray())
outputs.files(*fd.filter { it.first.isFile }.map { it.second }.toTypedArray())

@get:OutputDirectories
@get:ApiStatus.Internal
val outputDirectories: FileCollection
get() = outputCollection.filter { it.isDirectory }
outputs.files
}

init {
downgradeTo.convention(jvmdg.downgradeTo).finalizeValueOnRead()
Expand All @@ -63,7 +59,7 @@ abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {

@TaskAction
fun doDowngrade() {
var toDowngrade = toDowngrade.map { it.toPath() }.filter { it.exists() }
var toDowngrade = inputCollection.map { it.toPath() }.filter { it.exists() }
val classpath = classpath.files

val fileSystems = mutableSetOf<FileSystem>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package xyz.wagyourtail.jvmdg.gradle.task.files

import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.*
import xyz.wagyourtail.jvmdg.compile.ApiShader
import xyz.wagyourtail.jvmdg.gradle.JVMDowngraderExtension
import xyz.wagyourtail.jvmdg.gradle.flags.ShadeFlags
import xyz.wagyourtail.jvmdg.gradle.flags.toFlags
import xyz.wagyourtail.jvmdg.util.*
import java.io.File
import java.nio.file.FileSystem
import kotlin.io.path.exists
import kotlin.io.path.isDirectory
import kotlin.io.path.name

abstract class ShadeFiles : ConventionTask(), ShadeFlags {

@get:Internal
protected val jvmdg by lazy {
project.extensions.getByType(JVMDowngraderExtension::class.java)
}

@get:InputFiles
open var inputCollection: FileCollection by FinalizeOnRead(MustSet())

@get:Internal
val outputMap: Map<File, File>
get() = inputCollection.associateWith { temporaryDir.resolve(it.name) }

/**
* this is the true output, gradle just doesn't have a
* \@OutputDirectoriesAndFiles
*/
@get:Internal
val outputCollection: FileCollection by lazy {
val fd = inputCollection.map { it to temporaryDir.resolve(it.name) }

outputs.dirs(*fd.filter { it.first.isDirectory }.map { it.second }.toTypedArray())
outputs.files(*fd.filter { it.first.isFile }.map { it.second }.toTypedArray())

outputs.files
}

init {
downgradeTo.convention(jvmdg.downgradeTo).finalizeValueOnRead()
apiJar.convention(jvmdg.apiJar).finalizeValueOnRead()
quiet.convention(jvmdg.quiet).finalizeValueOnRead()
debug.convention(jvmdg.debug).finalizeValueOnRead()
debugSkipStubs.convention(jvmdg.debugSkipStubs).finalizeValueOnRead()
shadePath.convention(jvmdg.shadePath).finalizeValueOnRead()
}

@TaskAction
fun doDowngrade() {
val toDowngrade = inputCollection.map { it.toPath() }.filter { it.exists() }
val fileSystems = mutableSetOf<FileSystem>()

try {

outputs.files.forEach { it.deleteRecursively() }

val downgraded = toDowngrade.map { temporaryDir.resolve(it.name) }.map {
if (it.extension == "jar" || it.extension == "zip") {
val fs = Utils.openZipFileSystem(it.toPath(), true)
fileSystems.add(fs)
fs.getPath("/")
} else it.toPath()
}

val toDowngradePaths = toDowngrade.map {
if (it.isDirectory()) it else run {
val fs = Utils.openZipFileSystem(it, false)
fileSystems.add(fs)
fs.getPath("/")
}
}
for (i in toDowngradePaths.indices) {
val toDowngradeFile = toDowngradePaths[i]
val downgradedFile = downgraded[i]
ApiShader.shadeApis(this.toFlags(), shadePath.get().invoke(toDowngrade[i].name), toDowngradeFile, downgradedFile, jvmdg.downgradedApis[downgradeTo.get()])
}
} finally {
fileSystems.forEach { it.close() }
}
}

fun forInputs(files: Set<File>): FileCollection {
return project.files(outputMap.filterKeys { it in files }.values)
}

}
20 changes: 17 additions & 3 deletions gradle-plugin/test-downgrade/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@file:Suppress("DSL_SCOPE_VIOLATION")
import xyz.wagyourtail.jvmdg.gradle.JVMDowngraderExtension
import xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar
import xyz.wagyourtail.jvmdg.gradle.task.ShadeAPI
import xyz.wagyourtail.jvmdg.gradle.task.ShadeJar
import xyz.wagyourtail.jvmdg.gradle.task.files.DowngradeFiles
import xyz.wagyourtail.jvmdg.gradle.task.files.ShadeFiles
import java.util.*

buildscript {
Expand Down Expand Up @@ -91,7 +93,9 @@ if (project.hasProperty("runningTest")) {
}

val downgrade by configurations.creating
jvmdg.dg(downgrade)
jvmdg.dg(downgrade) {
downgradeTo = JavaVersion.VERSION_11
}

dependencies {
implementation("org.jetbrains:annotations-java5:24.1.0")
Expand All @@ -102,6 +106,16 @@ dependencies {
implementation(files(downgrade.files))
}

val downgradeFiles by tasks.creating(DowngradeFiles::class) {
inputCollection = sourceSets.main.get().runtimeClasspath
downgradeTo = JavaVersion.VERSION_1_8
}

val shadeFiles by tasks.creating(ShadeFiles::class) {
dependsOn(downgradeFiles)
inputCollection = downgradeFiles.outputCollection
downgradeTo = JavaVersion.VERSION_1_8
}

val downgradeJar9 by tasks.creating(DowngradeJar::class) {
inputFile.set(tasks.jar.get().archiveFile)
Expand All @@ -111,7 +125,7 @@ val downgradeJar9 by tasks.creating(DowngradeJar::class) {
// destinationDirectory.set(temporaryDir)
}

val shadeDowngradedApi9 by tasks.creating(ShadeAPI::class) {
val shadeDowngradedApi9 by tasks.creating(ShadeJar::class) {
inputFile.set(downgradeJar9.archiveFile)
archiveClassifier.set("downgraded-shaded-9")
downgradeTo = JavaVersion.VERSION_1_9
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ public static void shade(Map<String, List<String[]>> args) throws IOException {
throw new IllegalArgumentException("Downgraded api jar does not exist");
}
}
String prefix = args.get("--prefix").get(0)[0];

Map<Path, Path> targets = new HashMap<>();
List<FileSystem> fileSystems = new ArrayList<>();
Expand All @@ -246,7 +245,13 @@ public static void shade(Map<String, List<String[]>> args) throws IOException {
outputs.add(entry.getValue());
}

ApiShader.shadeApis(flags, prefix, inputs, outputs, downgradedApi);
List<String> prefixes = new ArrayList<>();
List<String[]> prefix = args.get("--prefix");
for (String[] strings : prefix) {
prefixes.add(strings[0]);
}

ApiShader.shadeApis(flags, prefixes, inputs, outputs, downgradedApi);
} finally {
for (FileSystem fileSystem : fileSystems) {
fileSystem.close();
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/xyz/wagyourtail/jvmdg/compile/ApiShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,21 @@ public static void shadeApis(Flags flags, String prefix, File input, File output
}
}

public static void shadeApis(Flags flags, String prefix, List<Path> inputRoots, List<Path> outputRoots, File downgradedApi) throws IOException {
if (!prefix.endsWith("/")) {
prefix += "/";
public static void shadeApis(Flags flags, String prefix, Path inputRoot, Path outputRoot, File downgradedApi) throws IOException {
shadeApis(flags, Collections.singletonList(prefix), Collections.singletonList(inputRoot), Collections.singletonList(outputRoot), downgradedApi);
}

public static void shadeApis(Flags flags, List<String> prefix, List<Path> inputRoots, List<Path> outputRoots, File downgradedApi) throws IOException {
for (String p : prefix) {
if (!p.endsWith("/")) {
throw new IllegalArgumentException("prefix \""+ p +"\" must end with /");
}
}
Path downgradedApiPath = resolveDowngradedApi(flags, downgradedApi);
try (FileSystem apiFs = Utils.openZipFileSystem(downgradedApiPath,false)) {
Pair<ReferenceGraph, Set<Type>> api = scanApis(apiFs.getPath("/"));
for (int i = 0; i < inputRoots.size(); i++) {
shadeApis(prefix, inputRoots.get(i), outputRoots.get(i), apiFs.getPath("/"), api.getFirst(), api.getSecond());
shadeApis(prefix.get(i % prefix.size()), inputRoots.get(i), outputRoots.get(i), apiFs.getPath("/"), api.getFirst(), api.getSecond());
}
}
}
Expand Down

0 comments on commit dee674b

Please sign in to comment.