Skip to content

Commit

Permalink
refactor gradle tasks and extension for full flags access
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jun 2, 2024
1 parent 7403735 commit 9fc02f1
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 198 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ in `build.gradle`:
```gradle
// add the plugin
plugins {
id 'xyz.wagyourtail.jvmdowngrader' version '0.5.0'
id 'xyz.wagyourtail.jvmdowngrader' version '0.7.0'
}
// optionally you can change some globals:
jvmdg.defaultMavens = false // stops from inserting my maven into the project repositories
jvmdg.group = "xyz.wagyourtail.jvmdowngrader" // default
jvmdg.coreArchiveName "jvmdowngrader" // default
jvmdg.apiArchiveName "jvmdowngrader-java-api" // default
jvmdg.version = "0.5.0" // default
jvmdg.asmVersion = "9.7" // default
// optionally you can change some globals, here are their default values:
jvmdg.downgradeTo = JavaVersion.VERSION_1_8
jvmdg.apiJar = this.getClass().getResourceAsStream("jvmdg/java-api-${version}.jar").writeToFile("build/jvmdg/java-api-${version}.jar")
jvmdg.quiet = false
jvmdg.debug = false
jvmdg.debugSkipStubs = [].toSet()
jvmdg.shadePath = {
it.substringBefore(".").substringBeforeLast("-").replace(Regex("[.;\\[/]"), "-")
}
```

This will create a default downgrade task for `jar` (or `shadowJar` if present) called `downgradeJar` that will
Expand All @@ -56,16 +55,17 @@ you can change the downgrade version by doing:

```gradle
downgradeJar {
downgradeTo = JavaVersion.VERSION_1_11
archiveClassifier = "downgraded-11"
downgradeTo = JavaVersion.VERSION_11
}
shadeDowngradedApi {
downgradeTo = JavaVersion.VERSION_1_11
archiveClassifier = "downgraded-11-shaded"
downgradeTo = JavaVersion.VERSION_11
}
```

The tasks have all the same flags as the extension, so you can change them separately,
their default value is to use the global one from the extension.

Optionally, you can also depend on the shadeDowngradedApi task when running build.

```gradle
Expand All @@ -84,8 +84,6 @@ task customDowngrade(type: xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar) {
task customShadeDowngradedApi(type: xyz.wagyourtail.jvmdg.gradle.task.ShadeApi) {
inputFile = customDowngrade.archiveFile
downgradeTo = JavaVersion.VERSION_1_8 // default
shadePath = "${archiveBaseName}/jvmdg/api" // default, where the shaded classes will be placed
archiveClassifier = "downgraded-8-shaded"
}
```
Expand All @@ -106,6 +104,7 @@ downgradeFileCollection.outputCollection
Make sure the task is configured before trying to use the outputCollection, it's computed from the `toDowngrade` files.

you can downgrade a configuration:

