Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 20, 2024
2 parents 326ffd2 + 6189d12 commit 42dbe6d
Show file tree
Hide file tree
Showing 26 changed files with 645 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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
import xyz.wagyourtail.jvmdg.util.LazyMutable
import xyz.wagyourtail.jvmdg.util.defaultedMapOf
import java.io.File
import javax.inject.Inject
Expand All @@ -41,40 +42,65 @@ abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val proj
}
}

init {
downgradeTo.convention(JavaVersion.VERSION_1_8).finalizeValueOnRead()
apiJar.convention(project.provider {
val apiJar = project.file(".gradle").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)
}
/**
* the main api jar to use for downgrading
*/
@get:Internal
var apiJarDefault by LazyMutable {
val apiJar = project.file(".gradle").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()
}
apiJar
}

init {
downgradeTo.convention(JavaVersion.VERSION_1_8).finalizeValueOnRead()
apiJar.convention(project.provider { setOf(apiJarDefault) }).finalizeValueOnRead()
quiet.convention(false).finalizeValueOnRead()
logAnsiColors.convention(true).finalizeValueOnRead()
logLevel.convention("INFO").finalizeValueOnRead()
ignoreWarningsIn.convention(emptySet()).finalizeValueOnRead()
debug.convention(false).finalizeValueOnRead()
debugSkipStubs.convention(emptySet()).finalizeValueOnRead()
debugDumpClasses.convention(false).finalizeValueOnRead()
shadePath.convention { it.substringBefore(".").substringBeforeLast("-").replace(Regex("[.;\\[/]"), "-") + "/" }
}

