mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-02 21:47:42 -05:00
Support configuring run environment variables & Fixes #653
This commit is contained in:
@@ -30,11 +30,13 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonElement;
|
||||
@@ -68,6 +70,8 @@ public class RunConfig {
|
||||
public List<String> vmArgs = new ArrayList<>();
|
||||
public List<String> programArgs = new ArrayList<>();
|
||||
public SourceSet sourceSet;
|
||||
public Map<String, Object> environmentVariables;
|
||||
public String projectName;
|
||||
|
||||
public Element genRuns(Element doc) {
|
||||
Element root = this.addXml(doc, "component", ImmutableMap.of("name", "ProjectRunConfigurationManager"));
|
||||
@@ -174,6 +178,9 @@ public class RunConfig {
|
||||
runConfig.programArgs.addAll(settings.getProgramArgs());
|
||||
runConfig.vmArgs.addAll(settings.getVmArgs());
|
||||
runConfig.vmArgs.add("-Dfabric.dli.main=" + getMainClass(environment, extension, defaultMain));
|
||||
runConfig.environmentVariables = new HashMap<>();
|
||||
runConfig.environmentVariables.putAll(settings.getEnvironmentVariables());
|
||||
runConfig.projectName = project.getName();
|
||||
|
||||
return runConfig;
|
||||
}
|
||||
@@ -202,10 +209,19 @@ public class RunConfig {
|
||||
dummyConfig = dummyConfig.replace("%RUN_DIRECTORY%", runDir);
|
||||
dummyConfig = dummyConfig.replace("%PROGRAM_ARGS%", joinArguments(programArgs).replaceAll("\"", """));
|
||||
dummyConfig = dummyConfig.replace("%VM_ARGS%", joinArguments(vmArgs).replaceAll("\"", """));
|
||||
dummyConfig = dummyConfig.replace("%IDEA_ENV_VARS%", getEnvVars("<env name=\"%s\" value=\"%s\"/>"));
|
||||
dummyConfig = dummyConfig.replace("%ECLIPSE_ENV_VARS%", getEnvVars("<mapEntry key=\"%s\" value=\"%s\"/>"));
|
||||
|
||||
return dummyConfig;
|
||||
}
|
||||
|
||||
private String getEnvVars(String pattern) {
|
||||
return environmentVariables.entrySet().stream()
|
||||
.map(entry ->
|
||||
pattern.formatted(entry.getKey(), entry.getValue().toString())
|
||||
).collect(Collectors.joining());
|
||||
}
|
||||
|
||||
public static String joinArguments(List<String> args) {
|
||||
final var sb = new StringBuilder();
|
||||
boolean first = true;
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
@@ -95,6 +96,8 @@ public final class RunConfigSettings implements Named {
|
||||
*/
|
||||
private boolean ideConfigGenerated;
|
||||
|
||||
private final Map<String, Object> environmentVariables = new HashMap<>();
|
||||
|
||||
private final Project project;
|
||||
private final LoomGradleExtension extension;
|
||||
|
||||
@@ -241,6 +244,14 @@ public final class RunConfigSettings implements Named {
|
||||
this.ideConfigGenerated = ideConfigGenerated;
|
||||
}
|
||||
|
||||
public Map<String, Object> getEnvironmentVariables() {
|
||||
return environmentVariables;
|
||||
}
|
||||
|
||||
public void environmentVariable(String name, Object value) {
|
||||
environmentVariables.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the {@code -XstartOnFirstThread} JVM argument when on OSX.
|
||||
*/
|
||||
|
||||
@@ -53,6 +53,7 @@ public abstract class AbstractRunTask extends JavaExec {
|
||||
@Override
|
||||
public void exec() {
|
||||
setWorkingDir(new File(getProject().getProjectDir(), config.runDir));
|
||||
environment(config.environmentVariables);
|
||||
|
||||
super.exec();
|
||||
}
|
||||
|
||||
@@ -29,7 +29,9 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.gradle.api.Project;
|
||||
@@ -99,6 +101,8 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
public String mainClass;
|
||||
public String vmArgs;
|
||||
public String args;
|
||||
public Map<String, Object> env;
|
||||
public String projectName;
|
||||
|
||||
VsCodeConfiguration(RunConfig runConfig) {
|
||||
this.name = runConfig.configName;
|
||||
@@ -106,6 +110,8 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
this.vmArgs = RunConfig.joinArguments(runConfig.vmArgs);
|
||||
this.args = RunConfig.joinArguments(runConfig.programArgs);
|
||||
this.cwd = "${workspaceFolder}/" + runConfig.runDir;
|
||||
this.env = new HashMap<>(runConfig.environmentVariables);
|
||||
this.projectName = runConfig.projectName;
|
||||
|
||||
if (getProject().getRootProject() != getProject()) {
|
||||
Path rootPath = getProject().getRootDir().toPath();
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
||||
<listEntry value="4"/>
|
||||
</listAttribute>
|
||||
<mapAttribute key="org.eclipse.debug.core.environmentVariables">
|
||||
%ECLIPSE_ENV_VARS%
|
||||
</mapAttribute>
|
||||
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_START_ON_FIRST_THREAD" value="true"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.buildship.core.classpathprovider"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="%MAIN_CLASS%"/>
|
||||
|
||||
@@ -8,5 +8,8 @@
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
<envs>
|
||||
%IDEA_ENV_VARS%
|
||||
</envs>
|
||||
</configuration>
|
||||
</component>
|
||||
|
||||
Reference in New Issue
Block a user