Skip to content

Commit c77ba70

Browse files
authored
More run options (#165)
* Ability to change the source set added to classpath * Support for environment variables in run configurations
1 parent 049c2db commit c77ba70

File tree

4 files changed

+87
-18
lines changed

4 files changed

+87
-18
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ private void applyToProject(final Project project) {
106106
c.extendsFrom(minecraftConfig.get());
107107
});
108108
});
109-
110-
project.afterEvaluate(p -> {
111-
final NamedDomainObjectProvider<SourceSet> mainSourceSet =
112-
p.getExtensions().getByType(SourceSetContainer.class).named(SourceSet.MAIN_SOURCE_SET_NAME);
113-
minecraft.getRuns().configureEach(
114-
run -> run.getClasspath().from(mainSourceSet.map(SourceSet::getOutput), mainSourceSet.map(SourceSet::getRuntimeClasspath)));
115-
});
116109
});
117110

118111
for (final String shadowPluginId : SHADOW_PLUGIN_IDS) {

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import org.gradle.api.provider.Provider;
5252
import org.gradle.api.tasks.Delete;
5353
import org.gradle.api.tasks.JavaExec;
54-
import org.gradle.api.tasks.SourceSet;
55-
import org.gradle.api.tasks.SourceSetContainer;
5654
import org.gradle.api.tasks.Sync;
5755
import org.gradle.api.tasks.TaskContainer;
5856
import org.gradle.api.tasks.TaskProvider;
@@ -73,7 +71,6 @@
7371
import org.spongepowered.gradle.vanilla.internal.util.StringUtils;
7472
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform;
7573
import org.spongepowered.gradle.vanilla.repository.MinecraftRepositoryExtension;
76-
import org.spongepowered.gradle.vanilla.repository.MinecraftSide;
7774
import org.spongepowered.gradle.vanilla.runs.ClientRunParameterTokens;
7875
import org.spongepowered.gradle.vanilla.task.DecompileJarTask;
7976
import org.spongepowered.gradle.vanilla.task.DownloadAssetsTask;
@@ -348,15 +345,10 @@ public void idea(final Project project, final IdeaModel idea, final ProjectSetti
348345
ideaRun.setWorkingDirectory(runDirectory.getAbsolutePath());
349346
runDirectory.mkdirs();
350347

351-
final SourceSet moduleSet;
352-
if (run.getIdeaRunSourceSet().isPresent()) {
353-
moduleSet = run.getIdeaRunSourceSet().get();
354-
} else {
355-
moduleSet = project.getExtensions().getByType(SourceSetContainer.class).getByName(SourceSet.MAIN_SOURCE_SET_NAME);
356-
}
357-
ideaRun.moduleRef(project, moduleSet);
348+
ideaRun.moduleRef(project, run.getIdeaRunSourceSet().orElse(run.getSourceSet()).get());
358349
ideaRun.setJvmArgs(StringUtils.join(run.getAllJvmArgumentProviders(), true));
359350
ideaRun.setProgramParameters(StringUtils.join(run.getAllArgumentProviders(), true));
351+
ideaRun.setEnvs(run.getActualEnvironment());
360352
});
361353
});
362354
}
@@ -383,6 +375,7 @@ private void createRunTasks(final MinecraftExtension extension, final TaskContai
383375
exec.setWorkingDir(config.getWorkingDirectory());
384376
exec.getJvmArgumentProviders().addAll(config.getAllJvmArgumentProviders());
385377
exec.getArgumentProviders().addAll(config.getAllArgumentProviders());
378+
exec.environment(config.getEnvironment());
386379
if (config.getRequiresAssetsAndNatives().get()) {
387380
exec.dependsOn(Constants.Tasks.DOWNLOAD_ASSETS);
388381
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: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.checkerframework.checker.nullness.qual.NonNull;
2828
import org.gradle.api.Action;
2929
import org.gradle.api.Named;
30+
import org.gradle.api.Project;
3031
import org.gradle.api.file.ConfigurableFileCollection;
3132
import org.gradle.api.file.DirectoryProperty;
3233
import org.gradle.api.file.ProjectLayout;
@@ -38,6 +39,7 @@
3839
import org.gradle.api.tasks.Nested;
3940
import org.gradle.api.tasks.Optional;
4041
import org.gradle.api.tasks.SourceSet;
42+
import org.gradle.api.tasks.SourceSetContainer;
4143
import org.gradle.jvm.toolchain.JavaLanguageVersion;
4244
import org.gradle.process.CommandLineArgumentProvider;
4345
import org.spongepowered.gradle.vanilla.internal.Constants;
@@ -46,7 +48,9 @@
4648
import java.util.ArrayList;
4749
import java.util.Collection;
4850
import java.util.Collections;
51+
import java.util.HashMap;
4952
import java.util.List;
53+
import java.util.Map;
5054
import java.util.Objects;
5155

5256
import javax.inject.Inject;
@@ -70,10 +74,12 @@ public class RunConfiguration implements Named {
7074
private final MapProperty<String, String> parameterTokens;
7175
private final Property<Boolean> requiresAssetsAndNatives;
7276
private final Property<SourceSet> ideaSourceSet;
77+
private final Property<SourceSet> sourceSet;
7378
private final Property<JavaLanguageVersion> targetVersion;
79+
private Map<String, Object> environment = new HashMap<>();
7480

7581
@Inject
76-
public RunConfiguration(final String name, final ProjectLayout layout, final ObjectFactory objects) {
82+
public RunConfiguration(final String name, final ProjectLayout layout, final ObjectFactory objects, final Project project) {
7783
this.name = name;
7884
this.displayName = objects.property(String.class);
7985

@@ -89,8 +95,12 @@ public RunConfiguration(final String name, final ProjectLayout layout, final Obj
8995
this.parameterTokens = objects.mapProperty(String.class, String.class);
9096
this.requiresAssetsAndNatives = objects.property(Boolean.class).convention(false);
9197
this.ideaSourceSet = objects.property(SourceSet.class);
98+
this.sourceSet = objects.property(SourceSet.class)
99+
.convention(project.getExtensions().getByType(SourceSetContainer.class).named(SourceSet.MAIN_SOURCE_SET_NAME));
92100
this.targetVersion = objects.property(JavaLanguageVersion.class);
93101

102+
this.classpath.from(this.sourceSet.map(SourceSet::getOutput), this.sourceSet.map(SourceSet::getRuntimeClasspath));
103+
94104
// Apply global environment here
95105
this.parameterTokens.put(ClientRunParameterTokens.LAUNCHER_NAME, Constants.NAME);
96106
this.parameterTokens.put(ClientRunParameterTokens.LAUNCHER_VERSION, Constants.VERSION);
@@ -141,6 +151,52 @@ public Property<Boolean> getRequiresAssetsAndNatives() {
141151
return this.requiresAssetsAndNatives;
142152
}
143153

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+
144200
/**
145201
* Get the classpath used to run this game.
146202
*
@@ -156,13 +212,26 @@ public ConfigurableFileCollection getClasspath() {
156212
*
157213
* @return the source set to use
158214
* @since 0.2
215+
* @deprecated Use {@link #getSourceSet()} instead.
159216
*/
217+
@Deprecated
160218
@Input
161219
@Optional
162220
public Property<SourceSet> getIdeaRunSourceSet() {
163221
return this.ideaSourceSet;
164222
}
165223

224+
/**
225+
* Get the source set to use in the classpath and IDE runs.
226+
*
227+
* @return the source set to use
228+
* @since 0.2.1
229+
*/
230+
@Input
231+
public Property<SourceSet> getSourceSet() {
232+
return this.sourceSet;
233+
}
234+
166235
@Nested
167236
public List<CommandLineArgumentProvider> getAllArgumentProviders() {
168237
return this.allArgs;

0 commit comments

Comments
 (0)