From 7ccfcb8937717dba26db0583c0fc6e113b574dd1 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Fri, 22 Mar 2024 20:19:29 -0700 Subject: [PATCH] Add BungeePluginYml (#6) --- README.md | 1 + .../ResourceFactoryExtension.kt | 8 ++ .../resourcefactory/bungee/BungeePluginYml.kt | 91 +++++++++++++++++++ .../xyz/jpenilla/resourcefactory/ext.kt | 3 + tester/build.gradle.kts | 3 + 5 files changed, 106 insertions(+) create mode 100644 plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt diff --git a/README.md b/README.md index 8bb4bd8..c58c442 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Gradle plugin for generating resource files at build time. - BukkitPluginYml - VelocityPluginJson - FabricModJson +- BungeePluginYml The included factories can be created in two ways. PaperPluginYml is used as an example, but the process is the same for the other included factories. diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt index 3159b29..2d02d90 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt @@ -6,6 +6,8 @@ import org.gradle.api.provider.ListProperty import org.gradle.kotlin.dsl.newInstance import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYml import xyz.jpenilla.resourcefactory.bukkit.bukkitPluginYml +import xyz.jpenilla.resourcefactory.bungee.BungeePluginYml +import xyz.jpenilla.resourcefactory.bungee.bungeePluginYml import xyz.jpenilla.resourcefactory.fabric.FabricModJson import xyz.jpenilla.resourcefactory.fabric.fabricModJson import xyz.jpenilla.resourcefactory.paper.PaperPluginYml @@ -45,6 +47,12 @@ abstract class ResourceFactoryExtension @Inject constructor( return config } + fun bungeePluginYml(op: BungeePluginYml.() -> Unit): BungeePluginYml { + val config = project.bungeePluginYml(op) + factory(config.resourceFactory()) + return config + } + fun factory( generatorType: KClass, vararg params: Any, diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt new file mode 100644 index 0000000..0ee4ee7 --- /dev/null +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bungee/BungeePluginYml.kt @@ -0,0 +1,91 @@ +package xyz.jpenilla.resourcefactory.bungee + +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.Property +import org.gradle.api.provider.SetProperty +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Optional +import org.gradle.kotlin.dsl.newInstance +import org.gradle.kotlin.dsl.property +import org.gradle.kotlin.dsl.setProperty +import org.spongepowered.configurate.objectmapping.ConfigSerializable +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 java.nio.file.Path + +fun Project.bungeePluginYml(op: BungeePluginYml.() -> Unit = {}): BungeePluginYml { + val yml = BungeePluginYml(objects) + yml.copyProjectMeta(this) + yml.op() + return yml +} + +class BungeePluginYml constructor( + @Transient + private val objects: ObjectFactory +) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider { + + @get:Input + val name: Property = objects.property() + + @get:Input + val main: Property = objects.property() + + @get:Input + @get:Optional + val version: Property = objects.property() + + @get:Input + @get:Optional + val description: Property = objects.property() + + @get:Input + @get:Optional + val author: Property = objects.property() + + @get:Input + val depends: SetProperty = objects.setProperty() + + @get:Input + val softDepends: SetProperty = objects.setProperty() + + override fun asConfigSerializable(): Any { + return Serializable(this) + } + + fun copyProjectMeta(project: Project) { + name.convention(project.name) + description.convention(project.description) + version.convention(project.version as String?) + } + + fun resourceFactory(): ResourceFactory { + val factory = objects.newInstance( + ConfigurateSingleFileResourceFactory.ObjectMapper::class, + { path: Path -> + YamlConfigurationLoader.builder() + .path(path) + .nodeStyle(NodeStyle.BLOCK) + .build() + } + ) + factory.path.set("bungee.yml") + factory.value.set(this) + return factory + } + + @ConfigSerializable + class Serializable(yml: BungeePluginYml) { + val name = yml.name.get() + val main = yml.main.get() + val version = yml.version.orNull + val author = yml.author.orNull + val depends = yml.depends.nullIfEmpty() + val softDepends = yml.softDepends.nullIfEmpty() + val description = yml.description.orNull + } +} diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt index ea3a717..e579a7f 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt @@ -3,9 +3,12 @@ package xyz.jpenilla.resourcefactory import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.provider.ListProperty import org.gradle.api.provider.MapProperty +import org.gradle.api.provider.SetProperty fun ListProperty.nullIfEmpty(): List? = if (get().isEmpty()) null else get().toList() +fun SetProperty.nullIfEmpty(): Set? = if (get().isEmpty()) null else get().toSet() + fun MapProperty.nullIfEmpty(): Map? = if (get().isEmpty()) null else get().toMap() fun NamedDomainObjectContainer.nullIfEmpty(): Map? = if (isEmpty()) null else asMap.toMap() diff --git a/tester/build.gradle.kts b/tester/build.gradle.kts index 783224f..b20973a 100644 --- a/tester/build.gradle.kts +++ b/tester/build.gradle.kts @@ -35,5 +35,8 @@ sourceSets.main { icon("icon.png") depends("some_other_mod", "*") } + bungeePluginYml { + main = "test" + } } }