mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-19 09:42:11 +03:00
Split IDE into 2 projects.
BEWARE: HIGHLY EXPERIMENTAL BRANCH
This commit is contained in:
278
arduino-core/src/processing/app/packages/Library.java
Normal file
278
arduino-core/src/processing/app/packages/Library.java
Normal file
@ -0,0 +1,278 @@
|
||||
package processing.app.packages;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import processing.app.helpers.FileUtils;
|
||||
import processing.app.helpers.PreferencesMap;
|
||||
|
||||
public class Library {
|
||||
|
||||
private String name;
|
||||
private String version;
|
||||
private String author;
|
||||
private String maintainer;
|
||||
private String sentence;
|
||||
private String paragraph;
|
||||
private String url;
|
||||
private String category;
|
||||
private String license;
|
||||
private List<String> architectures;
|
||||
private File folder;
|
||||
private boolean isLegacy;
|
||||
|
||||
private enum LibraryLayout { FLAT, RECURSIVE };
|
||||
private LibraryLayout layout;
|
||||
|
||||
private static final List<String> MANDATORY_PROPERTIES = Arrays
|
||||
.asList(new String[] { "name", "version", "author", "maintainer",
|
||||
"sentence", "paragraph", "url" });
|
||||
|
||||
private static final List<String> CATEGORIES = Arrays.asList(new String[] {
|
||||
"Display", "Communication", "Signal Input/Output", "Sensors",
|
||||
"Device Control", "Timing", "Data Storage", "Data Processing", "Other",
|
||||
"Uncategorized" });
|
||||
|
||||
/**
|
||||
* Scans inside a folder and create a Library object out of it. Automatically
|
||||
* detects legacy libraries. Automatically fills metadata from
|
||||
* library.properties file if found.
|
||||
*
|
||||
* @param libFolder
|
||||
* @return
|
||||
*/
|
||||
static public Library create(File libFolder) throws IOException {
|
||||
// A library is considered "new" if it contains a file called
|
||||
// "library.properties"
|
||||
File check = new File(libFolder, "library.properties");
|
||||
if (!check.exists() || !check.isFile())
|
||||
return createLegacyLibrary(libFolder);
|
||||
else
|
||||
return createLibrary(libFolder);
|
||||
}
|
||||
|
||||
private static Library createLibrary(File libFolder) throws IOException {
|
||||
// Parse metadata
|
||||
File propertiesFile = new File(libFolder, "library.properties");
|
||||
PreferencesMap properties = new PreferencesMap();
|
||||
properties.load(propertiesFile);
|
||||
|
||||
// Library sanity checks
|
||||
// ---------------------
|
||||
|
||||
// Compatibility with 1.5 rev.1 libraries:
|
||||
// "email" field changed to "maintainer"
|
||||
if (!properties.containsKey("maintainer") &&
|
||||
properties.containsKey("email"))
|
||||
properties.put("maintainer", properties.get("email"));
|
||||
|
||||
// Compatibility with 1.5 rev.1 libraries:
|
||||
// "arch" folder no longer supported
|
||||
File archFolder = new File(libFolder, "arch");
|
||||
if (archFolder.isDirectory())
|
||||
throw new IOException("'arch' folder is no longer supported! See "
|
||||
+ "http://goo.gl/gfFJzU for more information");
|
||||
|
||||
// Check mandatory properties
|
||||
for (String p : MANDATORY_PROPERTIES)
|
||||
if (!properties.containsKey(p))
|
||||
throw new IOException("Missing '" + p + "' from library");
|
||||
|
||||
// Check layout
|
||||
LibraryLayout layout;
|
||||
File srcFolder = new File(libFolder, "src");
|
||||
|
||||
if (srcFolder.exists() && srcFolder.isDirectory()) {
|
||||
// Layout with a single "src" folder and recursive compilation
|
||||
layout = LibraryLayout.RECURSIVE;
|
||||
|
||||
File utilFolder = new File(libFolder, "utility");
|
||||
if (utilFolder.exists() && utilFolder.isDirectory()) {
|
||||
throw new IOException(
|
||||
"Library can't use both 'src' and 'utility' folders.");
|
||||
}
|
||||
} else {
|
||||
// Layout with source code on library's root and "utility" folders
|
||||
layout = LibraryLayout.FLAT;
|
||||
}
|
||||
|
||||
// Warn if root folder contains development leftovers
|
||||
for (File file : libFolder.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
if (FileUtils.isSCCSOrHiddenFile(file)) {
|
||||
System.out.println("WARNING: Spurious " + file.getName() +
|
||||
" folder in '" + properties.get("name") + "' library");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract metadata info
|
||||
String architectures = properties.get("architectures");
|
||||
if (architectures == null)
|
||||
architectures = "*"; // defaults to "any"
|
||||
List<String> archs = new ArrayList<String>();
|
||||
for (String arch : architectures.split(","))
|
||||
archs.add(arch.trim());
|
||||
|
||||
String category = properties.get("category");
|
||||
if (category == null)
|
||||
category = "Uncategorized";
|
||||
if (!CATEGORIES.contains(category)) {
|
||||
category = "Uncategorized";
|
||||
System.out.println("WARNING: Category '" + category + "' in library " +
|
||||
properties.get("name") + " is not valid. Setting to 'Uncategorized'");
|
||||
}
|
||||
|
||||
String license = properties.get("license");
|
||||
if (license == null)
|
||||
license = "Unspecified";
|
||||
|
||||
Library res = new Library();
|
||||
res.folder = libFolder;
|
||||
res.name = properties.get("name").trim();
|
||||
res.version = properties.get("version").trim();
|
||||
res.author = properties.get("author").trim();
|
||||
res.maintainer = properties.get("maintainer").trim();
|
||||
res.sentence = properties.get("sentence").trim();
|
||||
res.paragraph = properties.get("paragraph").trim();
|
||||
res.url = properties.get("url").trim();
|
||||
res.category = category.trim();
|
||||
res.license = license.trim();
|
||||
res.architectures = archs;
|
||||
res.isLegacy = false;
|
||||
res.layout = layout;
|
||||
return res;
|
||||
}
|
||||
|
||||
private static Library createLegacyLibrary(File libFolder) {
|
||||
// construct an old style library
|
||||
Library res = new Library();
|
||||
res.folder = libFolder;
|
||||
res.layout = LibraryLayout.FLAT;
|
||||
res.name = libFolder.getName();
|
||||
res.architectures = Arrays.asList("*");
|
||||
res.isLegacy = true;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <b>true</b> if the library declares to support the specified
|
||||
* architecture (through the "architectures" property field).
|
||||
*
|
||||
* @param reqArch
|
||||
* @return
|
||||
*/
|
||||
public boolean supportsArchitecture(String reqArch) {
|
||||
return architectures.contains(reqArch) || architectures.contains("*");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <b>true</b> if the library declares to support at least one of the
|
||||
* specified architectures.
|
||||
*
|
||||
* @param reqArchs
|
||||
* A List of architectures to check
|
||||
* @return
|
||||
*/
|
||||
public boolean supportsArchitecture(List<String> reqArchs) {
|
||||
if (reqArchs.contains("*"))
|
||||
return true;
|
||||
for (String reqArch : reqArchs)
|
||||
if (supportsArchitecture(reqArch))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static final Comparator<Library> CASE_INSENSITIVE_ORDER = new Comparator<Library>() {
|
||||
@Override
|
||||
public int compare(Library o1, Library o2) {
|
||||
return o1.getName().compareToIgnoreCase(o2.getName());
|
||||
}
|
||||
};
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public File getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
public List<String> getArchitectures() {
|
||||
return architectures;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public String getParagraph() {
|
||||
return paragraph;
|
||||
}
|
||||
|
||||
public String getSentence() {
|
||||
return sentence;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public String getLicense() {
|
||||
return license;
|
||||
}
|
||||
|
||||
public static List<String> getCategories() {
|
||||
return CATEGORIES;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String getMaintainer() {
|
||||
return maintainer;
|
||||
}
|
||||
|
||||
public boolean useRecursion() {
|
||||
return (layout == LibraryLayout.RECURSIVE);
|
||||
}
|
||||
|
||||
public File getSrcFolder() {
|
||||
switch (layout) {
|
||||
case FLAT:
|
||||
return folder;
|
||||
case RECURSIVE:
|
||||
return new File(folder, "src");
|
||||
default:
|
||||
return null; // Keep compiler happy :-(
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLegacy() {
|
||||
return isLegacy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String res = "Library:";
|
||||
res += " (name=" + name + ")";
|
||||
res += " (version=" + version + ")";
|
||||
res += " (author=" + author + ")";
|
||||
res += " (maintainer=" + maintainer + ")";
|
||||
res += " (sentence=" + sentence + ")";
|
||||
res += " (paragraph=" + paragraph + ")";
|
||||
res += " (url=" + url + ")";
|
||||
res += " (architectures=" + architectures + ")";
|
||||
return res;
|
||||
}
|
||||
}
|
70
arduino-core/src/processing/app/packages/LibraryList.java
Normal file
70
arduino-core/src/processing/app/packages/LibraryList.java
Normal file
@ -0,0 +1,70 @@
|
||||
package processing.app.packages;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import processing.app.helpers.FileUtils;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class LibraryList extends ArrayList<Library> {
|
||||
|
||||
public LibraryList(LibraryList libs) {
|
||||
super(libs);
|
||||
}
|
||||
|
||||
public LibraryList() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Library getByName(String name) {
|
||||
for (Library l : this)
|
||||
if (l.getName().equals(name))
|
||||
return l;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addOrReplace(Library lib) {
|
||||
Library l = getByName(lib.getName());
|
||||
if (l != null)
|
||||
remove(l);
|
||||
add(lib);
|
||||
}
|
||||
|
||||
public void addOrReplaceAll(Collection<? extends Library> c) {
|
||||
for (Library l : c)
|
||||
addOrReplace(l);
|
||||
}
|
||||
|
||||
public void sort() {
|
||||
Collections.sort(this, Library.CASE_INSENSITIVE_ORDER);
|
||||
}
|
||||
|
||||
public Library search(String name, String arch) {
|
||||
for (Library lib : this) {
|
||||
if (!lib.getName().equals(name))
|
||||
continue;
|
||||
if (!lib.supportsArchitecture(arch))
|
||||
continue;
|
||||
return lib;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public LibraryList filterByArchitecture(String reqArch) {
|
||||
LibraryList res = new LibraryList();
|
||||
for (Library lib : this)
|
||||
if (lib.supportsArchitecture(reqArch))
|
||||
res.add(lib);
|
||||
return res;
|
||||
}
|
||||
|
||||
public LibraryList filterLibrariesInSubfolder(File subFolder) {
|
||||
LibraryList res = new LibraryList();
|
||||
for (Library lib : this)
|
||||
if (FileUtils.isSubDirectory(subFolder, lib.getFolder()))
|
||||
res.add(lib);
|
||||
return res;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user