@get:Internal
internal val downgradedApis = defaultedMapOf<JavaVersion, File> { version ->
val downgradedPath = project.file(".gradle").resolve("jvmdg/java-api-${this.version}-${version}-downgraded.jar")
fun convention(flags: ShadeFlags) {
convention(flags as DowngradeFlags)
flags.shadePath.convention(shadePath).finalizeValueOnRead()
}

if (!downgradedPath.exists() || project.gradle.startParameter.isRefreshDependencies) {
ClassDowngrader.downgradeTo(this.toFlags()).use {
ZipDowngrader.downgradeZip(it, apiJar.get().toPath(), emptySet(), downgradedPath.toPath())
fun convention(flags: DowngradeFlags) {
flags.downgradeTo.convention(downgradeTo).finalizeValueOnRead()
flags.apiJar.convention(apiJar).finalizeValueOnRead()
flags.quiet.convention(quiet).finalizeValueOnRead()
flags.debug.convention(debug).finalizeValueOnRead()
flags.debugSkipStubs.convention(debugSkipStubs).finalizeValueOnRead()
}

@get:Internal
internal val downgradedApis = defaultedMapOf<JavaVersion, Set<File>> { version ->
val jars = mutableSetOf<File>()
for (path in apiJar.get()) {
val downgraded = path.resolveSibling(path.nameWithoutExtension + "-downgraded-${version}.jar")
if (!downgraded.exists() || project.gradle.startParameter.isRefreshDependencies) {
ClassDowngrader.downgradeTo(this.toFlags()).use {
ZipDowngrader.downgradeZip(it, path.toPath(), emptySet(), downgraded.toPath())
}
}
}
downgradedPath
jars
}

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

@JvmOverloads
fun dg(dep: Configuration, shade: Boolean = true, config: DowngradeFlags.() -> Unit = {}) {
Expand All @@ -97,11 +123,7 @@ abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val proj
spec.to.attribute(artifactType, "jar").attribute(downgradeAttr, true).attribute(shadeAttr, false)

spec.parameters {
it.downgradeTo.set(downgradeTo)
it.apiJar.set(apiJar)
it.quiet.set(quiet)
it.debug.set(debug)
it.debugSkipStubs.set(debugSkipStubs)
this@JVMDowngraderExtension.convention(it)
config(it)
javaVersion = it.downgradeTo.get()
}
Expand All @@ -121,14 +143,7 @@ abstract class JVMDowngraderExtension @Inject constructor(@get:Internal val proj
spec.to.attribute(artifactType, "jar").attribute(shadeAttr, true).attribute(downgradeAttr, true)

spec.parameters {
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)
this@JVMDowngraderExtension.convention(it)
config(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,109 @@ package xyz.wagyourtail.jvmdg.gradle.flags

import org.gradle.api.JavaVersion
import org.gradle.api.artifacts.transform.TransformParameters
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.*
import xyz.wagyourtail.jvmdg.cli.Flags
import xyz.wagyourtail.jvmdg.logging.Logger
import xyz.wagyourtail.jvmdg.util.toOpcode
import java.io.File

interface DowngradeFlags : TransformParameters {

/**
* sets the target class version to downgrade to,
* default is [JavaVersion.VERSION_1_8]
*/
@get:Input
@get:Optional
val downgradeTo: Property<JavaVersion>

@get:Input
/**
* sets the api jar to use for downgrading
* default is null
*/
@get:InputFiles
@get:PathSensitive(PathSensitivity.NONE)
@get:Optional
val apiJar: Property<File>
val apiJar: ListProperty<File>

/**
* sets the log level to [Logger.Level.FATAL]
* @deprecated use [logLevel], if this is true, it will override [logLevel] to [Logger.Level.FATAL]
*/
@get:Input
@get:Optional
@get:Deprecated(message = "use logLevel", replaceWith = ReplaceWith("logLevel = LogLevel.DEBUG"))
val quiet: Property<Boolean>

/**
* sets if the logger should use ansi colors for the console to look pretty
*/
@get:Input
@get:Optional
val logAnsiColors: Property<Boolean>


/**
* sets the log level, default is [Logger.Level.INFO] as a string
*/
@get:Input
@get:Optional
val logLevel: Property<String>

/**
* sets if any classes should be set to ignore missing class/member warnings
* this will prevent [xyz.wagyourtail.jvmdg.version.VersionProvider.printWarnings] for
* any classes in this set
*
* This set also allows for packages to be ignored, by ending with a `*` or `**` to ignore all sub-packages
* @since 0.9.0
*/
@get:Input
@get:Optional
val ignoreWarningsIn: ListProperty<String>

/**
* sets if the logger should print debug messages
* @deprecated use [logLevel], if this is true, it will override [logLevel] to [Logger.Level.DEBUG]
*/
@get:Input
@get:Optional
@get:Deprecated(message = "use logLevel", replaceWith = ReplaceWith("logLevel = LogLevel.DEBUG"))
val debug: Property<Boolean>

/**
* this skips applying stubs for the specified input class version, this will still apply the
* [xyz.wagyourtail.jvmdg.version.VersionProvider.otherTransforms]
* such as `INVOKE_INTERFACE` -> `INVOKE_SPECIAL` for private interface methods in java 9 -> 8
*/
@get:Input
@get:Optional
val debugSkipStubs: SetProperty<JavaVersion>

/**
* sets if classes should be dumped to the [xyz.wagyourtail.jvmdg.Constants.DEBUG_DIR] directory
* @since 0.9.0
*/
@get:Input
@get:Optional
val debugDumpClasses: Property<Boolean>

}

fun DowngradeFlags.toFlags(): Flags {
val flags = Flags()
flags.api = apiJar.get()
flags.printDebug = debug.get()
flags.quiet = quiet.get()
flags.classVersion = downgradeTo.get().toOpcode()
flags.debugSkipStubs = debugSkipStubs.get().map { it.toOpcode() }.toSet()
flags.api = apiJar.orNull?.toSet()
flags.quiet = quiet.getOrElse(false)
flags.logAnsiColors = logAnsiColors.getOrElse(true)
flags.logLevel = Logger.Level.valueOf(logLevel.getOrElse("INFO").uppercase())
flags.printDebug = debug.getOrElse(false)
flags.classVersion = downgradeTo.getOrElse(JavaVersion.VERSION_1_8).toOpcode()
flags.debugSkipStubs = debugSkipStubs.getOrElse(emptySet()).map { it.toOpcode() }.toSet()
ignoreWarningsIn.getOrElse(emptyList()).forEach { flags.addIgnore(it) }
flags.debugDumpClasses = debugDumpClasses.getOrElse(false)
return flags
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ abstract class DowngradeJar : Jar(), DowngradeFlags {
group = "JVMDowngrader"
description = "Downgrades the jar to the specified version"

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()

jvmdg.convention(this)
}

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ abstract class ShadeJar : Jar(), ShadeFlags {
group = "JVMDowngrader"
description = "Downgrades the jar to the specified version"

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()
jvmdg.convention(this)
}

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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 org.jetbrains.annotations.ApiStatus
import xyz.wagyourtail.jvmdg.ClassDowngrader
import xyz.wagyourtail.jvmdg.compile.PathDowngrader
import xyz.wagyourtail.jvmdg.gradle.flags.DowngradeFlags
Expand All @@ -23,8 +22,20 @@ abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {
project.extensions.getByType(JVMDowngraderExtension::class.java)
}

private var _inputCollection: FileCollection by FinalizeOnRead(MustSet())

@get:InputFiles
var inputCollection: FileCollection by FinalizeOnRead(MustSet())
var inputCollection: FileCollection
get() = _inputCollection
set(value) {
_inputCollection = value

// use _inputCollection to finalize it immediately
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())

}

@get:InputFiles
var classpath: FileCollection by FinalizeOnRead(LazyMutable {
Expand All @@ -41,20 +52,11 @@ abstract class DowngradeFiles : ConventionTask(), DowngradeFlags {
*/
@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()
jvmdg.convention(this)
}

@TaskAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ abstract class ShadeFiles : ConventionTask(), ShadeFlags {
}

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()
jvmdg.convention(this)
}

// fun setShadePath(path: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class ShadeTransform : TransformAction<ShadeFlags> {
val flags = parameters
val output = outputs.file("${input.nameWithoutExtension}-shaded-${flags.downgradeTo.get()}.${input.extension}")

ApiShader.shadeApis(flags.toFlags(), flags.shadePath.get().invoke(input.nameWithoutExtension), input, output, flags.apiJar.get())
ApiShader.shadeApis(flags.toFlags(), flags.shadePath.get().invoke(input.nameWithoutExtension), input, output, flags.apiJar.get().toSet())
}

}
2 changes: 1 addition & 1 deletion gradle-plugin/test-downgrade/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ sourceSets {
val jvmdg = extensions.getByType(JVMDowngraderExtension::class.java)

if (project.hasProperty("runningTest")) {
jvmdg.apiJar = project.file("../../java-api/build/libs/jvmdowngrader-java-api-${props.getProperty("version")}.jar")
jvmdg.apiJar = listOf(project.file("../../java-api/build/libs/jvmdowngrader-java-api-${props.getProperty("version")}.jar"))
}

val downgrade by configurations.creating
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kotlin.code.style=official
org.gradle.jvmargs=-Xmx4G
org.gradle.parallel=true

version=0.8.4
version=0.9.0

asm_version=9.7

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class J_L_CharSequence {

@Stub
@Stub(excludeChild = "java/lang/String")
public static boolean isEmpty(CharSequence cs) {
return cs.length() == 0;
}
Expand Down
Loading

0 comments on commit 42dbe6d

Please sign in to comment.