Skip to content

Commit 55efa12

Browse files
committed
Support for environment variables in run configurations
1 parent d3994cc commit 55efa12

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public void idea(final Project project, final IdeaModel idea, final ProjectSetti
348348
ideaRun.moduleRef(project, run.getIdeaRunSourceSet().orElse(run.getSourceSet()).get());
349349
ideaRun.setJvmArgs(StringUtils.join(run.getAllJvmArgumentProviders(), true));
350350
ideaRun.setProgramParameters(StringUtils.join(run.getAllArgumentProviders(), true));
351+
ideaRun.setEnvs(run.getActualEnvironment());
351352
});
352353
});
353354
}
@@ -374,6 +375,7 @@ private void createRunTasks(final MinecraftExtension extension, final TaskContai
374375
exec.setWorkingDir(config.getWorkingDirectory());
375376
exec.getJvmArgumentProviders().addAll(config.getAllJvmArgumentProviders());
376377
exec.getArgumentProviders().addAll(config.getAllArgumentProviders());
378+
exec.environment(config.getEnvironment());
377379
if (config.getRequiresAssetsAndNatives().get()) {
378380
exec.dependsOn(Constants.Tasks.DOWNLOAD_ASSETS);
379381
exec.dependsOn(Constants.Tasks.COLLECT_NATIVES);

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/runs/EclipseRunConfigurationWriter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.nio.file.Path;
4040
import java.util.Arrays;
4141
import java.util.List;
42+
import java.util.Map;
4243

4344
import javax.xml.stream.XMLOutputFactory;
4445
import javax.xml.stream.XMLStreamException;
@@ -117,6 +118,8 @@ public void write(final RunConfiguration run) throws XMLStreamException {
117118
this.stringAttribute(this.launching("VM_ARGUMENTS"), StringUtils.join(run.getAllJvmArgumentProviders(), true));
118119
this.stringAttribute(this.launching("WORKING_DIRECTORY"), run.getWorkingDirectory().get().getAsFile().getAbsolutePath());
119120

121+
this.mapAttribute(this.debug("core.environmentVariables"), run.getActualEnvironment());
122+
120123
this.writer.writeEndElement();
121124
this.writer.writeEndDocument();
122125
}
@@ -129,6 +132,17 @@ private String debug(final String value) {
129132
return "org.eclipse.debug." + value;
130133
}
131134

135+
private void mapAttribute(final String key, final Map<String, String> map) throws XMLStreamException {
136+
this.writer.writeStartElement("mapAttribute");
137+
this.writer.writeAttribute("key", key);
138+
for (final Map.Entry<String, String> entry : map.entrySet()) {
139+
this.writer.writeEmptyElement("mapEntry");
140+
this.writer.writeAttribute("key", entry.getKey());
141+
this.writer.writeAttribute("value", entry.getValue());
142+
}
143+
this.writer.writeEndElement();
144+
}
145+
132146
private void listAttribute(final String key, final List<String> values) throws XMLStreamException {
133147
this.writer.writeStartElement("listAttribute");
134148
this.writer.writeAttribute("key", key);

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/runs/RunConfiguration.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
import java.util.ArrayList;
4949
import java.util.Collection;
5050
import java.util.Collections;
51+
import java.util.HashMap;
5152
import java.util.List;
53+
import java.util.Map;
5254
import java.util.Objects;
5355

5456
import javax.inject.Inject;
@@ -74,6 +76,7 @@ public class RunConfiguration implements Named {
7476
private final Property<SourceSet> ideaSourceSet;
7577
private final Property<SourceSet> sourceSet;
7678
private final Property<JavaLanguageVersion> targetVersion;
79+
private Map<String, Object> environment = new HashMap<>();
7780

7881
@Inject
7982
public RunConfiguration(final String name, final ProjectLayout layout, final ObjectFactory objects, final Project project) {
@@ -148,6 +151,52 @@ public Property<Boolean> getRequiresAssetsAndNatives() {
148151
return this.requiresAssetsAndNatives;
149152
}
150153

154+
public Map<String, String> getActualEnvironment() {
155+
final Map<String, String> actual = new HashMap<>();
156+
for (Map.Entry<String, Object> entry : this.getEnvironment().entrySet()) {
157+
actual.put(entry.getKey(), String.valueOf(entry.getValue()));
158+
}
159+
return actual;
160+
}
161+
162+
/**
163+
* The environment variables to use for the process.
164+
*
165+
* @return The environment.
166+
*/
167+
@Internal
168+
public Map<String, Object> getEnvironment() {
169+
return this.environment;
170+
}
171+
172+
/**
173+
* Sets the environment variable to use for the process.
174+
*
175+
* @param environment The environment variables.
176+
*/
177+
public void setEnvironment(Map<String, ?> environment) {
178+
this.environment = new HashMap<>(Objects.requireNonNull(environment, "environment"));
179+
}
180+
181+
/**
182+
* Adds an environment variable to the environment for this process.
183+
*
184+
* @param name The name of the variable.
185+
* @param value The value for the variable.
186+
*/
187+
public void environment(String name, Object value) {
188+
this.getEnvironment().put(Objects.requireNonNull(name, "name"), Objects.requireNonNull(value, "value"));
189+
}
190+
191+
/**
192+
* Adds some environment variables to the environment for this process.
193+
*
194+
* @param environment The environment variables.
195+
*/
196+
public void environment(Map<String, ?> environment) {
197+
this.getEnvironment().putAll(Objects.requireNonNull(environment, "environment"));
198+
}
199+
151200
/**
152201
* Get the classpath used to run this game.
153202
*

0 commit comments

Comments
 (0)