Debian Codegraph @

arduino-core/src/processing/app/debug/LegacyTargetPlatform.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 TargetPlatform - Represents a hardware platform
3 Part of the Arduino project - http://www.arduino.cc/
4
5 Copyright (c) 2009-2014 Arduino
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package processing.app.debug;
22
23 import processing.app.BaseNoGui;
24 import processing.app.helpers.PreferencesMap;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Set;
31
32 import static processing.app.I18n.format;
33 import static processing.app.I18n.tr;
34
35 public class LegacyTargetPlatform implements TargetPlatform {
36
37 private final String id;
38 private final File folder;
39
40 private final TargetPackage containerPackage;
41 private final PreferencesMap preferences = new PreferencesMap();
42
43 private final Map<String, TargetBoard> boards = new LinkedHashMap<>();
44 private TargetBoard defaultBoard;
45
46 /**
47 * Contains preferences for every defined programmer
48 */
49 private Map<String, PreferencesMap> programmers = new LinkedHashMap<>();
50
51 /**
52 * Contains labels for top level menus
53 */
54 private PreferencesMap customMenus = new PreferencesMap();
55
56 public LegacyTargetPlatform(String _name, File _folder, TargetPackage parent)
57 throws TargetPlatformException {
58
59 id = _name;
60 folder = _folder;
61 containerPackage = parent;
62
63 // If there is no boards.txt, this is not a valid 1.5 hardware folder
64 File boardsFile = new File(folder, "boards.txt");
65 if (!boardsFile.exists() || !boardsFile.canRead())
66 throw new TargetPlatformException(
67 format(tr("Could not find boards.txt in {0}. Is it pre-1.5?"),
68 folder.getAbsolutePath()));
69
70 // Load boards
71 try {
72 PreferencesMap bPrefs = new PreferencesMap(
73 boardsFile);
74
75 // Allow overriding values in boards.txt. This allows changing
76 // boards.txt (e.g. to add user-specific items to a menu), without
77 // having to modify boards.txt (which, when running from git,
78 // prevents files being marked as changed).
79 File localboardsFile = new File(folder, "boards.local.txt");
80 try {
81 if (localboardsFile.exists() && localboardsFile.canRead()) {
82 bPrefs.load(localboardsFile);
83 }
84 } catch (IOException e) {
85 throw new TargetPlatformException(
86 format(tr("Error loading {0}"), localboardsFile.getAbsolutePath()), e);
87 }
88 Map<String, PreferencesMap> boardsPreferences = bPrefs.firstLevelMap();
89
90 // Create custom menus for this platform
91 PreferencesMap menus = boardsPreferences.get("menu");
92 if (menus != null)
93 customMenus = menus.topLevelMap();
94 boardsPreferences.remove("menu");
95
96 // Create boards
97 Set<String> boardIds = boardsPreferences.keySet();
98 for (String boardId : boardIds) {
99 PreferencesMap prefs = boardsPreferences.get(boardId);
100 TargetBoard board = new LegacyTargetBoard(boardId, prefs, this);
101 boards.put(boardId, board);
102
103 // Pick the first board as default
104 if (defaultBoard == null)
105 defaultBoard = board;
106 }
107 } catch (IOException e) {
108 throw new TargetPlatformException(format(tr("Error loading {0}"),
109 boardsFile.getAbsolutePath()), e);
110 }
111
112 File platformsFile = new File(folder, "platform.txt");
113 try {
114 if (platformsFile.exists() && platformsFile.canRead()) {
115 preferences.load(platformsFile);
116 }
117 } catch (IOException e) {
118 throw new TargetPlatformException(
119 format(tr("Error loading {0}"), platformsFile.getAbsolutePath()), e);
120 }
121
122 // Allow overriding values in platform.txt. This allows changing
123 // platform.txt (e.g. to use a system-wide toolchain), without
124 // having to modify platform.txt (which, when running from git,
125 // prevents files being marked as changed).
126 File localPlatformsFile = new File(folder, "platform.local.txt");
127 try {
128 if (localPlatformsFile.exists() && localPlatformsFile.canRead()) {
129 preferences.load(localPlatformsFile);
130 }
131 } catch (IOException e) {
132 throw new TargetPlatformException(
133 format(tr("Error loading {0}"), localPlatformsFile.getAbsolutePath()), e);
134 }
135
136 if (!preferences.containsKey("rewriting") || !"disabled".equals(preferences.get("rewriting"))) {
137 try {
138 rewriteKeysOfOldPlatformsTxtAndWarnAboutIt();
139 } catch (IOException e) {
140 throw new TargetPlatformException(e);
141 }
142 }
143
144 File progFile = new File(folder, "programmers.txt");
145 try {
146 if (progFile.exists() && progFile.canRead()) {
147 PreferencesMap prefs = new PreferencesMap();
148 prefs.load(progFile);
149 programmers = prefs.firstLevelMap();
150 }
151 } catch (IOException e) {
152 throw new TargetPlatformException(format(tr("Error loading {0}"),
153 progFile.getAbsolutePath()), e);
154 }
155 }
156
157 private void rewriteKeysOfOldPlatformsTxtAndWarnAboutIt() throws IOException {
•158 File platformRewrite = new File("/usr/share/arduino-builder/platform.keys.rewrite.txt");
159 PreferencesMap platformRewriteProps = new PreferencesMap(platformRewrite);
160
161 PreferencesMap oldProps = platformRewriteProps.subTree("old");
162 PreferencesMap newProps = platformRewriteProps.subTree("new");
163
164 for (Map.Entry<String, String> entry : oldProps.entrySet()) {
165 String preferencesKey = entry.getKey().substring(entry.getKey().indexOf(".") + 1);
166 if (preferences.containsKey(preferencesKey) && entry.getValue().equals(preferences.get(preferencesKey))) {
167 preferences.put(preferencesKey, newProps.get(entry.getKey()));
168 }
169 }
170
171 PreferencesMap addedProps = platformRewriteProps.subTree("added");
172 for (Map.Entry<String, String> entry : addedProps.entrySet()) {
173 String keyToAdd = entry.getKey();
174 String[] keyToAddParts = keyToAdd.split("\\.");
175 String keyToAddFirstLevel = keyToAddParts[0];
176 String keyToAddSecondLevel = keyToAddParts[0] + "." + keyToAddParts[1];
177 if (!preferences.subTree(keyToAddFirstLevel).isEmpty() && !preferences.subTree(keyToAddSecondLevel).isEmpty() && !preferences.containsKey(keyToAdd)) {
178 preferences.put(keyToAdd, entry.getValue());
179 }
180 }
181 }
182
183 @Override
184 public String getId() {
185 return id;
186 }
187
188 @Override
189 public File getFolder() {
190 return folder;
191 }
192
193 @Override
194 public Map<String, TargetBoard> getBoards() {
195 return boards;
196 }
197
198 @Override
199 public PreferencesMap getCustomMenus() {
200 return customMenus;
201 }
202
203 @Override
204 public Set<String> getCustomMenuIds() {
205 return customMenus.keySet();
206 }
207
208 @Override
209 public Map<String, PreferencesMap> getProgrammers() {
210 return programmers;
211 }
212
213 @Override
214 public PreferencesMap getProgrammer(String programmer) {
215 return getProgrammers().get(programmer);
216 }
217
218 @Override
219 public PreferencesMap getTool(String tool) {
220 return getPreferences().subTree("tools").subTree(tool);
221 }
222
223 @Override
224 public PreferencesMap getPreferences() {
225 return preferences;
226 }
227
228 @Override
229 public TargetBoard getBoard(String boardId) {
230 if (boards.containsKey(boardId)) {
231 return boards.get(boardId);
232 }
233 return defaultBoard;
234 }
235
236 @Override
237 public TargetPackage getContainerPackage() {
238 return containerPackage;
239 }
240
241 @Override
242 public String toString() {
243 String res = "TargetPlatform: name=" + id + " boards={\n";
244 for (String boardId : boards.keySet())
245 res += " " + boardId + " = " + boards.get(boardId) + "\n";
246 return res + "}";
247 }
248
249 @Override
250 public boolean isInSketchbook() {
251 return getFolder().getAbsolutePath().startsWith(BaseNoGui.getSketchbookHardwareFolder().getAbsolutePath());
252 }
253 }