Skip to content

Commit

Permalink
Add ProjectMetaConventions interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Mar 23, 2024
1 parent 3ae0fa3 commit afb18f6
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.withType
import xyz.jpenilla.resourcefactory.util.ProjectMetaConventions

abstract class ResourceFactoryConventionPlugin<E : Any>(
private val extensionName: String,
Expand All @@ -29,5 +30,10 @@ abstract class ResourceFactoryConventionPlugin<E : Any>(
}
}

abstract fun touchExtension(target: Project, ext: E)
open fun touchExtension(target: Project, ext: E) {
if (ext is ProjectMetaConventions) {
// we need to set the conventions again, since the object was created eagerly.
target.afterEvaluate { ext.setConventionsFromProjectMeta(this) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package xyz.jpenilla.resourcefactory.bukkit

import org.gradle.api.Project
import xyz.jpenilla.resourcefactory.ResourceFactoryConventionPlugin

abstract class BukkitConvention : ResourceFactoryConventionPlugin<BukkitPluginYml>(
"bukkitPluginYml",
{ project -> project.bukkitPluginYml() },
"main",
{ factoryExt, ext -> factoryExt.factory(ext.resourceFactory()) }
) {
override fun touchExtension(target: Project, ext: BukkitPluginYml) {
target.afterEvaluate {
// we need to set the conventions again, since the object was created eagerly.
ext.copyProjectMeta(this)
}
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ 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.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import java.nio.file.Path

fun Project.bukkitPluginYml(configure: Action<BukkitPluginYml> = nullAction()): BukkitPluginYml {
val yml = BukkitPluginYml(objects)
yml.copyProjectMeta(this)
yml.setConventionsFromProjectMeta(this)
configure.execute(yml)
return yml
}

class BukkitPluginYml(
@Transient
private val objects: ObjectFactory
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider, ProjectMetaConventions {

@get:Input
@get:Optional
Expand Down Expand Up @@ -114,7 +115,7 @@ class BukkitPluginYml(
*
* [project] project
*/
fun copyProjectMeta(project: Project) {
override fun setConventionsFromProjectMeta(project: Project) {
name.convention(project.name)
version.convention(project.version as String?)
description.convention(project.description)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package xyz.jpenilla.resourcefactory.bungee

import org.gradle.api.Project
import xyz.jpenilla.resourcefactory.ResourceFactoryConventionPlugin

abstract class BungeeConvention : ResourceFactoryConventionPlugin<BungeePluginYml>(
"bungeePluginYml",
{ project -> project.bungeePluginYml() },
"main",
{ factoryExt, ext -> factoryExt.factory(ext.resourceFactory()) }
) {
override fun touchExtension(target: Project, ext: BungeePluginYml) {
target.afterEvaluate {
// we need to set the conventions again, since the object was created eagerly.
ext.copyProjectMeta(this)
}
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ 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.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import java.nio.file.Path

fun Project.bungeePluginYml(configure: Action<BungeePluginYml> = nullAction()): BungeePluginYml {
val yml = BungeePluginYml(objects)
yml.copyProjectMeta(this)
yml.setConventionsFromProjectMeta(this)
configure.execute(yml)
return yml
}

class BungeePluginYml constructor(
@Transient
private val objects: ObjectFactory
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider, ProjectMetaConventions {

@get:Input
val name: Property<String> = objects.property()
Expand Down Expand Up @@ -59,7 +60,7 @@ class BungeePluginYml constructor(
return Serializable(this)
}

fun copyProjectMeta(project: Project) {
override fun setConventionsFromProjectMeta(project: Project) {
name.convention(project.name)
description.convention(project.description)
version.convention(project.version as String?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package xyz.jpenilla.resourcefactory.fabric

import org.gradle.api.Project
import xyz.jpenilla.resourcefactory.ResourceFactoryConventionPlugin

abstract class FabricConvention : ResourceFactoryConventionPlugin<FabricModJson>(
"fabricModJson",
{ project -> project.fabricModJson() },
"main",
{ factoryExt, ext -> factoryExt.factory(ext.resourceFactory()) }
) {
override fun touchExtension(target: Project, ext: FabricModJson) {
target.afterEvaluate {
// we need to set the conventions again, since the object was created eagerly.
ext.copyProjectMeta(this)
}
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.spongepowered.configurate.serialize.TypeSerializer
import org.spongepowered.configurate.util.NamingSchemes
import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory
import xyz.jpenilla.resourcefactory.ResourceFactory
import xyz.jpenilla.resourcefactory.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import java.lang.reflect.Type
Expand All @@ -29,15 +30,15 @@ import javax.inject.Inject

fun Project.fabricModJson(configure: Action<FabricModJson> = nullAction()): FabricModJson {
val yml = FabricModJson(objects)
yml.copyProjectMeta(this)
yml.setConventionsFromProjectMeta(this)
configure.execute(yml)
return yml
}

open class FabricModJson constructor(
@Transient
private val objects: ObjectFactory
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider, ProjectMetaConventions {

@get:Input
val id: Property<String> = objects.property()
Expand Down Expand Up @@ -154,7 +155,7 @@ open class FabricModJson constructor(
*
* [project] project
*/
fun copyProjectMeta(project: Project) {
override fun setConventionsFromProjectMeta(project: Project) {
id.convention(project.name)
name.convention(project.name)
version.convention(project.version as String?)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package xyz.jpenilla.resourcefactory.paper

import org.gradle.api.Project
import xyz.jpenilla.resourcefactory.ResourceFactoryConventionPlugin

abstract class PaperConvention : ResourceFactoryConventionPlugin<PaperPluginYml>(
"paperPluginYml",
{ project -> project.paperPluginYml() },
"main",
{ factoryExt, ext -> factoryExt.factory(ext.resourceFactory()) }
) {
override fun touchExtension(target: Project, ext: PaperPluginYml) {
target.afterEvaluate {
// we need to set the conventions again, since the object was created eagerly.
ext.copyProjectMeta(this)
}
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ import org.spongepowered.configurate.yaml.YamlConfigurationLoader
import xyz.jpenilla.resourcefactory.ConfigurateSingleFileResourceFactory
import xyz.jpenilla.resourcefactory.ResourceFactory
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 java.nio.file.Path
import javax.inject.Inject

fun Project.paperPluginYml(configure: Action<PaperPluginYml> = nullAction()): PaperPluginYml {
val yml = PaperPluginYml(objects)
yml.copyProjectMeta(this)
yml.setConventionsFromProjectMeta(this)
configure.execute(yml)
return yml
}

class PaperPluginYml constructor(
@Transient
private val objects: ObjectFactory
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider, ProjectMetaConventions {

@get:Input
val apiVersion: Property<String> = objects.property()
Expand Down Expand Up @@ -101,7 +102,7 @@ class PaperPluginYml constructor(
*
* [project] project
*/
fun copyProjectMeta(project: Project) {
override fun setConventionsFromProjectMeta(project: Project) {
name.convention(project.name)
version.convention(project.version as String?)
description.convention(project.description)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package xyz.jpenilla.resourcefactory.util

import org.gradle.api.Project

interface ProjectMetaConventions {
fun setConventionsFromProjectMeta(project: Project)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package xyz.jpenilla.resourcefactory.velocity

import org.gradle.api.Project
import xyz.jpenilla.resourcefactory.ResourceFactoryConventionPlugin

abstract class VelocityConvention : ResourceFactoryConventionPlugin<VelocityPluginJson>(
"velocityPluginJson",
{ project -> project.velocityPluginJson() },
"main",
{ factoryExt, ext -> factoryExt.factory(ext.resourceFactory()) }
) {
override fun touchExtension(target: Project, ext: VelocityPluginJson) {
target.afterEvaluate {
// we need to set the conventions again, since the object was created eagerly.
ext.copyProjectMeta(this)
}
}
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ 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.util.ProjectMetaConventions
import xyz.jpenilla.resourcefactory.util.nullAction
import xyz.jpenilla.resourcefactory.util.nullIfEmpty
import java.nio.file.Path

fun Project.velocityPluginJson(configure: Action<VelocityPluginJson> = nullAction()): VelocityPluginJson {
val yml = VelocityPluginJson(objects)
yml.copyProjectMeta(this)
yml.setConventionsFromProjectMeta(this)
configure.execute(yml)
return yml
}

class VelocityPluginJson constructor(
@Transient
private val objects: ObjectFactory
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider {
) : ConfigurateSingleFileResourceFactory.ObjectMapper.ValueProvider, ProjectMetaConventions {

@get:Input
val id: Property<String> = objects.property()
Expand Down Expand Up @@ -65,7 +66,7 @@ class VelocityPluginJson constructor(
return Serializable(this)
}

fun copyProjectMeta(project: Project) {
override fun setConventionsFromProjectMeta(project: Project) {
id.convention(project.name)
name.convention(project.name)
description.convention(project.description)
Expand Down

0 comments on commit afb18f6

Please sign in to comment.