Skip to content

Commit

Permalink
More run options (#165)
Browse files Browse the repository at this point in the history
* Ability to change the source set added to classpath

* Support for environment variables in run configurations
  • Loading branch information
Yeregorix authored Nov 7, 2024
1 parent 049c2db commit c77ba70
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ private void applyToProject(final Project project) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
});
});
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -129,6 +132,17 @@ private String debug(final String value) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -141,6 +151,52 @@ public Property<Boolean> getRequiresAssetsAndNatives() {
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.
*
Expand All @@ -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<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;
Expand Down

0 comments on commit c77ba70

Please sign in to comment.