blog-software-java/src/main/java/eu/m724/blog/Main.java

85 lines
2.9 KiB
Java
Raw Normal View History

2025-01-07 11:41:30 +01:00
package eu.m724.blog;
2025-01-18 21:22:42 +01:00
import org.apache.commons.cli.*;
2025-01-07 11:41:30 +01:00
import java.io.IOException;
2025-01-08 18:40:11 +01:00
import java.nio.file.Path;
2025-01-07 11:41:30 +01:00
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Hello world!");
var commandLine = getCommandLine(args);
2025-01-20 17:34:01 +01:00
if (commandLine == null)
System.exit(2);
2025-01-10 18:50:12 +01:00
args = commandLine.getArgs();
2025-01-07 11:41:30 +01:00
var workingDirectory = Path.of(args[0]);
2025-01-18 21:22:42 +01:00
var templateDirectory = Path.of(commandLine.getOptionValue("output-dir", workingDirectory.resolve("template").toString()));
var outputDirectory = Path.of(commandLine.getOptionValue("output-dir", workingDirectory.resolve("generated_out").toString()));
var force = commandLine.hasOption("force");
var startServer = commandLine.hasOption("server");
var openBrowser = !commandLine.hasOption("no-browser");
var renderDrafts = commandLine.hasOption("draft") || startServer;
2025-01-07 11:41:30 +01:00
2025-01-08 18:40:11 +01:00
/* Build process */
2025-01-08 18:40:11 +01:00
var start = System.nanoTime();
2025-01-09 16:15:55 +01:00
var builder = BlogBuilder.fromPath(workingDirectory)
.templateDirectory(templateDirectory)
.outputDirectory(outputDirectory)
.renderDrafts(renderDrafts);
2025-01-10 18:50:12 +01:00
builder.mkdirs(force);
builder.build();
2025-01-10 18:50:12 +01:00
var end = System.nanoTime();
System.out.printf("Exported to %s (%.2f ms)\n", outputDirectory, (end - start) / 1000000.0);
/* Server process */
if (startServer) {
var server = new Server(outputDirectory);
server.start();
2025-01-10 18:50:12 +01:00
if (openBrowser) {
server.openBrowser();
2025-01-10 18:50:12 +01:00
}
}
2025-01-08 18:40:11 +01:00
}
private static CommandLine getCommandLine(String[] args) {
var options = new Options()
.addOption("h", "help", false, "Show help")
.addOption("f", "force", false, "Overwrite current build")
.addOption("t", "template-dir", true, "Template directory. Default: working directory/template")
.addOption("o", "output-dir", true, "Output directory. Default: working directory/generated_out")
.addOption("s", "server", false, "Run webserver. Default: no")
.addOption("no-browser", false, "Do NOT open browser on server start")
.addOption("d", "draft", false, "Render drafts. Default: only with server");
2025-01-10 14:43:24 +01:00
CommandLine commandLine;
try {
commandLine = new DefaultParser().parse(options, args);
2025-01-10 14:43:24 +01:00
if (commandLine.hasOption("help"))
throw new ParseException("Showing help");
2025-01-10 14:43:24 +01:00
if (commandLine.getArgs().length == 0)
throw new ParseException("Missing required argument: WORKDIR");
2025-01-08 18:40:11 +01:00
} catch (ParseException e) {
System.out.println(e.getMessage());
new HelpFormatter().printHelp("blog-software-java [OPTION]... [WORKDIR]", options);
2025-01-08 18:40:11 +01:00
return null;
2025-01-08 18:40:11 +01:00
}
return commandLine;
2025-01-07 11:41:30 +01:00
}
}