Skip to content

Commit

Permalink
Introduce support for lazy configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Jul 17, 2024
1 parent f188fa2 commit 3a578aa
Show file tree
Hide file tree
Showing 20 changed files with 597 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public void apply(Project project) {
runs
);

runs.configureEach(run -> {
RunsUtil.configureModClasses(run);
RunsUtil.createTasks(project, run);
});

setupAccessTransformerConfigurations(project, accessTransformers);

IdeRunIntegrationManager.getInstance().setup(project);
Expand Down Expand Up @@ -351,7 +356,7 @@ private void setupAccessTransformerConfigurations(Project project, AccessTransfo
private void applyAfterEvaluate(final Project project) {
//We now eagerly get all runs and configure them.
final NamedDomainObjectContainer<Run> runs = (NamedDomainObjectContainer<Run>) project.getExtensions().getByName(RunsConstants.Extensions.RUNS);
runs.forEach(run -> {
runs.configureEach(run -> {
if (run instanceof RunImpl) {
run.configure();

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ public Set<Entry> getEntries() {
}

private void create(Entry entry) {
this.entries.add(entry);
if (!this.entries.add(entry)) {
return;
}

write(entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,11 @@ private TaskProvider<?> createIdeBeforeRunTask(Project project, String name, Run
final TaskProvider<?> ideBeforeRunTask = project.getTasks().register(CommonRuntimeUtils.buildTaskName("ideBeforeRun", name), task -> {
RunsUtil.addRunSourcesDependenciesToTask(task, run);
});

if (!runImpl.getTaskDependencies().isEmpty()) {
ideBeforeRunTask.configure(task -> {
runImpl.getTaskDependencies().forEach(dep -> {
//noinspection Convert2MethodRef Creates a compiler error regarding incompatible types.
task.dependsOn(dep);
});
});
}


ideBeforeRunTask.configure(task -> {
task.getDependsOn().add(runImpl.getDependsOn());
});

return ideBeforeRunTask;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.google.common.collect.Sets;
import net.minecraftforge.gdi.ConfigurableDSLElement;
import net.neoforged.gradle.common.util.constants.RunsConstants;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.common.runs.run.RunSourceSets;
import net.neoforged.gradle.dsl.common.runs.type.RunType;
import net.neoforged.gradle.util.StringCapitalizationUtils;
import net.neoforged.gradle.util.TransformerUtils;
import org.gradle.api.GradleException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
Expand All @@ -30,7 +30,6 @@ public abstract class RunImpl implements ConfigurableDSLElement<Run>, Run {
private final Project project;
private final String name;
private final ListProperty<RunType> runTypes;
private final Set<TaskProvider<? extends Task>> dependencies = Sets.newHashSet();
private final RunSourceSets modSources;
private final RunSourceSets unitTestSources;

Expand Down Expand Up @@ -160,32 +159,57 @@ public void overrideSystemProperties(MapProperty<String, String> systemPropertie
this.systemProperties = systemProperties;
}

@Internal
public Set<TaskProvider<? extends Task>> getTaskDependencies() {
return ImmutableSet.copyOf(this.dependencies);
}

@Override
public final void configure() {
if (getConfigureFromTypeWithName().get()) {
configureInternally(getRunTypeByName(name));
runTypes.add(getRunTypeByName(name));
}

for (RunType runType : runTypes.get()) {
configureInternally(runType);
}
getEnvironmentVariables().putAll(runTypes.flatMap(TransformerUtils.combineAllMaps(
getProject(),
String.class,
String.class,
RunType::getEnvironmentVariables
)));
getMainClass().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getMainClass)));
getProgramArguments().addAll(runTypes.flatMap(TransformerUtils.combineAllLists(
getProject(),
String.class,
RunType::getArguments
)));
getJvmArguments().addAll(runTypes.flatMap(TransformerUtils.combineAllLists(
getProject(),
String.class,
RunType::getJvmArguments
)));
getIsSingleInstance().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsSingleInstance)));
getSystemProperties().putAll(runTypes.flatMap(TransformerUtils.combineAllMaps(
getProject(),
String.class,
String.class,
RunType::getSystemProperties
)));
getIsClient().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsClient)));
getIsServer().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsServer)));
getIsDataGenerator().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsDataGenerator)));
getIsGameTest().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsGameTest)));
getIsJUnit().convention(runTypes.flatMap(TransformerUtils.takeLast(getProject(), RunType::getIsJUnit)));
getClasspath().from(runTypes.map(TransformerUtils.combineFileCollections(
getProject(),
RunType::getClasspath
)));
}

