From e48c0668029849af07d1966729c5b703e89b1d00 Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 6 Dec 2019 03:42:00 +0800 Subject: update --- src/main/java/org/glavo/javah/Main.java | 142 +++++++++++++++++++------------- 1 file changed, 87 insertions(+), 55 deletions(-) (limited to 'src/main/java/org/glavo/javah/Main.java') diff --git a/src/main/java/org/glavo/javah/Main.java b/src/main/java/org/glavo/javah/Main.java index 6df9539..1229520 100644 --- a/src/main/java/org/glavo/javah/Main.java +++ b/src/main/java/org/glavo/javah/Main.java @@ -1,71 +1,103 @@ package org.glavo.javah; +import picocli.CommandLine; + +import java.io.File; import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; import java.util.jar.Manifest; +import java.util.stream.Stream; + +import static picocli.CommandLine.*; +@Command(name = "gjavah", version = "gjavah %1", sortOptions = false) public class Main { - private static final String HELP_MESSAGE = - "usage:\n" + - " gjavah [options] \n" + - "where [options] include:\n" + - " -o Output file (only one of -d or -o may be used)\n" + - " -d Output directory\n" + - " --module-path Path from which to search modules\n" + - " --class-path | " + - "-classpath | -cp \n" + - " " + - " Path from which to search classes\n" + - " --version Print version information\n" + - " -h --help -? Print this message"; + @Option(names = {"-p", "--module-path"}, description = "Path from which to search modules") + private String modulePath; + @Option(names = {"-cp", "-classpath", "--classpath", "--class-path"}, description = "Path from which to search classes") + private String classpath; + + @Option(names = {"-version", "--version"}, description = "Print version information") + private boolean showVersion; + + @Option(names = {"-h", "--help", "-?"}, usageHelp = true, description = "Print this message") + private boolean showHelp; + + @Option(names = {"-d"}, description = "Output directory") + private Path outputDir = Paths.get("").toAbsolutePath().normalize(); + + @Parameters(paramLabel = "classes") + private List classes; public static void main(String[] args) throws Exception { - if (args.length == 0) { - System.err.println(HELP_MESSAGE); + Main m = new Main(); + CommandLine cm = new CommandLine(m); + if (args == null || args.length == 0) { + cm.usage(System.err); System.exit(-1); } - JavahTask task = new JavahTask(); - task.addRuntimeClasspath(); - loop: - for (int i = 0; i < args.length; i++) { - String arg = args[i]; - switch (arg) { - case "--version": - case "-version": - try (InputStream in = Main.class.getResourceAsStream("/META-INF/MANIFEST.MF")) { - Manifest manifest = new Manifest(in); - System.out.println("gjavah version: " + manifest.getMainAttributes().getValue("GJavah-Version")); - } - return; - case "-h": - case "--help": - case "-help": - case "-?": - System.out.println(HELP_MESSAGE); - return; - case "-o": - task.setOutputToSignalFile(true); - task.setOutputPath(Paths.get(args[++i])); - break; - case "-d": - task.setOutputToSignalFile(false); - task.setOutputPath(Paths.get(args[++i])); - break; - case "--module-path": - task.addModulePaths(args[++i]); - break; - case "--class-path": - case "--classpath": - case "-classpath": - case "-cp": - task.addClasspaths(args[++i]); - break; - default: - while (i < args.length) { - task.addClass(args[i++]); - } + + cm.parseArgs(args); + System.out.println(m); + if (m.showHelp) { + cm.usage(System.out); + return; + } + if (m.showVersion) { + try (InputStream in = Main.class.getResourceAsStream("/META-INF/MANIFEST.MF")) { + cm.printVersionHelp(System.out, Help.Ansi.AUTO, new Manifest(in).getMainAttributes().getValue("GJavah-Version")); } + return; } + if (m.classes == null || m.classes.isEmpty()) { + cm.usage(System.err); + System.exit(-1); + } + + JavahTask task = new JavahTask(); + if (m.modulePath != null) { + Arrays.stream(m.modulePath.split(File.pathSeparator)) + .map(Paths::get) + .filter(Files::isDirectory) + .forEachOrdered(task::addModulePath); + } + if (m.classpath == null) { + m.classpath = System.getenv("CLASSPATH"); + } + if (m.classpath == null) { + m.classpath = Paths.get("").toAbsolutePath().normalize().toString(); + } + Arrays.stream(m.classpath.split(File.pathSeparator)) + .flatMap(p -> { + if (p.endsWith("/*") || p.equals("*")) { + try { + return Files.list(Paths.get(p.substring(0, p.length() - 1))) + .filter(Files::isRegularFile) + .filter(t -> t.toAbsolutePath().getFileName().toString().toLowerCase().endsWith(".jar")); + } catch (Exception e) { + return Stream.empty(); + } + } + return Stream.of(Paths.get(p)); + }) + .filter(Files::exists) + .map(Path::toAbsolutePath) + .forEachOrdered(task::addClassPath); + task.setOutputDir(m.outputDir); + task.addClasses(m.classes); + task.setErrorHandle(new PrintWriter(System.err, true)); + task.addRuntimeSearchPath(); task.run(); } + + @Override + public String toString() { + return String.format("Main[modulePath='%s', classpath='%s', showVersion=%s, showHelp=%s, outputDir=%s, classes=%s]", modulePath, classpath, showVersion, showHelp, outputDir, classes); + } } -- cgit v1.2.3