From c77ba70413651f4bdde1c096591e2723dcbc7d48 Mon Sep 17 00:00:00 2001 From: Yeregorix Date: Thu, 7 Nov 2024 20:34:03 +0100 Subject: [PATCH] More run options (#165) * Ability to change the source set added to classpath * Support for environment variables in run configurations --- .../gradle/vanilla/VanillaGradle.java | 7 -- .../internal/ProvideMinecraftPlugin.java | 13 +--- .../runs/EclipseRunConfigurationWriter.java | 14 ++++ .../gradle/vanilla/runs/RunConfiguration.java | 71 ++++++++++++++++++- 4 files changed, 87 insertions(+), 18 deletions(-) diff --git a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/VanillaGradle.java b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/VanillaGradle.java index 24572812..121fef5f 100644 --- a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/VanillaGradle.java +++ b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/VanillaGradle.java @@ -106,13 +106,6 @@ private void applyToProject(final Project project) { c.extendsFrom(minecraftConfig.get()); }); }); - - project.afterEvaluate(p -> { - final NamedDomainObjectProvider mainSourceSet = - p.getExtensions().getByType(SourceSetContainer.class).named(SourceSet.MAIN_SOURCE_SET_NAME); - minecraft.getRuns().configureEach( - run -> run.getClasspath().from(mainSourceSet.map(SourceSet::getOutput), mainSourceSet.map(SourceSet::getRuntimeClasspath))); - }); }); for (final String shadowPluginId : SHADOW_PLUGIN_IDS) { diff --git a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java index 94f2c9ee..51dd73c0 100644 --- a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java +++ b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java @@ -51,8 +51,6 @@ import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Delete; import org.gradle.api.tasks.JavaExec; -import org.gradle.api.tasks.SourceSet; -import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.Sync; import org.gradle.api.tasks.TaskContainer; import org.gradle.api.tasks.TaskProvider; @@ -73,7 +71,6 @@ import org.spongepowered.gradle.vanilla.internal.util.StringUtils; import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform; import org.spongepowered.gradle.vanilla.repository.MinecraftRepositoryExtension; -import org.spongepowered.gradle.vanilla.repository.MinecraftSide; import org.spongepowered.gradle.vanilla.runs.ClientRunParameterTokens; import org.spongepowered.gradle.vanilla.task.DecompileJarTask; import org.spongepowered.gradle.vanilla.task.DownloadAssetsTask; @@ -348,15 +345,10 @@ public void idea(final Project project, final IdeaModel idea, final ProjectSetti ideaRun.setWorkingDirectory(runDirectory.getAbsolutePath()); runDirectory.mkdirs(); - final SourceSet moduleSet; - if (run.getIdeaRunSourceSet().isPresent()) { - moduleSet = run.getIdeaRunSourceSet().get(); - } else { - moduleSet = project.getExtensions().getByType(SourceSetContainer.class).getByName(SourceSet.MAIN_SOURCE_SET_NAME); - } - ideaRun.moduleRef(project, moduleSet); + ideaRun.moduleRef(project, run.getIdeaRunSourceSet().orElse(run.getSourceSet()).get()); ideaRun.setJvmArgs(StringUtils.join(run.getAllJvmArgumentProviders(), true)); ideaRun.setProgramParameters(StringUtils.join(run.getAllArgumentProviders(), true)); + ideaRun.setEnvs(run.getActualEnvironment()); }); }); } @@ -383,6 +375,7 @@ private void createRunTasks(final MinecraftExtension extension, final TaskContai exec.setWorkingDir(config.getWorkingDirectory()); exec.getJvmArgumentProviders().addAll(config.getAllJvmArgumentProviders()); exec.getArgumentProviders().addAll(config.getAllArgumentProviders()); + exec.environment(config.getEnvironment()); if (config.getRequiresAssetsAndNatives().get()) { exec.dependsOn(Constants.Tasks.DOWNLOAD_ASSETS); exec.dependsOn(Constants.Tasks.COLLECT_NATIVES); diff --git a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/runs/EclipseRunConfigurationWriter.java b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/runs/EclipseRunConfigurationWriter.java index d45917a9..5a96ad73 100644 --- a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/runs/EclipseRunConfigurationWriter.java +++ b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/runs/EclipseRunConfigurationWriter.java @@ -39,6 +39,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.List; +import java.util.Map; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; @@ -117,6 +118,8 @@ public void write(final RunConfiguration run) throws XMLStreamException { this.stringAttribute(this.launching("VM_ARGUMENTS"), StringUtils.join(run.getAllJvmArgumentProviders(), true)); this.stringAttribute(this.launching("WORKING_DIRECTORY"), run.getWorkingDirectory().get().getAsFile().getAbsolutePath()); + this.mapAttribute(this.debug("core.environmentVariables"), run.getActualEnvironment()); + this.writer.writeEndElement(); this.writer.writeEndDocument(); } @@ -129,6 +132,17 @@ private String debug(final String value) { return "org.eclipse.debug." + value; } + private void mapAttribute(final String key, final Map map) throws XMLStreamException { + this.writer.writeStartElement("mapAttribute"); + this.writer.writeAttribute("key", key); + for (final Map.Entry entry : map.entrySet()) { + this.writer.writeEmptyElement("mapEntry"); + this.writer.writeAttribute("key", entry.getKey()); + this.writer.writeAttribute("value", entry.getValue()); + } + this.writer.writeEndElement(); + } + private void listAttribute(final String key, final List values) throws XMLStreamException { this.writer.writeStartElement("listAttribute"); this.writer.writeAttribute("key", key); diff --git a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/runs/RunConfiguration.java b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/runs/RunConfiguration.java index 315c1307..3bb86e87 100644 --- a/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/runs/RunConfiguration.java +++ b/subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/runs/RunConfiguration.java @@ -27,6 +27,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.gradle.api.Action; import org.gradle.api.Named; +import org.gradle.api.Project; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.ProjectLayout; @@ -38,6 +39,7 @@ import org.gradle.api.tasks.Nested; import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.SourceSet; +import org.gradle.api.tasks.SourceSetContainer; import org.gradle.jvm.toolchain.JavaLanguageVersion; import org.gradle.process.CommandLineArgumentProvider; import org.spongepowered.gradle.vanilla.internal.Constants; @@ -46,7 +48,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import javax.inject.Inject; @@ -70,10 +74,12 @@ public class RunConfiguration implements Named { private final MapProperty parameterTokens; private final Property requiresAssetsAndNatives; private final Property ideaSourceSet; + private final Property sourceSet; private final Property targetVersion; + private Map environment = new HashMap<>(); @Inject - public RunConfiguration(final String name, final ProjectLayout layout, final ObjectFactory objects) { + public RunConfiguration(final String name, final ProjectLayout layout, final ObjectFactory objects, final Project project) { this.name = name; this.displayName = objects.property(String.class); @@ -89,8 +95,12 @@ public RunConfiguration(final String name, final ProjectLayout layout, final Obj this.parameterTokens = objects.mapProperty(String.class, String.class); this.requiresAssetsAndNatives = objects.property(Boolean.class).convention(false); this.ideaSourceSet = objects.property(SourceSet.class); + this.sourceSet = objects.property(SourceSet.class) + .convention(project.getExtensions().getByType(SourceSetContainer.class).named(SourceSet.MAIN_SOURCE_SET_NAME)); this.targetVersion = objects.property(JavaLanguageVersion.class); + this.classpath.from(this.sourceSet.map(SourceSet::getOutput), this.sourceSet.map(SourceSet::getRuntimeClasspath)); + // Apply global environment here this.parameterTokens.put(ClientRunParameterTokens.LAUNCHER_NAME, Constants.NAME); this.parameterTokens.put(ClientRunParameterTokens.LAUNCHER_VERSION, Constants.VERSION); @@ -141,6 +151,52 @@ public Property getRequiresAssetsAndNatives() { return this.requiresAssetsAndNatives; } + public Map getActualEnvironment() { + final Map actual = new HashMap<>(); + for (Map.Entry entry : this.getEnvironment().entrySet()) { + actual.put(entry.getKey(), String.valueOf(entry.getValue())); + } + return actual; + } + + /** + * The environment variables to use for the process. + * + * @return The environment. + */ + @Internal + public Map getEnvironment() { + return this.environment; + } + + /** + * Sets the environment variable to use for the process. + * + * @param environment The environment variables. + */ + public void setEnvironment(Map environment) { + this.environment = new HashMap<>(Objects.requireNonNull(environment, "environment")); + } + + /** + * Adds an environment variable to the environment for this process. + * + * @param name The name of the variable. + * @param value The value for the variable. + */ + public void environment(String name, Object value) { + this.getEnvironment().put(Objects.requireNonNull(name, "name"), Objects.requireNonNull(value, "value")); + } + + /** + * Adds some environment variables to the environment for this process. + * + * @param environment The environment variables. + */ + public void environment(Map environment) { + this.getEnvironment().putAll(Objects.requireNonNull(environment, "environment")); + } + /** * Get the classpath used to run this game. * @@ -156,13 +212,26 @@ public ConfigurableFileCollection getClasspath() { * * @return the source set to use * @since 0.2 + * @deprecated Use {@link #getSourceSet()} instead. */ + @Deprecated @Input @Optional public Property getIdeaRunSourceSet() { return this.ideaSourceSet; } + /** + * Get the source set to use in the classpath and IDE runs. + * + * @return the source set to use + * @since 0.2.1 + */ + @Input + public Property getSourceSet() { + return this.sourceSet; + } + @Nested public List getAllArgumentProviders() { return this.allArgs;