From 7776ffac4f3165854559bbfb0c5c800db9988cbc Mon Sep 17 00:00:00 2001 From: Claudio Indellicati Date: Thu, 21 Aug 2014 13:18:54 +0200 Subject: [PATCH] Moved packages, currentDirectory and portableFolder (and related methods) from Base to BaseNoGui (work in progress). --- .../discoverers/NetworkDiscovery.java | 4 +- .../packages/discoverers/SerialDiscovery.java | 4 +- app/src/processing/app/Base.java | 103 ++++----------- app/src/processing/app/BaseNoGui.java | 117 +++++++++++++++++- app/src/processing/app/debug/Compiler.java | 4 +- 5 files changed, 149 insertions(+), 83 deletions(-) diff --git a/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java b/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java index 835d93158..2674a52ba 100644 --- a/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java +++ b/app/src/cc/arduino/packages/discoverers/NetworkDiscovery.java @@ -32,7 +32,7 @@ package cc.arduino.packages.discoverers; import cc.arduino.packages.BoardPort; import cc.arduino.packages.Discovery; import cc.arduino.packages.discoverers.network.*; -import processing.app.Base; +import processing.app.BaseNoGui; import processing.app.helpers.NetUtils; import processing.app.helpers.PreferencesMap; import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter; @@ -140,7 +140,7 @@ public class NetworkDiscovery implements Discovery, ServiceListener, cc.arduino. String label = name + " at " + address; if (board != null) { - String boardName = Base.getPlatform().resolveDeviceByBoardID(Base.packages, board); + String boardName = Base.getPlatform().resolveDeviceByBoardID(BaseNoGui.packages, board); label += " (" + boardName + ")"; } diff --git a/app/src/cc/arduino/packages/discoverers/SerialDiscovery.java b/app/src/cc/arduino/packages/discoverers/SerialDiscovery.java index a5f6d4f92..9cec93e91 100644 --- a/app/src/cc/arduino/packages/discoverers/SerialDiscovery.java +++ b/app/src/cc/arduino/packages/discoverers/SerialDiscovery.java @@ -32,7 +32,7 @@ package cc.arduino.packages.discoverers; import java.util.ArrayList; import java.util.List; -import processing.app.Base; +import processing.app.BaseNoGui; import processing.app.Platform; import processing.app.Serial; import processing.app.helpers.PreferencesMap; @@ -51,7 +51,7 @@ public class SerialDiscovery implements Discovery { List ports = Serial.list(); for (String port : ports) { - String boardName = os.resolveDeviceAttachedTo(port, Base.packages, devicesListOutput); + String boardName = os.resolveDeviceAttachedTo(port, BaseNoGui.packages, devicesListOutput); String label = port; if (boardName != null) label += " (" + boardName + ")"; diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 7fd113868..39b787305 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -40,7 +40,6 @@ import cc.arduino.packages.DiscoveryManager; import processing.app.debug.TargetBoard; import processing.app.debug.TargetPackage; import processing.app.debug.TargetPlatform; -import processing.app.debug.TargetPlatformException; import processing.app.helpers.FileUtils; import processing.app.helpers.OSUtils; import processing.app.helpers.PreferencesMap; @@ -100,14 +99,12 @@ public class Base { // found in the sketchbook) static public String librariesClassPath; - static public Map packages; - // Location for untitled items static File untitledFolder; // Current directory to use for relative paths specified on the // commandline - static String currentDirectory = System.getProperty("user.dir"); + static String currentDirectory = BaseNoGui.currentDirectory; // p5 icon for the window // static Image icon; @@ -116,7 +113,6 @@ public class Base { List editors = Collections.synchronizedList(new ArrayList()); Editor activeEditor; - static File portableFolder = null; static final String portableSketchbookFolder = "sketchbook"; static public void main(String args[]) throws Exception { @@ -125,10 +121,7 @@ public class Base { initPlatform(); - // Portable folder - portableFolder = getContentFile("portable"); - if (!portableFolder.exists()) - portableFolder = null; + BaseNoGui.initPortableFolder(); String preferencesFile = null; @@ -283,13 +276,7 @@ public class Base { // directory when starting the IDE (which is not the same as the // current working directory!). static public File absoluteFile(String path) { - if (path == null) return null; - - File file = new File(path); - if (!file.isAbsolute()) { - file = new File(currentDirectory, path); - } - return file; + return BaseNoGui.absoluteFile(path); } protected static enum ACTION { GUI, NOOP, VERIFY, UPLOAD, GET_PREF }; @@ -304,8 +291,8 @@ public class Base { // If it doesn't, warn the user that the sketchbook folder is being reset. if (sketchbookPath != null) { File sketchbookFolder; - if (portableFolder != null) - sketchbookFolder = new File(portableFolder, sketchbookPath); + if (BaseNoGui.getPortableFolder() != null) + sketchbookFolder = new File(BaseNoGui.getPortableFolder(), sketchbookPath); else sketchbookFolder = Base.absoluteFile(sketchbookPath); if (!sketchbookFolder.exists()) { @@ -322,7 +309,7 @@ public class Base { // If no path is set, get the default sketchbook folder for this platform if (sketchbookPath == null) { File defaultFolder = getDefaultSketchbookFolder(); - if (portableFolder != null) + if (BaseNoGui.getPortableFolder() != null) Preferences.set("sketchbook.path", portableSketchbookFolder); else Preferences.set("sketchbook.path", defaultFolder.getAbsolutePath()); @@ -331,13 +318,7 @@ public class Base { } } - packages = new HashMap(); - loadHardware(getHardwareFolder()); - loadHardware(getSketchbookHardwareFolder()); - if (packages.size() == 0) { - System.out.println(_("No valid configured cores found! Exiting...")); - System.exit(3); - } + BaseNoGui.initPackages(); // Setup board-dependent variables. onBoardOrPortChange(); @@ -643,8 +624,8 @@ public class Base { int opened = 0; for (int i = 0; i < count; i++) { String path = Preferences.get("last.sketch" + i + ".path"); - if (portableFolder != null) { - File absolute = new File(portableFolder, path); + if (BaseNoGui.getPortableFolder() != null) { + File absolute = new File(BaseNoGui.getPortableFolder(), path); try { path = absolute.getCanonicalPath(); } catch (IOException e) { @@ -689,8 +670,8 @@ public class Base { !editor.getSketch().isModified()) { continue; } - if (portableFolder != null) { - path = FileUtils.relativePath(portableFolder.toString(), path); + if (BaseNoGui.getPortableFolder() != null) { + path = FileUtils.relativePath(BaseNoGui.getPortableFolder().toString(), path); if (path == null) continue; } @@ -713,8 +694,8 @@ public class Base { if (path.startsWith(untitledPath)) { path = ""; } else - if (portableFolder != null) { - path = FileUtils.relativePath(portableFolder.toString(), path); + if (BaseNoGui.getPortableFolder() != null) { + path = FileUtils.relativePath(BaseNoGui.getPortableFolder().toString(), path); if (path == null) path = ""; } @@ -1456,7 +1437,7 @@ public class Base { // Generate custom menus for all platforms Set titles = new HashSet(); - for (TargetPackage targetPackage : packages.values()) { + for (TargetPackage targetPackage : BaseNoGui.packages.values()) { for (TargetPlatform targetPlatform : targetPackage.platforms()) titles.addAll(targetPlatform.getCustomMenus().values()); } @@ -1464,7 +1445,7 @@ public class Base { makeBoardCustomMenu(toolsMenu, _(title)); // Cycle through all packages - for (TargetPackage targetPackage : packages.values()) { + for (TargetPackage targetPackage : BaseNoGui.packages.values()) { // For every package cycle through all platform for (TargetPlatform targetPlatform : targetPackage.platforms()) { @@ -1674,7 +1655,7 @@ public class Base { public void rebuildProgrammerMenu(JMenu menu) { menu.removeAll(); ButtonGroup group = new ButtonGroup(); - for (TargetPackage targetPackage : packages.values()) { + for (TargetPackage targetPackage : BaseNoGui.packages.values()) { for (TargetPlatform targetPlatform : targetPackage.platforms()) { for (String programmer : targetPlatform.getProgrammers().keySet()) { String id = targetPackage.getId() + ":" + programmer; @@ -1860,30 +1841,7 @@ public class Base { } protected void loadHardware(File folder) { - if (!folder.isDirectory()) return; - - String list[] = folder.list(new OnlyDirs()); - - // if a bad folder or something like that, this might come back null - if (list == null) return; - - // alphabetize list, since it's not always alpha order - // replaced hella slow bubble sort with this feller for 0093 - Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); - - for (String target : list) { - // Skip reserved 'tools' folder. - if (target.equals("tools")) - continue; - File subfolder = new File(folder, target); - - try { - packages.put(target, new TargetPackage(target, subfolder)); - } catch (TargetPlatformException e) { - System.out.println("WARNING: Error loading hardware folder " + target); - System.out.println(" " + e.getMessage()); - } - } + BaseNoGui.loadHardware(folder); } @@ -1987,8 +1945,8 @@ public class Base { static public File getSettingsFolder() { - if (portableFolder != null) - return portableFolder; + if (BaseNoGui.getPortableFolder() != null) + return BaseNoGui.getPortableFolder(); File settingsFolder = null; @@ -2120,7 +2078,7 @@ public class Base { * @return */ static public TargetPackage getTargetPackage(String packageName) { - return packages.get(packageName); + return BaseNoGui.packages.get(packageName); } /** @@ -2129,9 +2087,7 @@ public class Base { * @return */ static public TargetPlatform getTargetPlatform() { - String packageName = Preferences.get("target_package"); - String platformName = Preferences.get("target_platform"); - return getTargetPlatform(packageName, platformName); + return BaseNoGui.getTargetPlatform(); } /** @@ -2143,10 +2099,7 @@ public class Base { */ static public TargetPlatform getTargetPlatform(String packageName, String platformName) { - TargetPackage p = packages.get(packageName); - if (p == null) - return null; - return p.get(platformName); + return BaseNoGui.getTargetPlatform(packageName, platformName); } static public TargetPlatform getCurrentTargetPlatformFromPackage(String pack) { @@ -2176,7 +2129,7 @@ public class Base { } static public File getPortableFolder() { - return portableFolder; + return BaseNoGui.getPortableFolder(); } @@ -2186,9 +2139,7 @@ public class Base { static public File getSketchbookFolder() { - if (portableFolder != null) - return new File(portableFolder, Preferences.get("sketchbook.path")); - return absoluteFile(Preferences.get("sketchbook.path")); + return BaseNoGui.getSketchbookFolder(); } @@ -2215,13 +2166,13 @@ public class Base { static public File getSketchbookHardwareFolder() { - return new File(getSketchbookFolder(), "hardware"); + return BaseNoGui.getSketchbookHardwareFolder(); } protected File getDefaultSketchbookFolder() { - if (portableFolder != null) - return new File(portableFolder, portableSketchbookFolder); + if (BaseNoGui.getPortableFolder() != null) + return new File(BaseNoGui.getPortableFolder(), portableSketchbookFolder); File sketchbookFolder = null; try { diff --git a/app/src/processing/app/BaseNoGui.java b/app/src/processing/app/BaseNoGui.java index 1713af6df..6a4fd3257 100644 --- a/app/src/processing/app/BaseNoGui.java +++ b/app/src/processing/app/BaseNoGui.java @@ -1,8 +1,17 @@ package processing.app; -import java.io.File; +import static processing.app.I18n._; +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatform; +import processing.app.debug.TargetPlatformException; import processing.app.helpers.OSUtils; +import processing.app.helpers.filefilters.OnlyDirs; public class BaseNoGui { @@ -12,6 +21,28 @@ public class BaseNoGui { /** Set true if this a proper release rather than a numbered revision. */ static public boolean RELEASE = false; + // Current directory to use for relative paths specified on the + // commandline + static String currentDirectory = System.getProperty("user.dir"); + + static public Map packages; + + static File portableFolder = null; + + // Returns a File object for the given pathname. If the pathname + // is not absolute, it is interpreted relative to the current + // directory when starting the IDE (which is not the same as the + // current working directory!). + static public File absoluteFile(String path) { + if (path == null) return null; + + File file = new File(path); + if (!file.isAbsolute()) { + file = new File(currentDirectory, path); + } + return file; + } + static public String getAvrBasePath() { String path = getHardwarePath() + File.separator + "tools" + File.separator + "avr" + File.separator + "bin" + File.separator; @@ -48,4 +79,88 @@ public class BaseNoGui { return getHardwareFolder().getAbsolutePath(); } + static public File getPortableFolder() { + return portableFolder; + } + + static public File getSketchbookFolder() { + if (portableFolder != null) + return new File(portableFolder, Preferences.get("sketchbook.path")); + return absoluteFile(Preferences.get("sketchbook.path")); + } + + static public File getSketchbookHardwareFolder() { + return new File(getSketchbookFolder(), "hardware"); + } + + /** + * Returns the currently selected TargetPlatform. + * + * @return + */ + static public TargetPlatform getTargetPlatform() { + String packageName = Preferences.get("target_package"); + String platformName = Preferences.get("target_platform"); + return getTargetPlatform(packageName, platformName); + } + + /** + * Returns a specific TargetPlatform searching Package/Platform + * + * @param packageName + * @param platformName + * @return + */ + static public TargetPlatform getTargetPlatform(String packageName, + String platformName) { + TargetPackage p = packages.get(packageName); + if (p == null) + return null; + return p.get(platformName); + } + + static public void initPackages() { + packages = new HashMap(); + loadHardware(getHardwareFolder()); + loadHardware(getSketchbookHardwareFolder()); + if (packages.size() == 0) { + System.out.println(_("No valid configured cores found! Exiting...")); + System.exit(3); + } + } + + static public void initPortableFolder() { + // Portable folder + portableFolder = getContentFile("portable"); + if (!portableFolder.exists()) + portableFolder = null; + } + + static protected void loadHardware(File folder) { + if (!folder.isDirectory()) return; + + String list[] = folder.list(new OnlyDirs()); + + // if a bad folder or something like that, this might come back null + if (list == null) return; + + // alphabetize list, since it's not always alpha order + // replaced hella slow bubble sort with this feller for 0093 + Arrays.sort(list, String.CASE_INSENSITIVE_ORDER); + + for (String target : list) { + // Skip reserved 'tools' folder. + if (target.equals("tools")) + continue; + File subfolder = new File(folder, target); + + try { + BaseNoGui.packages.put(target, new TargetPackage(target, subfolder)); + } catch (TargetPlatformException e) { + System.out.println("WARNING: Error loading hardware folder " + target); + System.out.println(" " + e.getMessage()); + } + } + } + } diff --git a/app/src/processing/app/debug/Compiler.java b/app/src/processing/app/debug/Compiler.java index 5667d617c..32848ed0c 100644 --- a/app/src/processing/app/debug/Compiler.java +++ b/app/src/processing/app/debug/Compiler.java @@ -127,7 +127,7 @@ public class Compiler implements MessageConsumer { System.out.println(); List archs = new ArrayList(); - archs.add(Base.getTargetPlatform().getId()); + archs.add(BaseNoGui.getTargetPlatform().getId()); if (prefs.containsKey("architecture.override_check")) { String[] overrides = prefs.get("architecture.override_check").split(","); archs.addAll(Arrays.asList(overrides)); @@ -186,7 +186,7 @@ public class Compiler implements MessageConsumer { } // Check if the board needs a platform from another package - TargetPlatform targetPlatform = Base.getTargetPlatform(); + TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform(); TargetPlatform corePlatform = null; PreferencesMap boardPreferences = Base.getBoardPreferences(); String core = boardPreferences.get("build.core");