```gradle
configurations {
downgrade
Expand All @@ -116,6 +115,7 @@ jvmdg.dg(configurations.downgrade)
dependencies {
downgrade "newer.java:version:1.0"
implementation "xyz.wagyourtail.jvmdowngrader:jvmdowngrader-java-api:0.7.0"
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,27 @@ import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Attribute
import org.gradle.api.tasks.Internal
import org.gradle.jvm.tasks.Jar
import org.jetbrains.annotations.ApiStatus
import xyz.wagyourtail.jvmdg.ClassDowngrader
import xyz.wagyourtail.jvmdg.cli.Flags
import xyz.wagyourtail.jvmdg.compile.ZipDowngrader
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.transform.DowngradeFlags
import xyz.wagyourtail.jvmdg.gradle.transform.DowngradeTransform
import xyz.wagyourtail.jvmdg.gradle.transform.ShadeTransform
import xyz.wagyourtail.jvmdg.util.FinalizeOnRead
import xyz.wagyourtail.jvmdg.util.LazyMutable
import xyz.wagyourtail.jvmdg.util.defaultedMapOf
import xyz.wagyourtail.jvmdg.util.toOpcode
import java.io.File
import javax.inject.Inject

abstract class JVMDowngraderExtension(val project: Project) {

var version by FinalizeOnRead(JVMDowngraderPlugin::class.java.`package`.implementationVersion ?: "0.3.0")

var asmVersion by FinalizeOnRead("9.7")

var shadeDebugSkipStubs = mutableListOf<Int>()

var apiJar by FinalizeOnRead(LazyMutable {
JVMDowngraderExtension::class.java.getResourceAsStream("/META-INF/lib/java-api.jar").use {
val apiJar = project.layout.buildDirectory.get().asFile.resolve("jvmdg/java-api-${version}.jar")
if (!apiJar.exists() || project.gradle.startParameter.isRefreshDependencies) {
apiJar.parentFile.mkdirs()
apiJar.outputStream().use { os ->
it.copyTo(os)
}
}
apiJar
}
})
abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val project: Project) : ShadeFlags {
@get:Internal
val version by FinalizeOnRead(JVMDowngraderPlugin::class.java.`package`.implementationVersion ?: "0.7.0")

@get:Internal
val defaultTask = project.tasks.register("downgradeJar", DowngradeJar::class.java).apply {
configure {
val jar = (project.tasks.findByName("shadowJar") ?: project.tasks.getByName("jar")) as Jar
Expand All @@ -49,33 +33,47 @@ abstract class JVMDowngraderExtension(val project: Project) {
}
}

@get:Internal
val defaultShadeTask = project.tasks.register("shadeDowngradedApi", ShadeAPI::class.java).apply {
configure {
it.inputFile.set(defaultTask.get().archiveFile)
it.archiveClassifier.set("downgraded-shaded")
}
}

@get:ApiStatus.Internal
internal val downgradedApis = defaultedMapOf<JavaVersion, File> { version ->
val downgradedPath = apiJar.resolveSibling("java-api-${version}-downgraded.jar")
init {
downgradeTo.convention(JavaVersion.VERSION_1_8).finalizeValueOnRead()
apiJar.convention(project.provider {
val apiJar = project.layout.buildDirectory.get().asFile.resolve("jvmdg/java-api-${version}.jar")
if (!apiJar.exists() || project.gradle.startParameter.isRefreshDependencies) {
apiJar.parentFile.mkdirs()
JVMDowngraderExtension::class.java.getResourceAsStream("/META-INF/lib/java-api.jar").use { stream ->
if (stream == null) throw IllegalStateException("java-api.jar not found in resources")
apiJar.outputStream().use { os ->
stream.copyTo(os)
}
}
}
apiJar
}).finalizeValueOnRead()
quiet.convention(false).finalizeValueOnRead()
debug.convention(false).finalizeValueOnRead()
debugSkipStubs.convention(emptySet()).finalizeValueOnRead()
shadePath.convention { it.substringBefore(".").substringBeforeLast("-").replace(Regex("[.;\\[/]"), "-") }
}

val flags = Flags()
flags.api = apiJar
flags.printDebug = false
flags.classVersion = version.toOpcode()
flags.debugSkipStubs = shadeDebugSkipStubs.toSet()
@get:Internal
internal val downgradedApis = defaultedMapOf<JavaVersion, File> { version ->
val downgradedPath = apiJar.get().resolveSibling("java-api-${version}-downgraded.jar")

ClassDowngrader.downgradeTo(flags).use {
ZipDowngrader.downgradeZip(it, flags.findJavaApi().toPath(), emptySet(), downgradedPath.toPath())
ClassDowngrader.downgradeTo(this.toFlags()).use {
ZipDowngrader.downgradeZip(it, apiJar.get().toPath(), emptySet(), downgradedPath.toPath())
}
downgradedPath
}

fun getDowngradedApi(version: JavaVersion): File = downgradedApis[version]

var depDgVersion by FinalizeOnRead(JavaVersion.VERSION_1_8)

@JvmOverloads
fun dg(dep: Configuration, shade: Boolean = true, config: DowngradeFlags.() -> Unit = {}) {
val artifactType = Attribute.of("artifactType", String::class.java)
Expand All @@ -95,8 +93,11 @@ abstract class JVMDowngraderExtension(val project: Project) {
spec.to.attribute(artifactType, "jar").attribute(downgradeAttr, true).attribute(shadeAttr, false)

spec.parameters {
it.downgradeTo.set(depDgVersion)
it.downgradeTo.set(downgradeTo)
it.apiJar.set(apiJar)
it.quiet.set(quiet)
it.debug.set(debug)
it.debugSkipStubs.set(debugSkipStubs)
config(it)
}
}
Expand All @@ -115,8 +116,14 @@ abstract class JVMDowngraderExtension(val project: Project) {
spec.to.attribute(artifactType, "jar").attribute(shadeAttr, true).attribute(downgradeAttr, true)

spec.parameters {
it.downgradeTo.set(depDgVersion)
it.apiJar.set(downgradedApis[depDgVersion])
it.downgradeTo.set(downgradeTo)
it.apiJar.set(project.provider {
downgradedApis[it.downgradeTo.get()]
})
it.quiet.set(quiet)
it.debug.set(debug)
it.debugSkipStubs.set(debugSkipStubs)
it.shadePath.set(shadePath)
config(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.wagyourtail.jvmdg.gradle.transform
package xyz.wagyourtail.jvmdg.gradle.flags

import org.gradle.api.JavaVersion
import org.gradle.api.artifacts.transform.TransformParameters
Expand All @@ -10,33 +10,27 @@ import xyz.wagyourtail.jvmdg.cli.Flags
import xyz.wagyourtail.jvmdg.util.toOpcode
import java.io.File

abstract class DowngradeFlags : TransformParameters {
interface DowngradeFlags : TransformParameters {

@get:Input
@get:Optional
abstract val downgradeTo: Property<JavaVersion>
val downgradeTo: Property<JavaVersion>

@get:Input
abstract val apiJar: Property<File>
@get:Optional
val apiJar: Property<File>

@get:Input
@get:Optional
abstract val quiet: Property<Boolean>
val quiet: Property<Boolean>

@get:Input
@get:Optional
abstract val debug: Property<Boolean>
val debug: Property<Boolean>

@get:Input
@get:Optional
abstract val debugSkipStubs: SetProperty<JavaVersion>

init {
downgradeTo.convention(JavaVersion.VERSION_1_8)
quiet.convention(false)
debug.convention(false)
debugSkipStubs.convention(emptySet())
}
val debugSkipStubs: SetProperty<JavaVersion>
}

fun DowngradeFlags.toFlags(): Flags {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package xyz.wagyourtail.jvmdg.gradle.flags

import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional

interface ShadeFlags : DowngradeFlags {

@get:Input
@get:Optional
val shadePath : Property<(fileName: String) -> String>

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
package xyz.wagyourtail.jvmdg.gradle.task

import org.gradle.api.JavaVersion
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.jetbrains.annotations.ApiStatus
import xyz.wagyourtail.jvmdg.ClassDowngrader
import xyz.wagyourtail.jvmdg.cli.Flags
import xyz.wagyourtail.jvmdg.compile.PathDowngrader
import xyz.wagyourtail.jvmdg.gradle.flags.DowngradeFlags
import xyz.wagyourtail.jvmdg.gradle.JVMDowngraderExtension
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 DowngradeFiles : ConventionTask() {
abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {

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

@get:Input
@get:Optional
var downgradeTo by FinalizeOnRead(JavaVersion.VERSION_1_8)

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

Expand Down Expand Up @@ -59,19 +53,12 @@ abstract class DowngradeFiles : ConventionTask() {
val outputDirectories: FileCollection
get() = outputCollection.filter { it.isDirectory }

@get:Input
@get:Optional
@get:ApiStatus.Experimental
abstract val debugSkipStubs: ListProperty<Int>

@get:Input
@get:Optional
@get:ApiStatus.Experimental
abstract val debugPrint: Property<Boolean>

init {
debugSkipStubs.convention(mutableListOf())
debugPrint.convention(false)
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()
}

@TaskAction
Expand All @@ -81,12 +68,6 @@ abstract class DowngradeFiles : ConventionTask() {

val fileSystems = mutableSetOf<FileSystem>()

val flags = Flags()
flags.api = jvmdg.apiJar
flags.printDebug = debugPrint.get()
flags.classVersion = downgradeTo.toOpcode()
flags.debugSkipStubs = debugSkipStubs.get().toSet()

try {

outputs.files.forEach { it.deleteRecursively() }
Expand All @@ -106,7 +87,7 @@ abstract class DowngradeFiles : ConventionTask() {
fs.getPath("/")
}
}
ClassDowngrader.downgradeTo(flags).use {
ClassDowngrader.downgradeTo(this.toFlags()).use {
PathDowngrader.downgradePaths(it, toDowngrade, downgraded, classpath.map { it.toURI().toURL() }.toSet())
}
} finally {
Expand Down
Loading

0 comments on commit 9fc02f1

Please sign in to comment.