Skip to content

Commit d844d66

Browse files
committed
Add Modrinth project files support
Uses the same syntax as curseforge (@/path/to/modrinth-mods.txt)
1 parent b3e2087 commit d844d66

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/main/java/me/itzg/helpers/modrinth/ModrinthCommand.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.net.URI;
1010
import java.nio.file.Files;
1111
import java.nio.file.Path;
12+
import java.nio.file.Paths;
1213
import java.util.ArrayList;
1314
import java.util.Collections;
1415
import java.util.HashSet;
@@ -46,10 +47,17 @@ public class ModrinthCommand implements Callable<Integer> {
4647

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

@@ -124,7 +132,7 @@ private List<Path> processProjects(List<String> projects) {
124132
//noinspection DataFlowIssue since it thinks block() may return null
125133
return
126134
modrinthApiClient.bulkGetProjects(
127-
projects.stream()
135+
expandProjectListings(projects).stream()
128136
.filter(s -> !s.trim().isEmpty())
129137
.map(ProjectRef::parse)
130138
)
@@ -142,6 +150,30 @@ private List<Path> processProjects(List<String> projects) {
142150
}
143151
}
144152

153+
private List<String> expandProjectListings(List<String> projects) {
154+
return projects.stream()
155+
.distinct()
156+
// handle @-file containing refs
157+
.flatMap(ref -> {
158+
if (ref.startsWith("@")) {
159+
try {
160+
return Files.readAllLines(Paths.get(ref.substring(1))).stream()
161+
.map(s -> s
162+
.replaceFirst("#.*", "")
163+
.trim()
164+
)
165+
.filter(s -> !s.isEmpty());
166+
} catch (IOException e) {
167+
throw new GenericException("Reading project refs from file: " + ref.substring(1), e);
168+
}
169+
}
170+
else {
171+
return Stream.of(ref);
172+
}
173+
})
174+
.collect(Collectors.toList());
175+
}
176+
145177
private ModrinthManifest loadManifest() throws IOException {
146178
final Path legacyManifestPath = outputDirectory.resolve(LegacyModrinthManifest.FILENAME);
147179

src/test/java/me/itzg/helpers/modrinth/ModrinthCommandTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
1616
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
1717
import java.nio.file.Path;
18+
import java.nio.file.Files;
19+
import java.util.Arrays;
1820
import java.util.function.Consumer;
1921
import me.itzg.helpers.LatchingExecutionExceptionHandler;
2022
import me.itzg.helpers.errors.InvalidParameterException;
@@ -378,6 +380,37 @@ void handlesDatapacksLatestVersion(@TempDir Path tempDir) {
378380
.hasContent("content of zip");
379381
}
380382

383+
@Test
384+
void usingListingFile(@TempDir Path tempDir) throws Exception {
385+
setupStubs();
386+
387+
final Path listingFile = Files.write(tempDir.resolve("listing.txt"),
388+
Arrays.asList(
389+
"fabric-api:vNBWcMLP",
390+
"# This is a comment",
391+
"cloth-config:qA00xo1O",
392+
"",
393+
"# Another comment"
394+
)
395+
);
396+
397+
final int exitCode = new CommandLine(
398+
new ModrinthCommand()
399+
)
400+
.execute(
401+
"--api-base-url", wm.getRuntimeInfo().getHttpBaseUrl(),
402+
"--output-directory", tempDir.toString(),
403+
"--game-version", "1.21.5",
404+
"--loader", "fabric",
405+
"--projects", "@" + listingFile
406+
);
407+
408+
assertThat(exitCode).isEqualTo(ExitCode.OK);
409+
410+
assertThat(tempDir.resolve("mods/fabric-api-0.127.1+1.21.5.jar")).exists();
411+
assertThat(tempDir.resolve("mods/cloth-config-18.0.145-fabric.jar")).exists();
412+
}
413+
381414
@NotNull
382415
private static RequestPatternBuilder projectVersionsRequest(String projectId) {
383416
return getRequestedFor(urlPathEqualTo("/v2/project/" + projectId + "/version"));

0 commit comments

Comments
 (0)