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

Support composite builds when generating runs #871

Open
wants to merge 1 commit into
base: FG_5.0
Choose a base branch
from
Open
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 @@ -104,7 +104,8 @@ public final String getTaskName() {
}

public final String getUniqueFileName() {
return project.getPath().length() > 1 ? String.join("_", String.join("_", project.getPath().substring(1).split(":")), getTaskName()) : getTaskName();
String prefix = Utils.getCompositePath(project);
return prefix.length() > 1 ? prefix.substring(1).replace(':', '_') + "_" + getTaskName() : getTaskName();
}

public final String getUniqueName() {
Expand Down
49 changes: 49 additions & 0 deletions src/common/java/net/minecraftforge/gradle/common/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.repositories.ArtifactRepository;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.TaskProvider;

Expand Down Expand Up @@ -491,4 +492,52 @@ public static String replaceTokens(Map<String, ?> tokens, String value) {

return buf.toString();
}

/**
* Get the root project of this composite build.
*
* @param project The current project.
* @return The <em>very</em> root project.
* @see Project#getRootProject()
*/
public static Project getCompositeRoot(Project project) {
Gradle gradle = project.getGradle();
while (gradle.getParent() != null) gradle = gradle.getParent();
return gradle.getRootProject();
}

/**
* Get the path to the current project relative to the composite root.
*
* @param project The current project.
* @return The root
* @see Project#getPath()
*/
public static String getCompositePath(Project project) {
Gradle gradle = project.getGradle();
if (gradle.getParent() == null) return project.getPath();

// Build up a list of paths. We start off with the current sub-project (we do nothing if we're the root
// project).
List<String> paths = new ArrayList<>();
if (project.getPath().length() > 1) paths.add(project.getPath().substring(1));

while (gradle.getParent() != null) {
// Then for each composite build parent, find the matching IncludedBuild in the parent and add its name to
// the path.
Project thisProject = gradle.getRootProject();
String projectName = gradle
.getParent().getIncludedBuilds().stream()
.filter(child -> child.getProjectDir().equals(thisProject.getRootDir()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Cannot find valid matching parent"))
.getName();
paths.add(projectName);

gradle = gradle.getParent();
}

Collections.reverse(paths);
return ":" + String.join(":", paths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import net.minecraftforge.gradle.common.util.RunConfig;
import net.minecraftforge.gradle.common.util.Utils;

import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
Expand Down Expand Up @@ -196,9 +195,12 @@ protected Map<String, Document> createRunConfiguration(@Nonnull final Project pr

final Element gradleTask = javaDocument.createElement("option");
{
String prefix = Utils.getCompositePath(project);
if (prefix.length() == 1) prefix = "";

gradleTask.setAttribute("name", "Gradle.BeforeRunTask");
gradleTask.setAttribute("enabled", "true");
gradleTask.setAttribute("tasks", project.getTasks().getByName("prepare" + Utils.capitalize(runConfig.getTaskName())).getPath());
gradleTask.setAttribute("tasks", prefix + ":prepare" + Utils.capitalize(runConfig.getTaskName()));
gradleTask.setAttribute("externalProjectPath", "$PROJECT_DIR$");
}
methods.appendChild(gradleTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,17 @@ public abstract class RunConfigGenerator

public static void createIDEGenRunsTasks(@Nonnull final MinecraftExtension minecraft, @Nonnull final TaskProvider<Task> prepareRuns, @Nonnull final TaskProvider<Task> makeSourceDirs, List<String> additionalClientArgs) {
final Project project = minecraft.getProject();
Project rootProject = Utils.getCompositeRoot(project);

final Map<String, Triple<List<Object>, File, Supplier<RunConfigGenerator>>> ideConfigurationGenerators = ImmutableMap.<String, Triple<List<Object>, File, Supplier<RunConfigGenerator>>>builder()
.put("genIntellijRuns", ImmutableTriple.of(Collections.singletonList(prepareRuns),
new File(project.getRootProject().getRootDir(), ".idea/runConfigurations"),
() -> new IntellijRunGenerator(project.getRootProject())))
new File(rootProject.getRootDir(), ".idea/runConfigurations"),
() -> new IntellijRunGenerator(rootProject)))
.put("genEclipseRuns", ImmutableTriple.of(ImmutableList.of(prepareRuns, makeSourceDirs),
project.getProjectDir(),
rootProject.getProjectDir(),
EclipseRunGenerator::new))
.put("genVSCodeRuns", ImmutableTriple.of(ImmutableList.of(prepareRuns, makeSourceDirs),
new File(project.getProjectDir(), ".vscode"),
new File(rootProject.getProjectDir(), ".vscode"),
VSCodeRunGenerator::new))
.build();

Expand Down Expand Up @@ -140,7 +141,7 @@ protected static String replaceRootDirBy(@Nonnull final Project project, String
if (value == null || value.isEmpty()) {
return value;
}
return value.replace(project.getRootDir().toString(), replacement);
return value.replace(Utils.getCompositeRoot(project).getRootDir().toString(), replacement);
}

protected static Stream<String> mapModClassesToGradle(Project project, RunConfig runConfig)
Expand Down