Skip to content

Commit

Permalink
Add BungeePluginYml
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Mar 23, 2024
1 parent 8b7cc9b commit a0cd0b0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <T : ResourceFactory> factory(
generatorType: KClass<T>,
vararg params: Any,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> = objects.property()

@get:Input
val main: 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 author: Property<String> = objects.property()

@get:Input
val depends: SetProperty<String> = objects.setProperty()

@get:Input
val softDepends: SetProperty<String> = 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
}
}
3 changes: 3 additions & 0 deletions plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> ListProperty<T>.nullIfEmpty(): List<T>? = if (get().isEmpty()) null else get().toList()

fun <T> SetProperty<T>.nullIfEmpty(): Set<T>? = if (get().isEmpty()) null else get().toSet()

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()
3 changes: 3 additions & 0 deletions tester/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ sourceSets.main {
icon("icon.png")
depends("some_other_mod", "*")
}
bungeePluginYml {
main = "test"
}
}
}

0 comments on commit a0cd0b0

Please sign in to comment.