More run options (#165)

* Ability to change the source set added to classpath

* Support for environment variables in run configurations
This commit is contained in:
Yeregorix authored and GitHub committed 2024-11-07 19:34:03 +00:00
1 parent 049c2db5d9
commit c77ba70413
4 files changed
+87 -18

No files matched your search

@@ -106,13 +106,6 @@ public final class VanillaGradle implements Plugin<Object> {
c.extendsFrom(minecraftConfig.get());
});
});
project.afterEvaluate(p -> {
final NamedDomainObjectProvider<SourceSet> 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) {
@@ -51,8 +51,6 @@ import org.gradle.api.provider.Property;
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.IdeConfigurer;
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 class ProvideMinecraftPlugin implements Plugin<Project> {
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 @@ public class ProvideMinecraftPlugin implements Plugin<Project> {
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);
@@ -39,6 +39,7 @@ import java.nio.file.Files;
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 class EclipseRunConfigurationWriter implements AutoCloseable {
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 @@ public class EclipseRunConfigurationWriter implements AutoCloseable {
return "org.eclipse.debug." + value;
}
private void mapAttribute(final String key, final Map<String, String> map) throws XMLStreamException {
this.writer.writeStartElement("mapAttribute");
this.writer.writeAttribute("key", key);
for (final Map.Entry<String, String> 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<String> values) throws XMLStreamException {
this.writer.writeStartElement("listAttribute");
this.writer.writeAttribute("key", key);
@@ -27,6 +27,7 @@ package org.spongepowered.gradle.vanilla.runs;
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.Internal;
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.io.File;
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<String, String> parameterTokens;
private final Property<Boolean> requiresAssetsAndNatives;
private final Property<SourceSet> ideaSourceSet;
private final Property<SourceSet> sourceSet;
private final Property<JavaLanguageVersion> targetVersion;
private Map<String, Object> 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 class RunConfiguration implements Named {
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 class RunConfiguration implements Named {
return this.requiresAssetsAndNatives;
}
public Map<String, String> getActualEnvironment() {
final Map<String, String> actual = new HashMap<>();
for (Map.Entry<String, Object> 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<String, Object> getEnvironment() {
return this.environment;
}
/**
* Sets the environment variable to use for the process.
*
* @param environment The environment variables.
*/
public void setEnvironment(Map<String, ?> 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<String, ?> environment) {
this.getEnvironment().putAll(Objects.requireNonNull(environment, "environment"));
}
/**
* Get the classpath used to run this game.
*
@@ -156,13 +212,26 @@ public class RunConfiguration implements Named {
*
* @return the source set to use
* @since 0.2
* @deprecated Use {@link #getSourceSet()} instead.
*/
@Deprecated
@Input
@Optional
public Property<SourceSet> 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<SourceSet> getSourceSet() {
return this.sourceSet;
}
@Nested
public List<CommandLineArgumentProvider> getAllArgumentProviders() {
return this.allArgs;