aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/glavo/javah/Main.java
blob: 1229520086ce649ded6b2dcc43a3f42bcd90b3f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 {
    @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<String> classes;

    public static void main(String[] args) throws Exception {
        Main m = new Main();
        CommandLine cm = new CommandLine(m);
        if (args == null || args.length == 0) {
            cm.usage(System.err);
            System.exit(-1);
        }

        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);
    }
}