use gradle properties to eliminate nullability
All checks were successful
Publish to snapshot maven / build (push) Successful in 25s

This commit is contained in:
moehreag 2024-07-05 14:15:29 +02:00
parent 97048555c6
commit dfb3aa47c5
3 changed files with 63 additions and 59 deletions

View file

@ -7,7 +7,7 @@ plugins {
} }
group = "dev.frogmc" group = "dev.frogmc"
version = "0.0.1-alpha.13" version = "0.0.1-alpha.14"
repositories { repositories {
maven { maven {

View file

@ -11,6 +11,7 @@ import dev.frogmc.thyroxine.provider.ParchmentProvider
import org.gradle.api.Action import org.gradle.api.Action
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.model.ObjectFactory import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider import org.gradle.api.provider.Provider
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.FileSystems import java.nio.file.FileSystems
@ -24,7 +25,7 @@ abstract class MinecraftConfiguration @Inject constructor(
private val objects: ObjectFactory private val objects: ObjectFactory
) { ) {
var version: String? = null val version: Property<String> = objects.property(String::class.java).unset()
var mappings: Provider<MappingBundle> = mojmapParchment() var mappings: Provider<MappingBundle> = mojmapParchment()
internal lateinit var mappingsName: String internal lateinit var mappingsName: String
internal lateinit var targetNamespace: String internal lateinit var targetNamespace: String
@ -36,23 +37,21 @@ abstract class MinecraftConfiguration @Inject constructor(
fun mojmapParchment(action: Action<ParchmentConfiguration>): Provider<MappingBundle> { fun mojmapParchment(action: Action<ParchmentConfiguration>): Provider<MappingBundle> {
return project.provider { return project.provider {
val conf = objects.newInstance(ParchmentConfiguration::class.java) val conf = objects.newInstance(ParchmentConfiguration::class.java)
conf.gameVersion = version conf.gameVersion.convention(version)
conf.version.convention(project.provider { ParchmentProvider.findForMinecraftVersion(conf.gameVersion.get()) })
action.execute(conf) action.execute(conf)
val cacheDir = PhytotelmaPlugin.globalCacheDir val cacheDir = PhytotelmaPlugin.globalCacheDir
if (conf.version == null) { mappingsName = "mojmap(${version.get()})+parchment(${conf.gameVersion.get()}, ${conf.version.get()})"
conf.version = ParchmentProvider.findForMinecraftVersion(conf.gameVersion)
}
mappingsName = "mojmap($version)+parchment(${conf.gameVersion}, ${conf.version})"
targetNamespace = "mojmap" targetNamespace = "mojmap"
return@provider MappingBundle.merge( return@provider MappingBundle.merge(
MojmapProvider.get( MojmapProvider.get(
version, version.get(),
cacheDir.resolve("net/minecraft/client/$version/client-$version.txt") cacheDir.resolve("net/minecraft/client/${version.get()}/client-${version.get()}.txt")
).orElseThrow().reverse(), ).orElseThrow().reverse(),
ParchmentProvider.getParchment( ParchmentProvider.getParchment(
version, version.get(),
cacheDir.resolve("org/parchmentmc/parchment/${conf.gameVersion}/${conf.version}") cacheDir.resolve("org/parchmentmc/parchment/${conf.gameVersion}/${conf.version}")
) )
).renameDstNamespace(targetNamespace) ).renameDstNamespace(targetNamespace)
@ -62,20 +61,17 @@ abstract class MinecraftConfiguration @Inject constructor(
fun parchment(action: Action<ParchmentConfiguration>): Provider<MappingBundle> { fun parchment(action: Action<ParchmentConfiguration>): Provider<MappingBundle> {
return project.provider { return project.provider {
val conf = objects.newInstance(ParchmentConfiguration::class.java) val conf = objects.newInstance(ParchmentConfiguration::class.java)
conf.gameVersion = version conf.gameVersion.convention(version)
conf.version.convention(project.provider { ParchmentProvider.findForMinecraftVersion(conf.gameVersion.get()) })
action.execute(conf) action.execute(conf)
val cacheDir = PhytotelmaPlugin.globalCacheDir val cacheDir = PhytotelmaPlugin.globalCacheDir
if (conf.version == null) { mappingsName = "parchment(${conf.gameVersion.get()}, ${conf.version.get()})"
conf.version = ParchmentProvider.findForMinecraftVersion(conf.gameVersion)
}
mappingsName = "parchment(${conf.gameVersion}, ${conf.version})"
targetNamespace = "parchment" targetNamespace = "parchment"
return@provider ParchmentProvider.getParchment( return@provider ParchmentProvider.getParchment(
version, version.get(),
cacheDir.resolve("org/parchmentmc/parchment/${conf.gameVersion}/${conf.version}") cacheDir.resolve("org/parchmentmc/parchment/${conf.gameVersion.get()}/${conf.version.get()}")
).renameNamespaces("mojmap", "parchment") ).renameNamespaces("mojmap", "parchment")
} }
} }
@ -83,11 +79,11 @@ abstract class MinecraftConfiguration @Inject constructor(
fun mojmap(): Provider<MappingBundle> { fun mojmap(): Provider<MappingBundle> {
return project.provider { return project.provider {
val cacheDir = PhytotelmaPlugin.globalCacheDir val cacheDir = PhytotelmaPlugin.globalCacheDir
mappingsName = "mojmap($version)" mappingsName = "mojmap(${version.get()})"
targetNamespace = "mojmap" targetNamespace = "mojmap"
return@provider MojmapProvider.get( return@provider MojmapProvider.get(
version, version.get(),
cacheDir.resolve("net/minecraft/client/$version/client-$version.txt") cacheDir.resolve("net/minecraft/client/${version.get()}/client-${version.get()}.txt")
).orElseThrow().reverse().renameDstNamespace(targetNamespace) ).orElseThrow().reverse().renameDstNamespace(targetNamespace)
} }
} }
@ -96,15 +92,15 @@ abstract class MinecraftConfiguration @Inject constructor(
return project.provider { return project.provider {
val conf = objects.newInstance(VersionConfiguration::class.java) val conf = objects.newInstance(VersionConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.version == null) { if (!conf.version.isPresent) {
error("No version provided for quilt mappings!") error("No version provided for quilt mappings!")
} }
mappingsName = "quilt-mappings(${conf.version})" mappingsName = "quilt-mappings(${conf.version.get()})"
targetNamespace = "quilt-mappings" targetNamespace = "quilt-mappings"
// Use qm via intermediary because hashed publications are broken // Use qm via intermediary because hashed publications are broken
return@provider twoStepMappings( return@provider twoStepMappings(
"net.fabricmc:intermediary:$version:v2", "net.fabricmc:intermediary:${version.get()}:v2",
"org.quiltmc:quilt-mappings:${conf.version}:intermediary-v2" "org.quiltmc:quilt-mappings:${conf.version.get()}:intermediary-v2"
).filterClasses { !it.startsWith("net/minecraft/unmapped") } ).filterClasses { !it.startsWith("net/minecraft/unmapped") }
.flatten(true).renameDstNamespace(targetNamespace) .flatten(true).renameDstNamespace(targetNamespace)
} }
@ -114,15 +110,15 @@ abstract class MinecraftConfiguration @Inject constructor(
return project.provider { return project.provider {
val conf = objects.newInstance(VersionConfiguration::class.java) val conf = objects.newInstance(VersionConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.version == null) { if (!conf.version.isPresent) {
error("No version provided for yarn!") error("No version provided for yarn!")
} }
mappingsName = "yarn(${conf.version})" mappingsName = "yarn(${conf.version.get()})"
targetNamespace = "yarn" targetNamespace = "yarn"
// Use qm via intermediary because hashed publications are broken // Use qm via intermediary because hashed publications are broken
return@provider twoStepMappings( return@provider twoStepMappings(
"net.fabricmc:intermediary:$version:v2", "net.fabricmc:intermediary:${version.get()}:v2",
"net.fabricmc:yarn:${conf.version}:v2" "net.fabricmc:yarn:${conf.version.get()}:v2"
).filterClasses { !it.startsWith("net/minecraft/class_") } ).filterClasses { !it.startsWith("net/minecraft/class_") }
.flatten(true).renameDstNamespace(targetNamespace) .flatten(true).renameDstNamespace(targetNamespace)
} }
@ -132,14 +128,14 @@ abstract class MinecraftConfiguration @Inject constructor(
return project.provider { return project.provider {
val conf = objects.newInstance(TwoStepMappingsConfiguration::class.java) val conf = objects.newInstance(TwoStepMappingsConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.first == null) { if (!conf.first.isPresent) {
error("No version provided for first mapping step!") error("No version provided for first mapping step!")
} }
if (conf.second == null) { if (!conf.second.isPresent) {
error("No version provided for second mapping step!") error("No version provided for second mapping step!")
} }
mappingsName = "custom/two-step(${conf.first!!}, ${conf.second!!})" mappingsName = "custom/two-step(${conf.first.get()}, ${conf.second.get()})"
val bundle = twoStepMappings(conf.first!!, conf.second!!) val bundle = twoStepMappings(conf.first.get(), conf.second.get())
targetNamespace = bundle.flattenData().dstNamespace targetNamespace = bundle.flattenData().dstNamespace
return@provider bundle return@provider bundle
} }
@ -149,20 +145,24 @@ abstract class MinecraftConfiguration @Inject constructor(
return project.provider { return project.provider {
val conf = objects.newInstance(NameConfiguration::class.java) val conf = objects.newInstance(NameConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.name == null) { if (!conf.name.isPresent) {
error("No maven coordinate provided for tiny mappings!") error("No maven coordinate provided for tiny mappings!")
} }
mappingsName = "custom/tiny(${conf.name})" mappingsName = "custom/tiny(${conf.name.get()})"
val bundle = tinyMappings(conf.name!!) val bundle = tinyMappings(conf.name.get())
targetNamespace = bundle.flattenData().dstNamespace targetNamespace = bundle.flattenData().dstNamespace
return@provider bundle return@provider bundle
} }
} }
fun mappings(action: Action<MappingBundle>): Provider<MappingBundle> { fun mappings(action: Action<MappingConfiguration>): Provider<MappingBundle> {
return project.provider { return project.provider {
val bundle = MappingBundle() val conf = objects.newInstance(MappingConfiguration::class.java)
action.execute(bundle) action.execute(conf)
if (!conf.mappings.isPresent) {
error("No mappings provided!")
}
val bundle = conf.mappings.get()
mappingsName = "custom(${bundle.srcNamespaces()}, ${bundle.dstNamespaces()})" mappingsName = "custom(${bundle.srcNamespaces()}, ${bundle.dstNamespaces()})"
targetNamespace = bundle.flattenData().dstNamespace targetNamespace = bundle.flattenData().dstNamespace
return@provider bundle return@provider bundle
@ -177,8 +177,8 @@ abstract class MinecraftConfiguration @Inject constructor(
var back = MappingBundle() var back = MappingBundle()
var name = "layer[" var name = "layer["
val layers = conf.layers.mapIndexed { index, provider -> val layers = conf.layers.get().mapIndexed { index, provider ->
val bundle = provider.get().flatten(true) val bundle = (provider.get() as MappingBundle).flatten(true)
if (index == 0) { if (index == 0) {
name += mappingsName name += mappingsName
back.insert(bundle) back.insert(bundle)
@ -229,31 +229,35 @@ abstract class MinecraftConfiguration @Inject constructor(
return path return path
} }
abstract class TwoStepMappingsConfiguration { abstract class TwoStepMappingsConfiguration @Inject constructor(objects: ObjectFactory) {
var first: String? = null val first: Property<String> = objects.property(String::class.java).unset()
var second: String? = null val second: Property<String> = objects.property(String::class.java).unset()
} }
abstract class ParchmentConfiguration { abstract class ParchmentConfiguration @Inject constructor(objects: ObjectFactory) {
var gameVersion: String? = null val gameVersion: Property<String> = objects.property(String::class.java).unset()
var version: String? = null val version: Property<String> = objects.property(String::class.java).unset()
} }
abstract class LayerConfiguration { abstract class LayerConfiguration @Inject constructor(objects: ObjectFactory) {
internal val layers = mutableListOf<Provider<MappingBundle>>() internal val layers = objects.listProperty(Provider::class.java)
fun add(mappings: Provider<MappingBundle>): LayerConfiguration { fun add(mappings: Provider<MappingBundle>): LayerConfiguration {
layers.add(mappings) layers.add(mappings)
return this return this
} }
} }
abstract class MappingConfiguration @Inject constructor(objects: ObjectFactory) {
val mappings: Property<MappingBundle> = objects.property(MappingBundle::class.java).unset()
}
} }
abstract class NameConfiguration { abstract class NameConfiguration @Inject constructor(objects: ObjectFactory) {
var name: String? = null val name: Property<String> = objects.property(String::class.java).unset()
} }
abstract class VersionConfiguration { abstract class VersionConfiguration @Inject constructor(objects: ObjectFactory) {
var version: String? = null val version: Property<String> = objects.property(String::class.java).unset()
} }

View file

@ -256,10 +256,10 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
val mcConf = objects.newInstance(MinecraftConfiguration::class.java) val mcConf = objects.newInstance(MinecraftConfiguration::class.java)
action.execute(mcConf) action.execute(mcConf)
if (mcConf.version == null) { if (!mcConf.version.isPresent) {
error("No Minecraft version provided!") error("No Minecraft version provided!")
} }
val version = mcConf.version!! val version = mcConf.version.get()
if (VersionChecker.validateVersion(version, offlineMode = project.gradle.startParameter.isOffline)) { if (VersionChecker.validateVersion(version, offlineMode = project.gradle.startParameter.isOffline)) {
@ -311,18 +311,18 @@ abstract class PhytotelmaGradleExtensionImpl @Inject constructor(
override fun loader(action: Action<VersionConfiguration>) { override fun loader(action: Action<VersionConfiguration>) {
val conf = objects.newInstance(VersionConfiguration::class.java) val conf = objects.newInstance(VersionConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.version == null) { if (!conf.version.isPresent) {
error("No loader version provided!") error("No loader version provided!")
} }
project.dependencies.add("implementation", "dev.frogmc:frogloader:${conf.version!!}") project.dependencies.add("implementation", "dev.frogmc:frogloader:${conf.version.get()}")
} }
override fun froglib(action: Action<VersionConfiguration>) { override fun froglib(action: Action<VersionConfiguration>) {
val conf = objects.newInstance(VersionConfiguration::class.java) val conf = objects.newInstance(VersionConfiguration::class.java)
action.execute(conf) action.execute(conf)
if (conf.version == null) { if (!conf.version.isPresent) {
error("No froglib version provided!") error("No froglib version provided!")
} }
project.dependencies.add("modImplementation", "dev.frogmc:froglib:${conf.version!!}") project.dependencies.add("modImplementation", "dev.frogmc:froglib:${conf.version.get()}")
} }
} }