Skip to content

Commit f369028

Browse files
authored
Fix the ability of the build script to use PR builds in installers (#1801)
1 parent 2002c21 commit f369028

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java

+28-22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.File;
3838
import java.net.URI;
3939
import java.util.ArrayList;
40+
import java.util.Collections;
4041
import java.util.List;
4142
import java.util.Map;
4243
import java.util.function.Consumer;
@@ -271,6 +272,7 @@ public void apply(Project project) {
271272
genProductionPatches.flatMap(GenerateSourcePatches::getPatchesFolder)
272273
);
273274

275+
var installerRepositoryUrls = getInstallerRepositoryUrls(project);
274276
// Launcher profile = the version.json file used by the Minecraft launcher.
275277
var createLauncherProfile = tasks.register("createLauncherProfile", CreateLauncherProfile.class, task -> {
276278
task.setGroup(INTERNAL_GROUP);
@@ -279,17 +281,7 @@ public void apply(Project project) {
279281
task.getNeoForgeVersion().set(neoForgeVersion);
280282
task.getRawNeoFormVersion().set(rawNeoFormVersion);
281283
task.setLibraries(configurations.launcherProfileClasspath);
282-
task.getRepositoryURLs().set(project.provider(() -> {
283-
List<URI> repos = new ArrayList<>();
284-
for (var repo : project.getRepositories().withType(MavenArtifactRepository.class)) {
285-
var uri = repo.getUrl();
286-
if (!uri.toString().endsWith("/")) {
287-
uri = URI.create(uri + "/");
288-
}
289-
repos.add(uri);
290-
}
291-
return repos;
292-
}));
284+
task.getRepositoryURLs().set(installerRepositoryUrls);
293285
// ${version_name}.jar will be filled out by the launcher. It corresponds to the raw SRG Minecraft client jar.
294286
task.getIgnoreList().addAll("client-extra", "${version_name}.jar");
295287
task.setModules(configurations.modulePath);
@@ -308,17 +300,7 @@ public void apply(Project project) {
308300
task.addLibraries(configurations.launcherProfileClasspath);
309301
// We need the NeoForm zip for the SRG mappings.
310302
task.addLibraries(configurations.neoFormDataOnly);
311-
task.getRepositoryURLs().set(project.provider(() -> {
312-
List<URI> repos = new ArrayList<>();
313-
for (var repo : project.getRepositories().withType(MavenArtifactRepository.class)) {
314-
var uri = repo.getUrl();
315-
if (!uri.toString().endsWith("/")) {
316-
uri = URI.create(uri + "/");
317-
}
318-
repos.add(uri);
319-
}
320-
return repos;
321-
}));
303+
task.getRepositoryURLs().set(installerRepositoryUrls);
322304
task.getUniversalJar().set(universalJar.flatMap(AbstractArchiveTask::getArchiveFile));
323305
task.getInstallerProfile().set(neoDevBuildDir.map(dir -> dir.file("installer-profile.json")));
324306

@@ -467,6 +449,30 @@ public void apply(Project project) {
467449
setupProductionServerTest(project, installerJar);
468450
}
469451

452+
/**
453+
* Get the list of Maven repositories that may contain artifacts for the installer.
454+
*/
455+
private static Provider<List<URI>> getInstallerRepositoryUrls(Project project) {
456+
return project.provider(() -> {
457+
List<URI> repos = new ArrayList<>();
458+
var projectRepos = project.getRepositories();
459+
if (!projectRepos.isEmpty()) {
460+
for (var repo : projectRepos.withType(MavenArtifactRepository.class)) {
461+
repos.add(repo.getUrl());
462+
}
463+
} else {
464+
// If no project repos are defined, use the repository list we exposed in settings.gradle via an extension
465+
// See the end of settings.gradle for details
466+
Collections.addAll(repos, (URI[]) project.getGradle().getExtensions().getByName("repositoryBaseUrls"));
467+
}
468+
469+
// Ensure all base urls end with a slash
470+
repos.replaceAll(uri -> uri.toString().endsWith("/") ? uri : URI.create(uri + "/"));
471+
472+
return repos;
473+
});
474+
}
475+
470476
private static TaskProvider<TransformSources> configureAccessTransformer(
471477
Project project,
472478
TaskProvider<CreateMinecraftArtifacts> createSourceArtifacts,

buildSrc/src/main/java/net/neoforged/neodev/installer/LibraryCollector.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.File;
88
import java.io.IOException;
9+
import java.io.UncheckedIOException;
910
import java.net.URI;
1011
import java.net.http.HttpClient;
1112
import java.net.http.HttpRequest;
@@ -21,6 +22,8 @@
2122
import java.util.List;
2223
import java.util.Locale;
2324
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.CompletionException;
26+
import java.util.concurrent.ExecutionException;
2427
import java.util.concurrent.Future;
2528
import java.util.function.Function;
2629

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

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

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

66-
private final HttpClient httpClient = HttpClient.newBuilder().build();
69+
private final HttpClient httpClient = HttpClient.newBuilder()
70+
.followRedirects(HttpClient.Redirect.NORMAL)
71+
.build();
6772

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

8792
LOGGER.info("Collecting libraries from:");
8893
for (var repo : repositoryUrls) {
89-
LOGGER.info(" - " + repo);
94+
LOGGER.info(" - {}", repo);
9095
}
9196
}
9297

@@ -109,15 +114,15 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
109114
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding())
110115
.thenApply(response -> {
111116
if (response.statusCode() != 200) {
112-
LOGGER.info(" Got %d for %s".formatted(response.statusCode(), artifactUri));
117+
LOGGER.info(" Got {} for {}", response.statusCode(), artifactUri);
113118
String message = "Could not find %s: %d".formatted(artifactUri, response.statusCode());
114119
// Prepend error message from previous repo if they all fail
115120
if (previousError != null) {
116121
message = previousError + "\n" + message;
117122
}
118123
throw new RuntimeException(message);
119124
}
120-
LOGGER.info(" Found %s -> %s".formatted(name, artifactUri));
125+
LOGGER.info(" Found {} -> {}", name, artifactUri);
121126
return new Library(
122127
name,
123128
new LibraryDownload(new LibraryArtifact(
@@ -132,6 +137,9 @@ private void addLibrary(File file, MavenIdentifier identifier) throws IOExceptio
132137
libraryFuture = makeRequest.apply(null);
133138
} else {
134139
libraryFuture = libraryFuture.exceptionallyCompose(error -> {
140+
if (error instanceof CompletionException e) {
141+
return makeRequest.apply(e.getCause().getMessage());
142+
}
135143
return makeRequest.apply(error.getMessage());
136144
});
137145
}

settings.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ include ':coremods'
4444
// Ensure a unique artifact id within JIJ that does not conflict with net.neoforged:coremods, which is
4545
// another external dependency.
4646
project(":coremods").name = "neoforge-coremods"
47+
48+
// Expose the repository base URLs to projects to be able to access them
49+
// See this Gradle issue for tracking a more permanent solution:
50+
// https://github.com/gradle/gradle/issues/27260
51+
gradle.settingsEvaluated {
52+
gradle.extensions.add("repositoryBaseUrls", dependencyResolutionManagement.repositories.findAll { it instanceof MavenArtifactRepository }.collect { it.url }.toArray(URI[]::new))
53+
}

0 commit comments

Comments
 (0)