Skip to content

Commit

Permalink
Implement a fallback when including source sets that do not have NeoG…
Browse files Browse the repository at this point in the history
…radle applied so they get merged with the sources from the project in which the runs are defined.
  • Loading branch information
shartte committed Dec 5, 2023
1 parent 0c49067 commit 08114ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -237,10 +237,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 BiFunction<Project, ListProperty<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.getProject(), run.getModSources()).get());
return environment;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void configureRun(RunImpl run) {
final Map<String, String> runtimeInterpolationData = buildRunInterpolationData(run);

final Map<String, String> workingInterpolationData = new HashMap<>(runtimeInterpolationData);
workingInterpolationData.put("source_roots", RunsUtil.buildGradleModClasses(run.getModSources()).get());
workingInterpolationData.put("source_roots", RunsUtil.buildGradleModClasses(run.getProject(), run.getModSources()).get());

if (run.getIsClient().get()) {
getVersionJson().getPlatformJvmArgs().forEach(arg -> run.getJvmArguments().add(arg));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.neoforged.gradle.common.util;

import net.neoforged.gradle.dsl.common.extensions.Minecraft;
import net.neoforged.gradle.dsl.common.extensions.ProjectHolder;
import net.neoforged.gradle.dsl.common.extensions.RunnableSourceSet;
import org.gradle.api.Project;
Expand Down Expand Up @@ -37,12 +38,18 @@ public static Project getProject(final SourceSet sourceSet) {

throw new IllegalStateException("Could not find project for source set " + sourceSet.getName());
}

public static String getModIdentifier(final SourceSet sourceSet) {

/**
* Gets the mod-id to use for passing this source set to FML.
* @param runProject If the source set isn't part of a project where the NeoGradle plugin has been applied,
* we use this project to determine the mod id instead.
*/
public static String getModIdentifier(SourceSet sourceSet, Project runProject) {
final RunnableSourceSet runnableSourceSet = sourceSet.getExtensions().findByType(RunnableSourceSet.class);
if (runnableSourceSet != null)
return runnableSourceSet.getModIdentifier().get();

return getProject(sourceSet).getName();

Minecraft minecraft = runProject.getExtensions().getByType(Minecraft.class);
return minecraft.getModIdentifier().get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ public static Run create(final Project project, final String name) {
run.getTaskDependencies().forEach(task::dependsOn);
}));

run.getEnvironmentVariables().put("MOD_CLASSES", buildGradleModClasses(run.getModSources()));
run.getEnvironmentVariables().put("MOD_CLASSES", buildGradleModClasses(project, run.getModSources()));

return run;
}

public static Provider<String> buildGradleModClasses(final ListProperty<SourceSet> sourceSetsProperty) {
public static Provider<String> buildGradleModClasses(Project runProject, ListProperty<SourceSet> sourceSetsProperty) {
return buildGradleModClasses(
runProject,
sourceSetsProperty,
sourceSet -> Stream.concat(Stream.of(sourceSet.getOutput().getResourcesDir()), sourceSet.getOutput().getClassesDirs().getFiles().stream())
);
}

public static Provider<String> buildRunWithIdeaModClasses(final ListProperty<SourceSet> sourceSetsProperty) {
public static Provider<String> buildRunWithIdeaModClasses(Project runProject, final ListProperty<SourceSet> sourceSetsProperty) {
return buildGradleModClasses(
runProject,
sourceSetsProperty,
sourceSet -> {
final Project project = SourceSetUtils.getProject(sourceSet);
Expand All @@ -86,8 +88,9 @@ public static Provider<String> buildRunWithIdeaModClasses(final ListProperty<Sou
}

@SuppressWarnings("UnstableApiUsage")
public static Provider<String> buildRunWithEclipseModClasses(final ListProperty<SourceSet> sourceSetsProperty) {
public static Provider<String> buildRunWithEclipseModClasses(Project runProject, final ListProperty<SourceSet> sourceSetsProperty) {
return buildGradleModClasses(
runProject,
sourceSetsProperty,
sourceSet -> {
final Project project = SourceSetUtils.getProject(sourceSet);
Expand All @@ -107,18 +110,18 @@ public static String getIntellijOutName(@Nonnull final SourceSet sourceSet) {
return sourceSet.getName().equals(SourceSet.MAIN_SOURCE_SET_NAME) ? "production" : sourceSet.getName();
}

public static Provider<String> buildGradleModClasses(final ListProperty<SourceSet> sourceSetsProperty, final Function<SourceSet, Stream<File>> directoryBuilder) {
public static Provider<String> buildGradleModClasses(Project runProject, ListProperty<SourceSet> sourceSetsProperty, Function<SourceSet, Stream<File>> directoryBuilder) {
return sourceSetsProperty.map(sourceSets -> {
final Multimap<String, SourceSet> sourceSetsByRunId = HashMultimap.create();
sourceSets.forEach(sourceSet -> sourceSetsByRunId.put(SourceSetUtils.getModIdentifier(sourceSet), sourceSet));
sourceSets.forEach(sourceSet -> sourceSetsByRunId.put(SourceSetUtils.getModIdentifier(sourceSet, runProject), sourceSet));

return sourceSetsByRunId.entries()
.stream().flatMap(entry -> directoryBuilder.apply(entry.getValue())
.map(directory -> String.format("%s%%%%%s", entry.getKey(), directory.getAbsolutePath())))
.collect(Collectors.joining(File.pathSeparator));
});
}

private static String createTaskName(final String runName) {
return createTaskName("run", runName);
}
Expand Down

0 comments on commit 08114ec

Please sign in to comment.