From 05a22265f36fa881874934abb899bda54c77de40 Mon Sep 17 00:00:00 2001 From: Luke Bemish Date: Thu, 28 Mar 2024 15:46:40 -0500 Subject: [PATCH] Change system for providing build properties (#26) * Remove unneeded groovy plugin application * Change property providing system and add fix for new neoforge location --- README.md | 28 ++++++------------- .../frontend/ModsDotGroovyFrontend.groovy | 2 +- .../modsdotgroovy/gradle/MDGExtension.groovy | 19 +++++++++++++ .../gradle/ModsDotGroovyGradlePlugin.groovy | 1 - .../AbstractGatherPlatformDetailsTask.groovy | 13 +++++++++ .../tasks/AbstractMDGConvertTask.groovy | 13 ++++----- test/forge/build.gradle | 4 +++ test/forge/src/main/resources/mods.groovy | 3 +- test/multiplatform/fabric/build.gradle | 7 +++-- test/multiplatform/forge/build.gradle | 7 +++-- test/multiplatform/neoforge/build.gradle | 3 ++ test/multiplatform/quilt/build.gradle | 7 +++-- test/neoforge/build.gradle | 3 ++ test/neogradle/build.gradle | 6 ++++ 14 files changed, 80 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 8600a32a..a421c2d8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ModsDotGroovy -(todo: badge) +[![Version](https://img.shields.io/maven-central/v/org.groovymc.modsdotgroovy/modsdotgroovy?style=for-the-badge&color=blue&label=Latest%20Version&prefix=v)](https://central.sonatype.com/artifact/org.groovymc.modsdotgroovy/modsdotgroovy/) ModsDotGroovy v2 is a tool that allows writing Minecraft mod metadata files in Groovy which is then compiled down to a `mods.toml`, `fabric.mod.json`, `quilt.mod.json` and/or `plugin.yml` when the mod is built. @@ -98,27 +98,14 @@ modsDotGroovy { This determines the output format (for example, mods.toml for `Platform.FORGE`) as well as which plugins and frontend to use (unless you explicitly tell the Gradle plugin not to set up the DSL and plugins for you). -### Blacklisting environment variables -In mods.groovy, you can access build properties with `buildProperties['propertyName']`. - -This contains the properties from your gradle.properties file and any global ones, so you may want to blacklist them to -avoid leaking private keys. - -By default, the Gradle plugin excludes properties whose name contains (case-insensitive): -- pass -- password -- token -- key -- secret - -You can change the blacklist with: +### Providing build properties +In mods.groovy, you can access build properties you expose with `buildProperties['propertyName']`. To do so, you must +first explicitly tell mods.groovy to include them: ```groovy -modsDotGroovy { - environmentBlacklist = ['private'] +modsDotGroovy.gather { + projectProperty 'propertyName' } ``` -Note! This overwrites the default blacklist, so you should copy over the words above if you want to add new entries -rather than replace. ### Automatic configuration and setup Most of the time you don't need to turn this off, but for the edge-cases where you do, you can do so with: @@ -132,6 +119,9 @@ modsDotGroovy { // adds the stock plugins to your project setupPlugins = false + + // attempts to automatically infer details such as platform or minecraft version for you project + inferGether = false } ``` diff --git a/core/src/main/groovy/org/groovymc/modsdotgroovy/frontend/ModsDotGroovyFrontend.groovy b/core/src/main/groovy/org/groovymc/modsdotgroovy/frontend/ModsDotGroovyFrontend.groovy index 6959d824..67b228c4 100644 --- a/core/src/main/groovy/org/groovymc/modsdotgroovy/frontend/ModsDotGroovyFrontend.groovy +++ b/core/src/main/groovy/org/groovymc/modsdotgroovy/frontend/ModsDotGroovyFrontend.groovy @@ -8,7 +8,7 @@ abstract class ModsDotGroovyFrontend implements VersionProducer { private final ModsDotGroovyCore core /**@ - * If running in a Gradle environment, this will be populated with the {@code gradle.properties}. + * If running in a Gradle environment, this will be populated properties exposed with modsDotGroovy.gather */ public final Map buildProperties = [:] diff --git a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/MDGExtension.groovy b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/MDGExtension.groovy index b27a51f2..7edb9e34 100644 --- a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/MDGExtension.groovy +++ b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/MDGExtension.groovy @@ -24,6 +24,7 @@ import org.gradle.api.tasks.SourceSet import org.gradle.api.tasks.TaskProvider import org.gradle.language.jvm.tasks.ProcessResources import org.groovymc.modsdotgroovy.core.Platform +import org.groovymc.modsdotgroovy.core.versioning.FlexVerComparator import org.groovymc.modsdotgroovy.gradle.tasks.* import javax.inject.Inject @@ -53,6 +54,7 @@ abstract class MDGExtension { final Property modsDotGroovyFile final Multiplatform multiplatform final ListProperty catalogs + final ListProperty> gatherActions private final Property multiplatformFlag private final SourceSet sourceSet @@ -74,6 +76,7 @@ abstract class MDGExtension { this.conversionOptions = project.objects.newInstance(MDGConversionOptions) this.modsDotGroovyFile = project.objects.property(FileCollection) this.catalogs = project.objects.listProperty(String) + this.gatherActions = project.objects.listProperty(Action.class as Class>) this.platforms.convention(inferPlatforms(project)) @@ -100,6 +103,7 @@ abstract class MDGExtension { this.modsDotGroovyFile.finalizeValueOnRead() this.multiplatformFlag.finalizeValueOnRead() this.catalogs.finalizeValueOnRead() + this.gatherActions.finalizeValueOnRead() } void conversionOptions(Action action) { @@ -138,6 +142,10 @@ abstract class MDGExtension { this.platforms.set(List.of(platform)) } + void gather(Action action) { + gatherActions.add(action) + } + void apply() { if (applied) { return @@ -401,6 +409,10 @@ abstract class MDGExtension { // If we have any version catalogs specified, we should set them up setupCatalogs(gatherTask, getCatalogs().get()) + gatherActions.get().each { action -> + gatherTask.configure(action) + } + TaskProvider convertTask String processResourcesDestPath switch (platform) { @@ -409,7 +421,14 @@ abstract class MDGExtension { processResourcesDestPath = 'META-INF' break case Platform.NEOFORGE: + // Handle change of metadata location in 20.5 + Provider conventionalLocation = gatherTask.flatMap { it.platformVersion.map { + FlexVerComparator.compare(it, '20.5.0-alpha') <= 0 ? 'mods.toml' : 'neoforge.mods.toml' + }.orElse('mods.toml') } convertTask = project.tasks.register(forSourceSetName(sourceSet.name, 'modsDotGroovyToTomlNeoForge'), ModsDotGroovyToToml) + convertTask.configure { + it.outputName.convention(conventionalLocation) + } processResourcesDestPath = 'META-INF' break case Platform.FABRIC: diff --git a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/ModsDotGroovyGradlePlugin.groovy b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/ModsDotGroovyGradlePlugin.groovy index e889f987..c5299e52 100644 --- a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/ModsDotGroovyGradlePlugin.groovy +++ b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/ModsDotGroovyGradlePlugin.groovy @@ -16,7 +16,6 @@ final class ModsDotGroovyGradlePlugin implements Plugin { void apply(Project project) { // setup required plugins project.plugins.apply('java') - project.plugins.apply('groovy') JavaPluginExtension javaPluginExtension = project.extensions.getByType(JavaPluginExtension) SourceSetContainer sourceSets = javaPluginExtension.sourceSets diff --git a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractGatherPlatformDetailsTask.groovy b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractGatherPlatformDetailsTask.groovy index f210314c..e70efa9f 100644 --- a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractGatherPlatformDetailsTask.groovy +++ b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractGatherPlatformDetailsTask.groovy @@ -13,6 +13,7 @@ import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Property +import org.gradle.api.provider.Provider import org.gradle.api.provider.ProviderFactory import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Classpath @@ -40,6 +41,9 @@ abstract class AbstractGatherPlatformDetailsTask extends DefaultTask { @Input abstract MapProperty getExtraProperties() + @Input + abstract MapProperty getBuildProperties() + @Optional @Input Property<@Nullable String> getMinecraftVersion() { return minecraftVersion @@ -65,6 +69,14 @@ abstract class AbstractGatherPlatformDetailsTask extends DefaultTask { throw new IllegalStateException('ObjectFactory not injected') } + void projectProperty(String name) { + buildProperties.put(name, project.providers.gradleProperty(name)) + } + + void projectProperty(Provider name) { + buildProperties.putAll(project.providers.gradleProperty(name).>map { it -> [(name): it] }) + } + AbstractGatherPlatformDetailsTask() { outputFile.convention(projectLayout.buildDirectory.dir("generated/modsDotGroovy/${name.uncapitalize()}").map((Directory dir) -> dir.file('mdgPlatform.json'))) extraProperties.convention([:]) @@ -96,6 +108,7 @@ abstract class AbstractGatherPlatformDetailsTask extends DefaultTask { if (platformVersion !== null) { map['platformVersion'] = platformVersion } + map['buildProperties'] = getBuildProperties().get() map.putAll(getExtraProperties().get()) writer.write(new JsonBuilder(map).toPrettyString()) } diff --git a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractMDGConvertTask.groovy b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractMDGConvertTask.groovy index 7b9012e8..d5592877 100644 --- a/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractMDGConvertTask.groovy +++ b/gradle-plugin/src/main/groovy/org/groovymc/modsdotgroovy/gradle/tasks/AbstractMDGConvertTask.groovy @@ -50,10 +50,6 @@ abstract class AbstractMDGConvertTask extends DefaultTask { @Optional abstract MapProperty getBuildProperties() - @Input - @Optional - abstract SetProperty getEnvironmentBlacklist() - @Input @Optional abstract Property getPlatform() @@ -87,8 +83,6 @@ abstract class AbstractMDGConvertTask extends DefaultTask { // default to e.g. build/modsDotGroovyToToml/mods.toml output.convention(projectLayout.buildDirectory.dir('generated/modsDotGroovy/' + name.replaceFirst('ConvertTo', 'modsDotGroovyTo')).map((Directory dir) -> dir.file(outputName.get()))) - environmentBlacklist.convention(project.provider(() -> conversionOptions.get().environmentBlacklist.get())) - buildProperties.convention(project.provider(() -> filterBuildProperties(project.extensions.extraProperties.properties, environmentBlacklist.get()))) projectVersion.convention(project.provider(() -> project.version.toString())) projectGroup.convention(project.provider(() -> project.group.toString())) isMultiplatform.convention(project.provider(() -> false)) @@ -151,7 +145,12 @@ abstract class AbstractMDGConvertTask extends DefaultTask { final json = new JsonSlurper() (json.parse(platformDetailsFile.get().asFile) as Map).each { key, value -> - bindingValues[key] = value + final original = value + if (original instanceof Map && value instanceof Map) { + bindingValues[key] = original + value + } else { + bindingValues[key] = value + } } final bindings = new Binding(bindingValues) diff --git a/test/forge/build.gradle b/test/forge/build.gradle index 8c30eb44..558aee00 100644 --- a/test/forge/build.gradle +++ b/test/forge/build.gradle @@ -5,6 +5,10 @@ plugins { id 'org.groovymc.modsdotgroovy' } +modsDotGroovy.gather { + projectProperty 'credits' +} + version = '1.0.0' group = 'com.example.examplemod' base.archivesName = 'examplemod' diff --git a/test/forge/src/main/resources/mods.groovy b/test/forge/src/main/resources/mods.groovy index 98c90740..69d858d9 100644 --- a/test/forge/src/main/resources/mods.groovy +++ b/test/forge/src/main/resources/mods.groovy @@ -20,11 +20,10 @@ final mdg = ForgeModsDotGroovy.make { displayUrl = 'https://curseforge.com/minecraft/mc-mods/spammycombat' // updateJsonUrl = 'https://forge.curseupdate.com/623297/spammycombat' - // TODO: make sure to pass this in for testing credits = buildProperties.credits //displayTest = DisplayTest.MATCH_VERSION dependencies { - // TODO: can we get IDE support for this? + // TODO: can/should we get IDE support for this? (or expose it only through the environment info?) forge = ">=${platformVersion}" minecraft = '[1.19.3]' diff --git a/test/multiplatform/fabric/build.gradle b/test/multiplatform/fabric/build.gradle index d08fa486..40d1587e 100644 --- a/test/multiplatform/fabric/build.gradle +++ b/test/multiplatform/fabric/build.gradle @@ -9,6 +9,9 @@ dependencies { modImplementation 'net.fabricmc:fabric-loader:0.14.24' } -modsDotGroovy.multiplatform { - from ':multiplatform:common' +modsDotGroovy { + multiplatform.from ':multiplatform:common' + gather { + projectProperty 'credits' + } } diff --git a/test/multiplatform/forge/build.gradle b/test/multiplatform/forge/build.gradle index 86bf31f3..e528adbd 100644 --- a/test/multiplatform/forge/build.gradle +++ b/test/multiplatform/forge/build.gradle @@ -17,6 +17,9 @@ dependencies { minecraft 'net.minecraftforge:forge:1.20.2-48.0.35' } -modsDotGroovy.multiplatform { - from ':multiplatform:common' +modsDotGroovy { + multiplatform.from ':multiplatform:common' + gather { + projectProperty 'credits' + } } diff --git a/test/multiplatform/neoforge/build.gradle b/test/multiplatform/neoforge/build.gradle index 9b304ec0..f7c7da1a 100644 --- a/test/multiplatform/neoforge/build.gradle +++ b/test/multiplatform/neoforge/build.gradle @@ -11,6 +11,9 @@ modsDotGroovy { multiplatform { from ':multiplatform:common' } + gather { + projectProperty 'credits' + } apply() } diff --git a/test/multiplatform/quilt/build.gradle b/test/multiplatform/quilt/build.gradle index db6afee0..d5569cdd 100644 --- a/test/multiplatform/quilt/build.gradle +++ b/test/multiplatform/quilt/build.gradle @@ -9,6 +9,9 @@ dependencies { modImplementation 'org.quiltmc:quilt-loader:0.21.0' } -modsDotGroovy.multiplatform { - from ':multiplatform:common' +modsDotGroovy { + multiplatform.from ':multiplatform:common' + gather { + projectProperty 'credits' + } } diff --git a/test/neoforge/build.gradle b/test/neoforge/build.gradle index da4eaef3..aa7a8a4c 100644 --- a/test/neoforge/build.gradle +++ b/test/neoforge/build.gradle @@ -10,6 +10,9 @@ versionCatalogs.find('libs') modsDotGroovy { platform = Platform.NEOFORGE inferGather.set false + gather { + projectProperty 'credits' + } apply() } diff --git a/test/neogradle/build.gradle b/test/neogradle/build.gradle index b5a7599e..d8b37f29 100644 --- a/test/neogradle/build.gradle +++ b/test/neogradle/build.gradle @@ -3,6 +3,12 @@ plugins { id 'org.groovymc.modsdotgroovy' } +modsDotGroovy { + gather { + projectProperty 'credits' + } +} + runs { server { modSource project.sourceSets.main