Implement a command line, configurable rule printer (#1357)

* Implement a command line, configurable rule printer

* Update publish action to rule printer

* Bump loom and lqf versions
This commit is contained in:
altrisi authored and GitHub committed 2022-08-09 20:44:35 +02:00
1 parent 1734930f7e
commit 4db1dd8f87
4 files changed
+54 -48

No files matched your search

+3 -28
View File
@@ -157,40 +157,15 @@ jobs:
distribution: 'temurin'
java-version: 17
cache: 'gradle'
- name: Replace fabric.mod.json
run: |
cd src/main/resources
rm fabric.mod.json
echo '
{
"schemaVersion": 1,
"id": "carpet",
"version": "1.4.36",
"entrypoints": {
"server": [ "carpet.utils.CarpetRulePrinter" ],
"preLaunch": [ "carpet.utils.CarpetRulePrinter" ]
}
}' > fabric.mod.json
cd ../../../
- name: Run solution
run: |
chmod +x gradlew
./gradlew runServer > settings-toProccess.txt
- name: Checkout wiki
uses: actions/checkout@v2
with:
repository: ${{github.repository}}.wiki
path: wiki
- name: Proccess Gradle log into a pretty wiki page
- name: Run rule printer into the wiki page
run: |
from1="# Carpet";
File=settings-toProccess.txt
if grep -q "Deprecated Gradle features" "$File"; then # Happens after update to Gradle 6
to2="Deprecated Gradle features";
else
to2="BUILD SUCCESSFUL"
fi
a="$(cat settings-toProccess.txt)"; a="$(echo "${a#*"$from1"}")"; echo "$from1${a%%"$to2"*}" > wiki/Current-Available-Settings.md
chmod +x gradlew
./gradlew runServer --args="-- -carpetDumpRules -dumpPath ../wiki/Current-Available-Settings.md"
- name: Commit updated wiki page
continue-on-error: true
run: |
+2 -2
View File
@@ -1,6 +1,6 @@
plugins {
id 'fabric-loom' version '0.11-SNAPSHOT'
id 'io.github.juuxel.loom-quiltflower' version '1.7.1'
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'io.github.juuxel.loom-quiltflower' version '1.7.3'
id 'maven-publish'
}
@@ -1,33 +1,63 @@
package carpet.utils;
import carpet.CarpetServer;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.fabricmc.loader.api.FabricLoader;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.System;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Runs only from GitHub Actions to generate the wiki
* page with all Carpet rules on it.
* It is here so it can be managed by the IDE
* Provides a command line interface to generate a dump with all rules
* in a pretty markdown format to a specified file, with an optional
* category filter
*
*/
public class CarpetRulePrinter implements DedicatedServerModInitializer, PreLaunchEntrypoint {
public static final PrintStream OLD_OUT = System.out;
public class CarpetRulePrinter implements DedicatedServerModInitializer {
@Override
public void onInitializeServer() {
// Minecraft (or whatever) changes the System.out to have prefixes,
// our simple parser doesn't like that. So we change it back
System.setOut(OLD_OUT);
CarpetServer.onGameStarted();
CarpetServer.settingsManager.printAllRulesToLog(null);
// When launching, we use the "--" separator to prevent the game rejecting to launch because of unknown options
// Clear it in case it's present given else our option parser would also ignore them!
String[] args = Arrays.stream(FabricLoader.getInstance().getLaunchArguments(true)).filter(opt -> !opt.equals("--")).toArray(String[]::new);
// Prepare an OptionParser for our parameters
OptionParser parser = new OptionParser();
OptionSpec<Void> shouldDump = parser.accepts("carpetDumpRules");
OptionSpec<String> pathSpec = parser.accepts("dumpPath").withRequiredArg();
OptionSpec<String> filterSpec = parser.accepts("dumpFilter").withRequiredArg();
parser.allowsUnrecognizedOptions(); // minecraft may need more stuff later that we don't want to special-case
OptionSet options = parser.parse(args);
// If our flag isn't set, continue regular launch
if (!options.has(shouldDump)) return;
Logger logger = LoggerFactory.getLogger("Carpet Rule Printer");
logger.info("Starting in rule dump mode...");
// at this point, onGameStarted() already ran given it as an entrypoint runs before
PrintStream outputStream;
try {
Path path = Path.of(options.valueOf(pathSpec)).toAbsolutePath();
logger.info("Printing rules to: " + path);
Files.createDirectories(path.getParent());
outputStream = new PrintStream(Files.newOutputStream(path));
} catch (IOException e) {
throw new IllegalArgumentException("Invalid path passed, or something went very wrong", e);
}
String filter = options.valueOf(filterSpec);
if (filter != null) logger.info("Applying category filter: " + filter);
CarpetServer.settingsManager.dumpAllRulesToStream(outputStream, filter);
outputStream.close();
logger.info("Rules have been printed");
System.exit(0);
}
@Override
public void onPreLaunch() {
// Just initializes our OLD_OUT by nooping the class
}
}
+2 -1
View File
@@ -23,7 +23,8 @@
"carpet.CarpetServer::onGameStarted"
],
"server": [
"carpet.CarpetServer::onGameStarted"
"carpet.CarpetServer::onGameStarted",
"carpet.utils.CarpetRulePrinter"
]
},
"mixins": [