Skip to content

Commit

Permalink
Refactor how and when runs apply the config from their run type to en…
Browse files Browse the repository at this point in the history
…sure it is always applied before configuring the run. (#55)
  • Loading branch information
shartte authored Nov 23, 2023
1 parent c9af4fc commit 902ae2c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ private void applyAfterEvaluate(final Project project) {

project.getExtensions().configure(RunsConstants.Extensions.RUNS, (Action<NamedDomainObjectContainer<Run>>) 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +30,7 @@ public abstract class RunImpl implements ConfigurableDSLElement<Run>, Run {
private MapProperty<String, String> environmentVariables;
private ListProperty<String> programArguments;
private MapProperty<String, String> systemProperties;
private final ListProperty<RunType> runTypes;

private final Set<TaskProvider<? extends Task>> dependencies = Sets.newHashSet();

Expand All @@ -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);
Expand Down Expand Up @@ -117,32 +118,30 @@ public Set<TaskProvider<? extends Task>> 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<NamedDomainObjectContainer<RunType>>) 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<RunType> typeProvider) {
configure(typeProvider.get());
this.runTypes.add(typeProvider);
}

@SafeVarargs
Expand All @@ -152,6 +151,7 @@ public final void dependsOn(TaskProvider<? extends Task>... 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());
Expand Down Expand Up @@ -179,4 +179,17 @@ public List<String> realiseJvmArguments() {

return args;
}

@SuppressWarnings("unchecked")
private RunType getRunTypeByName(String name) {
NamedDomainObjectContainer<RunType> runTypes = (NamedDomainObjectContainer<RunType>) 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ interface Run extends BaseDSLElement<Run>, 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.
* <p/>
* Picks a run type using the name of this run, if no specific run type has been set.
*/
abstract void configure();

Expand Down

0 comments on commit 902ae2c

Please sign in to comment.