Skip to content

Commit

Permalink
Change system for providing build properties (#26)
Browse files Browse the repository at this point in the history
* Remove unneeded groovy plugin application

* Change property providing system and add fix for new neoforge location
  • Loading branch information
lukebemish authored Mar 28, 2024
1 parent 5e1fc28 commit 05a2226
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 36 deletions.
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ?> buildProperties = [:]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +54,7 @@ abstract class MDGExtension {
final Property<FileCollection> modsDotGroovyFile
final Multiplatform multiplatform
final ListProperty<String> catalogs
final ListProperty<Action<AbstractGatherPlatformDetailsTask>> gatherActions

private final Property<Boolean> multiplatformFlag
private final SourceSet sourceSet
Expand All @@ -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<Action<AbstractGatherPlatformDetailsTask>>)

this.platforms.convention(inferPlatforms(project))

Expand All @@ -100,6 +103,7 @@ abstract class MDGExtension {
this.modsDotGroovyFile.finalizeValueOnRead()
this.multiplatformFlag.finalizeValueOnRead()
this.catalogs.finalizeValueOnRead()
this.gatherActions.finalizeValueOnRead()
}

void conversionOptions(Action<MDGConversionOptions> action) {
Expand Down Expand Up @@ -138,6 +142,10 @@ abstract class MDGExtension {
this.platforms.set(List.of(platform))
}

void gather(Action<AbstractGatherPlatformDetailsTask> action) {
gatherActions.add(action)
}

void apply() {
if (applied) {
return
Expand Down Expand Up @@ -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<? extends AbstractMDGConvertTask> convertTask
String processResourcesDestPath
switch (platform) {
Expand All @@ -409,7 +421,14 @@ abstract class MDGExtension {
processResourcesDestPath = 'META-INF'
break
case Platform.NEOFORGE:
// Handle change of metadata location in 20.5
Provider<String> 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ final class ModsDotGroovyGradlePlugin implements Plugin<Project> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +41,9 @@ abstract class AbstractGatherPlatformDetailsTask extends DefaultTask {
@Input
abstract MapProperty<String, Object> getExtraProperties()

@Input
abstract MapProperty<String, Object> getBuildProperties()

@Optional @Input
Property<@Nullable String> getMinecraftVersion() {
return minecraftVersion
Expand All @@ -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<String> name) {
buildProperties.putAll(project.providers.gradleProperty(name).<Map<String, Object>>map { it -> [(name): it] })
}

AbstractGatherPlatformDetailsTask() {
outputFile.convention(projectLayout.buildDirectory.dir("generated/modsDotGroovy/${name.uncapitalize()}").map((Directory dir) -> dir.file('mdgPlatform.json')))
extraProperties.convention([:])
Expand Down Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ abstract class AbstractMDGConvertTask extends DefaultTask {
@Optional
abstract MapProperty<String, Object> getBuildProperties()

@Input
@Optional
abstract SetProperty<String> getEnvironmentBlacklist()

@Input
@Optional
abstract Property<Platform> getPlatform()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -151,7 +145,12 @@ abstract class AbstractMDGConvertTask extends DefaultTask {

final json = new JsonSlurper()
(json.parse(platformDetailsFile.get().asFile) as Map<String, String>).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)
Expand Down
4 changes: 4 additions & 0 deletions test/forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ plugins {
id 'org.groovymc.modsdotgroovy'
}

modsDotGroovy.gather {
projectProperty 'credits'
}

version = '1.0.0'
group = 'com.example.examplemod'
base.archivesName = 'examplemod'
Expand Down
3 changes: 1 addition & 2 deletions test/forge/src/main/resources/mods.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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]'

Expand Down
7 changes: 5 additions & 2 deletions test/multiplatform/fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
7 changes: 5 additions & 2 deletions test/multiplatform/forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
3 changes: 3 additions & 0 deletions test/multiplatform/neoforge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ modsDotGroovy {
multiplatform {
from ':multiplatform:common'
}
gather {
projectProperty 'credits'
}
apply()
}

Expand Down
7 changes: 5 additions & 2 deletions test/multiplatform/quilt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
3 changes: 3 additions & 0 deletions test/neoforge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ versionCatalogs.find('libs')
modsDotGroovy {
platform = Platform.NEOFORGE
inferGather.set false
gather {
projectProperty 'credits'
}
apply()
}

Expand Down
6 changes: 6 additions & 0 deletions test/neogradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ plugins {
id 'org.groovymc.modsdotgroovy'
}

modsDotGroovy {
gather {
projectProperty 'credits'
}
}

runs {
server {
modSource project.sourceSets.main
Expand Down

0 comments on commit 05a2226

Please sign in to comment.