Skip to content

Commit

Permalink
Add VelocityPluginJson factory (#4)
Browse files Browse the repository at this point in the history
* paper-plugin.yml requires api-version

* Exclude empty maps from plugin ymls

* Add VelocityPluginJson factory
  • Loading branch information
jpenilla authored Mar 22, 2024
1 parent 03d6c46 commit e0be5b7
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 16 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1 change: 1 addition & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ repositories {

dependencies {
implementation(libs.configurateYaml)
implementation(libs.configurateGson)
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <T : ResourceFactory> factory(
generatorType: KClass<T>,
vararg params: Any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,6 +155,7 @@ class BukkitPluginYml(
}
}
.path(path)
.nodeStyle(NodeStyle.BLOCK)
.build()
}
)
Expand Down Expand Up @@ -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
}
}
6 changes: 6 additions & 0 deletions plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt
Original file line number Diff line number Diff line change
@@ -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 <T> ListProperty<T>.nullIfEmpty(): List<T>? = if (get().isEmpty()) null else get().toList()

fun <A, B> MapProperty<A, B>.nullIfEmpty(): Map<A, B>? = if (get().isEmpty()) null else get().toMap()

fun <A> NamedDomainObjectContainer<A>.nullIfEmpty(): Map<String, A>? = if (isEmpty()) null else asMap.toMap()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,7 +37,6 @@ class PaperPluginYml constructor(
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {

@get:Input
@get:Optional
val apiVersion: Property<String> = objects.property()

@get:Input
Expand Down Expand Up @@ -166,6 +166,7 @@ class PaperPluginYml constructor(
s.registerExact(Permission.Default::class.java, Permission.Default.Serializer)
}
}
.nodeStyle(NodeStyle.BLOCK)
.path(path)
.build()
}
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -207,14 +208,18 @@ class PaperPluginYml constructor(

@ConfigSerializable
data class SerializableDependencies(
val bootstrap: Map<String, SerializableDependency>,
val server: Map<String, SerializableDependency>
val bootstrap: Map<String, SerializableDependency>?,
val server: Map<String, SerializableDependency>?
) {
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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> = objects.property()

@get:Input
@get:Optional
val name: Property<String> = objects.property()

@get:Input
@get:Optional
val version: Property<String> = objects.property()

@get:Input
@get:Optional
val description: Property<String> = objects.property()

@get:Input
@get:Optional
val url: Property<String> = objects.property()

@get:Input
val authors: ListProperty<String> = objects.listProperty()

@get:Nested
val dependencies: ListProperty<Dependency> = objects.listProperty()

fun dependency(id: String, optional: Boolean) = dependencies.add(Dependency(id, optional))

@get:Input
val main: Property<String> = 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()
}
}
5 changes: 5 additions & 0 deletions tester/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}

0 comments on commit e0be5b7

Please sign in to comment.