Skip to content

Commit 2702cce

Browse files
bitroncmaglie
authored andcommitted
Added main() and init() methods to BaseNoGui.
Now the the GUI and the rest of the code should be completely separated.
1 parent 4d3599b commit 2702cce

File tree

2 files changed

+176
-2
lines changed

2 files changed

+176
-2
lines changed

app/src/processing/app/BaseNoGui.java

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.Arrays;
1212
import java.util.Date;
1313
import java.util.HashMap;
14+
import java.util.LinkedList;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.logging.Level;
@@ -20,12 +21,15 @@
2021
import org.apache.commons.logging.impl.NoOpLog;
2122

2223
import cc.arduino.packages.DiscoveryManager;
24+
import cc.arduino.packages.Uploader;
2325

26+
import processing.app.debug.Compiler;
2427
import processing.app.debug.TargetBoard;
2528
import processing.app.debug.TargetPackage;
2629
import processing.app.debug.TargetPlatform;
2730
import processing.app.debug.TargetPlatformException;
2831
import processing.app.helpers.BasicUserNotifier;
32+
import processing.app.helpers.CommandlineParser;
2933
import processing.app.helpers.OSUtils;
3034
import processing.app.helpers.PreferencesMap;
3135
import processing.app.helpers.UserNotifier;
@@ -132,7 +136,7 @@ static public String getAvrBasePath() {
132136

133137
static public File getBuildFolder() {
134138
if (buildFolder == null) {
135-
String buildPath = Preferences.get("build.path");
139+
String buildPath = PreferencesData.get("build.path");
136140
if (buildPath != null) {
137141
buildFolder = absoluteFile(buildPath);
138142
if (!buildFolder.exists())
@@ -407,6 +411,159 @@ static public String[] headerListFromIncludePath(File path) throws IOException {
407411
return list;
408412
}
409413

414+
static public void init(String[] args) {
415+
getPlatform().init();
416+
417+
String sketchbookPath = getSketchbookPath();
418+
419+
// If no path is set, get the default sketchbook folder for this platform
420+
if (sketchbookPath == null) {
421+
if (BaseNoGui.getPortableFolder() != null)
422+
PreferencesData.set("sketchbook.path", getPortableSketchbookFolder());
423+
else
424+
showError(_("No sketchbook"), _("Sketchbook path not defined"), null);
425+
}
426+
427+
BaseNoGui.initPackages();
428+
429+
// Setup board-dependent variables.
430+
onBoardOrPortChange();
431+
432+
CommandlineParser parser = CommandlineParser.newCommandlineParser(args);
433+
434+
for (String path: parser.getFilenames()) {
435+
// Correctly resolve relative paths
436+
File file = absoluteFile(path);
437+
438+
// Fix a problem with systems that use a non-ASCII languages. Paths are
439+
// being passed in with 8.3 syntax, which makes the sketch loader code
440+
// unhappy, since the sketch folder naming doesn't match up correctly.
441+
// http://dev.processing.org/bugs/show_bug.cgi?id=1089
442+
if (OSUtils.isWindows()) {
443+
try {
444+
file = file.getCanonicalFile();
445+
} catch (IOException e) {
446+
e.printStackTrace();
447+
}
448+
}
449+
450+
if (!parser.isVerifyOrUploadMode() && !parser.isGetPrefMode())
451+
showError(_("Mode not supported"), _("Only --verify, --upload or --get-pref are supported"), null);
452+
453+
if (!parser.isForceSavePrefs())
454+
PreferencesData.setDoSave(false);
455+
if (!file.exists()) {
456+
String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path);
457+
// Open failure is fatal in upload/verify mode
458+
showError(null, mess, 2);
459+
}
460+
}
461+
462+
// Save the preferences. For GUI mode, this happens in the quit
463+
// handler, but for other modes we should also make sure to save
464+
// them.
465+
PreferencesData.save();
466+
467+
if (parser.isVerifyOrUploadMode()) {
468+
// Set verbosity for command line build
469+
PreferencesData.set("build.verbose", "" + parser.isDoVerboseBuild());
470+
PreferencesData.set("upload.verbose", "" + parser.isDoVerboseUpload());
471+
472+
// Make sure these verbosity preferences are only for the
473+
// current session
474+
PreferencesData.setDoSave(false);
475+
476+
if (parser.isUploadMode()) {
477+
478+
if (parser.getFilenames().size() != 1)
479+
{
480+
showError(_("Multiple files not supported"), _("Only one file at time suported with --upload option"), null);
481+
}
482+
483+
List<String> warningsAccumulator = new LinkedList<String>();
484+
boolean success = false;
485+
try {
486+
// costruttore di Editor carica lo sketch usando handleOpenInternal() che fa
487+
// la new di Sketch che chiama load() nel suo costruttore
488+
// In questo punto questo si traduce in:
489+
// SketchData data = new SketchData(file);
490+
// File tempBuildFolder = getBuildFolder();
491+
// data.load();
492+
SketchData data = new SketchData(absoluteFile(parser.getFilenames().get(0)));
493+
File tempBuildFolder = getBuildFolder();
494+
data.load();
495+
496+
// Sketch.exportApplet()
497+
// - chiama Sketch.prepare() che chiama Sketch.ensureExistence()
498+
// - chiama Sketch.build(verbose=false) che chiama Sketch.ensureExistence(), imposta il progressListener e chiama Compiler.build()
499+
// - chiama Sketch.upload() (cfr. dopo...)
500+
if (!data.getFolder().exists()) showError(_("No sketch"), _("Can't find the sketch in the specified path"), null);
501+
String suggestedClassName = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, false);
502+
if (suggestedClassName == null) showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), null);
503+
504+
// - chiama Sketch.upload() ... to be continued ...
505+
Uploader uploader = Compiler.getUploaderByPreferences();
506+
if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) showError(_("..."), _("..."), null);
507+
try {
508+
success = Compiler.upload(data, uploader, tempBuildFolder.getAbsolutePath(), suggestedClassName, false, warningsAccumulator);
509+
} finally {
510+
if (uploader.requiresAuthorization() && !success) {
511+
PreferencesData.remove(uploader.getAuthorizationKey());
512+
}
513+
}
514+
} catch (Exception e) {
515+
showError(_("Error while verifying/uploading"), _("An error occurred while verifying/uploading the sketch"), e);
516+
}
517+
for (String warning : warningsAccumulator) {
518+
System.out.print(_("Warning"));
519+
System.out.print(": ");
520+
System.out.println(warning);
521+
}
522+
if (!success) showError(_("Error while uploading"), _("An error occurred while uploading the sketch"), null);
523+
} else {
524+
525+
for (String path : parser.getFilenames())
526+
{
527+
try {
528+
// costruttore di Editor carica lo sketch usando handleOpenInternal() che fa
529+
// la new di Sketch che chiama load() nel suo costruttore
530+
// In questo punto questo si traduce in:
531+
// SketchData data = new SketchData(file);
532+
// File tempBuildFolder = getBuildFolder();
533+
// data.load();
534+
SketchData data = new SketchData(absoluteFile(path));
535+
File tempBuildFolder = getBuildFolder();
536+
data.load();
537+
538+
// metodo Sketch.prepare() chiama Sketch.ensureExistence()
539+
// Sketch.build(verbose) chiama Sketch.ensureExistence() e poi imposta il progressListener e, finalmente, chiama Compiler.build()
540+
// In questo punto questo si traduce in:
541+
// if (!data.getFolder().exists()) showError(...);
542+
// String ... = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, verbose);
543+
if (!data.getFolder().exists()) showError(_("No sketch"), _("Can't find the sketch in the specified path"), null);
544+
String suggestedClassName = Compiler.build(data, tempBuildFolder.getAbsolutePath(), tempBuildFolder, null, parser.isDoVerboseBuild());
545+
if (suggestedClassName == null) showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), null);
546+
} catch (Exception e) {
547+
showError(_("Error while verifying"), _("An error occurred while verifying the sketch"), e);
548+
}
549+
}
550+
551+
}
552+
553+
// No errors exit gracefully
554+
System.exit(0);
555+
}
556+
else if (parser.isGetPrefMode()) {
557+
String value = PreferencesData.get(parser.getGetPref(), null);
558+
if (value != null) {
559+
System.out.println(value);
560+
System.exit(0);
561+
} else {
562+
System.exit(4);
563+
}
564+
}
565+
}
566+
410567
static public void initLogger() {
411568
System.setProperty(LogFactoryImpl.LOG_PROPERTY, NoOpLog.class.getCanonicalName());
412569
Logger.getLogger("javax.jmdns").setLevel(Level.OFF);
@@ -509,6 +666,18 @@ static public String loadFile(File file) throws IOException {
509666
return PApplet.join(contents, "\n");
510667
}
511668

669+
static public void main(String args[]) throws Exception {
670+
initPlatform();
671+
672+
initPortableFolder();
673+
674+
PreferencesData.init(null);
675+
676+
prescanParameters(args);
677+
678+
init(args);
679+
}
680+
512681
static public void onBoardOrPortChange() {
513682
TargetPlatform targetPlatform = getTargetPlatform();
514683
if (targetPlatform == null)

app/src/processing/app/debug/Compiler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ protected String buildPrefsString() {
229229
}
230230

231231
protected void setProgressListener(ProgressListener _progressListener) {
232-
progressListener = _progressListener;
232+
progressListener = (_progressListener == null ?
233+
new ProgressListener() {
234+
@Override
235+
public void progress(int percent) {
236+
}
237+
} : _progressListener);
233238
}
234239

235240
/**

0 commit comments

Comments
 (0)