blob: 8a9b6da4fd182399ebc450334263adae61d5c775 (
plain)
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
|
package org.zwobble.couscous.cli;
import org.apache.commons.cli.*;
import org.zwobble.couscous.Backend;
import org.zwobble.couscous.ast.TypeNode;
import org.zwobble.couscous.backends.csharp.CsharpBackend;
import org.zwobble.couscous.backends.python.PythonBackend;
import org.zwobble.couscous.frontends.java.JavaFrontend;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.List;
import static java.util.Arrays.asList;
import static org.zwobble.couscous.util.ExtraLists.*;
public class CouscousCli {
public static final String SOURCEPATH = "sourcepath";
public static final String BACKEND = "backend";
public static final String OUTPUT = "output";
public static void main(String[] rawArguments) throws Exception {
CommandLine arguments = parseArguments(rawArguments);
JavaFrontend frontend = new JavaFrontend();
List<TypeNode> classNodes = eagerFlatMap(
arguments.getArgList(),
path -> {
try {
return frontend.readSourceDirectory(
eagerMap(sourcePath(arguments), sourcePath -> path(sourcePath)),
path(path));
} catch (IOException exception) {
throw new RuntimeException(exception);
}
});
Backend backend = backend(
arguments.getOptionValue(BACKEND),
arguments.getOptionValue(OUTPUT));
backend.compile(classNodes);
}
private static List<String> sourcePath(CommandLine arguments) {
if (arguments.hasOption(SOURCEPATH)) {
return asList(arguments.getOptionValues(SOURCEPATH));
} else {
return list();
}
}
private static Backend backend(String backend, String output) {
switch (backend) {
case "python":
return new PythonBackend(path(output), "_couscous");
case "csharp":
return new CsharpBackend(path(output), "_Couscous");
default:
throw new RuntimeException("Unrecognised backend: " + backend);
}
}
private static Path path(String output) {
return FileSystems.getDefault().getPath(output);
}
private static CommandLine parseArguments(String[] rawArguments) throws ParseException {
Options options = new Options();
options.addOption(requiredOption(BACKEND));
options.addOption(requiredOption(OUTPUT));
options.addOption(multipleOption(SOURCEPATH));
return new DefaultParser().parse(options, rawArguments);
}
private static Option requiredOption(String name) {
return Option.builder()
.longOpt(name)
.required()
.hasArg()
.build();
}
private static Option multipleOption(String name) {
return Option.builder()
.longOpt(name)
.hasArgs()
.build();
}
}
|