From 902ae2cbc96881bc9ffd93f740ce793c1a966a21 Mon Sep 17 00:00:00 2001 From: shartte Date: Thu, 23 Nov 2023 20:27:42 +0100 Subject: [PATCH] Refactor how and when runs apply the config from their run type to ensure it is always applied before configuring the run. (#55) --- .../gradle/common/CommonProjectPlugin.java | 6 +-- .../gradle/common/runs/run/RunImpl.java | 41 ++++++++++++------- .../gradle/dsl/common/runs/run/Run.groovy | 5 ++- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java index 943458c5b..c87ce4924 100644 --- a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java +++ b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java @@ -130,10 +130,8 @@ private void applyAfterEvaluate(final Project project) { project.getExtensions().configure(RunsConstants.Extensions.RUNS, (Action>) runs -> runs.forEach(run -> { if (run instanceof RunImpl) { - if (run.getConfigureFromTypeWithName().get()) { - run.configure(); - } - + run.configure(); + if (run.getConfigureFromDependencies().get()) { final RunImpl runImpl = (RunImpl) run; runImpl.getModSources().get().forEach(sourceSet -> { diff --git a/common/src/main/java/net/neoforged/gradle/common/runs/run/RunImpl.java b/common/src/main/java/net/neoforged/gradle/common/runs/run/RunImpl.java index ea1f34ce9..899ec898b 100644 --- a/common/src/main/java/net/neoforged/gradle/common/runs/run/RunImpl.java +++ b/common/src/main/java/net/neoforged/gradle/common/runs/run/RunImpl.java @@ -3,11 +3,10 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import net.minecraftforge.gdi.ConfigurableDSLElement; -import net.neoforged.gradle.common.util.ProjectUtils; import net.neoforged.gradle.common.util.constants.RunsConstants; import net.neoforged.gradle.dsl.common.runs.run.Run; import net.neoforged.gradle.dsl.common.runs.type.RunType; -import org.gradle.api.Action; +import org.gradle.api.GradleException; import org.gradle.api.NamedDomainObjectContainer; import org.gradle.api.Project; import org.gradle.api.Task; @@ -31,6 +30,7 @@ public abstract class RunImpl implements ConfigurableDSLElement, Run { private MapProperty environmentVariables; private ListProperty programArguments; private MapProperty systemProperties; + private final ListProperty runTypes; private final Set> dependencies = Sets.newHashSet(); @@ -43,6 +43,7 @@ public RunImpl(final Project project, final String name) { this.environmentVariables = this.project.getObjects().mapProperty(String.class, String.class); this.programArguments = this.project.getObjects().listProperty(String.class); this.systemProperties = this.project.getObjects().mapProperty(String.class, String.class); + this.runTypes = this.project.getObjects().listProperty(RunType.class); getIsSingleInstance().convention(true); getIsClient().convention(false); @@ -117,32 +118,30 @@ public Set> getTaskDependencies() { @Override public final void configure() { - configure(getName()); + if (getConfigureFromTypeWithName().get()) { + configureInternally(getRunTypeByName(name)); + } + + for (RunType runType : runTypes.get()) { + configureInternally(runType); + } } @Override public final void configure(final @NotNull String name) { getConfigureFromTypeWithName().set(false); // Don't re-configure - ProjectUtils.afterEvaluate(getProject(), () -> { - project.getExtensions().configure(RunsConstants.Extensions.RUN_TYPES, (Action>) types -> { - if (types.getNames().contains(name)) { - configureInternally(types.getByName(name)); - } - }); - }); + runTypes.add(project.provider(() -> getRunTypeByName(name))); } @Override public final void configure(final @NotNull RunType runType) { getConfigureFromTypeWithName().set(false); // Don't re-configure - ProjectUtils.afterEvaluate(getProject(), () -> { - configureInternally(runType); - }); + this.runTypes.add(runType); } @Override public void configure(@NotNull Provider typeProvider) { - configure(typeProvider.get()); + this.runTypes.add(typeProvider); } @SafeVarargs @@ -152,6 +151,7 @@ public final void dependsOn(TaskProvider... tasks) { } public void configureInternally(final @NotNull RunType spec) { + project.getLogger().debug("Configuring run {} with run type {}", name, spec.getName()); getEnvironmentVariables().putAll(spec.getEnvironmentVariables()); getMainClass().convention(spec.getMainClass()); getProgramArguments().addAll(spec.getArguments()); @@ -179,4 +179,17 @@ public List realiseJvmArguments() { return args; } + + @SuppressWarnings("unchecked") + private RunType getRunTypeByName(String name) { + NamedDomainObjectContainer runTypes = (NamedDomainObjectContainer) project.getExtensions() + .getByName(RunsConstants.Extensions.RUN_TYPES); + + if (runTypes.getNames().contains(name)) { + return runTypes.getByName(name); + } else { + throw new GradleException("Could not find run type " + name + ". Available run types: " + + runTypes.getNames()); + } + } } diff --git a/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/runs/run/Run.groovy b/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/runs/run/Run.groovy index 440ee1bf2..50781275d 100644 --- a/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/runs/run/Run.groovy +++ b/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/runs/run/Run.groovy @@ -207,8 +207,9 @@ interface Run extends BaseDSLElement, NamedDSLElement { /** - * Configures the run using the type with the same name. - * Throwing an exception if no type could be found. + * Configures the run using the settings of the associated run type. + *

+ * Picks a run type using the name of this run, if no specific run type has been set. */ abstract void configure();