From 17a88105eebf20c4b9cb7dd14105ca83d496dece Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Fri, 22 Mar 2024 22:42:01 -0700 Subject: [PATCH] Replace `Type.() -> Unit` with `Action` --- .../ResourceFactoryExtension.kt | 15 ++++---- .../resourcefactory/bukkit/BukkitPluginYml.kt | 8 ++-- .../resourcefactory/bungee/BungeePluginYml.kt | 8 ++-- .../resourcefactory/fabric/FabricModJson.kt | 37 ++++++++++--------- .../resourcefactory/paper/PaperPluginYml.kt | 12 +++--- .../resourcefactory/util/NullAction.kt | 11 ++++++ .../resourcefactory/{ => util}/ext.kt | 2 +- .../velocity/VelocityPluginJson.kt | 8 ++-- tester/build.gradle.kts | 1 + 9 files changed, 63 insertions(+), 39 deletions(-) create mode 100644 plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/NullAction.kt rename plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/{ => util}/ext.kt (93%) diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt index 2d02d90..691acb5 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty @@ -23,31 +24,31 @@ abstract class ResourceFactoryExtension @Inject constructor( ) { abstract val factories: ListProperty - fun paperPluginYml(op: PaperPluginYml.() -> Unit): PaperPluginYml { + fun paperPluginYml(op: Action): PaperPluginYml { val config = project.paperPluginYml(op) factory(config.resourceFactory()) return config } - fun bukkitPluginYml(op: BukkitPluginYml.() -> Unit): BukkitPluginYml { + fun bukkitPluginYml(op: Action): BukkitPluginYml { val config = project.bukkitPluginYml(op) factory(config.resourceFactory()) return config } - fun velocityPluginJson(op: VelocityPluginJson.() -> Unit): VelocityPluginJson { + fun velocityPluginJson(op: Action): VelocityPluginJson { val config = project.velocityPluginJson(op) factory(config.resourceFactory()) return config } - fun fabricModJson(op: FabricModJson.() -> Unit): FabricModJson { + fun fabricModJson(op: Action): FabricModJson { val config = project.fabricModJson(op) factory(config.resourceFactory()) return config } - fun bungeePluginYml(op: BungeePluginYml.() -> Unit): BungeePluginYml { + fun bungeePluginYml(op: Action): BungeePluginYml { val config = project.bungeePluginYml(op) factory(config.resourceFactory()) return config @@ -56,10 +57,10 @@ abstract class ResourceFactoryExtension @Inject constructor( fun factory( generatorType: KClass, vararg params: Any, - op: T.() -> Unit + op: Action ): T { val o = objects.newInstance(generatorType, *params) - o.op() + op.execute(o) factory(o) return o } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt index e672fa1..4a29762 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory.bukkit +import org.gradle.api.Action import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Project import org.gradle.api.model.ObjectFactory @@ -16,13 +17,14 @@ import org.spongepowered.configurate.yaml.NodeStyle import org.spongepowered.configurate.yaml.YamlConfigurationLoader import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory import xyz.jpenilla.resourcefactory.ResourceFactory -import xyz.jpenilla.resourcefactory.nullIfEmpty +import xyz.jpenilla.resourcefactory.util.nullAction +import xyz.jpenilla.resourcefactory.util.nullIfEmpty import java.nio.file.Path -fun Project.bukkitPluginYml(op: BukkitPluginYml.() -> Unit = {}): BukkitPluginYml { +fun Project.bukkitPluginYml(op: Action = nullAction()): BukkitPluginYml { val yml = BukkitPluginYml(objects) yml.copyProjectMeta(this) - yml.op() + op.execute(yml) return yml } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt index 0ee4ee7..6dec742 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory.bungee +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.Property @@ -14,13 +15,14 @@ import org.spongepowered.configurate.yaml.NodeStyle import org.spongepowered.configurate.yaml.YamlConfigurationLoader import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory import xyz.jpenilla.resourcefactory.ResourceFactory -import xyz.jpenilla.resourcefactory.nullIfEmpty +import xyz.jpenilla.resourcefactory.util.nullAction +import xyz.jpenilla.resourcefactory.util.nullIfEmpty import java.nio.file.Path -fun Project.bungeePluginYml(op: BungeePluginYml.() -> Unit = {}): BungeePluginYml { +fun Project.bungeePluginYml(op: Action = nullAction()): BungeePluginYml { val yml = BungeePluginYml(objects) yml.copyProjectMeta(this) - yml.op() + op.execute(yml) return yml } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/fabric/FabricModJson.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/fabric/FabricModJson.kt index 365495f..549da9e 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/fabric/FabricModJson.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/fabric/FabricModJson.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory.fabric +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty @@ -20,15 +21,16 @@ import org.spongepowered.configurate.serialize.TypeSerializer import org.spongepowered.configurate.util.NamingSchemes import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory import xyz.jpenilla.resourcefactory.ResourceFactory -import xyz.jpenilla.resourcefactory.nullIfEmpty +import xyz.jpenilla.resourcefactory.util.nullAction +import xyz.jpenilla.resourcefactory.util.nullIfEmpty import java.lang.reflect.Type import java.nio.file.Path import javax.inject.Inject -fun Project.fabricModJson(op: FabricModJson.() -> Unit = {}): FabricModJson { +fun Project.fabricModJson(op: Action = nullAction()): FabricModJson { val yml = FabricModJson(objects) yml.copyProjectMeta(this) - yml.op() + op.execute(yml) return yml } @@ -50,20 +52,20 @@ open class FabricModJson constructor( @get:Nested val entrypoints: ListProperty = objects.listProperty() - fun mainEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) = + fun mainEntrypoint(value: String, op: Action = nullAction()) = entrypoint("main", value, op) - fun clientEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) = + fun clientEntrypoint(value: String, op: Action = nullAction()) = entrypoint("client", value, op) - fun serverEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) = + fun serverEntrypoint(value: String, op: Action = nullAction()) = entrypoint("server", value, op) - fun entrypoint(type: String, value: String, op: Entrypoint.() -> Unit = {}): Entrypoint { + fun entrypoint(type: String, value: String, op: Action = nullAction()): Entrypoint { val ep = objects.newInstance(Entrypoint::class) ep.type.set(type) ep.value.set(value) - ep.op() + op.execute(ep) entrypoints.add(ep) return ep } @@ -74,10 +76,10 @@ open class FabricModJson constructor( @get:Nested val mixins: ListProperty = objects.listProperty() - fun mixin(name: String, op: MixinConfig.() -> Unit = {}): MixinConfig { + fun mixin(name: String, op: Action = nullAction()): MixinConfig { val mixin = objects.newInstance(MixinConfig::class) mixin.config.set(name) - mixin.op() + op.execute(mixin) mixins.add(mixin) return mixin } @@ -122,17 +124,17 @@ open class FabricModJson constructor( @get:Nested val authors: ListProperty = objects.listProperty() - fun author(name: String, op: Person.() -> Unit = {}) = authors.add(person(name, op)) + fun author(name: String, op: Action = nullAction()) = authors.add(person(name, op)) @get:Nested val contributors: ListProperty = objects.listProperty() - fun contributor(name: String, op: Person.() -> Unit = {}) = contributors.add(person(name, op)) + fun contributor(name: String, op: Action = nullAction()) = contributors.add(person(name, op)) @get:Nested val contact: ContactInformation = objects.newInstance() - fun contact(op: ContactInformation.() -> Unit) = contact.apply(op) + fun contact(op: Action) = contact.apply { op.execute(this) } @get:Input val license: ListProperty = objects.listProperty() @@ -189,9 +191,10 @@ open class FabricModJson constructor( val icons: Map ) : Icon - fun person(name: String, op: Person.() -> Unit = {}): Person = objects.newInstance() - .apply { this.name.set(name) } - .apply(op) + fun person(name: String, op: Action = nullAction()): Person = objects.newInstance().apply { + this.name.set(name) + op.execute(this) + } abstract class Person @Inject constructor(objects: ObjectFactory) { @get:Input @@ -200,7 +203,7 @@ open class FabricModJson constructor( @get:Nested val contact: ContactInformation = objects.newInstance() - fun contact(op: ContactInformation.() -> Unit) = contact.apply(op) + fun contact(op: Action) = contact.apply { op.execute(this) } } abstract class ContactInformation { diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt index b2fd716..12dda89 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory.paper +import org.gradle.api.Action import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectProvider import org.gradle.api.Project @@ -20,14 +21,15 @@ import org.spongepowered.configurate.yaml.YamlConfigurationLoader import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory import xyz.jpenilla.resourcefactory.ResourceFactory import xyz.jpenilla.resourcefactory.bukkit.Permission -import xyz.jpenilla.resourcefactory.nullIfEmpty +import xyz.jpenilla.resourcefactory.util.nullAction +import xyz.jpenilla.resourcefactory.util.nullIfEmpty import java.nio.file.Path import javax.inject.Inject -fun Project.paperPluginYml(op: PaperPluginYml.() -> Unit = {}): PaperPluginYml { +fun Project.paperPluginYml(op: Action = nullAction()): PaperPluginYml { val yml = PaperPluginYml(objects) yml.copyProjectMeta(this) - yml.op() + op.execute(yml) return yml } @@ -90,8 +92,8 @@ class PaperPluginYml constructor( @get:Nested val permissions: NamedDomainObjectContainer = objects.domainObjectContainer(Permission::class) { Permission(it) } - fun dependencies(op: Dependencies.() -> Unit) { - dependencies.op() + fun dependencies(op: Action) { + op.execute(dependencies) } /** diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/NullAction.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/NullAction.kt new file mode 100644 index 0000000..388d782 --- /dev/null +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/NullAction.kt @@ -0,0 +1,11 @@ +package xyz.jpenilla.resourcefactory.util + +import org.gradle.api.Action + +object NullAction : Action { + override fun execute(t: Any) { + } +} + +@Suppress("UNCHECKED_CAST") +fun nullAction(): Action = NullAction as Action diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/ext.kt similarity index 93% rename from plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt rename to plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/ext.kt index e579a7f..832a09a 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/ext.kt @@ -1,4 +1,4 @@ -package xyz.jpenilla.resourcefactory +package xyz.jpenilla.resourcefactory.util import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.provider.ListProperty diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt index e657873..80e9a86 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt @@ -1,5 +1,6 @@ package xyz.jpenilla.resourcefactory.velocity +import org.gradle.api.Action import org.gradle.api.Project import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty @@ -14,13 +15,14 @@ import org.spongepowered.configurate.gson.GsonConfigurationLoader import org.spongepowered.configurate.objectmapping.ConfigSerializable import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory import xyz.jpenilla.resourcefactory.ResourceFactory -import xyz.jpenilla.resourcefactory.nullIfEmpty +import xyz.jpenilla.resourcefactory.util.nullAction +import xyz.jpenilla.resourcefactory.util.nullIfEmpty import java.nio.file.Path -fun Project.velocityPluginJson(op: VelocityPluginJson.() -> Unit = {}): VelocityPluginJson { +fun Project.velocityPluginJson(op: Action = nullAction()): VelocityPluginJson { val yml = VelocityPluginJson(objects) yml.copyProjectMeta(this) - yml.op() + op.execute(yml) return yml } diff --git a/tester/build.gradle.kts b/tester/build.gradle.kts index b20973a..3272ef2 100644 --- a/tester/build.gradle.kts +++ b/tester/build.gradle.kts @@ -34,6 +34,7 @@ sourceSets.main { } icon("icon.png") depends("some_other_mod", "*") + apache2License() } bungeePluginYml { main = "test"