Skip to content

Commit

Permalink
Merge branch 'lts'
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed May 13, 2024
2 parents a582948 + 40e4b64 commit a293ba7
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package xyz.wagyourtail.jvmdg.gradle
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import org.gradle.api.file.FileCollection
import org.gradle.jvm.tasks.Jar
import xyz.wagyourtail.jvmdg.gradle.task.DowngradeJar
Expand All @@ -13,6 +14,13 @@ import java.io.File

abstract class JVMDowngraderExtension(val project: Project) {

var defaultMavens: Boolean by FinalizeOnRead(true)

var group by FinalizeOnRead("xyz.wagyourtail.jvmdowngrader")

val coreArchiveName by FinalizeOnRead("jvmdowngrader")
var apiArchiveName by FinalizeOnRead("jvmdowngrader-java-api")

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

var asmVersion by FinalizeOnRead("9.7")
Expand All @@ -33,8 +41,11 @@ abstract class JVMDowngraderExtension(val project: Project) {
}

val core by lazy {
if (this.defaultMavens) {
project.logger.info("Adding $defaultMavenResolver")
}
project.configurations.detachedConfiguration(
project.dependencies.create("xyz.wagyourtail.jvmdowngrader:jvmdowngrader:${version}"),
project.dependencies.create("$group:$coreArchiveName:${version}"),
project.dependencies.create("org.ow2.asm:asm:$asmVersion"),
project.dependencies.create("org.ow2.asm:asm-commons:$asmVersion"),
project.dependencies.create("org.ow2.asm:asm-tree:$asmVersion"),
Expand All @@ -43,16 +54,20 @@ abstract class JVMDowngraderExtension(val project: Project) {
}

val api by lazy {
if (this.defaultMavens) {
project.logger.info("Adding $defaultMavenResolver")
}
project.configurations.detachedConfiguration(
project.dependencies.create("xyz.wagyourtail.jvmdowngrader:jvmdowngrader-java-api:${version}")
project.dependencies.create("$group:$apiArchiveName:${version}")
)
}

private val downgradedApis = defaultedMapOf<JavaVersion, Dependency> { version ->
// if it's 8 or 11, premade exists, grab off maven
if (version.isJava8 || version.isJava11) {
if (version.isJava8 || version.isJava11 && defaultMavens) {
project.logger.lifecycle("Using pre-downgraded api for ${version.majorVersion}")
return@defaultedMapOf project.dependencies.create("xyz.wagyourtail.jvmdowngrader:jvmdowngrader-java-api:${this.version}:downgraded-${version.majorVersion}")
project.logger.info("Adding $defaultMavenResolver")
return@defaultedMapOf project.dependencies.create("$group:$apiArchiveName:${this.version}:downgraded-${version.majorVersion}")
}
project.logger.lifecycle("Generating downgraded api for ${version.majorVersion}")
// else, generate it
Expand Down Expand Up @@ -99,4 +114,29 @@ abstract class JVMDowngraderExtension(val project: Project) {
return downgradedApis[version]
}

private val defaultMavenResolver by lazy {
insertMaven { }
"Wagyourtail's Mavens"
}

fun insertMaven(action: MavenArtifactRepository.() -> Unit) {
project.repositories.maven {
it.name = "WagYourTail (Releases)"
it.url = project.uri("https://maven.wagyourtail.xyz/releases/")
it.metadataSources { ms ->
ms.mavenPom()
ms.artifact()
}
}

project.repositories.maven {
it.name = "WagYourTail (Snapshots)"
it.url = project.uri("https://maven.wagyourtail.xyz/snapshots/")
it.metadataSources { ms ->
ms.mavenPom()
ms.artifact()
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@ import org.gradle.api.Project
class JVMDowngraderPlugin: Plugin<Project> {

override fun apply(project: Project) {
project.repositories.maven {
it.name = "WagYourTail (Releases)"
it.url = project.uri("https://maven.wagyourtail.xyz/releases/")
it.metadataSources { ms ->
ms.mavenPom()
ms.artifact()
}
}

project.repositories.maven {
it.name = "WagYourTail (Snapshots)"
it.url = project.uri("https://maven.wagyourtail.xyz/snapshots/")
it.metadataSources { ms ->
ms.mavenPom()
ms.artifact()
}
}

project.extensions.create("jvmdg", JVMDowngraderExtension::class.java, project)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

package xyz.wagyourtail.jvmdg.gradle.task

import groovy.lang.Closure
import groovy.lang.DelegatesTo
import org.apache.commons.io.output.NullOutputStream
import org.gradle.api.JavaVersion
import org.gradle.api.file.FileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.ShowStacktrace
import org.gradle.api.tasks.*
import org.gradle.jvm.tasks.Jar
import org.gradle.process.JavaExecSpec
import xyz.wagyourtail.jvmdg.gradle.JVMDowngraderExtension
import xyz.wagyourtail.jvmdg.gradle.deleteIfExists
import xyz.wagyourtail.jvmdg.gradle.jvToOpc
Expand All @@ -28,8 +35,8 @@ abstract class DowngradeJar : Jar() {


@get:Internal
var sourceSet: SourceSet by FinalizeOnRead(LazyMutable {
project.extensions.getByType(SourceSetContainer::class.java).getByName("main")
var classpath: FileCollection by FinalizeOnRead(LazyMutable {
project.extensions.getByType(SourceSetContainer::class.java).getByName("main").compileClasspath
})

@get:InputFile
Expand All @@ -40,22 +47,57 @@ abstract class DowngradeJar : Jar() {
description = "Downgrades the jar to the specified version"
}

private var configureDowngrade: JavaExecSpec.() -> Unit = {}

fun configureDowngrade(spec: JavaExecSpec.() -> Unit) {
val old = configureDowngrade
configureDowngrade = {
old()
spec()
}
}

fun configureDowngrade(
@DelegatesTo(
JavaExecSpec::class,
strategy = Closure.DELEGATE_FIRST
)
closure: Closure<*>
) {
configureDowngrade {
closure.delegate = this
closure.call(this)
}
}

@TaskAction
fun doDowngrade() {
val tempOutput = temporaryDir.resolve("downgradedInput.jar")
tempOutput.deleteIfExists()

project.javaexec {
it.mainClass.set("xyz.wagyourtail.jvmdg.compile.ZipDowngrader")
it.args = listOf(
project.javaexec { spec ->
spec.mainClass.set("xyz.wagyourtail.jvmdg.compile.ZipDowngrader")
spec.args = listOf(
jvToOpc(downgradeTo).toString(),
inputFile.get().asFile.absolutePath,
tempOutput.absolutePath,
sourceSet.compileClasspath.files.joinToString(File.pathSeparator) { it.absolutePath }
classpath.files.joinToString(File.pathSeparator) { it.absolutePath }
)
it.workingDir = temporaryDir
it.classpath = jvmdg.core
it.jvmArgs = listOf("-Djvmdg.java-api=${jvmdg.api.resolve().first { it.extension == "jar" }.absolutePath}")
spec.workingDir = temporaryDir
spec.classpath = jvmdg.core
spec.jvmArgs = listOf("-Djvmdg.java-api=${jvmdg.api.resolve().first { it.extension == "jar" }.absolutePath}")

if (project.gradle.startParameter.logLevel < LogLevel.LIFECYCLE) {
spec.standardOutput = System.out
} else {
spec.standardOutput = NullOutputStream.NULL_OUTPUT_STREAM
}
if (project.gradle.startParameter.logLevel < LogLevel.LIFECYCLE || project.gradle.startParameter.showStacktrace != ShowStacktrace.INTERNAL_EXCEPTIONS) {
spec.errorOutput = System.err
} else {
spec.errorOutput = NullOutputStream.NULL_OUTPUT_STREAM
}
configureDowngrade(spec)
}.assertNormalExitValue().rethrowFailure()

inputFile.asFile.get().toPath().readZipInputStreamFor("META-INF/MANIFEST.MF", false) { inp ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

package xyz.wagyourtail.jvmdg.gradle.task

import groovy.lang.Closure
import groovy.lang.DelegatesTo
import org.apache.commons.io.output.NullOutputStream
import org.gradle.api.JavaVersion
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.ShowStacktrace
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.gradle.jvm.tasks.Jar
import org.gradle.process.JavaExecSpec
import xyz.wagyourtail.jvmdg.gradle.JVMDowngraderExtension
import xyz.wagyourtail.jvmdg.gradle.deleteIfExists
import xyz.wagyourtail.jvmdg.gradle.readZipInputStreamFor
Expand All @@ -31,6 +37,30 @@ abstract class ShadeAPI : Jar() {
@get:Optional
var shadePath by FinalizeOnRead(LazyMutable { archiveBaseName.get().replace(Regex("[ -]"), "_") + "/jvmdg/api" })


private var configureShade: JavaExecSpec.() -> Unit = {}

fun configureShade(spec: JavaExecSpec.() -> Unit) {
val old = configureShade
configureShade = {
old()
spec()
}
}

fun configureShade(
@DelegatesTo(
JavaExecSpec::class,
strategy = Closure.DELEGATE_FIRST
)
closure: Closure<*>
) {
configureShade {
closure.delegate = this
closure.call(this)
}
}

/**
* must already be downgraded
*/
Expand All @@ -57,6 +87,18 @@ abstract class ShadeAPI : Jar() {
)
spec.workingDir = temporaryDir
spec.classpath = jvmdg.core

if (project.gradle.startParameter.logLevel < LogLevel.LIFECYCLE) {
spec.standardOutput = System.out
} else {
spec.standardOutput = NullOutputStream.NULL_OUTPUT_STREAM
}
if (project.gradle.startParameter.logLevel < LogLevel.LIFECYCLE || project.gradle.startParameter.showStacktrace != ShowStacktrace.INTERNAL_EXCEPTIONS) {
spec.errorOutput = System.err
} else {
spec.errorOutput = NullOutputStream.NULL_OUTPUT_STREAM
}
configureShade(spec)
}.assertNormalExitValue().rethrowFailure()

inputFile.asFile.get().toPath().readZipInputStreamFor("META-INF/MANIFEST.MF", false) { inp ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package xyz.wagyourtail.jvmdg.j16.stub.java_base;

import xyz.wagyourtail.jvmdg.version.Stub;

import java.lang.ref.Reference;

public class J_L_R_Reference {

@Stub
public static boolean refersTo(Reference<?> ref, Object o) {
return ref.get() == o;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void init() {
// ElementType
// MethodHandles
// VarHandle
// Reference
stub(J_L_R_Reference.class);
// InvocationHandler
stub(J_L_R_RecordComponent.class);
stub(J_N_ByteBuffer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,25 @@ public class JvmDowngraderTest {
public JvmDowngraderTest() throws Exception {
}

@BeforeAll
public static void before() throws Exception {
new JvmDowngraderTest().cleanup();
}

public void cleanup() throws Exception {
if (Files.exists(downgraded)) {
Files.delete(downgraded);
}
if (Files.exists(shaded)) {
Files.delete(shaded);
}
if (Files.exists(downgradedJavaApi)) {
Files.delete(downgradedJavaApi);
}
}

private Path getDowngradedPath(Path originalPath, String suffix) throws Exception {
Path path = originalPath.getParent().resolve(originalPath.getFileName().toString().replace(".jar", suffix));
if (!Files.exists(path)) {
ZipDowngrader.downgradeZip(target.toOpcode(), originalPath, new HashSet<>(), path);
}
Files.deleteIfExists(path);
ZipDowngrader.downgradeZip(target.toOpcode(), originalPath, new HashSet<>(), path);
return path;
}

private Path getDowngradedJavaApi(Path javaApi, String suffix) throws IOException {
// resolve temp file in build
Path output = Path.of("./build/tmp/test/" + javaApi.getFileName().toString().replace(".jar", suffix));
if (!Files.exists(output)) {
ApiShader.downgradedApi(target.toOpcode(), javaApi, output);
}
Files.deleteIfExists(output);
ApiShader.downgradedApi(target.toOpcode(), javaApi, output);
return output;
}

private Path getShadedPath(Path originalPath, Path downgradedJavaApi, String suffix) throws IOException {
Path path = originalPath.getParent().resolve(originalPath.getFileName().toString().replace(".jar", suffix));
if (!Files.exists(path)) {
ApiShader.shadeApis("jvmdg/shade/", originalPath, path, downgradedJavaApi);
}
Files.deleteIfExists(path);
ApiShader.shadeApis("jvmdg/shade/", originalPath, path, downgradedJavaApi);
return path;
}

Expand Down

0 comments on commit a293ba7

Please sign in to comment.