Skip to content

Commit

Permalink
Add support for setting per-run environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jun 3, 2024
1 parent b8a2fab commit 3380f5d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ neoForge {
]
systemProperty("a.b.c", "xyz")
// Set or add environment variables
environment = [
"FOO_BAR": "123"
]
environment("FOO_BAR", "123")
// Optionally set the log-level used by the game
logLevel = org.slf4j.event.Level.DEBUG
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public String getName() {

public abstract DirectoryProperty getGameDirectory();

public abstract MapProperty<String, String> getEnvironment();

public void environment(String key, String value) {
getEnvironment().put(key, value);
}

public abstract MapProperty<String, String> getSystemProperties();

public void systemProperty(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public void apply(Project project) {
task.getClasspathProvider().from(sourceSet.getRuntimeClasspath());
task.getGameDirectory().set(run.getGameDirectory());

task.getEnvironmentProperty().set(run.getEnvironment());
task.jvmArgs(RunUtils.getArgFileParameter(prepareRunTask.get().getVmArgsFile().get()).replace("\\", "\\\\"));
task.getMainClass().set(RunUtils.DEV_LAUNCH_MAIN_CLASS);
task.args(RunUtils.getArgFileParameter(prepareRunTask.get().getProgramArgsFile().get()).replace("\\", "\\\\"));
Expand Down Expand Up @@ -645,6 +646,7 @@ private static void addIntelliJRunConfiguration(Project project,
var sourceSets = ExtensionUtils.getExtension(project, "sourceSets", SourceSetContainer.class);
appRun.setModuleRef(new ModuleRef(project, sourceSets.getByName("main")));
appRun.setWorkingDirectory(run.getGameDirectory().get().getAsFile().getAbsolutePath());
appRun.setEnvs(run.getEnvironment().get());

appRun.setJvmArgs(
RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(prepareTask.getVmArgsFile().get()))
Expand Down Expand Up @@ -817,6 +819,7 @@ private static void addEclipseLaunchConfiguration(Project project,
RunUtils.escapeJvmArg(RunUtils.getIdeaModFoldersProvider(project, outputDirectory, run.getMods(), false).getArgument())
)
.args(RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(prepareTask.getProgramArgsFile().get())))
.envVar(run.getEnvironment().get())
.workingDirectory(run.getGameDirectory().get().getAsFile().getAbsolutePath())
.jreContainer("JavaSE-21") // TODO
.build(RunUtils.DEV_LAUNCH_MAIN_CLASS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.JavaExec;
Expand All @@ -13,6 +15,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.HashMap;

/**
* By extending JavaExec, we allow IntelliJ to automatically attach a debugger to the forked JVM, making
Expand All @@ -24,6 +27,9 @@ public abstract class RunGameTask extends JavaExec {
@InputFiles
public abstract ConfigurableFileCollection getClasspathProvider();

@Input
public abstract MapProperty<String, String> getEnvironmentProperty();

@Internal
public abstract DirectoryProperty getGameDirectory();

Expand All @@ -41,6 +47,8 @@ public void exec() {
throw new UncheckedIOException("Failed to create run directory", e);
}

getEnvironment().putAll(getEnvironmentProperty().get());

classpath(getClasspathProvider());
setWorkingDir(runDir);
super.exec();
Expand Down
1 change: 1 addition & 0 deletions testproject/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ neoForge {
}
server {
server()
environment("BLAH_BLUBB", "123");
}
}

Expand Down

0 comments on commit 3380f5d

Please sign in to comment.