diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 11dd6c982..b48da543c 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -91,6 +91,15 @@ public class Base { splashScreenHelper.splashText(_("Loading configuration...")); + try { + guardedMain(args); + } catch (Throwable e) { + e.printStackTrace(System.err); + System.exit(255); + } + } + + static public void guardedMain(String args[]) throws Exception { BaseNoGui.initLogger(); BaseNoGui.notifier = new GUIUserNotifier(); diff --git a/app/test/processing/app/debug/UploaderFactoryTest.java b/app/test/processing/app/debug/UploaderFactoryTest.java index 5365f97bc..566887272 100644 --- a/app/test/processing/app/debug/UploaderFactoryTest.java +++ b/app/test/processing/app/debug/UploaderFactoryTest.java @@ -19,7 +19,7 @@ public class UploaderFactoryTest extends AbstractWithPreferencesTest { @Before public void setUp() throws Exception { - targetPackage = new TargetPackage("arduino", new File(".", "hardware/arduino/")); + targetPackage = new LegacyTargetPackage("arduino", new File(".", "hardware/arduino/")); } @Test diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedBoard.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedBoard.java new file mode 100644 index 000000000..a9b854e62 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedBoard.java @@ -0,0 +1,35 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +public interface ContributedBoard { + + public String getName(); + +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedPackage.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedPackage.java new file mode 100644 index 000000000..2ca938ba3 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedPackage.java @@ -0,0 +1,95 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.List; + +public abstract class ContributedPackage { + + public abstract String getName(); + + public abstract String getMaintainer(); + + public abstract String getWebsiteURL(); + + public abstract String getEmail(); + + public abstract List getPlatforms(); + + public abstract List getTools(); + + public ContributedPlatform findPlatform(String architecture, String version) { + for (ContributedPlatform platform : getPlatforms()) { + if (platform.getArchitecture().equals(architecture) && + platform.getVersion().equals(version)) + return platform; + } + return null; + } + + public ContributedTool findTool(String name, String version) { + for (ContributedTool tool : getTools()) { + if (tool.getName().equals(name) && tool.getVersion().equals(version)) + return tool; + } + return null; + } + + @Override + public String toString() { + String res; + res = "Package name : " + getName() + "\n"; + res += " maintaner : " + getMaintainer() + " (" + getEmail() + ")\n"; + if (getPlatforms() != null) { + for (ContributedPlatform plat : getPlatforms()) { + res += "\n Plaform : name : " + plat.getName(); + if (plat.isInstalled()) { + res += "\n " + ((DownloadableContribution) plat); + } + res += "\n category : " + plat.getCategory(); + res += "\n architecture : " + + plat.getArchitecture() + " " + plat.getVersion() + "\n"; + if (plat.getToolsDependencies() != null) + for (ContributedToolReference t : plat.getToolsDependencies()) { + res += " tool dep : " + t.getName() + " " + + t.getVersion() + "\n"; + } + if (plat.getBoards() != null) + for (ContributedBoard board : plat.getBoards()) + res += " board : " + board.getName() + + "\n"; + } + } + if (getTools() != null) { + for (ContributedTool tool : getTools()) + res += tool + "\n"; + } + return res; + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedPlatform.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedPlatform.java new file mode 100644 index 000000000..c35e87d40 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedPlatform.java @@ -0,0 +1,87 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public abstract class ContributedPlatform extends DownloadableContribution { + + public abstract String getName(); + + public abstract String getVersion(); + + public abstract String getCategory(); + + public abstract String getArchitecture(); + + public abstract String getChecksum(); + + public abstract List getToolsDependencies(); + + public abstract List getBoards(); + + private List resolvedTools = null; + + private ContributedPackage parentPackage; + + public List getResolvedTools() { + return resolvedTools; + } + + public List resolveToolsDependencies(Collection packages) { + resolvedTools = new ArrayList(); + + // If there are no dependencies return empty list + if (getToolsDependencies() == null) + return resolvedTools; + + // For each tool dependency + for (ContributedToolReference dep : getToolsDependencies()) { + // Search the referenced tool + ContributedTool tool = dep.resolve(packages); + if (tool == null) { + System.err + .println("Index error: could not find referenced tool " + dep); + } + resolvedTools.add(tool); + } + return resolvedTools; + } + + public ContributedPackage getParentPackage() { + return parentPackage; + } + + public void setParentPackage(ContributedPackage parentPackage) { + this.parentPackage = parentPackage; + } + +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPackage.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPackage.java new file mode 100644 index 000000000..9cd4959a0 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPackage.java @@ -0,0 +1,80 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatform; + +public class ContributedTargetPackage implements TargetPackage { + + private String id; + private Map platforms; + + public ContributedTargetPackage(String _id) { + id = _id; + platforms = new HashMap(); + } + + void addPlatform(TargetPlatform p) { + platforms.put(p.getId(), p); + } + + boolean hasPlatforms() { + return platforms.size() > 0; + } + + @Override + public String getId() { + return id; + } + + @Override + public Map getPlatforms() { + return platforms; + } + + @Override + public Collection platforms() { + return platforms.values(); + } + + @Override + public TargetPlatform get(String platform) { + return platforms.get(platform); + } + + @Override + public String toString() { + return "TargetPackage: " + getId(); + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPlatform.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPlatform.java new file mode 100644 index 000000000..abd9dd3dc --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedTargetPlatform.java @@ -0,0 +1,80 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import static processing.app.I18n._; +import static processing.app.I18n.format; + +import java.io.File; +import java.util.Set; + +import processing.app.debug.LegacyTargetPlatform; +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatformException; +import processing.app.helpers.PreferencesMap; + +public class ContributedTargetPlatform extends LegacyTargetPlatform { + + public ContributedTargetPlatform(String _name, File _folder, + TargetPackage parent, + ContributionsIndex index) + throws TargetPlatformException { + super(_name, _folder, parent); + + // Populate tools + PreferencesMap toolsPrefs = preferences.subTree("tools"); + Set names = toolsPrefs.firstLevelMap().keySet(); + for (String name : names) { + String version = toolsPrefs.get(name + ".version"); + if (version == null) { + throw new TargetPlatformException( + format(_("Tool {0} must define a version property ({1})"), // + name, "tools." + name + ".version")); + } + + String packageName = getContainerPackage().getId(); + ContributedTool tool = index.findTool(packageName, name, version); + if (tool == null) { + throw new TargetPlatformException( + format(_("Tool {0} not found in package {1}"), + name + ":" + version, packageName)); + } + + DownloadableContribution download = tool.getDownloadableContribution(); + if (!download.isInstalled()) { + throw new TargetPlatformException( + format(_("Tool {0} is required but it's not installed."), // + name + ":" + version)); + } + preferences.put("tools." + name + ".path", // + download.getInstalledFolder().getAbsolutePath()); + } + + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedTool.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedTool.java new file mode 100644 index 000000000..bda26c535 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedTool.java @@ -0,0 +1,61 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.List; + +public abstract class ContributedTool { + + public abstract String getName(); + + public abstract String getVersion(); + + public abstract List getSystems(); + + public DownloadableContribution getDownloadableContribution() { + for (HostDependentDownloadableContribution c : getSystems()) { + if (c.isCompatible()) + return c; + } + return null; + } + + @Override + public String toString() { + String res; + res = "Tool name : " + getName() + " " + getVersion() + "\n"; + for (HostDependentDownloadableContribution sys : getSystems()) { + res += " sys"; + res += sys.isCompatible() ? "*" : " "; + res += " : " + sys + "\n"; + } + return res; + } + +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributedToolReference.java b/arduino-core/src/cc/arduino/packages/contributions/ContributedToolReference.java new file mode 100644 index 000000000..1c6e01b02 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributedToolReference.java @@ -0,0 +1,57 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.Collection; + +public abstract class ContributedToolReference { + + public abstract String getName(); + + public abstract String getVersion(); + + public abstract String getPackager(); + + public ContributedTool resolve(Collection packages) { + for (ContributedPackage pack : packages) { + for (ContributedTool tool : pack.getTools()) + if (tool.getName().equals(getName()) && + tool.getVersion().equals(getVersion()) && + pack.getName().equals(getPackager())) + return tool; + } + return null; + } + + @Override + public String toString() { + return "name=" + getName() + " version=" + getVersion() + " packager=" + + getPackager(); + } +} \ No newline at end of file diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndex.java b/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndex.java new file mode 100644 index 000000000..8787266a6 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndex.java @@ -0,0 +1,81 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.ArrayList; +import java.util.List; + +public abstract class ContributionsIndex { + + public abstract List getPackages(); + + public ContributedPackage findPackage(String packageName) { + for (ContributedPackage pack : getPackages()) { + if (pack.getName().equals(packageName)) + return pack; + } + return null; + } + + public ContributedTool findTool(String packageName, String name, + String version) { + ContributedPackage pack = findPackage(packageName); + if (pack == null) + return null; + return pack.findTool(name, version); + } + + private List categories = new ArrayList(); + + public List getCategories() { + return categories; + } + + public void fillCategories() { + categories.clear(); + for (ContributedPackage pack : getPackages()) { + for (ContributedPlatform platform : pack.getPlatforms()) { + if (!categories.contains(platform.getCategory())) + categories.add(platform.getCategory()); + } + } + } + + @Override + public String toString() { + String res = ""; + res += "Categories: "; + for (String c : getCategories()) + res += "'" + c + "' "; + res += "\n"; + for (ContributedPackage pack : getPackages()) + res += pack + "\n"; + return res; + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndexer.java b/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndexer.java new file mode 100644 index 000000000..1b60f3aea --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/ContributionsIndexer.java @@ -0,0 +1,200 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import static processing.app.helpers.filefilters.OnlyDirs.ONLY_DIRS; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import processing.app.debug.TargetPackage; +import processing.app.debug.TargetPlatform; +import processing.app.debug.TargetPlatformException; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.module.mrbean.MrBeanModule; + +public class ContributionsIndexer { + + private File preferencesFolder; + private File packagesFolder; + private ContributionsIndex index; + + public ContributionsIndexer(File _preferencesFolder) { + preferencesFolder = _preferencesFolder; + packagesFolder = new File(preferencesFolder, "packages"); + } + + // public static void main(String args[]) throws Exception { + // File indexFile = new File(args[0]); + // + // // VerifyResult verify = ClearSignedVerifier.verify(indexFile, + // // new PackagersPublicKeys()); + // // if (!verify.verified) + // // throw new Exception("Invalid index file!"); + // + // ContributionsIndexer indexer = new ContributionsIndexer(null); + // // indexer.parse(new ByteArrayInputStream(verify.clearText)); + // indexer.parseIndex(indexFile); + // indexer.syncWithFilesystem(); + // } + + public void parseIndex() throws JsonParseException, IOException { + // Parse index file + parseIndex(new File(preferencesFolder, "package_index.json")); + + List packages = index.getPackages(); + for (ContributedPackage pack : packages) { + for (ContributedPlatform platform : pack.getPlatforms()) { + // Set a reference to parent packages + platform.setParentPackage(pack); + + // Resolve tools dependencies (works also as a check for file integrity) + platform.resolveToolsDependencies(packages); + } + } + + index.fillCategories(); + } + + private void parseIndex(File indexFile) throws JsonParseException, + IOException { + InputStream indexIn = new FileInputStream(indexFile); + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new MrBeanModule()); + mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); + index = mapper.readValue(indexIn, ContributionsIndex.class); + } + + public void syncWithFilesystem() { + if (!packagesFolder.isDirectory()) + return; + + // Scan all hardware folders and mark as installed all the + // platforms found. + for (File folder : packagesFolder.listFiles(ONLY_DIRS)) { + ContributedPackage pack = index.findPackage(folder.getName()); + if (pack != null) + syncPackageWithFilesystem(pack, folder); + } + } + + private void syncPackageWithFilesystem(ContributedPackage pack, File root) { + // Scan all hardware folders and mark as installed all the tools found. + File hardwareFolder = new File(root, "hardware"); + if (hardwareFolder.isDirectory()) { + for (File platformFolder : hardwareFolder.listFiles(ONLY_DIRS)) { + for (File versionFolder : platformFolder.listFiles(ONLY_DIRS)) + syncHardwareWithFilesystem(pack, platformFolder, versionFolder); + } + } + + // Scan all tools folders and mark as installed all the tools found. + File toolsFolder = new File(root, "tools"); + if (toolsFolder.isDirectory()) { + for (File toolFolder : toolsFolder.listFiles(ONLY_DIRS)) { + for (File versionFolder : toolFolder.listFiles(ONLY_DIRS)) + syncToolWithFilesystem(pack, toolFolder, versionFolder); + } + } + } + + private void syncToolWithFilesystem(ContributedPackage pack, File toolFolder, + File versionFolder) { + ContributedTool tool = pack.findTool(toolFolder.getName(), + versionFolder.getName()); + if (tool == null) + return; + DownloadableContribution contrib = tool.getDownloadableContribution(); + if (contrib == null) { + System.err.println(tool + + " seems to have no downloadable contributions for your " + + "operating system, but it is installed in\n" + versionFolder); + return; + } + contrib.setInstalled(true); + contrib.setInstalledFolder(versionFolder); + return; + } + + private void syncHardwareWithFilesystem(ContributedPackage pack, + File platformFolder, + File versionFolder) { + String architecture = platformFolder.getName(); + String version = versionFolder.getName(); + ContributedPlatform platform = pack.findPlatform(architecture, version); + if (platform != null) { + platform.setInstalled(true); + platform.setInstalledFolder(versionFolder); + } + } + + @Override + public String toString() { + return index.toString(); + } + + public List createTargetPackages() { + List res = new ArrayList(); + + for (ContributedPackage pack : index.getPackages()) { + ContributedTargetPackage targetPackage; + targetPackage = new ContributedTargetPackage(pack.getName()); + + for (ContributedPlatform platform : pack.getPlatforms()) { + if (!platform.isInstalled()) + continue; + + String arch = platform.getArchitecture(); + File folder = platform.getInstalledFolder(); + + try { + TargetPlatform targetPlatform; + targetPlatform = new ContributedTargetPlatform(arch, folder, + targetPackage, index); + targetPackage.addPlatform(targetPlatform); + } catch (TargetPlatformException e) { + e.printStackTrace(); + } + } + + if (targetPackage.hasPlatforms()) + res.add(targetPackage); + } + return res; + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/DownloadableContribution.java b/arduino-core/src/cc/arduino/packages/contributions/DownloadableContribution.java new file mode 100644 index 000000000..18bc0a7c2 --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/DownloadableContribution.java @@ -0,0 +1,94 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.io.File; + +public abstract class DownloadableContribution { + + private boolean installed; + private File installedFolder; + + private boolean downloaded; + private File downloadedFile; + + public abstract String getUrl(); + + public abstract String getChecksum(); + + public abstract long getSize(); + + public abstract String getArchiveFileName(); + + public boolean isDownloaded() { + return downloaded; + } + + public void setDownloaded(boolean downloaded) { + this.downloaded = downloaded; + } + + public File getDownloadedFile() { + return downloadedFile; + } + + public void setDownloadedFile(File downloadedFile) { + this.downloadedFile = downloadedFile; + } + + public boolean isInstalled() { + return installed; + } + + public void setInstalled(boolean installed) { + this.installed = installed; + } + + public File getInstalledFolder() { + return installedFolder; + } + + public void setInstalledFolder(File installedFolder) { + this.installedFolder = installedFolder; + } + + @Override + public String toString() { + String chk = getChecksum(); + if (chk.length() > 14) + chk = getChecksum().substring(0, 14); + String res = ""; + // res += getUrl() + " (" + chk + ") "; + if (installed) { + res += "installed on " + installedFolder.getAbsolutePath() + " (" + + getSize() + " bytes)"; + } + return res; + } +} diff --git a/arduino-core/src/cc/arduino/packages/contributions/HostDependentDownloadableContribution.java b/arduino-core/src/cc/arduino/packages/contributions/HostDependentDownloadableContribution.java new file mode 100644 index 000000000..e2dd6579a --- /dev/null +++ b/arduino-core/src/cc/arduino/packages/contributions/HostDependentDownloadableContribution.java @@ -0,0 +1,80 @@ +/* + * This file is part of Arduino. + * + * Copyright 2014 Arduino LLC (http://www.arduino.cc/) + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + */ +package cc.arduino.packages.contributions; + +import java.util.Properties; + +public abstract class HostDependentDownloadableContribution extends + DownloadableContribution { + + public abstract String getHost(); + + @Override + public String toString() { + return getHost() + " " + super.toString(); + } + + public boolean isCompatible() { + // TODO: add missing host detections + + Properties prop = System.getProperties(); + String osName = prop.getProperty("os.name"); + String osArch = prop.getProperty("os.arch"); + // for (Object k : properties.keySet()) + // System.out.println(k + " = " + properties.get(k)); + + String host = getHost(); + + if (osName.contains("Linux")) { + if (osArch.contains("amd64")) { + // os.arch = amd64 + return host.matches("x86_64-.*linux-gnu"); + } else { + // 32 bit systems + return host.matches("i[3456]86-.*linux-gnu"); + } + } + + if (osName.contains("Windows")) { + if (host.matches("i[3456]86-.*mingw32")) + return true; + if (host.matches("i[3456]86-.*cygwin")) + return true; + } + + if (osName.contains("Mac")) { + if (osArch.contains("86")) { + if (host.matches("i[3456]86-apple-darwin.*")) + return true; + } + } + + return false; + } +} diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index 0f7b0c885..98310dd9d 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -17,9 +17,10 @@ import org.apache.commons.logging.impl.NoOpLog; import cc.arduino.packages.DiscoveryManager; import cc.arduino.packages.Uploader; - import processing.app.debug.Compiler; +import cc.arduino.packages.contributions.ContributionsIndexer; import processing.app.debug.TargetBoard; +import processing.app.debug.LegacyTargetPackage; import processing.app.debug.TargetPackage; import processing.app.debug.TargetPlatform; import processing.app.debug.TargetPlatformException; @@ -421,7 +422,7 @@ public class BaseNoGui { return list; } - static public void init(String[] args) { + static public void init(String[] args) throws Exception { getPlatform().init(); String sketchbookPath = getSketchbookPath(); @@ -582,14 +583,16 @@ public class BaseNoGui { Logger.getLogger("javax.jmdns").setLevel(Level.OFF); } - static public void initPackages() { + static public void initPackages() throws Exception { + ContributionsIndexer indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder()); + indexer.parseIndex(); + indexer.syncWithFilesystem(); + System.out.println(indexer); + packages = new HashMap(); loadHardware(getHardwareFolder()); loadHardware(getSketchbookHardwareFolder()); - if (packages.size() == 0) { - System.out.println(_("No valid configured cores found! Exiting...")); - System.exit(3); - } + loadContributedHardware(indexer); } static protected void initPlatform() { @@ -649,7 +652,7 @@ public class BaseNoGui { File subfolder = new File(folder, target); try { - packages.put(target, new TargetPackage(target, subfolder)); + packages.put(target, new LegacyTargetPackage(target, subfolder)); } catch (TargetPlatformException e) { System.out.println("WARNING: Error loading hardware folder " + target); System.out.println(" " + e.getMessage()); @@ -714,6 +717,12 @@ public class BaseNoGui { populateImportToLibraryTable(); } + static protected void loadContributedHardware(ContributionsIndexer indexer) { + for (TargetPackage pack : indexer.createTargetPackages()) { + packages.put(pack.getId(), pack); + } + } + static public void populateImportToLibraryTable() { // Populate importToLibraryTable importToLibraryTable = new HashMap(); diff --git a/arduino-core/src/processing/app/debug/LegacyTargetBoard.java b/arduino-core/src/processing/app/debug/LegacyTargetBoard.java new file mode 100644 index 000000000..ff06ab29d --- /dev/null +++ b/arduino-core/src/processing/app/debug/LegacyTargetBoard.java @@ -0,0 +1,113 @@ +/* + TargetPackage - Represents a hardware package + Part of the Arduino project - http://www.arduino.cc/ + + Copyright (c) 2014 Cristian Maglie + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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.app.debug; + +import static processing.app.I18n._; +import static processing.app.I18n.format; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import processing.app.helpers.PreferencesMap; + +public class LegacyTargetBoard implements TargetBoard { + + private String id; + private PreferencesMap prefs; + private Map menuOptions = new LinkedHashMap(); + private TargetPlatform containerPlatform; + + /** + * Create a TargetBoard based on preferences passed as argument. + * + * @param _prefs + * @return + */ + public LegacyTargetBoard(String _id, PreferencesMap _prefs, + TargetPlatform parent) { + containerPlatform = parent; + id = _id; + prefs = new PreferencesMap(_prefs); + + // Setup sub-menus + PreferencesMap menus = prefs.firstLevelMap().get("menu"); + if (menus != null) + menuOptions = menus.firstLevelMap(); + + // Auto generate build.board if not set + if (!prefs.containsKey("build.board")) { + String board = containerPlatform.getId() + "_" + id; + board = board.toUpperCase(); + prefs.put("build.board", board); + System.out + .println(format(_("Board {0}:{1}:{2} doesn''t define a ''build.board'' preference. Auto-set to: {3}"), + containerPlatform.getContainerPackage().getId(), + containerPlatform.getId(), id, board)); + } + } + + @Override + public String getName() { + return prefs.get("name"); + } + + @Override + public String getId() { + return id; + } + + @Override + public PreferencesMap getPreferences() { + return prefs; + } + + @Override + public boolean hasMenu(String menuId) { + return menuOptions.containsKey(menuId); + } + + @Override + public PreferencesMap getMenuLabels(String menuId) { + return menuOptions.get(menuId).topLevelMap(); + } + + @Override + public String getMenuLabel(String menuId, String selectionId) { + return getMenuLabels(menuId).get(selectionId); + } + + @Override + public Set getMenuIds() { + return menuOptions.keySet(); + } + + @Override + public PreferencesMap getMenuPreferences(String menuId, String selectionId) { + return menuOptions.get(menuId).subTree(selectionId); + } + + @Override + public TargetPlatform getContainerPlatform() { + return containerPlatform; + } + +} diff --git a/arduino-core/src/processing/app/debug/LegacyTargetPackage.java b/arduino-core/src/processing/app/debug/LegacyTargetPackage.java new file mode 100644 index 000000000..8d381706c --- /dev/null +++ b/arduino-core/src/processing/app/debug/LegacyTargetPackage.java @@ -0,0 +1,84 @@ +/* + TargetPackage - Represents a hardware package + Part of the Arduino project - http://www.arduino.cc/ + + Copyright (c) 2011 Cristian Maglie + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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.app.debug; + +import static processing.app.I18n._; +import static processing.app.helpers.filefilters.OnlyDirs.ONLY_DIRS; + +import java.io.File; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import processing.app.I18n; + +public class LegacyTargetPackage implements TargetPackage { + + private String id; + private Map platforms; + + public LegacyTargetPackage(String _id, File _folder) throws TargetPlatformException { + id = _id; + platforms = new LinkedHashMap(); + + File[] folders = _folder.listFiles(ONLY_DIRS); + if (folders == null) + return; + + for (File subFolder : folders) { + if (!subFolder.exists() || !subFolder.canRead()) + continue; + String arch = subFolder.getName(); + try { + TargetPlatform platform = new LegacyTargetPlatform(arch, subFolder, this); + platforms.put(arch, platform); + } catch (TargetPlatformException e) { + System.out.println(e.getMessage()); + } + } + + if (platforms.size() == 0) { + throw new TargetPlatformException(I18n + .format(_("No valid hardware definitions found in folder {0}."), + _folder.getName())); + } + } + + @Override + public Map getPlatforms() { + return platforms; + } + + @Override + public Collection platforms() { + return platforms.values(); + } + + @Override + public TargetPlatform get(String platform) { + return platforms.get(platform); + } + + @Override + public String getId() { + return id; + } +} diff --git a/arduino-core/src/processing/app/debug/LegacyTargetPlatform.java b/arduino-core/src/processing/app/debug/LegacyTargetPlatform.java new file mode 100644 index 000000000..cbc5d16c1 --- /dev/null +++ b/arduino-core/src/processing/app/debug/LegacyTargetPlatform.java @@ -0,0 +1,198 @@ +/* + TargetPlatform - Represents a hardware platform + Part of the Arduino project - http://www.arduino.cc/ + + Copyright (c) 2009-2014 Arduino + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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.app.debug; + +import static processing.app.I18n._; +import static processing.app.I18n.format; + +import java.io.File; +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import processing.app.helpers.PreferencesMap; + +public class LegacyTargetPlatform implements TargetPlatform { + + private String id; + private File folder; + + private TargetPackage containerPackage; + protected PreferencesMap preferences = new PreferencesMap(); + + private Map boards = new LinkedHashMap(); + private TargetBoard defaultBoard; + + /** + * Contains preferences for every defined programmer + */ + private Map programmers = new LinkedHashMap(); + + /** + * Contains labels for top level menus + */ + private PreferencesMap customMenus = new PreferencesMap(); + + public LegacyTargetPlatform(String _name, File _folder, TargetPackage parent) + throws TargetPlatformException { + + id = _name; + folder = _folder; + containerPackage = parent; + + // If there is no boards.txt, this is not a valid 1.5 hardware folder + File boardsFile = new File(folder, "boards.txt"); + if (!boardsFile.exists() || !boardsFile.canRead()) + throw new TargetPlatformException( + format(_("Could not find boards.txt in {0}. Is it pre-1.5?"), + folder.getAbsolutePath())); + + // Load boards + try { + Map boardsPreferences = new PreferencesMap( + boardsFile).firstLevelMap(); + + // Create custom menus for this platform + PreferencesMap menus = boardsPreferences.get("menu"); + if (menus != null) + customMenus = menus.topLevelMap(); + boardsPreferences.remove("menu"); + + // Create boards + Set boardIds = boardsPreferences.keySet(); + for (String boardId : boardIds) { + PreferencesMap preferences = boardsPreferences.get(boardId); + TargetBoard board = new LegacyTargetBoard(boardId, preferences, this); + boards.put(boardId, board); + + // Pick the first board as default + if (defaultBoard == null) + defaultBoard = board; + } + } catch (IOException e) { + throw new TargetPlatformException(format(_("Error loading {0}"), + boardsFile.getAbsolutePath()), e); + } + + File platformsFile = new File(folder, "platform.txt"); + try { + if (platformsFile.exists() && platformsFile.canRead()) { + preferences.load(platformsFile); + } + } catch (IOException e) { + throw new TargetPlatformException( + format(_("Error loading {0}"), platformsFile.getAbsolutePath()), e); + } + + // Allow overriding values in platform.txt. This allows changing + // platform.txt (e.g. to use a system-wide toolchain), without + // having to modify platform.txt (which, when running from git, + // prevents files being marked as changed). + File localPlatformsFile = new File(folder, "platform.local.txt"); + try { + if (localPlatformsFile.exists() && localPlatformsFile.canRead()) { + preferences.load(localPlatformsFile); + } + } catch (IOException e) { + throw new TargetPlatformException( + format(_("Error loading {0}"), localPlatformsFile.getAbsolutePath()), e); + } + + File progFile = new File(folder, "programmers.txt"); + try { + if (progFile.exists() && progFile.canRead()) { + PreferencesMap prefs = new PreferencesMap(); + prefs.load(progFile); + programmers = prefs.firstLevelMap(); + } + } catch (IOException e) { + throw new TargetPlatformException(format(_("Error loading {0}"), + progFile.getAbsolutePath()), e); + } + } + + @Override + public String getId() { + return id; + } + + @Override + public File getFolder() { + return folder; + } + + @Override + public Map getBoards() { + return boards; + } + + @Override + public PreferencesMap getCustomMenus() { + return customMenus; + } + + @Override + public Set getCustomMenuIds() { + return customMenus.keySet(); + } + + @Override + public Map getProgrammers() { + return programmers; + } + + @Override + public PreferencesMap getProgrammer(String programmer) { + return getProgrammers().get(programmer); + } + + @Override + public PreferencesMap getTool(String tool) { + return getPreferences().subTree("tools").subTree(tool); + } + + @Override + public PreferencesMap getPreferences() { + return preferences; + } + + @Override + public TargetBoard getBoard(String boardId) { + if (boards.containsKey(boardId)) { + return boards.get(boardId); + } + return defaultBoard; + } + + @Override + public TargetPackage getContainerPackage() { + return containerPackage; + } + + @Override + public String toString() { + String res = "TargetPlatform: name=" + id + " boards={\n"; + for (String boardId : boards.keySet()) + res += " " + boardId + " = " + boards.get(boardId) + "\n"; + return res + "}"; + } +} diff --git a/arduino-core/src/processing/app/debug/TargetBoard.java b/arduino-core/src/processing/app/debug/TargetBoard.java index 721b69613..5dae86906 100644 --- a/arduino-core/src/processing/app/debug/TargetBoard.java +++ b/arduino-core/src/processing/app/debug/TargetBoard.java @@ -1,76 +1,51 @@ +/* + TargetBoard - Represents a hardware board + Part of the Arduino project - http://www.arduino.cc/ + + Copyright (c) 2014 Cristian Maglie + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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.app.debug; -import static processing.app.I18n._; -import static processing.app.I18n.format; - -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Set; import processing.app.helpers.PreferencesMap; -public class TargetBoard { - - private String id; - private PreferencesMap prefs; - private Map menuOptions = new LinkedHashMap(); - private TargetPlatform containerPlatform; - - /** - * Create a TargetBoard based on preferences passed as argument. - * - * @param _prefs - * @return - */ - public TargetBoard(String _id, PreferencesMap _prefs, TargetPlatform parent) { - containerPlatform = parent; - id = _id; - prefs = new PreferencesMap(_prefs); - - // Setup sub-menus - PreferencesMap menus = prefs.firstLevelMap().get("menu"); - if (menus != null) - menuOptions = menus.firstLevelMap(); - - // Auto generate build.board if not set - if (!prefs.containsKey("build.board")) { - String board = containerPlatform.getId() + "_" + id; - board = board.toUpperCase(); - prefs.put("build.board", board); - System.out - .println(format( - _("Board {0}:{1}:{2} doesn''t define a ''build.board'' preference. Auto-set to: {3}"), - containerPlatform.getContainerPackage().getId(), - containerPlatform.getId(), id, board)); - } - } +public interface TargetBoard { /** * Get the name of the board. * * @return */ - public String getName() { - return prefs.get("name"); - } + public String getName(); /** * Get the identifier of the board * * @return */ - public String getId() { - return id; - } + public String getId(); /** - * Get the full preferences map of the board with a given identifier + * Get the full preferences map of the board * * @return */ - public PreferencesMap getPreferences() { - return prefs; - } + public PreferencesMap getPreferences(); /** * Check if the board has a sub menu. @@ -79,9 +54,7 @@ public class TargetBoard { * The menu ID to check * @return */ - public boolean hasMenu(String menuId) { - return menuOptions.containsKey(menuId); - } + public boolean hasMenu(String menuId); /** * Returns the options available on a specific menu @@ -90,9 +63,7 @@ public class TargetBoard { * The menu ID * @return */ - public PreferencesMap getMenuLabels(String menuId) { - return menuOptions.get(menuId).topLevelMap(); - } + public PreferencesMap getMenuLabels(String menuId); /** * Returns the label of the specified option in the specified menu @@ -103,13 +74,9 @@ public class TargetBoard { * The option ID * @return */ - public String getMenuLabel(String menuId, String selectionId) { - return getMenuLabels(menuId).get(selectionId); - } + public String getMenuLabel(String menuId, String selectionId); - public Set getMenuIds() { - return menuOptions.keySet(); - } + public Set getMenuIds(); /** * Returns the configuration parameters to override (as a PreferenceMap) when @@ -121,12 +88,8 @@ public class TargetBoard { * The option ID * @return */ - public PreferencesMap getMenuPreferences(String menuId, String selectionId) { - return menuOptions.get(menuId).subTree(selectionId); - } + public PreferencesMap getMenuPreferences(String menuId, String selectionId); - public TargetPlatform getContainerPlatform() { - return containerPlatform; - } + public TargetPlatform getContainerPlatform(); } diff --git a/arduino-core/src/processing/app/debug/TargetPackage.java b/arduino-core/src/processing/app/debug/TargetPackage.java index dce91d178..5644272d0 100644 --- a/arduino-core/src/processing/app/debug/TargetPackage.java +++ b/arduino-core/src/processing/app/debug/TargetPackage.java @@ -1,9 +1,8 @@ -/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* TargetPackage - Represents a hardware package Part of the Arduino project - http://www.arduino.cc/ - Copyright (c) 2011 Cristian Maglie + Copyright (c) 2014 Cristian Maglie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,61 +20,17 @@ */ package processing.app.debug; -import static processing.app.I18n._; - -import java.io.File; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.Map; -import processing.app.I18n; -import processing.app.helpers.filefilters.OnlyDirs; +public interface TargetPackage { -public class TargetPackage { + public String getId(); + + public Map getPlatforms(); - private String id; + public Collection platforms(); - Map platforms = new LinkedHashMap(); - - public TargetPackage(String _id, File _folder) throws TargetPlatformException { - id = _id; - - File[] folders = _folder.listFiles(new OnlyDirs()); - if (folders == null) - return; - - for (File subFolder : folders) { - if (!subFolder.exists() || !subFolder.canRead()) - continue; - String arch = subFolder.getName(); - try { - TargetPlatform platform = new TargetPlatform(arch, subFolder, this); - platforms.put(arch, platform); - } catch (TargetPlatformException e) { - System.out.println(e.getMessage()); - } - } - - if (platforms.size() == 0) { - throw new TargetPlatformException(I18n - .format(_("No valid hardware definitions found in folder {0}."), - _folder.getName())); - } - } - - public Map getPlatforms() { - return platforms; - } - - public Collection platforms() { - return platforms.values(); - } - - public TargetPlatform get(String platform) { - return platforms.get(platform); - } - - public String getId() { - return id; - } + public TargetPlatform get(String platform); + } diff --git a/arduino-core/src/processing/app/debug/TargetPlatform.java b/arduino-core/src/processing/app/debug/TargetPlatform.java index 611784618..4b13cf87a 100644 --- a/arduino-core/src/processing/app/debug/TargetPlatform.java +++ b/arduino-core/src/processing/app/debug/TargetPlatform.java @@ -1,9 +1,8 @@ -/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* TargetPlatform - Represents a hardware platform Part of the Arduino project - http://www.arduino.cc/ - Copyright (c) 2009 David A. Mellis + Copyright (c) 2014 Arduino This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,174 +20,78 @@ */ package processing.app.debug; -import static processing.app.I18n._; -import static processing.app.I18n.format; - import java.io.File; -import java.io.IOException; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import processing.app.helpers.PreferencesMap; -public class TargetPlatform { +public interface TargetPlatform { - private String id; - private File folder; - private TargetPackage containerPackage; + public String getId(); + + public File getFolder(); /** - * Contains preferences for every defined board + * Get TargetBoards under this TargetPlatform into a Map that maps the board + * id with the corresponding TargetBoard + * + * @return a Map */ - private Map boards = new LinkedHashMap(); - private TargetBoard defaultBoard; + public Map getBoards(); + + public PreferencesMap getCustomMenus(); /** - * Contains preferences for every defined programmer + * Return ids for top level menus + * + * @return a Set with the ids of the top level custom menus */ - private Map programmers = new LinkedHashMap(); + public Set getCustomMenuIds(); /** - * Contains preferences for platform + * Get preferences for all programmers + * + * @return */ - private PreferencesMap preferences = new PreferencesMap(); + public Map getProgrammers(); /** - * Contains labels for top level menus + * Get preferences for a specific programmer + * + * @param programmer + * @return */ - private PreferencesMap customMenus = new PreferencesMap(); + public PreferencesMap getProgrammer(String programmer); - public TargetPlatform(String _name, File _folder, TargetPackage parent) - throws TargetPlatformException { + /** + * Get preferences for a specific tool + * + * @param tool + * @return + */ + public PreferencesMap getTool(String tool); - id = _name; - folder = _folder; - containerPackage = parent; + /** + * Return TargetPlatform preferences + * + * @return + */ + public PreferencesMap getPreferences(); - // If there is no boards.txt, this is not a valid 1.5 hardware folder - File boardsFile = new File(folder, "boards.txt"); - if (!boardsFile.exists() || !boardsFile.canRead()) - throw new TargetPlatformException( - format(_("Could not find boards.txt in {0}. Is it pre-1.5?"), - folder.getAbsolutePath())); + /** + * Get a target board + * + * @param boardId + * @return + */ + public TargetBoard getBoard(String boardId); - // Load boards - try { - Map boardsPreferences = new PreferencesMap( - boardsFile).firstLevelMap(); + /** + * Get the TargetPackage that contains this TargetPlatform + * + * @return + */ + public TargetPackage getContainerPackage(); - // Create custom menus for this platform - PreferencesMap menus = boardsPreferences.get("menu"); - if (menus != null) - customMenus = menus.topLevelMap(); - boardsPreferences.remove("menu"); - - // Create boards - Set boardIds = boardsPreferences.keySet(); - for (String boardId : boardIds) { - PreferencesMap preferences = boardsPreferences.get(boardId); - TargetBoard board = new TargetBoard(boardId, preferences, this); - boards.put(boardId, board); - - // Pick the first board as default - if (defaultBoard == null) - defaultBoard = board; - } - } catch (IOException e) { - throw new TargetPlatformException(format(_("Error loading {0}"), - boardsFile.getAbsolutePath()), e); - } - - File platformsFile = new File(folder, "platform.txt"); - try { - if (platformsFile.exists() && platformsFile.canRead()) { - preferences.load(platformsFile); - } - } catch (IOException e) { - throw new TargetPlatformException( - format(_("Error loading {0}"), platformsFile.getAbsolutePath()), e); - } - - // Allow overriding values in platform.txt. This allows changing - // platform.txt (e.g. to use a system-wide toolchain), without - // having to modify platform.txt (which, when running from git, - // prevents files being marked as changed). - File localPlatformsFile = new File(folder, "platform.local.txt"); - try { - if (localPlatformsFile.exists() && localPlatformsFile.canRead()) { - preferences.load(localPlatformsFile); - } - } catch (IOException e) { - throw new TargetPlatformException( - format(_("Error loading {0}"), localPlatformsFile.getAbsolutePath()), e); - } - - File progFile = new File(folder, "programmers.txt"); - try { - if (progFile.exists() && progFile.canRead()) { - PreferencesMap prefs = new PreferencesMap(); - prefs.load(progFile); - programmers = prefs.firstLevelMap(); - } - } catch (IOException e) { - throw new TargetPlatformException(format(_("Error loading {0}"), progFile - .getAbsolutePath()), e); - } - } - - public String getId() { - return id; - } - - public File getFolder() { - return folder; - } - - public Map getBoards() { - return boards; - } - - public PreferencesMap getCustomMenus() { - return customMenus; - } - - public Set getCustomMenuIds() { - return customMenus.keySet(); - } - - public Map getProgrammers() { - return programmers; - } - - public PreferencesMap getProgrammer(String programmer) { - return getProgrammers().get(programmer); - } - - public PreferencesMap getTool(String tool) { - return getPreferences().subTree("tools").subTree(tool); - } - - public PreferencesMap getPreferences() { - return preferences; - } - - public TargetBoard getBoard(String boardId) { - if (boards.containsKey(boardId)) { - return boards.get(boardId); - } - return defaultBoard; - } - - public TargetPackage getContainerPackage() { - return containerPackage; - } - - @Override - public String toString() { - String res = "TargetPlatform: name=" + id + " boards={\n"; - for (String boardId : boards.keySet()) - res += " " + boardId + " = " + boards.get(boardId) + "\n"; - return res + "}"; - } } diff --git a/arduino-core/src/processing/app/helpers/filefilters/OnlyDirs.java b/arduino-core/src/processing/app/helpers/filefilters/OnlyDirs.java index 46f407248..0cbe1cc7c 100644 --- a/arduino-core/src/processing/app/helpers/filefilters/OnlyDirs.java +++ b/arduino-core/src/processing/app/helpers/filefilters/OnlyDirs.java @@ -32,11 +32,17 @@ import java.io.FilenameFilter; */ public class OnlyDirs implements FilenameFilter { - public boolean accept(File dir, String name) { - if (name.charAt(0) == '.') - return false; - if (name.equals("CVS")) - return false; - return new File(dir, name).isDirectory(); - } + public boolean accept(File dir, String name) { + if (name.charAt(0) == '.') + return false; + if (name.equals("CVS")) + return false; + return new File(dir, name).isDirectory(); + } + + /** + * An handy pre-instantiated object + */ + public static final OnlyDirs ONLY_DIRS = new OnlyDirs(); + }