Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure imperatively #177

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.neoforged.moddevgradle.dsl;

import groovyjarjarantlr4.v4.runtime.misc.Nullable;
Copy link
Contributor

@lukebemish lukebemish Oct 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this may be the wrong Nullable (also can I just say how obnoxious all those shaded groovy deps can be?)


public record ImmutableModdingVersions(@Nullable String neoForgeVersion, @Nullable String neoFormVersion) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.neoforged.moddevgradle.dsl;

import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.SourceSet;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

public abstract class ModdingVersionSettings {
@Nullable
private String neoForgeVersion;

@Nullable
private String neoFormVersion;

/**
* NeoForge version number. You have to set either this or {@link #setNeoFormVersion}.
*/
public void setNeoForgeVersion(String version) {
this.neoForgeVersion = version;
}

/**
* You can set this property to a version of <a href="https://projects.neoforged.net/neoforged/neoform">NeoForm</a>
* to either override the version used in the version of NeoForge you set, or to compile against
* Vanilla artifacts that have no NeoForge code added.
*/
public void setNeoFormVersion(String version) {
this.neoFormVersion = version;
}

/**
* Contains the list of source sets for which access to Minecraft classes should be configured.
* Defaults to the main source set, but can also be set to an empty list.
*/
public abstract ListProperty<SourceSet> getEnabledSourceSets();

@ApiStatus.Internal
public ImmutableModdingVersions toImmutable() {
return new ImmutableModdingVersions(neoForgeVersion, neoFormVersion);
}
}
50 changes: 24 additions & 26 deletions src/main/java/net/neoforged/moddevgradle/dsl/NeoForgeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.neoforged.moddevgradle.internal.ModDevPlugin;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserCodeException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand All @@ -15,6 +15,7 @@

import javax.inject.Inject;
import java.io.File;
import java.util.List;

/**
* This is the top-level {@code neoForge} extension, used to configure the moddev plugin.
Expand Down Expand Up @@ -44,34 +45,31 @@ public NeoForgeExtension(Project project, DataFileCollection accessTransformers,
unitTest.getLoadedMods().convention(getMods());
}

/**
* Adds the necessary dependencies to develop a Minecraft mod to the given source set.
* The plugin automatically adds these dependencies to the main source set.
*/
public void addModdingDependenciesTo(SourceSet sourceSet) {
var configurations = project.getConfigurations();
var sourceSets = ExtensionUtils.getSourceSets(project);
if (!sourceSets.contains(sourceSet)) {
throw new GradleException("Cannot add to the source set in another project.");
}

configurations.getByName(sourceSet.getRuntimeClasspathConfigurationName())
.extendsFrom(configurations.getByName(ModDevPlugin.CONFIGURATION_RUNTIME_DEPENDENCIES));
configurations.getByName(sourceSet.getCompileClasspathConfigurationName())
.extendsFrom(configurations.getByName(ModDevPlugin.CONFIGURATION_COMPILE_DEPENDENCIES));
@Deprecated(forRemoval = true)
public void setVersion(Object any) {
throw new InvalidUserCodeException("Please use enableModding { neoForgeVersion = ... } instead of the version property.");
}

/**
* NeoForge version number. You have to set either this or {@link #getNeoFormVersion()}.
*/
public abstract Property<String> getVersion();
public void enableModding(Action<ModdingVersionSettings> customizer) {
var modDevPlugin = project.getPlugins().getPlugin(ModDevPlugin.class);

/**
* You can set this property to a version of <a href="https://projects.neoforged.net/neoforged/neoform">NeoForm</a>
* to either override the version used in the version of NeoForge you set, or to compile against
* Vanilla artifacts that have no NeoForge code added.
*/
public abstract Property<String> getNeoFormVersion();
var settings = project.getObjects().newInstance(ModdingVersionSettings.class);
// By default enable modding deps only for the main source set
settings.getEnabledSourceSets().convention(project.provider(() -> {
var sourceSets = ExtensionUtils.getSourceSets(project);
return List.of(sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME));
}));
customizer.execute(settings);

var versions = settings.toImmutable();

modDevPlugin.enableModding(
project,
settings.getEnabledSourceSets().get(),
versions.neoForgeVersion(),
versions.neoFormVersion()
);
}

/**
* The list of additional access transformers that should be applied to the Minecraft source code.
Expand Down
Loading
Loading