blog-software-java/src/main/java/eu/m724/blog/Main.java
Minecon724 6d58c7d232
Some checks are pending
/ build (push) Waiting to run
feat: overhaul blog system architecture
- Replace Git dependency in `Site` with `Path` for config files
- Refactor `Server` initialization and add browser-opening functionality
- Introduce `BlogBuilder` for blog building logic separation
- Simplify `Main` by delegating build and server logic to `BlogBuilder` and `Server`
- Add CLI enhancements with new options and improved readability

Signed-off-by: Minecon724 <git@m724.eu>
2025-02-08 12:44:53 +01:00

85 lines
No EOL
2.9 KiB
Java

package eu.m724.blog;
import org.apache.commons.cli.*;
import java.io.IOException;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Hello world!");
var commandLine = getCommandLine(args);
if (commandLine == null)
System.exit(2);
args = commandLine.getArgs();
var workingDirectory = Path.of(args[0]);
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;
/* Build process */
var start = System.nanoTime();
var builder = BlogBuilder.fromPath(workingDirectory)
.templateDirectory(templateDirectory)
.outputDirectory(outputDirectory)
.renderDrafts(renderDrafts);
builder.mkdirs(force);
builder.build();
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();
if (openBrowser) {
server.openBrowser();
}
}
}
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");
CommandLine commandLine;
try {
commandLine = new DefaultParser().parse(options, args);
if (commandLine.hasOption("help"))
throw new ParseException("Showing help");
if (commandLine.getArgs().length == 0)
throw new ParseException("Missing required argument: WORKDIR");
} catch (ParseException e) {
System.out.println(e.getMessage());
new HelpFormatter().printHelp("blog-software-java [OPTION]... [WORKDIR]", options);
return null;
}
return commandLine;
}
}