| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 75 |
File propertiesFile = new File(libFolder, "library.properties"); |
| 76 |
PreferencesMap properties = new PreferencesMap(); |
| 77 |
properties.load(propertiesFile); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if (!properties.containsKey("maintainer") && properties.containsKey("email")) { |
| 85 |
properties.put("maintainer", properties.get("email")); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|
| 95 |
for (String p : Constants.LIBRARY_MANDATORY_PROPERTIES) |
| 96 |
if (!properties.containsKey(p)) |
| 97 |
throw new IOException("Missing '" + p + "' from library"); |
| 98 |
|
| 99 |
|
| 100 |
LibraryLayout layout; |
| 101 |
File srcFolder = new File(libFolder, "src"); |
| 102 |
|
| 103 |
if (srcFolder.exists() && srcFolder.isDirectory()) { |
| 104 |
|
| 105 |
layout = LibraryLayout.RECURSIVE; |
| 106 |
} else { |
| 107 |
|
| 108 |
layout = LibraryLayout.FLAT; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
File[] files = libFolder.listFiles(); |
| 113 |
if (files == null) { |
| 114 |
throw new IOException("Unable to list files of library in " + libFolder); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
String architectures = properties.get("architectures"); |
| 119 |
if (architectures == null) |
| 120 |
architectures = "*"; |
| 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; |
| 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 |
} |