diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index bfec4d2..82f3a2c 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -1,6 +1,7 @@ # Builds the mod for every v* tag and publishes it as a release with lua_mod_automator.zip # attached, so .../releases/download/latest/lua_mod_automator.zip always serves the newest one. -# Pre-release tags (containing "-") are built and tested but not released (see ci/release.sh). +# Pre-release tags (containing "-") and tags on commits that are not on master are built and +# tested but not released (see ci/release.sh). # Uses the repository secret RELEASE_TOKEN (a token with "repository: Read and Write") when it # exists, else the job's automatic token. name: release @@ -16,7 +17,7 @@ jobs: steps: - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 with: - # release.sh reads the v* tag on HEAD + # release.sh reads the v* tag on HEAD and checks that HEAD is on origin/master fetch-depth: 0 - uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 diff --git a/README.md b/README.md index ba9d5bd..4e8b7e5 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,9 @@ download (see ZomboidForge's mod development guide). ```shell ./gradlew build # compiles, tests, checks the mod against the game, and packs build/distributions/ ./gradlew runServer # a ZomboidForge server in a container with the mod (default settings: idle) + +# What a config.toml resolves to, the way the server does it at startup, without a server: +./gradlew -q resolveCollection --args=path/to/config.toml ``` `runServer` mounts the mod read-only, so it runs with the default settings, in which the mod @@ -164,7 +167,8 @@ directory (or set `zomboidForge.deployDirectory` and run `./gradlew deployMod`) Pushing a tag `v` that matches `version` in `build.gradle.kts` builds the mod and publishes it as a release (`.gitea/workflows/release.yml`, `ci/release.sh`). Tags containing -`-` (e.g. `v0.2.0-rc.1`) are built and tested but not released. +`-` (e.g. `v0.2.0-rc.1`) and tags on commits that are not on `master` are built and tested but +not released. ## License diff --git a/build.gradle.kts b/build.gradle.kts index 4fe9c1e..e110ea1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -39,3 +39,12 @@ tasks.withType().configureEach { tasks.test { useJUnitPlatform() } + +// Prints what the server would set for a config.toml: ./gradlew -q resolveCollection --args= +tasks.register("resolveCollection") { + group = "zomboidforge" + description = "Resolves the collection of a config.toml like the server does at startup and prints Mods=, WorkshopItems=, and Map=." + classpath = sourceSets.test.get().runtimeClasspath + mainClass = "dev.overlord.luamodautomator.ResolveCollection" + workingDir = File(System.getProperty("user.dir")) +} diff --git a/ci/release.sh b/ci/release.sh index 960e781..3389a1b 100755 --- a/ci/release.sh +++ b/ci/release.sh @@ -4,8 +4,9 @@ # attached twice: as lua_mod_automator-.zip, and as lua_mod_automator.zip, the stable # name behind ///releases/download/latest/lua_mod_automator.zip. # -# A pre-release tag (containing "-", e.g. v0.2.0-rc.1) is built and tested but not released. -# The tag must match the version in build.gradle.kts. +# A pre-release tag (containing "-", e.g. v0.2.0-rc.1), or a tag on a commit that is not on +# master, is built and tested but not released. The tag must match the version in +# build.gradle.kts. # # Usage: # ci/release.sh build downloads the game jar and builds the mod (tests and checkMod included) @@ -27,6 +28,7 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" readonly REPO_ROOT readonly MOD_ID="lua_mod_automator" +readonly RELEASE_BRANCH="master" readonly WORK_DIR="${REPO_ROOT}/build/ci" # DepotDownloader downloads single files of a depot; steamcmd only whole apps. Same release @@ -47,6 +49,11 @@ fail() { exit 1 } +# Whether HEAD is on the release branch, as fetched from origin. +on_release_branch() { + git -C "${REPO_ROOT}" merge-base --is-ancestor HEAD "origin/${RELEASE_BRANCH}" 2> /dev/null +} + # Prints the v* tag on HEAD, or nothing. release_tag() { git -C "${REPO_ROOT}" describe --tags --exact-match --match 'v*' 2> /dev/null || true @@ -174,6 +181,11 @@ publish() { return fi + if ! on_release_branch; then + log "${tag} is not on ${RELEASE_BRANCH}; nothing is released." + return + fi + : "${GITEA_TOKEN:?GITEA_TOKEN is not set}" : "${GITHUB_API_URL:?GITHUB_API_URL is not set}" : "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is not set}" diff --git a/src/main/java/dev/overlord/luamodautomator/Resolution.java b/src/main/java/dev/overlord/luamodautomator/Resolution.java index 300318a..e19542b 100644 --- a/src/main/java/dev/overlord/luamodautomator/Resolution.java +++ b/src/main/java/dev/overlord/luamodautomator/Resolution.java @@ -62,18 +62,23 @@ record Resolution(List items, List excluded, L modIds = Math.max(modIds, Resolution.modIds(item).length()); } - lines.add("# Mod list"); + lines.add("Mod List:"); for (int i = 0; i < this.items.size(); i++) { Item item = this.items.get(i); - lines.add(("# %" + number + "d. %-" + title + "s %10s %-" + modIds + "s").formatted(i + 1, item.title(), item.id(), Resolution.modIds(item)).stripTrailing()); + //noinspection StringConcatenationInFormatCall + lines.add( + ("%" + number + "d. %-" + title + "s %10s %-" + modIds + "s").formatted( + i + 1, item.title(), item.id(), Resolution.modIds(item) + ).stripTrailing() + ); } if (!this.excluded.isEmpty()) { - lines.add("# Excluded"); - for (Item item : this.excluded) lines.add("# %s %s".formatted(item.title(), item.id())); + lines.add("Excluded:"); + for (Item item : this.excluded) lines.add(" %s %s".formatted(item.title(), item.id())); } return lines; diff --git a/src/test/java/dev/overlord/luamodautomator/CollectionResolverTest.java b/src/test/java/dev/overlord/luamodautomator/CollectionResolverTest.java index d7e607f..3727aec 100644 --- a/src/test/java/dev/overlord/luamodautomator/CollectionResolverTest.java +++ b/src/test/java/dev/overlord/luamodautomator/CollectionResolverTest.java @@ -190,13 +190,13 @@ class CollectionResolverTest List modList = CollectionResolver.resolve(CollectionResolverTest.COLLECTION, CollectionResolverTest.DEFAULT_ORDER, items).modList(); assertEquals(List.of( - "# Mod list", - "# 1. A Map 300 MapMod", - "# 2. Framework 200 Frame", - "# 3. Misc One 100 MiscOne", - "# 4. Misc Two 400 MiscTwo, MiscTwoExtra", - "# Excluded", - "# Misc Three 500" + "Mod List:", + "1. A Map 300 MapMod", + "2. Framework 200 Frame", + "3. Misc One 100 MiscOne", + "4. Misc Two 400 MiscTwo, MiscTwoExtra", + "Excluded:", + " Misc Three 500" ), modList); } diff --git a/src/test/java/dev/overlord/luamodautomator/ResolveCollection.java b/src/test/java/dev/overlord/luamodautomator/ResolveCollection.java new file mode 100644 index 0000000..7719d33 --- /dev/null +++ b/src/test/java/dev/overlord/luamodautomator/ResolveCollection.java @@ -0,0 +1,70 @@ +package dev.overlord.luamodautomator; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +/** + * Resolves a collection like the server does at startup, without the server: binds the shipped + * defaults with a {@code config.toml} on top, asks Steam, and prints the mod list, + * {@code Mods=}, {@code WorkshopItems=}, {@code Map=}, and every warning. Run through + * {@code ./gradlew resolveCollection --args=}. + */ +public final class ResolveCollection +{ + private ResolveCollection() {} + + public static void main(String[] args) throws IOException + { + if (args.length != 1) + { + System.err.println("Usage: ./gradlew resolveCollection --args="); + System.exit(2); + } + + Path config = Path.of(args[0]); + + if (!Files.isRegularFile(config)) + { + System.err.println("'" + config.toAbsolutePath() + "' does not exist."); + System.exit(1); + } + + List warnings = new ArrayList<>(); + Settings settings = SettingsTest.bind(Files.readString(config, StandardCharsets.UTF_8), warnings); + String id = SteamWorkshopApi.collectionId(settings.collection()).orElse(""); + + if (id.isEmpty()) + { + System.err.println("'collection' in '" + args[0] + "' holds no collection id."); + System.exit(1); + } + + try (SteamWorkshopApi api = new SteamWorkshopApi(SteamWorkshopApi.BASE, Duration.ofSeconds(settings.steam().timeoutS()), "LuaModAutomator/resolveCollection")) + { + Resolution resolution = CollectionResolver.resolve(api.collection(id, warnings), settings.order(), settings.items()); + ServerValues values = resolution.values(settings.ini().baseMap()); + + warnings.addAll(resolution.warnings()); + + resolution.modList().forEach(System.out::println); + System.out.println(); + System.out.println("Mods=" + values.mods()); + System.out.println(); + System.out.println("WorkshopItems=" + values.workshopItems()); + System.out.println(); + System.out.println("Map=" + values.map()); + + for (String warning : warnings) System.err.println("WARNING: " + warning); + } + catch (SteamException | ConfigurationException e) + { + System.err.println(e.getMessage()); + System.exit(1); + } + } +} diff --git a/src/test/java/dev/overlord/luamodautomator/SteamWorkshopApiTest.java b/src/test/java/dev/overlord/luamodautomator/SteamWorkshopApiTest.java index 9183554..8e6e265 100644 --- a/src/test/java/dev/overlord/luamodautomator/SteamWorkshopApiTest.java +++ b/src/test/java/dev/overlord/luamodautomator/SteamWorkshopApiTest.java @@ -109,9 +109,9 @@ class SteamWorkshopApiTest Settings settings = SettingsTest.bind(""); Resolution resolution = CollectionResolver.resolve(this.api.collection(SteamWorkshopApiTest.recordedId(), new ArrayList<>()), settings.order(), settings.items()); ServerValues values = resolution.values(settings.ini().baseMap()); - List expected = SteamWorkshopApiTest.resource("script-output.txt").lines().toList(); + List expected = SteamWorkshopApiTest.withoutCommentPrefix(SteamWorkshopApiTest.resource("script-output.txt").lines().toList()); - List actual = new ArrayList<>(resolution.modList()); + List actual = new ArrayList<>(resolution.modList().subList(1, resolution.modList().size())); actual.add("Mods=" + values.mods()); actual.add("WorkshopItems=" + values.workshopItems()); @@ -207,6 +207,9 @@ class SteamWorkshopApiTest assertFalse(items.get(0).found()); } + /** The script's output without its mod list header, and the mod list lines without their {@code "# "}. */ + private static List withoutCommentPrefix(List output) { return output.stream().skip(1).map(line -> line.startsWith("# ") ? line.substring(2) : line).toList(); } + /** Details for every even id of the request: title {@code Item }, Mod ID {@code Mod}. */ private static Response details(Map form) {