-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild.java
More file actions
156 lines (143 loc) · 6.37 KB
/
Build.java
File metadata and controls
156 lines (143 loc) · 6.37 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* Build: A command-line Java builder for SAS lexer/parser project.
*
* Usage:
* java Build [command]
*
* Commands:
* gen_lexer - Generate SASLexer.java via JFlex
* gen_parser - Generate SASParser.java & sym.java via CUP
* compile - Compile Java sources into bin/
* run - Execute Main on all .SAS test files inside tests/ (with ANSI colors)
* run_nc - Execute Main without ANSI color codes
* clean - Remove generated sources and bin/
* all - Run clean, gen_lexer, gen_parser, compile, run
* all_nc - Run clean, gen_lexer, gen_parser, compile, run_nc
* help - Print usage
* exit - Exit interactive mode
*/
public class Build {
private static final String JFLEX_JAR = "lib/jflex-full-1.9.1.jar";
private static final String CUP_JAR = "lib/java-cup-11b.jar";
private static final String BIN_DIR = "bin";
private static final String SRC_DIR = ".";
private static final String TEST_GLOB = "tests/*.SAS";
private static final Pattern ANSI_PATTERN = Pattern.compile("\\u001B\\[[;\\d]*m");
public static void main(String[] args) {
if (args.length == 0) {
interactiveMode();
} else {
dispatch(args[0].toLowerCase());
}
}
private static void interactiveMode() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Available commands: gen_lexer, gen_parser, compile, run, run_nc, clean, all, all_nc, help, exit");
System.out.print("Enter command: ");
String cmd = sc.nextLine().trim().toLowerCase();
if (cmd.equals("exit")) {
break;
}
if (cmd.isEmpty()) continue;
if (cmd.equals("help")) {
printUsage();
continue;
}
dispatch(cmd);
}
sc.close();
System.out.println("Goodbye.");
}
private static void dispatch(String cmd) {
switch (cmd) {
case "gen_lexer": genLexer(); break;
case "gen_parser": genParser(); break;
case "compile": compile(); break;
case "run": runTests(false); break;
case "run_nc": runTests(true); break;
case "clean": clean(); break;
case "all": all(false); break;
case "all_nc": all(true); break;
case "help": printUsage(); break;
default:
System.err.println("Unknown command: " + cmd);
printUsage();
}
}
private static void printUsage() {
System.out.println("Usage: java Build [command]");
System.out.println("Commands:");
System.out.println(" gen_lexer - Generate SASLexer.java via JFlex");
System.out.println(" gen_parser - Generate SASParser.java & sym.java via CUP");
System.out.println(" compile - Compile Java sources into bin/");
System.out.println(" run - Execute Main on all .SAS test files (ANSI colors)");
System.out.println(" run_nc - Execute Main without ANSI color codes");
System.out.println(" clean - Remove generated sources and binaries");
System.out.println(" all - Run clean, gen_lexer, gen_parser, compile, run");
System.out.println(" all_nc - Run clean, gen_lexer, gen_parser, compile, run_nc");
System.out.println(" help - Print this help message");
System.out.println(" exit - Exit interactive mode");
}
private static void clean() {
System.out.println("[1/5] Cleaning generated sources and binaries...");
exec(Arrays.asList(isWindows() ? "cmd" : "sh",
isWindows() ? "/c" : "-c",
isWindows() ? "del /Q SASLexer.java* SASParser.java sym.java && rmdir /S /Q " + BIN_DIR
: "rm -f SASLexer.java SASParser.java sym.java && rm -rf " + BIN_DIR), false);
}
private static void genLexer() {
System.out.println("[2/5] Generating SASLexer.java...");
exec(Arrays.asList("java", "-jar", JFLEX_JAR, SRC_DIR + "/SASLexer.jflex"), false);
}
private static void genParser() {
System.out.println("[3/5] Generating SASParser.java & sym.java...");
exec(Arrays.asList("java", "-jar", CUP_JAR, "-parser", "SASParser", "-symbols", "sym", SRC_DIR + "/SASParser.cup"), false);
}
private static void compile() {
System.out.println("[4/5] Compiling Java sources...");
new File(BIN_DIR).mkdirs();
exec(Arrays.asList("javac", "-classpath", "." + File.pathSeparator + CUP_JAR,
"-d", BIN_DIR,
"Main.java", "SASLexer.java", "SASParser.java", "sym.java"), false);
}
private static void runTests(boolean stripAnsi) {
System.out.println("[5/5] Running Main on all .SAS test files" + (stripAnsi ? " without color..." : "..."));
exec(Arrays.asList("java", "-classpath", BIN_DIR + File.pathSeparator + CUP_JAR, "Main", TEST_GLOB), stripAnsi);
}
private static void all(boolean stripAnsi) {
clean();
genLexer();
genParser();
compile();
runTests(stripAnsi);
}
private static void exec(List<String> command, boolean stripAnsi) {
ProcessBuilder pb = new ProcessBuilder(command).redirectErrorStream(true);
try {
Process p = pb.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = br.readLine()) != null) {
if (stripAnsi) {
line = ANSI_PATTERN.matcher(line).replaceAll("");
}
System.out.println(line);
}
}
} catch (IOException e) {
System.err.println("Error executing " + command + ": " + e.getMessage());
}
}
private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}