Skip to content

Commit

Permalink
Fix the ability of the build script to use PR builds in installers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Dec 25, 2024
1 parent 2002c21 commit f369028
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
50 changes: 28 additions & 22 deletions buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
Expand Down Expand Up @@ -271,6 +272,7 @@ public void apply(Project project) {
genProductionPatches.flatMap(GenerateSourcePatches::getPatchesFolder)
);

var installerRepositoryUrls = getInstallerRepositoryUrls(project);
// Launcher profile = the version.json file used by the Minecraft launcher.
var createLauncherProfile = tasks.register("createLauncherProfile", CreateLauncherProfile.class, task -> {
task.setGroup(INTERNAL_GROUP);
Expand All @@ -279,17 +281,7 @@ public void apply(Project project) {
task.getNeoForgeVersion().set(neoForgeVersion);
task.getRawNeoFormVersion().set(rawNeoFormVersion);
task.setLibraries(configurations.launcherProfileClasspath);
task.getRepositoryURLs().set(project.provider(() -> {
List<URI> repos = new ArrayList<>();
for (var repo : project.getRepositories().withType(MavenArtifactRepository.class)) {
var uri = repo.getUrl();
if (!uri.toString().endsWith("/")) {
uri = URI.create(uri + "/");
}
repos.add(uri);
}
return repos;
}));
task.getRepositoryURLs().set(installerRepositoryUrls);
// ${version_name}.jar will be filled out by the launcher. It corresponds to the raw SRG Minecraft client jar.
task.getIgnoreList().addAll("client-extra", "${version_name}.jar");
task.setModules(configurations.modulePath);
Expand All @@ -308,17 +300,7 @@ public void apply(Project project) {
task.addLibraries(configurations.launcherProfileClasspath);
// We need the NeoForm zip for the SRG mappings.
task.addLibraries(configurations.neoFormDataOnly);
task.getRepositoryURLs().set(project.provider(() -> {
List<URI> repos = new ArrayList<>();
for (var repo : project.getRepositories().withType(MavenArtifactRepository.class)) {
var uri = repo.getUrl();
if (!uri.toString().endsWith("/")) {
uri = URI.create(uri + "/");
}
repos.add(uri);
}
return repos;
}));
task.getRepositoryURLs().set(installerRepositoryUrls);
task.getUniversalJar().set(universalJar.flatMap(AbstractArchiveTask::getArchiveFile));
task.getInstallerProfile().set(neoDevBuildDir.map(dir -> dir.file("installer-profile.json")));

Expand Down Expand Up @@ -467,6 +449,30 @@ public void apply(Project project) {
setupProductionServerTest(project, installerJar);
}

/**
* Get the list of Maven repositories that may contain artifacts for the installer.
*/
private static Provider<List<URI>> getInstallerRepositoryUrls(Project project) {
return project.provider(() -> {
List<URI> repos = new ArrayList<>();
var projectRepos = project.getRepositories();
if (!projectRepos.isEmpty()) {
for (var repo : projectRepos.withType(MavenArtifactRepository.class)) {
repos.add(repo.getUrl());
}
} else {
// If no project repos are defined, use the repository list we exposed in settings.gradle via an extension
// See the end of settings.gradle for details
Collections.addAll(repos, (URI[]) project.getGradle().getExtensions().getByName("repositoryBaseUrls"));
}

// Ensure all base urls end with a slash
repos.replaceAll(uri -> uri.toString().endsWith("/") ? uri : URI.create(uri + "/"));

return repos;
});
}

private static TaskProvider<TransformSources> configureAccessTransformer(
Project project,
TaskProvider<CreateMinecraftArtifacts> createSourceArtifacts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -21,6 +22,8 @@
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.Function;

Expand All @@ -41,7 +44,7 @@ public static List<Library> resolveLibraries(List<URI> repositoryUrls, Collectio
throw new RuntimeException(e);
}
}).toList();
LOGGER.info("Collected %d libraries".formatted(result.size()));
LOGGER.info("Collected {} libraries", result.size());
return result;
}

Expand All @@ -63,7 +66,9 @@ public static List<Library> resolveLibraries(List<URI> repositoryUrls, Collectio

private final List<Future<Library>> libraries = new ArrayList<>();

private final HttpClient httpClient = HttpClient.newBuilder().build();
private final HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build();

private LibraryCollector(List<URI> repoUrl) {
this.repositoryUrls = new ArrayList<>(repoUrl);
Expand All @@ -86,7 +91,7 @@ private LibraryCollector(List<URI> repoUrl) {

LOGGER.info("Collecting libraries from:");
for (var repo : repositoryUrls) {
LOGGER.info(" - " + repo);
LOGGER.info(" - {}", repo);
}
}

Expand All @@ -109,15 +114,15 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
.thenApply(response -> {
if (response.statusCode() != 200) {
LOGGER.info(" Got %d for %s".formatted(response.statusCode(), artifactUri));
LOGGER.info(" Got {} for {}", response.statusCode(), artifactUri);
String message = "Could not find %s: %d".formatted(artifactUri, response.statusCode());
// Prepend error message from previous repo if they all fail
if (previousError != null) {
message = previousError + "\n" + message;
}
throw new RuntimeException(message);
}
LOGGER.info(" Found %s -> %s".formatted(name, artifactUri));
LOGGER.info(" Found {} -> {}", name, artifactUri);
return new Library(
name,
new LibraryDownload(new LibraryArtifact(
Expand All @@ -132,6 +137,9 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
libraryFuture = makeRequest.apply(null);
} else {
libraryFuture = libraryFuture.exceptionallyCompose(error -> {
if (error instanceof CompletionException e) {
return makeRequest.apply(e.getCause().getMessage());
}
return makeRequest.apply(error.getMessage());
});
}
Expand Down
7 changes: 7 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ include ':coremods'
// Ensure a unique artifact id within JIJ that does not conflict with net.neoforged:coremods, which is
// another external dependency.
project(":coremods").name = "neoforge-coremods"

// Expose the repository base URLs to projects to be able to access them
// See this Gradle issue for tracking a more permanent solution:
// https://github.com/gradle/gradle/issues/27260
gradle.settingsEvaluated {
gradle.extensions.add("repositoryBaseUrls", dependencyResolutionManagement.repositories.findAll { it instanceof MavenArtifactRepository }.collect { it.url }.toArray(URI[]::new))
}

0 comments on commit f369028

Please sign in to comment.