@Override
public final void configure(final @NotNull String name) {
getConfigureFromTypeWithName().set(false); // Don't re-configure
runTypes.add(project.provider(() -> getRunTypeByName(name)));
runTypes.add(getRunTypeByName(name));
}

@Override
public final void configure(final @NotNull RunType runType) {
getConfigureFromTypeWithName().set(false); // Don't re-configure
this.runTypes.add(runType);
this.runTypes.add(project.provider(() -> runType));
}

@Override
Expand All @@ -194,32 +218,6 @@ public void configure(@NotNull Provider<RunType> typeProvider) {
this.runTypes.add(typeProvider);
}

@SafeVarargs
@Override
public final void dependsOn(TaskProvider<? extends Task>... tasks) {
this.dependencies.addAll(Arrays.asList(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());
getJvmArguments().addAll(spec.getJvmArguments());
getIsSingleInstance().convention(spec.getIsSingleInstance());
getSystemProperties().putAll(spec.getSystemProperties());
getIsClient().convention(spec.getIsClient());
getIsServer().convention(spec.getIsServer());
getIsDataGenerator().convention(spec.getIsDataGenerator());
getIsGameTest().convention(spec.getIsGameTest());
getIsJUnit().convention(spec.getIsJUnit());
getClasspath().from(spec.getClasspath());

if (spec.getRunAdapter().isPresent()) {
spec.getRunAdapter().get().adapt(this);
}
}

@NotNull
public List<String> realiseJvmArguments() {
final List<String> args = new ArrayList<>(getJvmArguments().get());
Expand All @@ -237,15 +235,17 @@ public List<String> realiseJvmArguments() {
}

@SuppressWarnings("unchecked")
private RunType getRunTypeByName(String name) {
private Provider<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());
}
return project.provider(() -> {
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 @@ -8,23 +8,18 @@
import net.neoforged.gradle.common.util.VersionJson;
import net.neoforged.gradle.common.util.run.RunsUtil;
import net.neoforged.gradle.dsl.common.runtime.definition.Definition;
import net.neoforged.gradle.dsl.common.runtime.naming.NamingChannel;
import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime;
import net.neoforged.gradle.dsl.common.tasks.ArtifactProvider;
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.dsl.common.util.GameArtifact;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -181,7 +176,8 @@ public void configureRun(RunImpl run) {
run.overrideSystemProperties(interpolate(run.getSystemProperties(), workingInterpolationData));

if (run.getIsClient().get() || run.getIsDataGenerator().get()) {
run.dependsOn(getAssets(), getNatives());
run.getDependsOn().add(getAssets());
run.getDependsOn().add(getNatives());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import net.neoforged.gradle.common.util.ToolUtilities;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import org.apache.commons.io.FileUtils;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.plugins.JavaPluginExtension;
Expand Down Expand Up @@ -55,6 +56,18 @@ public SourceAccessTransformer() {
getLogLevel().set(LogLevel.DISABLED);
}

@Override
public File doExecute() throws Throwable {
//We need a separate check here that skips the execute call if there are no transformers.
if (getTransformers().isEmpty()) {
final File output = ensureFileWorkspaceReady(getOutput());
FileUtils.copyFile(getInputFile().get().getAsFile(), output);
return output;
}

return super.doExecute();
}

@InputFile
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getInputFile();
Expand All @@ -69,7 +82,6 @@ public SourceAccessTransformer() {
public abstract ConfigurableFileCollection getClasspath();

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.NONE)
public abstract ConfigurableFileCollection getTransformers();
}
Loading

0 comments on commit 3a578aa

Please sign in to comment.