Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a fallback when including non-NeoGradle source sets in a run #65

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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