Skip to content

Commit

Permalink
[Feature] Introduce the possibility to manual group sourcesets togeth…
Browse files Browse the repository at this point in the history
…er (#200)
  • Loading branch information
marchermans authored Jun 7, 2024
1 parent 602139f commit 281a603
Show file tree
Hide file tree
Showing 16 changed files with 1,220 additions and 59 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,15 @@ To include the sibling project in your run, you need to add it as a modSource to
```groovy
runs {
someRun {
modSource sourceSets.main
modSource project(':siblingProject').sourceSets.main
modSources {
add project.sourceSets.main // Adds the owning projects main sourceset to a group based on that sourcesets mod identifier (could be anything here, depending on the sourcesets extension values, or the project name)
add project(':api').sourceSets.main // Assuming the API project is not using NeoGradle, this would add the api project to a group using the `api` key, because the default mod identifier for non-neogradle projects is the projects name, here api
local project(':api').sourceSets.main // Assuming the API project is not using NeoGradle, this would add the api project to a group using the owning projects name, instead of the api projects name as a fallback (could be anything here, depending on the sourcesets extension values, or the project name)
add('something', project(':api').sourceSets.main) // This hardcodes the group identifier to 'something', performing no lookup of the mod identifier on the sourceset, or using the owning project, or the sourcesets project.
}
}
}
```
```
No other action is needed.

## Using conventions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.neoforged.gradle.common.extensions.subsystems.SubsystemsExtension;
import net.neoforged.gradle.common.runs.ide.IdeRunIntegrationManager;
import net.neoforged.gradle.common.runs.run.RunImpl;
import net.neoforged.gradle.common.runs.tasks.RunsReport;
import net.neoforged.gradle.common.runtime.definition.CommonRuntimeDefinition;
import net.neoforged.gradle.common.runtime.extensions.RuntimesExtension;
import net.neoforged.gradle.common.runtime.naming.OfficialNamingChannelConfigurator;
Expand Down Expand Up @@ -151,6 +152,9 @@ public void apply(Project project) {
//Needs to be before after evaluate
configureConventions(project);

//Set up reporting tasks
project.getTasks().register("runs", RunsReport.class);

final DevLogin devLogin = project.getExtensions().getByType(Subsystems.class).getDevLogin();
if (devLogin.getEnabled().get()) {
runs.configureEach(run -> {
Expand Down Expand Up @@ -198,7 +202,7 @@ private void configureSourceSetConventions(Project project, Conventions conventi
}

if (sourceSets.getShouldSourceSetsLocalRunRuntimesBeAutomaticallyAddedToRuns().get() && configurations.getIsEnabled().get()) {
run.getModSources().get().forEach(sourceSet -> {
run.getModSources().all().get().values().forEach(sourceSet -> {
if (project.getConfigurations().findByName(ConfigurationUtils.getSourceSetName(sourceSet, configurations.getRunRuntimeConfigurationPostFix().get())) != null) {
run.getDependencies().get().getRuntime().add(project.getConfigurations().getByName(ConfigurationUtils.getSourceSetName(sourceSet, configurations.getRunRuntimeConfigurationPostFix().get())));
}
Expand Down Expand Up @@ -356,7 +360,7 @@ private void applyAfterEvaluate(final Project project) {
}
}

if (run.getModSources().get().isEmpty()) {
if (run.getModSources().all().get().isEmpty()) {
throw new InvalidUserDataException("Run: " + run.getName() + " has no source sets configured. Please configure at least one source set.");
}

Expand All @@ -367,7 +371,7 @@ private void applyAfterEvaluate(final Project project) {
//TODO: Determine handling of multiple different runtimes, in multiple projects....
final Map<String, CommonRuntimeDefinition<?>> definitionSet = new HashMap<>();

runImpl.getModSources().get().forEach(sourceSet -> {
runImpl.getModSources().all().get().values().forEach(sourceSet -> {
try {
final Optional<CommonRuntimeDefinition<?>> definition = TaskDependencyUtils.findRuntimeDefinition(sourceSet);
if (definition.isPresent()) {
Expand Down Expand Up @@ -400,7 +404,7 @@ private void applyAfterEvaluate(final Project project) {
final String mainClass = runImpl.getMainClass().get();

//We add the dev login tool to a custom configuration which runtime classpath extends from the default runtime classpath
final SourceSet defaultSourceSet = runImpl.getModSources().get().get(0);
final SourceSet defaultSourceSet = runImpl.getModSources().all().get().entries().iterator().next().getValue();
final String runtimeOnlyDevLoginConfigurationName = ConfigurationUtils.getSourceSetName(defaultSourceSet, devLogin.getConfigurationSuffix().get());
final Configuration sourceSetRuntimeOnlyDevLoginConfiguration = project.getConfigurations().maybeCreate(runtimeOnlyDevLoginConfigurationName);
final Configuration sourceSetRuntimeClasspathConfiguration = project.getConfigurations().maybeCreate(defaultSourceSet.getRuntimeClasspathConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.neoforged.gradle.common.runs.ide;

import com.google.common.collect.Multimap;
import net.neoforged.elc.configs.GradleLaunchConfig;
import net.neoforged.elc.configs.JavaApplicationLaunchConfig;
import net.neoforged.elc.configs.LaunchConfig;
Expand Down Expand Up @@ -255,7 +256,7 @@ private TaskProvider<?> createIdeBeforeRunTask(Project project, String name, Run

private List<TaskProvider<?>> createEclipseCopyResourcesTasks(EclipseModel eclipse, Run run) {
final List<TaskProvider<?>> copyProcessResources = new ArrayList<>();
for (SourceSet sourceSet : run.getModSources().get()) {
for (SourceSet sourceSet : run.getModSources().all().get().values()) {
final Project sourceSetProject = SourceSetUtils.getProject(sourceSet);

final String taskName = CommonRuntimeUtils.buildTaskName("eclipseCopy", sourceSet.getProcessResourcesTaskName());
Expand Down Expand Up @@ -302,10 +303,10 @@ private static void writeLaunchToFile(Project project, String fileName, LaunchCo

private static Map<String, String> adaptEnvironment(
final RunImpl run,
final Function<ListProperty<SourceSet>, Provider<String>> modClassesProvider
final Function<Provider<Multimap<String, SourceSet>>, Provider<String>> modClassesProvider
) {
final Map<String, String> environment = new HashMap<>(run.getEnvironmentVariables().get());
environment.put("MOD_CLASSES", modClassesProvider.apply(run.getModSources()).get());
environment.put("MOD_CLASSES", modClassesProvider.apply(run.getModSources().all()).get());
return environment;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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 org.gradle.api.GradleException;
Expand All @@ -17,6 +18,7 @@
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;

Expand All @@ -27,19 +29,23 @@ 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;

private ListProperty<String> jvmArguments;
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();

@Inject
public RunImpl(final Project project, final String name) {
this.project = project;
this.name = name;
this.modSources = project.getObjects().newInstance(RunSourceSetsImpl.class, project);
this.unitTestSources = project.getObjects().newInstance(RunSourceSetsImpl.class, project);

this.jvmArguments = this.project.getObjects().listProperty(String.class);
this.environmentVariables = this.project.getObjects().mapProperty(String.class, String.class);
Expand Down Expand Up @@ -87,6 +93,46 @@ public void overrideEnvironmentVariables(MapProperty<String, String> environment
@Override
public abstract Property<Boolean> getShouldBuildAllProjects();

@Override
public RunSourceSets getUnitTestSources() {
return this.unitTestSources;
}

@Override
public void unitTestSource(@NotNull final SourceSet sourceSet) {
getUnitTestSources().add(sourceSet);
}

@Override
public void unitTestSources(@NotNull final SourceSet... sourceSets) {
getUnitTestSources().add(sourceSets);
}

@Override
public void unitTestSources(@NotNull final Iterable<? extends SourceSet> sourceSets) {
getUnitTestSources().add(sourceSets);
}

@Override
public RunSourceSets getModSources() {
return this.modSources;
}

@Override
public void modSource(@NotNull final SourceSet sourceSet) {
getModSources().add(sourceSet);
}

@Override
public void modSources(@NotNull final SourceSet... sourceSets) {
getModSources().add(sourceSets);
}

@Override
public void modSources(@NotNull final Iterable<? extends SourceSet> sourceSets) {
getModSources().add(sourceSets);
}

@Override
public ListProperty<String> getProgramArguments() {
return programArguments;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package net.neoforged.gradle.common.runs.run;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.neoforged.gradle.common.util.SourceSetUtils;
import net.neoforged.gradle.dsl.common.runs.run.RunSourceSets;
import org.gradle.api.Project;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;

import javax.inject.Inject;

public abstract class RunSourceSetsImpl implements RunSourceSets {

private final Project project;
private final Provider<Multimap<String, SourceSet>> provider;
private final Multimap<String, SourceSet> sourceSets;

@Inject
public RunSourceSetsImpl(Project project) {
this.project = project;
this.sourceSets = HashMultimap.create();
this.provider = project.provider(() -> sourceSets);
}


@Override
public void add(SourceSet sourceSet) {
this.sourceSets.put(SourceSetUtils.getModIdentifier(sourceSet, null), sourceSet);
}

@Override
public void add(Iterable<? extends SourceSet> sourceSets) {
for (SourceSet sourceSet : sourceSets) {
add(sourceSet);
}
}

@Override
public void add(SourceSet... sourceSets) {
for (SourceSet sourceSet : sourceSets) {
add(sourceSet);
}
}

@Override
public void local(SourceSet sourceSet) {
this.sourceSets.put(SourceSetUtils.getModIdentifier(sourceSet, project), sourceSet);
}

@Override
public void local(Iterable<? extends SourceSet> sourceSets) {
for (SourceSet sourceSet : sourceSets) {
local(sourceSet);
}
}

@Override
public void local(SourceSet... sourceSets) {
for (SourceSet sourceSet : sourceSets) {
local(sourceSet);
}
}

@Override
public void add(String groupId, SourceSet sourceSet) {
this.sourceSets.put(groupId, sourceSet);
}

@Override
public void add(String groupId, Iterable<? extends SourceSet> sourceSets) {
this.sourceSets.putAll(groupId, sourceSets);
}

@Override
public void add(String groupId, SourceSet... sourceSets) {
for (SourceSet sourceSet : sourceSets) {
add(groupId, sourceSet);
}
}

@Override
public Provider<Multimap<String, SourceSet>> all() {
return this.provider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void exec() {
environment(run.getEnvironmentVariables().get());
systemProperties(run.getSystemProperties().get());

run.getModSources().get().stream()
run.getModSources().all().get().values().stream()
.map(SourceSet::getRuntimeClasspath)
.forEach(this::classpath);

Expand Down
Loading

0 comments on commit 281a603

Please sign in to comment.