diff --git a/pom.xml b/pom.xml index 94b01e1..e1b36b2 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,21 @@ jar + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven-compiler-plugin-version} + + + + info.picocli + picocli-codegen + 4.2.0 + + + + org.apache.maven.plugins maven-assembly-plugin @@ -57,10 +72,11 @@ test - org.kohsuke.args4j - args4j-maven-plugin - 2.32 + info.picocli + picocli + 4.2.0 + com.github.rjeschke txtmark @@ -79,7 +95,7 @@ com.github.jknack handlebars - 2.2.1 + 4.1.2 org.zeroturnaround @@ -99,6 +115,7 @@ UTF-8 + 3.8.1 1.8 1.8 diff --git a/src/main/java/com/pawandubey/griffin/Griffin.java b/src/main/java/com/pawandubey/griffin/Griffin.java index 09cb15a..95962c4 100644 --- a/src/main/java/com/pawandubey/griffin/Griffin.java +++ b/src/main/java/com/pawandubey/griffin/Griffin.java @@ -15,15 +15,15 @@ */ package com.pawandubey.griffin; -import static com.pawandubey.griffin.Configurator.LINE_SEPARATOR; -import static com.pawandubey.griffin.Data.config; -import static com.pawandubey.griffin.Data.fileQueue; import com.pawandubey.griffin.cache.Cacher; -import com.pawandubey.griffin.cli.GriffinCommand; import com.pawandubey.griffin.cli.NewCommand; import com.pawandubey.griffin.cli.PreviewCommand; import com.pawandubey.griffin.cli.PublishCommand; import com.pawandubey.griffin.model.Parsable; +import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.ParameterException; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -32,47 +32,38 @@ import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileSystemException; import java.nio.file.Files; -import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentMap; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.CmdLineException; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.Option; -import org.kohsuke.args4j.ParserProperties; -import org.kohsuke.args4j.spi.BooleanOptionHandler; -import org.kohsuke.args4j.spi.SubCommand; -import org.kohsuke.args4j.spi.SubCommandHandler; -import org.kohsuke.args4j.spi.SubCommands; + +import static com.pawandubey.griffin.Configurator.LINE_SEPARATOR; +import static com.pawandubey.griffin.Data.config; +import static com.pawandubey.griffin.Data.fileQueue; /** * * @author Pawan Dubey pawandubey@outlook.com */ -public class Griffin { +@Command(name = "griffin", + version = "griffin 0.3.1", + mixinStandardHelpOptions = true, + synopsisSubcommandLabel = "COMMAND", + subcommands = { + NewCommand.class, + PublishCommand.class, + PreviewCommand.class + }, + description = "a simple and fast static site generator. ") +public class Griffin implements Runnable { private final DirectoryCrawler crawler; private Parser parser; private Cacher cacher; - @Option(name = "--version", aliases = {"-v"}, handler = BooleanOptionHandler.class, usage = "print the current version") - private boolean version = false; - - @Option(name = "--help", aliases = {"-h"}, handler = BooleanOptionHandler.class, usage = "print help message for the command") - private boolean help = false; - - @Argument(usage = "Execute subcommands", metaVar = "", handler = SubCommandHandler.class) - @SubCommands({ - @SubCommand(name = "new", impl = NewCommand.class), - @SubCommand(name = "publish", impl = PublishCommand.class), - @SubCommand(name = "preview", impl = PreviewCommand.class) - }) - public GriffinCommand commands; + @CommandLine.Spec + CommandLine.Model.CommandSpec spec; /** * Creates a new instance of Griffin @@ -174,31 +165,6 @@ public void preview(Integer port) { server.openBrowser(); } - private void printHelpMessage() { - String title = this.getClass().getPackage().getImplementationTitle(); - String ver = this.getClass().getPackage().getImplementationVersion(); - String author = this.getClass().getPackage().getImplementationVendor(); - String header = title + " version " + ver + " copyright " + author; - String desc = "a simple and fast static site generator"; - String usage = "usage: " + title + " [subcommand] [options..] [arguments...]"; - String moreHelp = "run " + title + " " + "--help to see more help about individual subcommands"; - - StringBuilder sb = new StringBuilder(); - sb.append(header).append(LINE_SEPARATOR); - if (this.version) { - System.out.println(sb.toString()); - } - else { - sb.append(desc) - .append(LINE_SEPARATOR) - .append(usage) - .append(LINE_SEPARATOR) - .append(moreHelp) - .append(LINE_SEPARATOR + LINE_SEPARATOR); - System.out.println(sb.toString()); - } - } - private void initializeConfigurationSettings(Path path, String name) throws NumberFormatException, IOException { String nam, tag, auth, src, out, date;// = config.getSiteName();//, String port;// = config.getPort(); @@ -269,7 +235,7 @@ public void printAsciiGriffin() { System.out.println(" \\_/__/ "); } - private void checkPathValidity(Path path, String name) throws FileSystemException, NotDirectoryException, FileAlreadyExistsException { + private void checkPathValidity(Path path, String name) throws FileSystemException { if (!Files.isWritable(path)) { System.out.println("That path doesn't seem to be writable :(" + LINE_SEPARATOR + "Check if you have write permission to that path and try again."); throw new java.nio.file.FileSystemException(path.toString()); @@ -284,29 +250,12 @@ private void checkPathValidity(Path path, String name) throws FileSystemExceptio } } - /** - * @param args the command line arguments - * @throws java.io.IOException the exception - * @throws java.lang.InterruptedException the exception - */ - public static void main(String[] args) throws IOException, InterruptedException { - try { - Griffin griffin = new Griffin(); - - CmdLineParser parser = new CmdLineParser(griffin, ParserProperties.defaults().withUsageWidth(120)); - parser.parseArgument(args); - - if (griffin.help || griffin.version || args.length == 0) { - griffin.printHelpMessage(); - parser.printUsage(System.out); - } - else { - griffin.commands.execute(); - } - } - catch (CmdLineException ex) { - Logger.getLogger(Griffin.class.getName()).log(Level.SEVERE, null, ex); - } + public static void main(String[] args) { + System.exit(new CommandLine(new Griffin()).execute(args)); } + @Override + public void run() { + throw new ParameterException(spec.commandLine(), "Missing required subcommand"); + } } diff --git a/src/main/java/com/pawandubey/griffin/InfoHandler.java b/src/main/java/com/pawandubey/griffin/InfoHandler.java index ceccbee..fe8363e 100644 --- a/src/main/java/com/pawandubey/griffin/InfoHandler.java +++ b/src/main/java/com/pawandubey/griffin/InfoHandler.java @@ -46,8 +46,12 @@ public class InfoHandler { public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd HH:mm:ss"); static String LAST_PARSE_DATE; - public InfoHandler() { - try (BufferedReader br = Files.newBufferedReader(Paths.get(DirectoryCrawler.INFO_FILE), + public InfoHandler() throws IOException { + final Path infoFilePath = Paths.get(DirectoryCrawler.INFO_FILE); + if (!Files.exists(infoFilePath)) { + throw new IOException(DirectoryCrawler.INFO_FILE + " doesn't exist"); + } + try (BufferedReader br = Files.newBufferedReader(infoFilePath, StandardCharsets.UTF_8)) { LAST_PARSE_DATE = br.readLine(); } diff --git a/src/main/java/com/pawandubey/griffin/cli/GriffinCommand.java b/src/main/java/com/pawandubey/griffin/cli/GriffinCommand.java deleted file mode 100644 index 96e696d..0000000 --- a/src/main/java/com/pawandubey/griffin/cli/GriffinCommand.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2015 Pawan Dubey pawandubey@outlook.com. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.pawandubey.griffin.cli; - -/** - * Provides an interface which needs to be implemented by all top level griffin - * subcommands. - * - * @author Pawan Dubey pawandubey@outlook.com - */ -public interface GriffinCommand { - public void execute(); -} diff --git a/src/main/java/com/pawandubey/griffin/cli/NewCommand.java b/src/main/java/com/pawandubey/griffin/cli/NewCommand.java index 9fb2aab..d91f5dd 100644 --- a/src/main/java/com/pawandubey/griffin/cli/NewCommand.java +++ b/src/main/java/com/pawandubey/griffin/cli/NewCommand.java @@ -15,62 +15,42 @@ */ package com.pawandubey.griffin.cli; -import static com.pawandubey.griffin.Configurator.LINE_SEPARATOR; import com.pawandubey.griffin.Griffin; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; + +import java.io.File; import java.io.IOException; import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; +import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.Option; -import org.kohsuke.args4j.ParserProperties; -import org.kohsuke.args4j.spi.BooleanOptionHandler; /** - * * @author Pawan Dubey pawandubey@outlook.com */ -public class NewCommand implements GriffinCommand { - @Argument(usage = "creates a new skeleton site at the given path", metaVar = "") - public List args = new ArrayList<>();// - - Path filePath; +@Command(name = "new", + description = "Scaffold out a new Griffin directory structure.") +public class NewCommand implements Callable { - @Option(name = "--help", aliases = {"-h"}, handler = BooleanOptionHandler.class, usage = "find help about this command") - private boolean help = false; + @Parameters(description = "creates a new skeleton site at the given path", paramLabel = "") + public File file; - @Option(name = "-name", aliases = {"-n"}, metaVar = "", usage = "name of the directory to be created") - private String name = "griffin"; + @Option(names = {"--name", "-n"}, paramLabel = "", description = "name of the directory to be created") + private String name = "griffin"; - /** - * Executes the command - */ - @Override - public void execute() { - try { + @Override + public Integer call() { + try { - if (help || args.isEmpty()) { - System.out.println("Scaffold out a new Griffin directory structure."); - System.out.println("usage: griffin new [option] "); - System.out.println("Options: " + LINE_SEPARATOR); - CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(120)); - parser.printUsage(System.out); - return; - } - else { - filePath = Paths.get(args.get(0)); - } - Griffin griffin = new Griffin(filePath.resolve(name)); - griffin.initialize(filePath, name); - System.out.println("Successfully created new site."); - } - catch (IOException | URISyntaxException ex) { - Logger.getLogger(NewCommand.class.getName()).log(Level.SEVERE, null, ex); - } - } + Griffin griffin = new Griffin(file.toPath().resolve(name)); + griffin.initialize(file.toPath(), name); + System.out.println("Successfully created new site."); + } catch (IOException | URISyntaxException ex) { + Logger.getLogger(NewCommand.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } + return 0; + } } diff --git a/src/main/java/com/pawandubey/griffin/cli/PreviewCommand.java b/src/main/java/com/pawandubey/griffin/cli/PreviewCommand.java index 865495c..554c7b8 100644 --- a/src/main/java/com/pawandubey/griffin/cli/PreviewCommand.java +++ b/src/main/java/com/pawandubey/griffin/cli/PreviewCommand.java @@ -15,44 +15,40 @@ */ package com.pawandubey.griffin.cli; -import static com.pawandubey.griffin.Configurator.LINE_SEPARATOR; -import static com.pawandubey.griffin.Data.config; import com.pawandubey.griffin.Griffin; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.Option; -import org.kohsuke.args4j.ParserProperties; -import org.kohsuke.args4j.spi.BooleanOptionHandler; -import org.kohsuke.args4j.spi.IntOptionHandler; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +import java.util.concurrent.Callable; +import java.util.logging.Level; +import java.util.logging.Logger; + +import static com.pawandubey.griffin.Data.config; /** * * @author Pawan Dubey pawandubey@outlook.com */ -public class PreviewCommand implements GriffinCommand { +@Command(name = "preview", mixinStandardHelpOptions = true, + description = "Preview the site on the given port: default: 9090") +public class PreviewCommand implements Callable { - @Option(name = "--port", aliases = {"-p"}, metaVar = "", handler = IntOptionHandler.class, usage = "Port on which to launch the preview. Default to your configuredData. port.") + @Option(names = {"--port", "-p"}, paramLabel = "", description = "Port on which to launch the preview. Default to your configuredData. port.") private Integer port = config.getPort(); - @Option(name = "--help", aliases = {"-h"}, handler = BooleanOptionHandler.class, usage = "find help about this command") - private boolean help = false; - - /** - * Executes the command - */ @Override - public void execute() { - Griffin griffin = new Griffin(); - if (help) { - System.out.println("Preview the site on the given port: default: 9090"); - System.out.println("usage: griffin preview [option]"); - System.out.println("Options: " + LINE_SEPARATOR); - CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(120)); - parser.printUsage(System.out); - } - else { + public Integer call() { + try { + Griffin griffin = new Griffin(); griffin.printAsciiGriffin(); System.out.println("Starting preview on port " + port); griffin.preview(port); } + catch (Exception ex) { + Logger.getLogger(PublishCommand.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } + + return 0; } } diff --git a/src/main/java/com/pawandubey/griffin/cli/PublishCommand.java b/src/main/java/com/pawandubey/griffin/cli/PublishCommand.java index 792ba9e..4c4de1d 100644 --- a/src/main/java/com/pawandubey/griffin/cli/PublishCommand.java +++ b/src/main/java/com/pawandubey/griffin/cli/PublishCommand.java @@ -15,49 +15,32 @@ */ package com.pawandubey.griffin.cli; -import static com.pawandubey.griffin.Configurator.LINE_SEPARATOR; import com.pawandubey.griffin.Griffin; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + import java.io.IOException; +import java.util.concurrent.Callable; import java.util.logging.Level; import java.util.logging.Logger; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.Option; -import org.kohsuke.args4j.ParserProperties; -import org.kohsuke.args4j.spi.BooleanOptionHandler; /** * * @author Pawan Dubey pawandubey@outlook.com */ -public class PublishCommand implements GriffinCommand { - - @Option(name = "--help", aliases = {"-h"}, handler = BooleanOptionHandler.class, usage = "find help about this command") - private boolean help = false; +@Command(name = "publish", mixinStandardHelpOptions = true, + description = "Publish the content in the current Griffin directory.") +public class PublishCommand implements Callable { - @Option(name = "--quick", aliases = {"-q"}, handler = BooleanOptionHandler.class, usage = "Publish only the files which have changed since the last modification." + @Option(names = {"--quick", "-q"}, description = "Publish only the files which have changed since the last modification." ) private Boolean fastParse = false; - @Option(name = "--rebuild", aliases = {"-r"}, handler = BooleanOptionHandler.class, usage = "Rebuild the site from scratch. This may take time for more number of posts.") + @Option(names = {"--rebuild", "-r"}, description = "Rebuild the site from scratch. This may take time for more number of posts.") private Boolean rebuild = false; - public PublishCommand() throws InterruptedException, IOException { - - } - - /** - * Executes the command - */ @Override - public void execute() { - if (help) { - System.out.println("Publish the content in the current Griffin directory."); - System.out.println("usage: griffin publish [option]"); - System.out.println("Options: " + LINE_SEPARATOR); - CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(120)); - parser.printUsage(System.out); - return; - } + public Integer call() { try { Griffin griffin = new Griffin(); griffin.printAsciiGriffin(); @@ -66,6 +49,8 @@ public void execute() { } catch (IOException | InterruptedException ex) { Logger.getLogger(PublishCommand.class.getName()).log(Level.SEVERE, null, ex); + return -1; } + return 0; } }