diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 2950ade52..5354f2fb5 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -463,59 +463,11 @@ public class Base { } protected void processBoardArgument(String selectBoard) { - // No board selected? Nothing to do - if (selectBoard == null) - return; - - String[] split = selectBoard.split(":", 4); - - if (split.length < 3) { - showError(null, I18n.format(_("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), 3); - } - - TargetPackage targetPackage = getTargetPackage(split[0]); - if (targetPackage == null) { - showError(null, I18n.format(_("{0}: Unknown package"), split[0]), 3); - } - - TargetPlatform targetPlatform = targetPackage.get(split[1]); - if (targetPlatform == null) { - showError(null, I18n.format(_("{0}: Unknown architecture"), split[1]), 3); - } - - TargetBoard targetBoard = targetPlatform.getBoard(split[2]); - if (targetBoard == null) { - showError(null, I18n.format(_("{0}: Unknown board"), split[2]), 3); - } - - selectBoard(targetBoard); - - if (split.length > 3) { - String[] options = split[3].split(","); - for (String option : options) { - String[] keyValue = option.split("=", 2); - - if (keyValue.length != 2) - showError(null, I18n.format(_("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), 3); - String key = keyValue[0].trim(); - String value = keyValue[1].trim(); - - if (!targetBoard.hasMenu(key)) - showError(null, I18n.format(_("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), 3); - if (targetBoard.getMenuLabel(key, value) == null) - showError(null, I18n.format(_("{0}: Invalid option for \"{1}\" option for board \"{2}\""), value, key, targetBoard.getId()), 3); - - Preferences.set("custom_" + key, targetBoard.getId() + "_" + value); - } - } + BaseNoGui.processBoardArgument(selectBoard); } protected void processPrefArgument(String arg) { - String[] split = arg.split("=", 2); - if (split.length != 2 || split[0].isEmpty()) - showError(null, I18n.format(_("{0}: Invalid argument to --pref, should be of the form \"pref=value\""), arg), 3); - - Preferences.set(split[0], split[1]); + BaseNoGui.processPrefArgument(arg); } /** @@ -1483,24 +1435,11 @@ public class Base { private void selectBoard(TargetBoard targetBoard) { - TargetPlatform targetPlatform = targetBoard.getContainerPlatform(); - TargetPackage targetPackage = targetPlatform.getContainerPackage(); - - Preferences.set("target_package", targetPackage.getId()); - Preferences.set("target_platform", targetPlatform.getId()); - Preferences.set("board", targetBoard.getId()); - - File platformFolder = targetPlatform.getFolder(); - Preferences.set("runtime.platform.path", platformFolder.getAbsolutePath()); - Preferences.set("runtime.hardware.path", platformFolder.getParentFile().getAbsolutePath()); + BaseNoGui.selectBoard(targetBoard); } public static void selectSerialPort(String port) { - Preferences.set("serial.port", port); - if (port.startsWith("/dev/")) - Preferences.set("serial.port.file", port.substring(5)); - else - Preferences.set("serial.port.file", port); + BaseNoGui.selectSerialPort(port); } public void rebuildProgrammerMenu(JMenu menu) { @@ -1903,7 +1842,7 @@ public class Base { * @return */ static public TargetPackage getTargetPackage(String packageName) { - return BaseNoGui.packages.get(packageName); + return BaseNoGui.getTargetPackage(packageName); } /** diff --git a/app/src/processing/app/BaseNoGui.java b/app/src/processing/app/BaseNoGui.java index d62ce1682..b59985b4c 100644 --- a/app/src/processing/app/BaseNoGui.java +++ b/app/src/processing/app/BaseNoGui.java @@ -259,6 +259,16 @@ public class BaseNoGui { return getTargetPlatform().getBoard(boardId); } + /** + * Returns a specific TargetPackage + * + * @param packageName + * @return + */ + static public TargetPackage getTargetPackage(String packageName) { + return packages.get(packageName); + } + /** * Returns the currently selected TargetPlatform. * @@ -499,6 +509,62 @@ public class BaseNoGui { PreferencesData.init(absoluteFile(preferencesFile)); } + static protected void processBoardArgument(String selectBoard) { + // No board selected? Nothing to do + if (selectBoard == null) + return; + + String[] split = selectBoard.split(":", 4); + + if (split.length < 3) { + showError(null, I18n.format(_("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), 3); + } + + TargetPackage targetPackage = getTargetPackage(split[0]); + if (targetPackage == null) { + showError(null, I18n.format(_("{0}: Unknown package"), split[0]), 3); + } + + TargetPlatform targetPlatform = targetPackage.get(split[1]); + if (targetPlatform == null) { + showError(null, I18n.format(_("{0}: Unknown architecture"), split[1]), 3); + } + + TargetBoard targetBoard = targetPlatform.getBoard(split[2]); + if (targetBoard == null) { + showError(null, I18n.format(_("{0}: Unknown board"), split[2]), 3); + } + + selectBoard(targetBoard); + + if (split.length > 3) { + String[] options = split[3].split(","); + for (String option : options) { + String[] keyValue = option.split("=", 2); + + if (keyValue.length != 2) + showError(null, I18n.format(_("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), 3); + String key = keyValue[0].trim(); + String value = keyValue[1].trim(); + + if (!targetBoard.hasMenu(key)) + showError(null, I18n.format(_("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), 3); + if (targetBoard.getMenuLabel(key, value) == null) + showError(null, I18n.format(_("{0}: Invalid option for \"{1}\" option for board \"{2}\""), value, key, targetBoard.getId()), 3); + + Preferences.set("custom_" + key, targetBoard.getId() + "_" + value); + } + } + } + + static protected void processPrefArgument(String arg) { + String[] split = arg.split("=", 2); + if (split.length != 2 || split[0].isEmpty()) + showError(null, I18n.format(_("{0}: Invalid argument to --pref, should be of the form \"pref=value\""), arg), 3); + + PreferencesData.set(split[0], split[1]); + } + /** * Recursively remove all files within a directory, * used with removeDir(), or when the contents of a dir @@ -649,6 +715,27 @@ public class BaseNoGui { return res; } + static protected void selectBoard(TargetBoard targetBoard) { + TargetPlatform targetPlatform = targetBoard.getContainerPlatform(); + TargetPackage targetPackage = targetPlatform.getContainerPackage(); + + PreferencesData.set("target_package", targetPackage.getId()); + PreferencesData.set("target_platform", targetPlatform.getId()); + PreferencesData.set("board", targetBoard.getId()); + + File platformFolder = targetPlatform.getFolder(); + PreferencesData.set("runtime.platform.path", platformFolder.getAbsolutePath()); + PreferencesData.set("runtime.hardware.path", platformFolder.getParentFile().getAbsolutePath()); + } + + public static void selectSerialPort(String port) { + PreferencesData.set("serial.port", port); + if (port.startsWith("/dev/")) + PreferencesData.set("serial.port.file", port.substring(5)); + else + PreferencesData.set("serial.port.file", port); + } + static public void showError(String title, String message, int exit_code) { showError(title, message, null, exit_code); }