add included mod ids to manifest

This commit is contained in:
moehreag 2024-05-27 12:03:19 +02:00
parent 4094397909
commit 056350e1dd
2 changed files with 55 additions and 24 deletions

View file

@ -21,10 +21,12 @@ 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.charset.StandardCharsets
import java.nio.file.FileSystems 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
import kotlin.io.path.appendText
import kotlin.io.path.createDirectories import kotlin.io.path.createDirectories
import kotlin.io.path.deleteExisting import kotlin.io.path.deleteExisting
import kotlin.io.path.exists import kotlin.io.path.exists
@ -129,18 +131,20 @@ class NonsenseGradlePlugin : Plugin<Project> {
it.isCanBeConsumed = false it.isCanBeConsumed = false
} }
project.tasks.getByName("jar").actions.addLast { task -> project.tasks.getByName("jar") { task ->
task.outputs.upToDateWhen { false }
task.actions.addLast { _ ->
task.outputs.files.forEach { file -> 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."))) {
val outFile = output.resolve(file.name.substring(0, file.name.length - 4) + ".frogmod") val outFile = output.resolve(file.name.substring(0, file.name.length - 4) + ".frogmod")
Files.copy(file.toPath(), outFile, StandardCopyOption.REPLACE_EXISTING) Files.copy(file.toPath(), outFile, StandardCopyOption.REPLACE_EXISTING)
if (includeConfiguration.isPresent) {
FileSystems.newFileSystem(outFile).use { fs -> FileSystems.newFileSystem(outFile).use { fs ->
if (includeConfiguration.isPresent) {
val jijPath = fs.getPath("META-INF/jars") val jijPath = fs.getPath("META-INF/jars")
jijPath.createDirectories() val files =
val files = Nester.run(includeConfiguration.get(), jijPath).map { it.toString() }.toList() listOf(Nester.run(includeConfiguration.get(), jijPath).map { it.toml() }.toList())
if (files.isNotEmpty()) { if (files.isNotEmpty()) {
val manifest = fs.getPath("frog.mod.toml") val manifest = fs.getPath("frog.mod.toml")
val config: CommentedConfig = val config: CommentedConfig =
@ -151,12 +155,20 @@ class NonsenseGradlePlugin : Plugin<Project> {
TomlWriter().write(config, manifest, WritingMode.REPLACE) TomlWriter().write(config, manifest, WritingMode.REPLACE)
} }
} }
fs.getPath("META-INF/MANIFEST.MF").appendText(
"""
Built-By: Phytotelma ${this.javaClass.`package`.implementationVersion}
Target-Namespace: Mojmap
Built-For: Minecraft $minecraftVersion
""".trimIndent(), StandardCharsets.UTF_8
)
} }
println("Built mod to ${outFile.toUri()}") println("Built mod to ${outFile.toUri()}")
} }
} }
} }
} }
}
companion object { companion object {
lateinit var nonsenseCacheDir: Path lateinit var nonsenseCacheDir: Path

View file

@ -1,5 +1,7 @@
package org.ecorous.esnesnon.gradle.nest package org.ecorous.esnesnon.gradle.nest
import com.electronwill.nightconfig.core.Config
import com.electronwill.nightconfig.core.UnmodifiableConfig
import com.google.common.jimfs.Jimfs import com.google.common.jimfs.Jimfs
import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.artifacts.ProjectDependency
@ -13,8 +15,8 @@ import java.util.*
import kotlin.io.path.* import kotlin.io.path.*
object Nester { object Nester {
fun run(configuration: Configuration, path: Path): Collection<Path> { fun run(configuration: Configuration, path: Path): Collection<NestedJar> {
val files = mutableListOf<Path>() val files = mutableListOf<NestedJar>()
Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()).use { memFs -> Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()).use { memFs ->
configuration.dependencies.filterIsInstance<ProjectDependency>().forEach { dependency -> configuration.dependencies.filterIsInstance<ProjectDependency>().forEach { dependency ->
val project = dependency.dependencyProject val project = dependency.dependencyProject
@ -31,9 +33,13 @@ object Nester {
memFs memFs
) )
}.get() }.get()
path.createDirectories()
val target = path.resolve(location.fileName.toString()) val target = path.resolve(location.fileName.toString())
Files.copy(location, target) Files.copy(location, target)
files.add(target.absolute()) files.add(NestedJar(("${dependency.group}_${dependency.name}${if (task.archiveClassifier.isPresent) "_${task.archiveClassifier}" else ""}").replace(
"\\.".toRegex(),
"_"
).lowercase(Locale.ROOT), target.absolute().toString()))
} }
} }
configuration.resolvedConfiguration.firstLevelModuleDependencies.forEach { dep -> configuration.resolvedConfiguration.firstLevelModuleDependencies.forEach { dep ->
@ -46,9 +52,13 @@ object Nester {
artifact.classifier, artifact.classifier,
memFs memFs
) )
path.createDirectories()
val target = path.resolve(location.fileName.toString()) val target = path.resolve(location.fileName.toString())
Files.copy(location, target) Files.copy(location, target)
files.add(target.absolute()) files.add(NestedJar(("${dep.moduleGroup}_${dep.moduleName}${if (artifact.classifier != null) "_${artifact.classifier}" else ""}").replace(
"\\.".toRegex(),
"_"
).lowercase(Locale.ROOT), target.absolute().toString()))
} }
} }
} }
@ -111,3 +121,12 @@ object Nester {
} }
} }
} }
class NestedJar(val id: String, val path: String){
fun toml(): UnmodifiableConfig {
val config = Config.inMemory()
config.add("id", id)
config.add("path", path)
return config
}
}