diff --git a/README.md b/README.md index c788927..77417d0 100644 --- a/README.md +++ b/README.md @@ -24,18 +24,21 @@ Gradle plugin for generating resource files at build time. } } ``` - + ## Included factories - PaperPluginYml - BukkitPluginYml +- VelocityPluginJson + +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. -Bukkit and Paper plugin YMLs can be created in two ways: 1) Directly on the project instance, and then registered manually ```kotlin - import xyz.jpenilla.resourcefactory.(bukkit|paper).(bukkit|paper)PluginYml + import xyz.jpenilla.resourcefactory.paper.paperPluginYml - val yml = (bukkit|paper)PluginYml { + val yml = paperPluginYml { // Defaults for name, version, and description are inherited from the Gradle project main = "main.class.Name" authors.add("MyName") @@ -52,7 +55,7 @@ Bukkit and Paper plugin YMLs can be created in two ways: ```kotlin sourceSets.main { resourceFactory { - (bukkit|paper)PluginYml { + paperPluginYml { // Defaults for name, version, and description are inherited from the Gradle project main = "main.class.Name" authors.add("MyName") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index dec2fb2..e89fb8b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,5 +11,6 @@ configurate = "4.1.2" [libraries] configurateYaml = { group = "org.spongepowered", name = "configurate-yaml", version.ref = "configurate" } +configurateGson = { group = "org.spongepowered", name = "configurate-gson", version.ref = "configurate" } [bundles] diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index eb4450c..2fc346f 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -20,6 +20,7 @@ repositories { dependencies { implementation(libs.configurateYaml) + implementation(libs.configurateGson) } kotlin { diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt index b393a46..d7e9363 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ResourceFactoryExtension.kt @@ -8,6 +8,8 @@ import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYml import xyz.jpenilla.resourcefactory.bukkit.bukkitPluginYml import xyz.jpenilla.resourcefactory.paper.PaperPluginYml import xyz.jpenilla.resourcefactory.paper.paperPluginYml +import xyz.jpenilla.resourcefactory.velocity.VelocityPluginJson +import xyz.jpenilla.resourcefactory.velocity.velocityPluginJson import javax.inject.Inject import kotlin.reflect.KClass @@ -29,6 +31,12 @@ abstract class ResourceFactoryExtension @Inject constructor( return config } + fun velocityPluginJson(op: VelocityPluginJson.() -> Unit): VelocityPluginJson { + val config = project.velocityPluginJson(op) + factory(config.resourceFactory()) + return config + } + fun factory( generatorType: KClass, vararg params: Any, 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 e017be5..e672fa1 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/bukkit/BukkitPluginYml.kt @@ -12,6 +12,7 @@ import org.gradle.kotlin.dsl.listProperty import org.gradle.kotlin.dsl.newInstance import org.gradle.kotlin.dsl.property 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 @@ -154,6 +155,7 @@ class BukkitPluginYml( } } .path(path) + .nodeStyle(NodeStyle.BLOCK) .build() } ) @@ -184,8 +186,8 @@ class BukkitPluginYml( val defaultPermission = yml.defaultPermission.orNull val provides = yml.provides.nullIfEmpty() val libraries = yml.libraries.nullIfEmpty() - val commands = yml.commands.asMap.toMap() - val permissions = yml.permissions.asMap.toMap() + val commands = yml.commands.nullIfEmpty() + val permissions = yml.permissions.nullIfEmpty() val foliaSupported = yml.foliaSupported.orNull } } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt index 02d1cd9..ea3a717 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt @@ -1,5 +1,11 @@ package xyz.jpenilla.resourcefactory +import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.MapProperty fun ListProperty.nullIfEmpty(): List? = if (get().isEmpty()) null else get().toList() + +fun MapProperty.nullIfEmpty(): Map? = if (get().isEmpty()) null else get().toMap() + +fun NamedDomainObjectContainer.nullIfEmpty(): Map? = if (isEmpty()) null else asMap.toMap() 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 012afa2..b2fd716 100644 --- a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/paper/PaperPluginYml.kt @@ -15,6 +15,7 @@ import org.gradle.kotlin.dsl.listProperty import org.gradle.kotlin.dsl.newInstance import org.gradle.kotlin.dsl.property 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 @@ -36,7 +37,6 @@ class PaperPluginYml constructor( ) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider { @get:Input - @get:Optional val apiVersion: Property = objects.property() @get:Input @@ -166,6 +166,7 @@ class PaperPluginYml constructor( s.registerExact(Permission.Default::class.java, Permission.Default.Serializer) } } + .nodeStyle(NodeStyle.BLOCK) .path(path) .build() } @@ -181,7 +182,7 @@ class PaperPluginYml constructor( @ConfigSerializable class Serializable(yml: PaperPluginYml) { - val apiVersion = yml.apiVersion.orNull + val apiVersion = yml.apiVersion.get() val name = yml.name.get() val version = yml.version.get() val main = yml.main.get() @@ -195,7 +196,7 @@ class PaperPluginYml constructor( val defaultPermission = yml.defaultPermission.orNull val foliaSupported = yml.foliaSupported.orNull val dependencies = SerializableDependencies.from(yml.dependencies) - val permissions = yml.permissions.asMap.toMap() + val permissions = yml.permissions.nullIfEmpty() } @ConfigSerializable @@ -207,14 +208,18 @@ class PaperPluginYml constructor( @ConfigSerializable data class SerializableDependencies( - val bootstrap: Map, - val server: Map + val bootstrap: Map?, + val server: Map? ) { companion object { - fun from(deps: Dependencies) = SerializableDependencies( - deps.bootstrap.asMap.mapValues { SerializableDependency.from(it.value) }, - deps.server.asMap.mapValues { SerializableDependency.from(it.value) } - ) + fun from(deps: Dependencies): SerializableDependencies? { + val bs = deps.bootstrap.nullIfEmpty()?.mapValues { SerializableDependency.from(it.value) } + val server = deps.server.nullIfEmpty()?.mapValues { SerializableDependency.from(it.value) } + if (bs == null && server == null) { + return null + } + return SerializableDependencies(bs, server) + } } } } diff --git a/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt new file mode 100644 index 0000000..e657873 --- /dev/null +++ b/plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/velocity/VelocityPluginJson.kt @@ -0,0 +1,105 @@ +package xyz.jpenilla.resourcefactory.velocity + +import org.gradle.api.Project +import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.ListProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Nested +import org.gradle.api.tasks.Optional +import org.gradle.kotlin.dsl.listProperty +import org.gradle.kotlin.dsl.newInstance +import org.gradle.kotlin.dsl.property +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 java.nio.file.Path + +fun Project.velocityPluginJson(op: VelocityPluginJson.() -> Unit = {}): VelocityPluginJson { + val yml = VelocityPluginJson(objects) + yml.copyProjectMeta(this) + yml.op() + return yml +} + +class VelocityPluginJson constructor( + @Transient + private val objects: ObjectFactory +) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider { + + @get:Input + val id: Property = objects.property() + + @get:Input + @get:Optional + val name: 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 url: Property = objects.property() + + @get:Input + val authors: ListProperty = objects.listProperty() + + @get:Nested + val dependencies: ListProperty = objects.listProperty() + + fun dependency(id: String, optional: Boolean) = dependencies.add(Dependency(id, optional)) + + @get:Input + val main: Property = objects.property() + + override fun asConfigSerializable(): Any { + return Serializable(this) + } + + fun copyProjectMeta(project: Project) { + id.convention(project.name) + 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 -> + GsonConfigurationLoader.builder() + .path(path) + .build() + } + ) + factory.path.set("velocity-plugin.json") + factory.value.set(this) + return factory + } + + @ConfigSerializable + class Dependency( + @get:Input + val id: String, + @get:Input + val optional: Boolean + ) + + @ConfigSerializable + class Serializable(json: VelocityPluginJson) { + val id = json.id.get() + val name = json.name.orNull + val version = json.version.orNull + val description = json.description.orNull + val url = json.url.orNull + val dependencies = json.dependencies.nullIfEmpty() + val main = json.main.get() + } +} diff --git a/tester/build.gradle.kts b/tester/build.gradle.kts index dadb0d5..b49031e 100644 --- a/tester/build.gradle.kts +++ b/tester/build.gradle.kts @@ -4,14 +4,19 @@ plugins { } version = "0.0.1-test" +description = "Resource Factory tester" sourceSets.main { resourceFactory { paperPluginYml { main = "test" + apiVersion = "1.20" } bukkitPluginYml { main = "test" } + velocityPluginJson { + main = "test" + } } }