add jij inclusions

This commit is contained in:
moehreag 2024-05-27 00:35:39 +02:00
parent 61cfc68ddd
commit 4094397909
3 changed files with 149 additions and 12 deletions

View file

@ -1,5 +1,5 @@
plugins { plugins {
kotlin("jvm") version "1.9.23" kotlin("jvm") version "2.0.0"
`java-gradle-plugin` `java-gradle-plugin`
`maven-publish` `maven-publish`
} }
@ -25,6 +25,8 @@ dependencies {
implementation("com.google.code.gson:gson:2.10.1") implementation("com.google.code.gson:gson:2.10.1")
implementation("org.vineflower:vineflower:1.10.1") implementation("org.vineflower:vineflower:1.10.1")
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
implementation("com.electronwill.night-config:toml:3.7.1")
implementation("com.google.jimfs:jimfs:1.3.0")
} }
gradlePlugin { gradlePlugin {

View file

@ -1,7 +1,13 @@
package org.ecorous.esnesnon.gradle package org.ecorous.esnesnon.gradle
import com.electronwill.nightconfig.core.CommentedConfig
import com.electronwill.nightconfig.core.file.FileNotFoundAction
import com.electronwill.nightconfig.core.io.WritingMode
import com.electronwill.nightconfig.toml.TomlParser
import com.electronwill.nightconfig.toml.TomlWriter
import net.fabricmc.fernflower.api.IFabricJavadocProvider import net.fabricmc.fernflower.api.IFabricJavadocProvider
import org.ecorous.esnesnon.gradle.common.Env import org.ecorous.esnesnon.gradle.common.Env
import org.ecorous.esnesnon.gradle.nest.Nester
import org.ecorous.esnesnon.gradle.run.AssetDownloader import org.ecorous.esnesnon.gradle.run.AssetDownloader
import org.ecorous.esnesnon.gradle.run.RunConfigGenerator import org.ecorous.esnesnon.gradle.run.RunConfigGenerator
import org.ecorous.esnesnon.gradle.run.task.RunGameTask import org.ecorous.esnesnon.gradle.run.task.RunGameTask
@ -15,6 +21,7 @@ import org.jetbrains.java.decompiler.main.decompiler.SingleFileSaver
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences
import java.io.PrintStream import java.io.PrintStream
import java.net.URI import java.net.URI
import java.nio.file.FileSystems
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
@ -117,23 +124,38 @@ class NonsenseGradlePlugin : Plugin<Project> {
} }
} }
project.tasks.getByName("jar").actions.addLast { val includeConfiguration = project.configurations.register("include") {
it.outputs.files.forEach {file -> it.isCanBeResolved = true
it.isCanBeConsumed = false
}
project.tasks.getByName("jar").actions.addLast { task ->
task.outputs.files.forEach {file ->
val output = file.toPath().parent.resolveSibling("frog") val output = file.toPath().parent.resolveSibling("frog")
output.createDirectories() output.createDirectories()
if (file.name.endsWith(".jar") && !(file.name.contains("-dev.") || file.name.contains("-sources."))){ if (file.name.endsWith(".jar") && !(file.name.contains("-dev.") || file.name.contains("-sources."))){
Files.copy(file.toPath(), output.resolve(file.name.substring(0, file.name.length-4)+".frogmod"), StandardCopyOption.REPLACE_EXISTING) val outFile = output.resolve(file.name.substring(0, file.name.length-4)+".frogmod")
Files.copy(file.toPath(), outFile, StandardCopyOption.REPLACE_EXISTING)
if (includeConfiguration.isPresent) {
FileSystems.newFileSystem(outFile).use {fs ->
val jijPath = fs.getPath("META-INF/jars")
jijPath.createDirectories()
val files = Nester.run(includeConfiguration.get(), jijPath).map { it.toString() }.toList()
if (files.isNotEmpty()) {
val manifest = fs.getPath("frog.mod.toml")
val config: CommentedConfig =
TomlParser().parse(manifest, FileNotFoundAction.THROW_ERROR)
if (!config.add("frog.extensions.included_jars", files)) {
println("Failed to add included jars to mod manifest, make sure it doesn't include a key at 'frog.extensions.included_jars'!")
}
TomlWriter().write(config, manifest, WritingMode.REPLACE)
}
}
}
println("Built mod to ${outFile.toUri()}")
} }
} }
} }
val includeConfiguration = project.configurations.create("include").apply {
extendsFrom(project.configurations.getByName("implementation"))
}
project.tasks.getByName("jar") {
it.outputs.files(includeConfiguration.artifacts.files)
}
} }
companion object { companion object {

View file

@ -0,0 +1,113 @@
package org.ecorous.esnesnon.gradle.nest
import com.google.common.jimfs.Jimfs
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.util.*
import kotlin.io.path.*
object Nester {
fun run(configuration: Configuration, path: Path): Collection<Path> {
val files = mutableListOf<Path>()
Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()).use { memFs ->
configuration.dependencies.filterIsInstance<ProjectDependency>().forEach { dependency ->
val project = dependency.dependencyProject
project.getTasksByName(JavaPlugin.JAR_TASK_NAME, false).map { t -> t as AbstractArchiveTask }
.forEach { task ->
val location = task.archiveFile.map {
addMetadata(
it.asFile.toPath(),
dependency.group!!,
dependency.name,
dependency.version!!,
task.archiveClassifier.orNull,
memFs
)
}.get()
val target = path.resolve(location.fileName.toString())
Files.copy(location, target)
files.add(target.absolute())
}
}
configuration.resolvedConfiguration.firstLevelModuleDependencies.forEach { dep ->
dep.moduleArtifacts.forEach { artifact ->
val location = addMetadata(
artifact.file.toPath(),
dep.moduleGroup,
dep.moduleName,
dep.moduleVersion,
artifact.classifier,
memFs
)
val target = path.resolve(location.fileName.toString())
Files.copy(location, target)
files.add(target.absolute())
}
}
}
return files
}
private fun addMetadata(
input: Path,
group: String,
name: String,
version: String,
classifier: String?,
memFs: FileSystem
): Path {
FileSystems.newFileSystem(input).use { original ->
val toml = original.getPath("frog.mod.toml")
if (toml.notExists()) {
val tempFile = memFs.getPath(
"${
group.replace(
".",
"/"
)
}/${name}/${version}/${input.name}/${group.replace(".", "_")}_${input.name}"
)
if (tempFile.exists()) {
FileSystems.newFileSystem(tempFile).use {
if (it.getPath("frog.mod.toml").exists()){
return tempFile
}
}
}
tempFile.createParentDirectories()
input.copyTo(tempFile)
FileSystems.newFileSystem(tempFile).use {
it.getPath("frog.mod.toml").writeText(
"""
[frog]
manifest_version = "1.0.0"
[frog.mod]
id = "${
("${group}_$name${if (classifier != null) "_$classifier" else ""}").replace(
"\\.".toRegex(),
"_"
).lowercase(Locale.ROOT)
}"
name = "$name"
version = "${version.replace("(\\d+\\.\\d+\\.\\d+)(.*)".toRegex(), "$1+$2")}"
license = ""
[frog.extensions]
phytotelma.generated = true
""".trimIndent()
)
}
return tempFile
}
return input
}
}
}