Debian Codegraph @

arduino-core/src/processing/app/packages/UserLibrary.java

raw symbols SCIP
1 /*
2 * This file is part of Arduino.
3 *
4 * Copyright 2014 Arduino LLC (http://www.arduino.cc/)
5 *
6 * Arduino is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
28 */
29 package processing.app.packages;
30
31 import static processing.app.I18n.format;
32 import static processing.app.I18n.tr;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.Optional;
42
43 import com.github.zafarkhaja.semver.Version;
44
45 import cc.arduino.Constants;
46 import cc.arduino.contributions.VersionHelper;
47 import cc.arduino.contributions.libraries.ContributedLibraryDependency;
48 import processing.app.helpers.PreferencesMap;
49 import processing.app.packages.UserLibraryFolder.Location;
50
51 public class UserLibrary {
52
53 private String name;
54 private String version;
55 private String author;
56 private String maintainer;
57 private String sentence;
58 private String paragraph;
59 private String website;
60 private String category;
61 private String license;
62 private List<String> architectures;
63 private List<String> types = new ArrayList<>();
64 private List<String> declaredTypes;
65 private boolean onGoingDevelopment;
66 private List<String> includes;
67 protected File installedFolder;
68 protected Location location;
69
70 public static UserLibrary create(UserLibraryFolder libFolderDesc) throws IOException {
71 File libFolder = libFolderDesc.folder;
72 Location location = libFolderDesc.location;
73
74 // Parse metadata
75 File propertiesFile = new File(libFolder, "library.properties");
76 PreferencesMap properties = new PreferencesMap();
77 properties.load(propertiesFile);
78
79 // Library sanity checks
80 // ---------------------
81
82 // Compatibility with 1.5 rev.1 libraries:
83 // "email" field changed to "maintainer"
84 if (!properties.containsKey("maintainer") && properties.containsKey("email")) {
85 properties.put("maintainer", properties.get("email"));
86 }
87
88 // Compatibility with 1.5 rev.1 libraries:
89 // "arch" folder no longer supported
90 File archFolder = new File(libFolder, "arch");
91 if (archFolder.isDirectory())
92 throw new IOException("'arch' folder is no longer supported! See http://goo.gl/gfFJzU for more information");
93
94 // Check mandatory properties
95 for (String p : Constants.LIBRARY_MANDATORY_PROPERTIES)
96 if (!properties.containsKey(p))
97 throw new IOException("Missing '" + p + "' from library");
98
99 // Check layout
100 LibraryLayout layout;
101 File srcFolder = new File(libFolder, "src");
102
103 if (srcFolder.exists() && srcFolder.isDirectory()) {
104 // Layout with a single "src" folder and recursive compilation
105 layout = LibraryLayout.RECURSIVE;
106 } else {
107 // Layout with source code on library's root and "utility" folders
108 layout = LibraryLayout.FLAT;
109 }
110
111 // Warn if root folder contains development leftovers
112 File[] files = libFolder.listFiles();
113 if (files == null) {
114 throw new IOException("Unable to list files of library in " + libFolder);
115 }
116
117 // Extract metadata info
118 String architectures = properties.get("architectures");
119 if (architectures == null)
120 architectures = "*"; // defaults to "any"
121 List<String> archs = new ArrayList<>();
122 for (String arch : architectures.split(","))
123 archs.add(arch.trim());
124
125 String category = properties.get("category");
126 if (category == null) {
127 category = "Uncategorized";
128 }
129 if (!Constants.LIBRARY_CATEGORIES.contains(category)) {
130 category = "Uncategorized";
131 }
132
133 String license = properties.get("license");
134 if (license == null) {
135 license = "Unspecified";
136 }
137
138 String types = properties.get("types");
139 if (types == null) {
140 types = "Contributed";
141 }
142 List<String> typesList = new LinkedList<>();
143 for (String type : types.split(",")) {
144 typesList.add(type.trim());
145 }
146
147 List<String> includes = null;
148 if (properties.containsKey("includes") && !properties.get("includes").trim().isEmpty()) {
149 includes = new ArrayList<>();
150 for (String i : properties.get("includes").split(","))
151 includes.add(i.trim());
152 }
153
154 String declaredVersion = properties.get("version").trim();
155 Optional<Version> version = VersionHelper.valueOf(declaredVersion);
156 if (!version.isPresent()) {
157 System.out.println(
158 format(tr("Invalid version '{0}' for library in: {1}"), declaredVersion, libFolder.getAbsolutePath()));
159 }
160
161 UserLibrary res = new UserLibrary();
162 res.installedFolder = libFolder;
163 res.name = properties.get("name").trim();
164 res.version = version.isPresent() ? version.get().toString() : declaredVersion;
165 res.author = properties.get("author").trim();
166 res.maintainer = properties.get("maintainer").trim();
167 res.sentence = properties.get("sentence").trim();
168 res.paragraph = properties.get("paragraph").trim();
169 res.website = properties.get("url").trim();
170 res.category = category.trim();
171 res.license = license.trim();
172 res.architectures = archs;
173 res.layout = layout;
174 res.declaredTypes = typesList;
175 res.onGoingDevelopment = Files.exists(Paths.get(libFolder.getAbsolutePath(), Constants.LIBRARY_DEVELOPMENT_FLAG_FILE));
176 res.includes = includes;
177 res.location = location;
178 return res;
179 }
180
181 public String getName() {
182 return name;
183 }
184
185 public List<String> getArchitectures() {
186 return architectures;
187 }
188
189 public String getAuthor() {
190 return author;
191 }
192
193 public String getParagraph() {
194 return paragraph;
195 }
196
197 public String getSentence() {
198 return sentence;
199 }
200
201 public String getWebsite() {
202 return website;
203 }
204
205 public String getCategory() {
206 return category;
207 }
208
209 public List<String> getTypes() {
210 return types;
211 }
212
213 public void setTypes(List<String> types) {
214 this.types = types;
215 }
216
217 public String getLicense() {
218 return license;
219 }
220
221 public void setCategory(String category) {
222 this.category = category;
223 }
224
225 public String getVersion() {
226 return version;
227 }
228
229 public String getMaintainer() {
230 return maintainer;
231 }
232
233 public List<ContributedLibraryDependency> getRequires() {
234 return null;
235 }
236
237 public List<String> getDeclaredTypes() {
238 return declaredTypes;
239 }
240
241 public boolean onGoingDevelopment() {
242 return onGoingDevelopment;
243 }
244
245 public List<String> getIncludes() {
246 return includes;
247 }
248
249 protected enum LibraryLayout {
250 FLAT, RECURSIVE
251 }
252
253 protected LibraryLayout layout;
254
255 public File getSrcFolder() {
256 switch (layout) {
257 case FLAT:
258 return installedFolder;
259 case RECURSIVE:
260 return new File(installedFolder, "src");
261 default:
262 return null; // Keep compiler happy :-(
263 }
264 }
265
266 public boolean useRecursion() {
267 return (layout == LibraryLayout.RECURSIVE);
268 }
269
270 public Location getLocation() {
271 return location;
272 }
273
274 public boolean isIDEBuiltIn() {
275 return getLocation() == Location.IDE_BUILTIN;
276 }
277
278 @Override
279 public String toString() {
280 return name + ":" + version + " " + architectures + " " + location;
281 }
282
283 public File getInstalledFolder() {
284 return installedFolder;
285 }
286
287 }