Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add picocli #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher -->
<version>${maven-compiler-plugin-version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>4.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down Expand Up @@ -57,10 +72,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kohsuke.args4j</groupId>
<artifactId>args4j-maven-plugin</artifactId>
<version>2.32</version>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.2.0</version>
</dependency>

<dependency>
<groupId>com.github.rjeschke</groupId>
<artifactId>txtmark</artifactId>
Expand All @@ -79,7 +95,7 @@
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>2.2.1</version>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
Expand All @@ -99,6 +115,7 @@
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin-version>3.8.1</maven-compiler-plugin-version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Expand Down
107 changes: 28 additions & 79 deletions src/main/java/com/pawandubey/griffin/Griffin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 [email protected]
*/
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 = "<commands>", 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
Expand Down Expand Up @@ -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 + " <subcommand> " + "--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();
Expand Down Expand Up @@ -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());
Expand All @@ -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");
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/pawandubey/griffin/InfoHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/com/pawandubey/griffin/cli/GriffinCommand.java

This file was deleted.

70 changes: 25 additions & 45 deletions src/main/java/com/pawandubey/griffin/cli/NewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]
*/
public class NewCommand implements GriffinCommand {
@Argument(usage = "creates a new skeleton site at the given path", metaVar = "<path>")
public List<String> args = new ArrayList<>();//

Path filePath;
@Command(name = "new",
description = "Scaffold out a new Griffin directory structure.")
public class NewCommand implements Callable<Integer> {

@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 = "<PATH>")
public File file;

@Option(name = "-name", aliases = {"-n"}, metaVar = "<folder_name>", usage = "name of the directory to be created")
private String name = "griffin";
@Option(names = {"--name", "-n"}, paramLabel = "<FOLDER_NAME>", 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] <path>");
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;
}
}
Loading