Skip to content

Add Modrinth project files support #580

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

Merged
merged 4 commits into from
Jun 21, 2025
Merged
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
38 changes: 35 additions & 3 deletions src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -46,10 +47,17 @@ public class ModrinthCommand implements Callable<Integer> {

@Option(
names = "--projects",
description = "Project ID or Slug. Prefix with loader: e.g. fabric:project-id",
description = "Project ID or Slug. Can be <project ID>|<slug>,"
+ " <loader>:<project ID>|<slug>,"
+ " <loader>:<project ID>|<slug>:<version ID|version number|release type>,"
+ " '@'<filename with ref per line (supports # comments)>"
+ "%nExamples: fabric-api, fabric:fabric-api, fabric:fabric-api:0.76.1+1.19.2,"
+ " datapack:terralith, @/path/to/modrinth-mods.txt"
+ "%nValid release types: release, beta, alpha"
+ "%nValid loaders: fabric, forge, paper, datapack, etc.",
split = SPLIT_COMMA_NL,
splitSynopsisLabel = SPLIT_SYNOPSIS_COMMA_NL,
paramLabel = "[loader:]id|slug"
paramLabel = "[loader:]id|slug[:version]"
)
List<String> projects;

Expand Down Expand Up @@ -124,7 +132,7 @@ private List<Path> processProjects(List<String> projects) {
//noinspection DataFlowIssue since it thinks block() may return null
return
modrinthApiClient.bulkGetProjects(
projects.stream()
expandProjectListings(projects).stream()
.filter(s -> !s.trim().isEmpty())
.map(ProjectRef::parse)
)
Expand All @@ -142,6 +150,30 @@ private List<Path> processProjects(List<String> projects) {
}
}

private List<String> expandProjectListings(List<String> projects) {
return projects.stream()
.distinct()
// handle @-file containing refs
.flatMap(ref -> {
if (ref.startsWith("@")) {
try {
return Files.readAllLines(Paths.get(ref.substring(1))).stream()
.map(s -> s
.replaceFirst("#.*", "")
.trim()
)
.filter(s -> !s.isEmpty());
} catch (IOException e) {
throw new GenericException("Reading project refs from file: " + ref.substring(1), e);
}
}
else {
return Stream.of(ref);
}
})
.collect(Collectors.toList());
}

private ModrinthManifest loadManifest() throws IOException {
final Path legacyManifestPath = outputDirectory.resolve(LegacyModrinthManifest.FILENAME);

Expand Down
33 changes: 33 additions & 0 deletions src/test/java/me/itzg/helpers/modrinth/ModrinthCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.function.Consumer;
import me.itzg.helpers.LatchingExecutionExceptionHandler;
import me.itzg.helpers.errors.InvalidParameterException;
Expand Down Expand Up @@ -378,6 +380,37 @@ void handlesDatapacksLatestVersion(@TempDir Path tempDir) {
.hasContent("content of zip");
}

@Test
void usingListingFile(@TempDir Path tempDir) throws Exception {
setupStubs();

final Path listingFile = Files.write(tempDir.resolve("listing.txt"),
Arrays.asList(
"fabric-api",
"# This is a comment",
"cloth-config",
"",
"# Another comment"
)
);

final int exitCode = new CommandLine(
new ModrinthCommand()
)
.execute(
"--api-base-url", wm.getRuntimeInfo().getHttpBaseUrl(),
"--output-directory", tempDir.toString(),
"--game-version", "1.19.2",
"--loader", "fabric",
"--projects=@" + listingFile
);

assertThat(exitCode).isEqualTo(ExitCode.OK);

assertThat(tempDir.resolve("mods/fabric-api-0.76.1+1.19.2.jar")).exists();
assertThat(tempDir.resolve("mods/cloth-config-8.3.103-fabric.jar")).exists();
}

@NotNull
private static RequestPatternBuilder projectVersionsRequest(String projectId) {
return getRequestedFor(urlPathEqualTo("/v2/project/" + projectId + "/version"));
Expand Down