Skip to content

Commit

Permalink
Start adding validation from schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Mar 23, 2024
1 parent 4bd26f3 commit f8f4d7e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import xyz.jpenilla.resourcefactory.ResourceFactory
import xyz.jpenilla.resourcefactory.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import xyz.jpenilla.resourcefactory.util.validate

fun Project.bukkitPluginYml(configure: Action<BukkitPluginYml> = nullAction()): BukkitPluginYml {
val yml = BukkitPluginYml(objects)
Expand Down Expand Up @@ -166,9 +167,9 @@ class BukkitPluginYml(
@ConfigSerializable
class Serializable(yml: BukkitPluginYml) {
val apiVersion = yml.apiVersion.orNull
val name = yml.name.get()
val name = yml.name.get().validate("Bukkit plugin name", "^[A-Za-z0-9_\\.-]+$")
val version = yml.version.get()
val main = yml.main.get()
val main = yml.main.get().validate("Bukkit plugin main class name", "^(?!org\\.bukkit\\.)([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*$")
val description = yml.description.orNull
val load = yml.load.orNull
val author = yml.author.orNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import xyz.jpenilla.resourcefactory.ResourceFactory
import xyz.jpenilla.resourcefactory.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import xyz.jpenilla.resourcefactory.util.validate

fun Project.bungeePluginYml(configure: Action<BungeePluginYml> = nullAction()): BungeePluginYml {
val yml = BungeePluginYml(objects)
Expand Down Expand Up @@ -74,7 +75,7 @@ class BungeePluginYml constructor(

@ConfigSerializable
class Serializable(yml: BungeePluginYml) {
val name = yml.name.get()
val name = yml.name.get().validate("Bungee plugin name", "^[A-Za-z0-9_\\.-]+$")
val main = yml.main.get()
val version = yml.version.orNull
val author = yml.author.orNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import xyz.jpenilla.resourcefactory.bukkit.Permission
import xyz.jpenilla.resourcefactory.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import xyz.jpenilla.resourcefactory.util.validate
import javax.inject.Inject

fun Project.paperPluginYml(configure: Action<PaperPluginYml> = nullAction()): PaperPluginYml {
Expand Down Expand Up @@ -177,12 +178,16 @@ class PaperPluginYml constructor(

@ConfigSerializable
class Serializable(yml: PaperPluginYml) {
companion object {
private const val PLUGIN_CLASS_PATTERN: String = "^(?!io\\.papermc\\.)([a-zA-Z_$][a-zA-Z\\d_$]*\\.)*[a-zA-Z_$][a-zA-Z\\d_$]*$"
}

val apiVersion = yml.apiVersion.get()
val name = yml.name.get()
val name = yml.name.get().validate("Paper plugin name", "^[A-Za-z0-9_\\.-]+$")
val version = yml.version.get()
val main = yml.main.get()
val loader = yml.loader.orNull
val bootstrapper = yml.bootstrapper.orNull
val main = yml.main.get().validate("Paper plugin main class name", PLUGIN_CLASS_PATTERN)
val loader = yml.loader.orNull?.validate("Paper plugin loader class name", PLUGIN_CLASS_PATTERN)
val bootstrapper = yml.bootstrapper.orNull?.validate("Paper plugin bootstrapper class name", PLUGIN_CLASS_PATTERN)
val description = yml.description.orNull
val author = yml.author.orNull
val authors = yml.authors.nullIfEmpty()
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/main/kotlin/xyz/jpenilla/resourcefactory/util/ext.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package xyz.jpenilla.resourcefactory.util

import org.gradle.api.GradleException
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.SetProperty
import java.util.regex.Pattern

fun <T> ListProperty<T>.nullIfEmpty(): List<T>? = if (get().isEmpty()) null else get().toList()

Expand All @@ -12,3 +14,11 @@ fun <T> SetProperty<T>.nullIfEmpty(): Set<T>? = if (get().isEmpty()) null else g
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()

fun String.validate(thing: String, pattern: String): String {
val regex = Pattern.compile(pattern)
if (!regex.matcher(this).matches()) {
throw GradleException("Invalid $thing '$this', must match pattern '$pattern'.")
}
return this
}

0 comments on commit f8f4d7e

Please sign in to comment.