Skip to content

Commit a709ae4

Browse files
committed
Fix run config inheritence Closes #1050
1 parent eaa5ac8 commit a709ae4

File tree

1 file changed

+119
-58
lines changed

1 file changed

+119
-58
lines changed

src/main/java/net/minecraftforge/gradle/internal/SlimeLauncherOptionsImpl.java

Lines changed: 119 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.gradle.api.file.ConfigurableFileCollection;
1111
import org.gradle.api.file.DirectoryProperty;
1212
import org.gradle.api.file.ProjectLayout;
13+
import org.gradle.api.logging.LogLevel;
1314
import org.gradle.api.model.ObjectFactory;
1415
import org.gradle.api.provider.ListProperty;
1516
import org.gradle.api.provider.MapProperty;
@@ -23,6 +24,8 @@
2324
import java.util.Map;
2425

2526
public abstract class SlimeLauncherOptionsImpl implements SlimeLauncherOptionsInternal {
27+
private static final LogLevel level = LogLevel.INFO;
28+
2629
private final String name;
2730

2831
private final Property<String> mainClass = this.getObjects().property(String.class);
@@ -269,85 +272,143 @@ public void environment(Provider<? extends Map<String, ?>> properties) {
269272

270273

271274
/* INHERITANCE */
272-
273275
@Override
274276
public SlimeLauncherOptionsInternal inherit(Map<String, RunConfig> configs, String sourceSetName, String name) {
277+
LOGGER.log(level, "Baking Launch Options: {} for sourceset {}", name, sourceSetName);
275278
var target = getObjects().newInstance(SlimeLauncherOptionsImpl.class, name);
276-
target.getMainClass().convention(this.getMainClass());
277-
target.getInheritArgs().convention(this.getInheritArgs());
278-
target.getArgs().convention(this.getArgs()).addAll(this.getArgs());
279-
target.getInheritJvmArgs().convention(this.getInheritJvmArgs());
280-
target.getJvmArgs().convention(this.getJvmArgs()).addAll(this.getJvmArgs());
281-
target.getClasspath().convention(this.getClasspath()).setFrom(this.getClasspath());
282-
target.getMinHeapSize().convention(this.getMinHeapSize());
283-
target.getMaxHeapSize().convention(this.getMaxHeapSize());
284-
target.getSystemProperties().convention(this.getSystemProperties()).putAll(this.getSystemProperties());
285-
target.getEnvironment().convention(this.getEnvironment()).putAll(this.getEnvironment());
286-
target.getWorkingDir().convention(this.getWorkingDir().orElse(getProjectLayout().getProjectDirectory().dir("runs/" + sourceSetName + '/' + this.name)));
287-
target.getClient().convention(this.getClient());
288-
return this.inherit(target, sourceSetName, configs, name);
289-
}
290-
291-
private SlimeLauncherOptionsInternal inherit(SlimeLauncherOptionsInternal target, String sourceSetName, Map<String, RunConfig> configs, String name) {
279+
target.getWorkingDir().set(getProjectLayout().getProjectDirectory().dir("runs/" + sourceSetName + '/' + this.name));
280+
target.getClasspath().setFrom(this.getClasspath());
281+
282+
// Pull from userdev config
283+
LOGGER.log(level, "Inheriting Json Parent {}", name);
284+
target.inheritFromJson(" ", configs, name);
285+
286+
// Pull from this container
287+
LOGGER.log(level, "Inheriting From Self");
288+
target.inherit(this);
289+
290+
// Gradle is stupid and boolean properties are always present, so only allow upgrading from false -> true
291+
if (this.getClient().isPresent() && this.getClient().getOrElse(Boolean.FALSE)) {
292+
LOGGER.log(level, " Client: {}", this.getClient().get());
293+
target.getClient().set(this.getClient().get());
294+
}
295+
296+
// Inherit from sourceset specific container
297+
var child = this.getNested().getting(sourceSetName).getOrNull();
298+
if (child != null) {
299+
LOGGER.log(level, "Inheriting from Child");
300+
target.inherit(child);
301+
}
302+
return target;
303+
}
304+
305+
// Set the values from
306+
private void inheritFromJson(String prefix, Map<String, RunConfig> configs, String name) {
292307
var config = configs.get(name);
293-
if (config != null) {
294-
if (config.parents != null && !config.parents.isEmpty())
295-
config.parents.forEach(parent -> this.inherit(target, sourceSetName, configs, parent));
296-
297-
if (config.main != null)
298-
target.getMainClass().convention(config.main);
299-
300-
if (config.args != null && !config.args.isEmpty()) {
301-
if (target.getInheritArgs().getOrElse(Boolean.TRUE)) {
302-
var args = new ArrayList<>(config.args);
303-
args.addAll(target.getArgs().get());
304-
target.getArgs().convention(args);
305-
}
306-
}
308+
if (config == null)
309+
return;
307310

308-
if (config.jvmArgs != null && !config.jvmArgs.isEmpty()) {
309-
if (target.getInheritJvmArgs().getOrElse(Boolean.TRUE)) {
310-
var args = new ArrayList<>(config.jvmArgs);
311-
args.addAll(target.getJvmArgs().get());
312-
target.getJvmArgs().convention(args);
313-
}
311+
if (config.parents != null) {
312+
for (var parent : config.parents) {
313+
LOGGER.log(level, "{}Inheriting Json Parent {}", prefix, name);
314+
this.inheritFromJson(prefix + " ", configs, parent);
314315
}
316+
}
317+
318+
if (config.main != null) {
319+
LOGGER.log(level, "{}Main-Class: {}", prefix, config.main);
320+
getMainClass().set(config.main);
321+
}
322+
323+
if (config.args != null && !config.args.isEmpty()) {
324+
LOGGER.log(level, "{}Args: {}", prefix, config.args);
325+
var args = new ArrayList<>(config.args);
326+
args.addAll(this.getArgs().getOrElse(List.of()));
327+
getArgs().set(args);
328+
}
315329

316-
target.getClient().convention(config.client);
330+
if (config.jvmArgs != null && !config.jvmArgs.isEmpty()) {
331+
LOGGER.log(level, "{}JVM Args: {}", prefix, config.jvmArgs);
332+
var args = new ArrayList<>(config.jvmArgs);
333+
args.addAll(this.getJvmArgs().getOrElse(List.of()));
334+
getJvmArgs().set(args);
335+
}
317336

318-
if (config.buildAllProjects)
319-
LOGGER.warn("WARNING: ForgeGradle 7 does not support the buildAllProjects feature.");
337+
LOGGER.log(level, "{}Client: {}", prefix, config.client);
338+
getClient().convention(config.client); // Neds to be convention because boolean properties can only be set once
320339

321-
if (config.env != null && !config.env.isEmpty())
322-
target.environment(config.env);
340+
if (config.buildAllProjects)
341+
LOGGER.warn("WARNING: ForgeGradle 7 does not support the buildAllProjects feature.");
323342

324-
if (config.props != null && !config.props.isEmpty())
325-
target.systemProperties(config.props);
343+
if (config.env != null && !config.env.isEmpty()) {
344+
for (var entry : config.env.entrySet()) {
345+
LOGGER.log(level, "{}Env: {} = {}", prefix, entry.getKey(), entry.getValue());
346+
this.environment(entry.getKey(), entry.getValue());
347+
}
326348
}
327349

328-
var child = this.getNested().getting(sourceSetName).getOrNull();
329-
if (child != null) {
330-
if (child.getMainClass().filter(Util::isPresent).isPresent())
331-
target.getMainClass().set(child.getMainClass());
350+
if (config.props != null && !config.props.isEmpty()) {
351+
for (var entry : config.props.entrySet()) {
352+
LOGGER.log(level, "{}System: {} = {}", prefix, entry.getKey(), entry.getValue());
353+
this.systemProperty(entry.getKey(), entry.getValue());
354+
}
355+
}
356+
}
332357

333-
target.args(child.getArgs().getOrElse(List.of()));
358+
private void inherit(SlimeLauncherOptionsNested target) {
359+
if (target.getMainClass().isPresent()) {
360+
LOGGER.log(level, " MainClass: {}", target.getMainClass().get());
361+
getMainClass().set(target.getMainClass());
362+
}
334363

335-
target.jvmArgs(child.getJvmArgs().getOrElse(List.of()));
364+
// ListProperties are ALWAYS present, there is no way to tell if this is intentionally set to empty
365+
if (target.getArgs().isPresent() && !target.getArgs().get().isEmpty()) {
366+
var args = new ArrayList<>(this.getArgs().getOrElse(List.of()));
367+
if (!target.getInheritArgs().getOrElse(Boolean.TRUE))
368+
args.clear();
369+
args.addAll(target.getArgs().get());
370+
getArgs().set(args);
371+
LOGGER.log(level, " Args: {}", args);
372+
}
336373

337-
if (child.getMaxHeapSize().filter(Util::isPresent).isPresent())
338-
target.getMaxHeapSize().set(child.getMaxHeapSize());
374+
// ListProperties are ALWAYS present, there is no way to tell if this is intentionally set to empty
375+
if (target.getJvmArgs().isPresent() && !target.getJvmArgs().get().isEmpty()) {
376+
var args = new ArrayList<>(this.getJvmArgs().getOrElse(List.of()));
377+
if (!target.getInheritJvmArgs().getOrElse(Boolean.TRUE))
378+
args.clear();
379+
args.addAll(target.getJvmArgs().get());
380+
getJvmArgs().set(args);
381+
LOGGER.log(level, " JVM Args: {}", args);
382+
}
339383

340-
if (child.getMinHeapSize().filter(Util::isPresent).isPresent())
341-
target.getMinHeapSize().set(child.getMinHeapSize());
384+
if (target.getMinHeapSize().isPresent()) {
385+
LOGGER.log(level, " Min Heap Space: {}", target.getMinHeapSize().get());
386+
getMinHeapSize().set(target.getMinHeapSize());
387+
}
342388

343-
target.environment(child.getEnvironment().getOrElse(Map.of()));
389+
if (target.getMaxHeapSize().isPresent()) {
390+
LOGGER.log(level, " Max Heap Space: {}", target.getMaxHeapSize().get());
391+
getMaxHeapSize().set(target.getMaxHeapSize());
392+
}
344393

345-
target.systemProperties(child.getSystemProperties().getOrElse(Map.of()));
394+
if (target.getEnvironment().isPresent()) {
395+
for (var entry : target.getEnvironment().get().entrySet()) {
396+
LOGGER.log(level, " Env: {} = {}", entry.getKey(), entry.getValue());
397+
this.environment(entry.getKey(), entry.getValue());
398+
}
399+
}
346400

347-
target.getWorkingDir().set(child.getWorkingDir());
401+
if (target.getSystemProperties().isPresent()) {
402+
for (var entry : target.getSystemProperties().get().entrySet()) {
403+
LOGGER.log(level, " System: {} = {}", entry.getKey(), entry.getValue());
404+
this.systemProperty(entry.getKey(), entry.getValue());
405+
}
348406
}
349407

350-
return target;
408+
if (target.getWorkingDir().isPresent()) {
409+
LOGGER.log(level, " WorkingDir: {}", target.getWorkingDir().get());
410+
this.getWorkingDir().set(target.getWorkingDir());
411+
}
351412
}
352413

353414
/* DEBUGGING */

0 commit comments

Comments
 (0)