Skip to content

Commit

Permalink
Replace Type.() -> Unit with Action<Type>
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Mar 23, 2024
1 parent 68868a5 commit 17a8810
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,31 +24,31 @@ abstract class ResourceFactoryExtension @Inject constructor(
) {
abstract val factories: ListProperty<ResourceFactory>

fun paperPluginYml(op: PaperPluginYml.() -> Unit): PaperPluginYml {
fun paperPluginYml(op: Action<PaperPluginYml>): PaperPluginYml {
val config = project.paperPluginYml(op)
factory(config.resourceFactory())
return config
}

fun bukkitPluginYml(op: BukkitPluginYml.() -> Unit): BukkitPluginYml {
fun bukkitPluginYml(op: Action<BukkitPluginYml>): BukkitPluginYml {
val config = project.bukkitPluginYml(op)
factory(config.resourceFactory())
return config
}

fun velocityPluginJson(op: VelocityPluginJson.() -> Unit): VelocityPluginJson {
fun velocityPluginJson(op: Action<VelocityPluginJson>): VelocityPluginJson {
val config = project.velocityPluginJson(op)
factory(config.resourceFactory())
return config
}

fun fabricModJson(op: FabricModJson.() -> Unit): FabricModJson {
fun fabricModJson(op: Action<FabricModJson>): FabricModJson {
val config = project.fabricModJson(op)
factory(config.resourceFactory())
return config
}

fun bungeePluginYml(op: BungeePluginYml.() -> Unit): BungeePluginYml {
fun bungeePluginYml(op: Action<BungeePluginYml>): BungeePluginYml {
val config = project.bungeePluginYml(op)
factory(config.resourceFactory())
return config
Expand All @@ -56,10 +57,10 @@ abstract class ResourceFactoryExtension @Inject constructor(
fun <T : ResourceFactory> factory(
generatorType: KClass<T>,
vararg params: Any,
op: T.() -> Unit
op: Action<T>
): T {
val o = objects.newInstance(generatorType, *params)
o.op()
op.execute(o)
factory(o)
return o
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<BukkitPluginYml> = nullAction()): BukkitPluginYml {
val yml = BukkitPluginYml(objects)
yml.copyProjectMeta(this)
yml.op()
op.execute(yml)
return yml
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<BungeePluginYml> = nullAction()): BungeePluginYml {
val yml = BungeePluginYml(objects)
yml.copyProjectMeta(this)
yml.op()
op.execute(yml)
return yml
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<FabricModJson> = nullAction()): FabricModJson {
val yml = FabricModJson(objects)
yml.copyProjectMeta(this)
yml.op()
op.execute(yml)
return yml
}

Expand All @@ -50,20 +52,20 @@ open class FabricModJson constructor(
@get:Nested
val entrypoints: ListProperty<Entrypoint> = objects.listProperty()

fun mainEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) =
fun mainEntrypoint(value: String, op: Action<Entrypoint> = nullAction()) =
entrypoint("main", value, op)

fun clientEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) =
fun clientEntrypoint(value: String, op: Action<Entrypoint> = nullAction()) =
entrypoint("client", value, op)

fun serverEntrypoint(value: String, op: Entrypoint.() -> Unit = {}) =
fun serverEntrypoint(value: String, op: Action<Entrypoint> = nullAction()) =
entrypoint("server", value, op)

fun entrypoint(type: String, value: String, op: Entrypoint.() -> Unit = {}): Entrypoint {
fun entrypoint(type: String, value: String, op: Action<Entrypoint> = 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
}
Expand All @@ -74,10 +76,10 @@ open class FabricModJson constructor(
@get:Nested
val mixins: ListProperty<MixinConfig> = objects.listProperty()

fun mixin(name: String, op: MixinConfig.() -> Unit = {}): MixinConfig {
fun mixin(name: String, op: Action<MixinConfig> = nullAction()): MixinConfig {
val mixin = objects.newInstance(MixinConfig::class)
mixin.config.set(name)
mixin.op()
op.execute(mixin)
mixins.add(mixin)
return mixin
}
Expand Down Expand Up @@ -122,17 +124,17 @@ open class FabricModJson constructor(
@get:Nested
val authors: ListProperty<Person> = objects.listProperty()

fun author(name: String, op: Person.() -> Unit = {}) = authors.add(person(name, op))
fun author(name: String, op: Action<Person> = nullAction()) = authors.add(person(name, op))

@get:Nested
val contributors: ListProperty<Person> = objects.listProperty()

fun contributor(name: String, op: Person.() -> Unit = {}) = contributors.add(person(name, op))
fun contributor(name: String, op: Action<Person> = 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<ContactInformation>) = contact.apply { op.execute(this) }

@get:Input
val license: ListProperty<String> = objects.listProperty()
Expand Down Expand Up @@ -189,9 +191,10 @@ open class FabricModJson constructor(
val icons: Map<String, String>
) : Icon

fun person(name: String, op: Person.() -> Unit = {}): Person = objects.newInstance<Person>()
.apply { this.name.set(name) }
.apply(op)
fun person(name: String, op: Action<Person> = nullAction()): Person = objects.newInstance<Person>().apply {
this.name.set(name)
op.execute(this)
}

abstract class Person @Inject constructor(objects: ObjectFactory) {
@get:Input
Expand All @@ -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<ContactInformation>) = contact.apply { op.execute(this) }
}

abstract class ContactInformation {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PaperPluginYml> = nullAction()): PaperPluginYml {
val yml = PaperPluginYml(objects)
yml.copyProjectMeta(this)
yml.op()
op.execute(yml)
return yml
}

Expand Down Expand Up @@ -90,8 +92,8 @@ class PaperPluginYml constructor(
@get:Nested
val permissions: NamedDomainObjectContainer<Permission> = objects.domainObjectContainer(Permission::class) { Permission(it) }

fun dependencies(op: Dependencies.() -> Unit) {
dependencies.op()
fun dependencies(op: Action<Dependencies>) {
op.execute(dependencies)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xyz.jpenilla.resourcefactory.util

import org.gradle.api.Action

object NullAction : Action<Any> {
override fun execute(t: Any) {
}
}

@Suppress("UNCHECKED_CAST")
fun <T> nullAction(): Action<T> = NullAction as Action<T>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.jpenilla.resourcefactory
package xyz.jpenilla.resourcefactory.util

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.provider.ListProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<VelocityPluginJson> = nullAction()): VelocityPluginJson {
val yml = VelocityPluginJson(objects)
yml.copyProjectMeta(this)
yml.op()
op.execute(yml)
return yml
}

Expand Down
1 change: 1 addition & 0 deletions tester/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sourceSets.main {
}
icon("icon.png")
depends("some_other_mod", "*")
apache2License()
}
bungeePluginYml {
main = "test"
Expand Down

0 comments on commit 17a8810

Please sign in to comment.