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

@@ -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
}
}