forked from stoicflame/enunciate
/
Main.java
309 lines (271 loc) · 9.45 KB
/
Main.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright 2006-2008 Web Cohesion
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.enunciate.main;
import java.io.File;
/**
* Class for invoking Enunciate from the command line.
*
* @author Ryan Heaton
*/
public class Main {
private Main() {
}
public static void main(String[] args) throws Exception {
Enunciate enunciate = new Enunciate();
if ((args == null) || (args.length < 1)) {
printUsage();
System.exit(1);
}
for (int argIndex = 0; argIndex < args.length; argIndex++) {
String option = args[argIndex];
//if it doesn't start with a '-', consider it (and the rest of the args) a source file...
if (!option.startsWith("-")) {
String[] sourceFiles = new String[args.length - argIndex];
System.arraycopy(args, argIndex, sourceFiles, 0, sourceFiles.length);
enunciate.setSourceFiles(sourceFiles);
break;
}
else {
option = option.startsWith("--") ? option.substring(2) : option.substring(1);
//first see if the option can be handled without a value.
boolean handled = false;
for (Option opt : Option.values()) {
handled = opt.handle(option, enunciate);
if (handled) {
break;
}
}
if (!handled) {
argIndex++;
if (args.length > argIndex) {
String value = args[argIndex];
for (Option opt : Option.values()) {
handled = opt.handle(option, value, enunciate);
if (handled) {
break;
}
}
}
else {
System.out.println("Unable to parse option: " + option + " (try specifying a value).");
printUsage();
System.exit(1);
}
}
if (!handled) {
System.out.println("Unknown option: " + option);
printUsage();
System.exit(1);
}
}
}
enunciate.execute();
}
public static void printUsage() {
System.out.println("Usage: <enunciate command> [options] [source files]");
System.out.println("Where possible options include:");
for (Option option : Option.values()) {
System.out.println(option.getHelpInfo());
}
}
public enum Option {
verbose("v", "Print verbose output to the console."),
debug("vv", "Print debug-level output to the console."),
disableDebugInfo("xg", "Disable compilation with debug info."),
javacCheck("Xc", "Do a javac check before invoking Enunciate."),
javacArguments("Xa", "argument", "Extra arguments to pass to javac. May be repeated."),
configFile("f", "file", "The enunciate xml config file."),
generateDir("g", "dir", "The output directory for the \"generate\" step."),
compileDir("c", "dir", "The output directory for the \"compile\" step."),
buildDir("b", "dir", "The output directory for the \"build\" step."),
packageDir("p", "dir", "The output directory for the \"package\" step."),
classpath("cp", "path", "The classpath to use (defaults to the system classpath)."),
target("t", "target", "The target step (defaults to \"package\"). Possible values: \"generate\", \"compile\", \"build\", \"package\"."),
export("E[artifactId]", "file or dir", "The file (or directory) to which to export the artifact identified by [artifactId]");
private final String id;
private final String valueName;
private final String description;
Option(String id, String description) {
this(id, null, description);
}
Option(String id, String valueName, String description) {
this.id = id;
this.valueName = valueName;
this.description = description;
}
/**
* The id of the option.
*
* @return The id of the option.
*/
public String getId() {
return id;
}
/**
* The name of the option.
*
* @return The name of the option.
*/
public String getName() {
return toString();
}
/**
* The name of the value.
*
* @return The name of the value.
*/
public String getValueName() {
return valueName;
}
/**
* The description of the option.
*
* @return The description of the option.
*/
public String getDescription() {
return description;
}
/**
* Print out the help information for this option.
*
* @return The help information.
*/
public String getHelpInfo() {
String optionFormat;
if (this == export) {
optionFormat = String.format("-%s %s", getId(), getValueName());
}
else {
optionFormat = this.valueName == null ? String.format("[%-3s| --%s]", "-" + getId(), getName())
: String.format("[%-3s| --%s] <%s>", "-" + getId(), getName(), getValueName());
}
return String.format(" %-30s %s", optionFormat, getDescription());
}
/**
* Handle the specified option (with no value).
*
* @param option The option to handle.
* @param enunciate The mechanism to manipulate.
* @return Whether the option was successfully handled without a value.
*/
public boolean handle(String option, Enunciate enunciate) {
if ((!getId().equals(option)) && (!getName().equals(option))) {
return false;
}
switch (this) {
case verbose:
enunciate.setVerbose(true);
return true;
case debug:
enunciate.setDebug(true);
return true;
case javacCheck:
enunciate.setJavacCheck(true);
return true;
case disableDebugInfo:
enunciate.setCompileDebugInfo(false);
return true;
default:
return false;
}
}
/**
* Handle the specified option with the specified value for the given
* enunciate mechanism.
*
* @param option The option to handle.
* @param value The value to handle.
* @param enunciate The mechansim to use.
* @return Whether the option was successfully handled with the specified value.
*/
public boolean handle(String option, String value, Enunciate enunciate) {
if ((this != export) && (!getId().equals(option)) && (!getName().equals(option))) {
return false;
}
switch (this) {
case configFile:
File file = new File(value);
if (!file.exists()) {
throw new IllegalArgumentException("File not found: " + file.getAbsolutePath());
}
enunciate.setConfigFile(file);
return true;
case generateDir:
File dir = new File(value);
if (!dir.exists()) {
throw new IllegalArgumentException("Generate directory not found: " + dir.getAbsolutePath());
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException("Generate directory " + dir.getAbsolutePath() + " is not a directory.");
}
enunciate.setGenerateDir(dir);
return true;
case compileDir:
dir = new File(value);
if (!dir.exists()) {
throw new IllegalArgumentException("Compile directory not found: " + dir.getAbsolutePath());
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException("Compile directory " + dir.getAbsolutePath() + " is not a directory.");
}
enunciate.setCompileDir(dir);
return true;
case buildDir:
dir = new File(value);
if (!dir.exists()) {
throw new IllegalArgumentException("Build directory not found: " + dir.getAbsolutePath());
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException("Build directory " + dir.getAbsolutePath() + " is not a directory.");
}
enunciate.setBuildDir(dir);
return true;
case packageDir:
dir = new File(value);
if (!dir.exists()) {
throw new IllegalArgumentException("Package directory not found: " + dir.getAbsolutePath());
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException("Package directory " + dir.getAbsolutePath() + " is not a directory.");
}
enunciate.setPackageDir(dir);
return true;
case classpath:
enunciate.setRuntimeClasspath(value);
return true;
case target:
try {
enunciate.setTarget(Enunciate.Target.valueOf(value.toUpperCase()));
}
catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unknown target: " + value);
}
return true;
case export:
if (!option.startsWith("E")) {
return false;
}
enunciate.addExport(option.substring(1), new File(value));
return true;
case javacArguments:
enunciate.getConfiguredJavacArguments().add(value);
return true;
default:
return false;
}
}
}
}