forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommander.java
More file actions
357 lines (303 loc) · 11.8 KB
/
Commander.java
File metadata and controls
357 lines (303 loc) · 11.8 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-16 The Processing Foundation
Copyright (c) 2008-12 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.mode.android;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import processing.app.Base;
import processing.app.Platform;
import processing.app.Preferences;
import processing.app.RunnerListener;
import processing.app.Sketch;
import processing.app.SketchException;
import processing.app.Util;
import processing.app.contrib.ModeContribution;
/**
* Class to handle running Android mode of Processing from the command line.
*
* @author ostap.andrusiv
*/
public class Commander implements RunnerListener {
static final String helpArg = "--help";
static final String buildArg = "--build";
static final String runArg = "--run";
static final String runArg_DEVICE = "d";
static final String runArg_EMULATOR = "e";
static final String targetArg = "--target";
static final String targetArg_DEBUG = "debug";
static final String targetArg_RELEASE = "release";
static final String componentArg = "--component";
static final String targetArg_FRAGMENT = "fragment";
static final String targetArg_WALLPAPER = "wallpaper";
static final String targetArg_WATCHFACE = "watchface";
static final String targetArg_VR = "vr";
static final String sketchArg = "--sketch=";
static final String forceArg = "--force";
static final String outputArg = "--output=";
static final String exportApplicationArg = "--export";
static final int HELP = -1;
static final int BUILD = 1;
static final int RUN = 2;
static final int EXPORT = 4;
private AndroidMode androidMode = null;
private int task = HELP;
private Sketch sketch;
private PrintStream systemOut;
private PrintStream systemErr;
private String sketchPath = null;
private File sketchFolder = null;
private String pdePath = null; // path to the .pde file
private String outputPath = null;
private File outputFolder = null;
private int appComponent = AndroidBuild.FRAGMENT;
private boolean force = false; // replace that no good output folder
private String device = runArg_DEVICE;
private String target = targetArg_DEBUG;
static public void main(String[] args) {
// Do this early so that error messages go to the console
Base.setCommandLine();
// init the platform so that prefs and other native code is ready to go
Platform.init();
// launch command line handler
Commander commander = new Commander(args);
commander.execute();
}
public Commander(String[] args) {
if (processing.app.Base.DEBUG) {
System.out.println(Arrays.toString(args));
}
// Turns out the output goes as MacRoman or something else useless.
// http://code.google.com/p/processing/issues/detail?id=1418
try {
systemOut = new PrintStream(System.out, true, "UTF-8");
systemErr = new PrintStream(System.err, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.exit(1);
}
parseArgs(args);
initValues();
}
private void parseArgs(String[] args) {
for (String arg : args) {
if (arg.length() == 0) {
// ignore it, just the crappy shell script
} else if (arg.equals(helpArg)) {
// mode already set to HELP
} else if (arg.startsWith(targetArg)) {
target = extractValue(arg, targetArg, targetArg_DEBUG);
} else if (arg.startsWith(componentArg)) {
String compStr = extractValue(arg, targetArg, targetArg_FRAGMENT);
if (compStr.equals(targetArg_FRAGMENT)) {
appComponent = AndroidBuild.FRAGMENT;
} else if (compStr.equals(targetArg_WALLPAPER)) {
appComponent = AndroidBuild.WALLPAPER;
} else if (compStr.equals(targetArg_WATCHFACE)) {
appComponent = AndroidBuild.WATCHFACE;
} else if (compStr.equals(targetArg_VR)) {
appComponent = AndroidBuild.VR;
}
} else if (arg.equals(buildArg)) {
task = BUILD;
} else if (arg.startsWith(runArg)) {
task = RUN;
device = extractValue(arg, runArg, runArg_DEVICE);
} else if (arg.equals(exportApplicationArg)) {
task = EXPORT;
} else if (arg.startsWith(sketchArg)) {
sketchPath = arg.substring(sketchArg.length());
sketchFolder = new File(sketchPath);
checkOrQuit(sketchFolder.exists(), sketchFolder + " does not exist.", false);
File pdeFile = new File(sketchFolder, sketchFolder.getName() + ".pde");
checkOrQuit(pdeFile.exists(), "Not a valid sketch folder. " + pdeFile + " does not exist.", true);
pdePath = pdeFile.getAbsolutePath();
} else if (arg.startsWith(outputArg)) {
outputPath = arg.substring(outputArg.length());
} else if (arg.equals(forceArg)) {
force = true;
} else {
complainAndQuit("I don't know anything about " + arg + ".", true);
}
}
}
/**
* <pre>
* extractValue("--target=release", "--target", "debug") ==> "release"
* extractValue("--target=", "--target", "debug") ==> ""
* extractValue("--target", "--target", "debug") ==> "debug"
* </pre>
*
* @param arg
* @param template
* @param def
* @return
*/
private static String extractValue(String arg, String template, String def) {
String result = def;
String withEq = arg.substring(template.length());
if (withEq.startsWith("=")) {
result = withEq.substring(1);
}
return result;
}
private void initValues() {
checkOrQuit(outputPath != null, "An output path must be specified.", true);
outputFolder = new File(outputPath);
if (outputFolder.exists()) {
if (force) {
Util.removeDir(outputFolder);
} else {
complainAndQuit("The output folder already exists. " + "Use --force to remove it.", false);
}
}
Preferences.init();
Base.locateSketchbookFolder();
checkOrQuit(sketchPath != null, "No sketch path specified.", true);
checkOrQuit(!outputPath.equals(sketchPath), "The sketch path and output path cannot be identical.", false);
androidMode = (AndroidMode) ModeContribution.load(null, Platform.getContentFile("modes/android"),
"processing.mode.android.AndroidMode").getMode();
androidMode.checkSDK(null);
}
private void execute() {
if (processing.app.Base.DEBUG) {
systemOut.println("Build status: ");
systemOut.println("Sketch: " + sketchPath);
systemOut.println("Output: " + outputPath);
systemOut.println("Force: " + force);
systemOut.println("Target: " + target);
systemOut.println("Component: " + appComponent);
systemOut.println("==== Task ====");
systemOut.println("--build: " + (task == BUILD));
systemOut.println("--run: " + (task == RUN));
systemOut.println("--export: " + (task == EXPORT));
systemOut.println();
}
if (task == HELP) {
printCommandLine(systemOut);
System.exit(0);
}
checkOrQuit(outputFolder.mkdirs(), "Could not create the output folder.", false);
boolean success = false;
try {
boolean runOnEmu = runArg_EMULATOR.equals(device);
sketch = new Sketch(pdePath, androidMode);
if (task == BUILD || task == RUN) {
AndroidBuild build = new AndroidBuild(sketch, androidMode, appComponent, runOnEmu);
build.build(target);
if (task == RUN) {
AndroidRunner runner = new AndroidRunner(build, this);
runner.launch(runOnEmu ?
Devices.getInstance().getEmulator(build.isWear(), build.usesOpenGL()) :
Devices.getInstance().getHardware(), build.isWear());
}
success = true;
} else if (task == EXPORT) {
AndroidBuild build = new AndroidBuild(sketch, androidMode, appComponent, false);
build.exportProject();
success = true;
}
if (!success) { // error already printed
System.exit(1);
}
systemOut.println("Finished.");
System.exit(0);
} catch (SketchException re) {
statusError(re);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void statusNotice(String message) {
systemErr.println(message);
}
public void statusError(String message) {
systemErr.println(message);
}
public void statusError(Exception exception) {
if (exception instanceof SketchException) {
SketchException re = (SketchException) exception;
int codeIndex = re.getCodeIndex();
if (codeIndex != -1) {
// format the runner exception like emacs
// blah.java:2:10:2:13: Syntax Error: This is a big error message
String filename = sketch.getCode(codeIndex).getFileName();
int line = re.getCodeLine() + 1;
int column = re.getCodeColumn() + 1;
// if (column == -1) column = 0;
// TODO if column not specified, should just select the whole line.
systemErr.println(filename + ":" + line + ":" + column + ":" + line + ":" + column + ":" + " "
+ re.getMessage());
} else { // no line number, pass the trace along to the user
exception.printStackTrace();
}
} else {
exception.printStackTrace();
}
}
private void checkOrQuit(boolean condition, String lastWords, boolean schoolEmFirst) {
if (!condition) {
complainAndQuit(lastWords, schoolEmFirst);
}
}
void complainAndQuit(String lastWords, boolean schoolEmFirst) {
if (schoolEmFirst) {
printCommandLine(systemErr);
}
systemErr.println(lastWords);
System.exit(1);
}
static void printCommandLine(PrintStream out) {
out.println();
out.println("Command line edition for Processing " + Base.getVersionName() + " (Android Mode)");
out.println();
out.println("--help Show this help text. Congratulations.");
out.println();
out.println("--sketch=<name> Specify the sketch folder (required)");
out.println("--output=<name> Specify the output folder (required and");
out.println(" cannot be the same as the sketch folder.)");
out.println();
out.println("--force The sketch will not build if the output");
out.println(" folder already exists, because the contents");
out.println(" will be replaced. This option erases the");
out.println(" folder first. Use with extreme caution!");
out.println();
out.println("--target=<target> \"debug\" or \"release\" target.");
out.println(" \"debug\" by default.");
out.println("--build Preprocess and compile a sketch into .apk file.");
out.println("--run=<d|e> Preprocess, compile, and run a sketch on device");
out.println(" or emulator. Device will be used by default.");
out.println();
out.println("--export Export an application.");
out.println();
}
@Override
public void startIndeterminate() {
}
@Override
public void stopIndeterminate() {
}
@Override
public void statusHalt() {
}
@Override
public boolean isHalted() {
return false;
}
}