From 061d1f14b6f67ed308c5335e445afcee0fd23423 Mon Sep 17 00:00:00 2001 From: Claudio Indellicati Date: Mon, 25 Aug 2014 13:10:53 +0200 Subject: [PATCH] Moved the sketch uploading code from Sketch to Compiler. --- app/src/processing/app/Sketch.java | 12 +---- app/src/processing/app/debug/Compiler.java | 51 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 49bc21191..059860132 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -23,13 +23,10 @@ package processing.app; -import cc.arduino.packages.BoardPort; -import cc.arduino.packages.UploaderFactory; import cc.arduino.packages.Uploader; import processing.app.debug.Compiler; import processing.app.debug.Compiler.ProgressListener; import processing.app.debug.RunnerException; -import processing.app.debug.TargetPlatform; import processing.app.forms.PasswordAuthorizationDialog; import processing.app.helpers.OSUtils; import processing.app.packages.Library; @@ -1193,12 +1190,7 @@ public class Sketch { protected boolean upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws Exception { - TargetPlatform target = Base.getTargetPlatform(); - String board = Preferences.get("board"); - - BoardPort boardPort = Base.getDiscoveryManager().find(Preferences.get("serial.port")); - - Uploader uploader = new UploaderFactory().newUploader(target.getBoards().get(board), boardPort); + Uploader uploader = Compiler.getUploaderByPreferences(); boolean success = false; do { @@ -1217,7 +1209,7 @@ public class Sketch { List warningsAccumulator = new LinkedList(); try { - success = uploader.uploadUsingPreferences(getFolder(), buildPath, suggestedClassName, usingProgrammer, warningsAccumulator); + success = Compiler.upload(data, uploader, buildPath, suggestedClassName, usingProgrammer, warningsAccumulator); } finally { if (uploader.requiresAuthorization() && !success) { Preferences.remove(uploader.getAuthorizationKey()); diff --git a/app/src/processing/app/debug/Compiler.java b/app/src/processing/app/debug/Compiler.java index d11cf9c87..7c4341a3b 100644 --- a/app/src/processing/app/debug/Compiler.java +++ b/app/src/processing/app/debug/Compiler.java @@ -34,11 +34,16 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; +import cc.arduino.packages.BoardPort; +import cc.arduino.packages.Uploader; +import cc.arduino.packages.UploaderFactory; + import processing.app.BaseNoGui; import processing.app.I18n; import processing.app.PreferencesData; @@ -117,6 +122,52 @@ public class Compiler implements MessageConsumer { return null; } + static public Uploader getUploaderByPreferences() { + TargetPlatform target = BaseNoGui.getTargetPlatform(); + String board = PreferencesData.get("board"); + + BoardPort boardPort = BaseNoGui.getDiscoveryManager().find(PreferencesData.get("serial.port")); + + return new UploaderFactory().newUploader(target.getBoards().get(board), boardPort); + } + + static public boolean upload(SketchData data, Uploader uploader, String buildPath, String suggestedClassName, boolean usingProgrammer, List warningsAccumulator) throws Exception { + + if (uploader == null) + uploader = getUploaderByPreferences(); + + boolean success = false; + + if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) { + BaseNoGui.showError(_("Authorization required"), + _("No athorization data found"), null); + } + + boolean useNewWarningsAccumulator = false; + if (warningsAccumulator == null) { + warningsAccumulator = new LinkedList(); + useNewWarningsAccumulator = true; + } + + try { + success = uploader.uploadUsingPreferences(data.getFolder(), buildPath, suggestedClassName, usingProgrammer, warningsAccumulator); + } finally { + if (uploader.requiresAuthorization() && !success) { + PreferencesData.remove(uploader.getAuthorizationKey()); + } + } + + if (useNewWarningsAccumulator) { + for (String warning : warningsAccumulator) { + System.out.print(_("Warning")); + System.out.print(": "); + System.out.println(warning); + } + } + + return success; + } + /** * Create a new Compiler * @param _sketch Sketch object to be compiled.