forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
566 lines (465 loc) · 19 KB
/
Library.java
File metadata and controls
566 lines (465 loc) · 19 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package processing.app;
import java.io.*;
import java.util.*;
import processing.app.contrib.*;
import processing.core.*;
import processing.data.StringDict;
import processing.data.StringList;
public class Library extends LocalContribution {
static final String[] platformNames = PConstants.platformNames;
//protected File folder; // /path/to/shortname
protected File libraryFolder; // shortname/library
protected File examplesFolder; // shortname/examples
protected File referenceFile; // shortname/reference/index.html
/**
* Subfolder for grouping libraries in a menu. Basic subfolder support
* is provided so that some organization can be done in the import menu.
* (This is the replacement for the "library compilation" type.)
*/
protected String group;
/** Packages provided by this library. */
StringList packageList;
/** Per-platform exports for this library. */
HashMap<String, String[]> exportList;
/** Applet exports (cross-platform by definition). */
String[] appletExportList;
/** Android exports (single platform for now, may not exist). */
String[] androidExportList;
/** True if there are separate 32/64 bit for the specified platform index. */
boolean[] multipleArch = new boolean[platformNames.length];
/**
* For runtime, the native library path for this platform. e.g. on Windows 64,
* this might be the windows64 subfolder with the library.
*/
String nativeLibraryPath;
static public final String propertiesFileName = "library.properties";
/**
* Filter to pull out just files and none of the platform-specific
* directories, and to skip export.txt. As of 2.0a2, other directories are
* included, because we need things like the 'plugins' subfolder w/ video.
*/
static FilenameFilter standardFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
// skip .DS_Store files, .svn folders, etc
if (name.charAt(0) == '.') return false;
if (name.equals("CVS")) return false;
if (name.equals("export.txt")) return false;
File file = new File(dir, name);
// return (!file.isDirectory());
if (file.isDirectory()) {
if (name.equals("macosx")) return false;
if (name.equals("macosx32")) return false;
if (name.equals("macosx64")) return false;
if (name.equals("windows")) return false;
if (name.equals("windows32")) return false;
if (name.equals("windows64")) return false;
if (name.equals("linux")) return false;
if (name.equals("linux32")) return false;
if (name.equals("linux64")) return false;
if (name.equals("linux-armv6hf")) return false;
if (name.equals("android")) return false;
}
return true;
}
};
static FilenameFilter jarFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.charAt(0) == '.') return false; // skip ._blah.jar crap on OS X
if (new File(dir, name).isDirectory()) return false;
String lc = name.toLowerCase();
return lc.endsWith(".jar") || lc.endsWith(".zip");
}
};
static public Library load(File folder) {
try {
return new Library(folder);
// } catch (IgnorableException ig) {
// Base.log(ig.getMessage());
} catch (Error err) {
// Handles UnsupportedClassVersionError and others
err.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public Library(File folder) {
this(folder, null);
}
private Library(File folder, String groupName) {
super(folder);
this.group = groupName;
libraryFolder = new File(folder, "library");
examplesFolder = new File(folder, "examples");
referenceFile = new File(folder, "reference/index.html");
handle();
}
/**
* Handles all the Java-specific parsing for library handling.
*/
protected void handle() {
File exportSettings = new File(libraryFolder, "export.txt");
StringDict exportTable = exportSettings.exists() ?
Util.readSettings(exportSettings) : new StringDict();
exportList = new HashMap<String, String[]>();
// get the list of files just in the library root
String[] baseList = libraryFolder.list(standardFilter);
// System.out.println("Loading " + name + "...");
// PApplet.println(baseList);
String appletExportStr = exportTable.get("applet");
if (appletExportStr != null) {
appletExportList = PApplet.splitTokens(appletExportStr, ", ");
} else {
appletExportList = baseList;
}
String androidExportStr = exportTable.get("android");
if (androidExportStr != null) {
androidExportList = PApplet.splitTokens(androidExportStr, ", ");
} else {
androidExportList = baseList;
}
// for the host platform, need to figure out what's available
File nativeLibraryFolder = libraryFolder;
String hostPlatform = Platform.getName();
// System.out.println("1 native lib folder now " + nativeLibraryFolder);
// see if there's a 'windows', 'macosx', or 'linux' folder
File hostLibrary = new File(libraryFolder, hostPlatform);
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
}
// System.out.println("2 native lib folder now " + nativeLibraryFolder);
// check for bit-specific version, e.g. on windows, check if there
// is a window32 or windows64 folder (on windows)
hostLibrary =
new File(libraryFolder, hostPlatform + Platform.getNativeBits());
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
}
// System.out.println("3 native lib folder now " + nativeLibraryFolder);
if (hostPlatform.equals("linux") && System.getProperty("os.arch").equals("arm")) {
hostLibrary = new File(libraryFolder, "linux-armv6hf");
if (hostLibrary.exists()) {
nativeLibraryFolder = hostLibrary;
}
}
// save that folder for later use
nativeLibraryPath = nativeLibraryFolder.getAbsolutePath();
// for each individual platform that this library supports, figure out what's around
for (int i = 1; i < platformNames.length; i++) {
String platformName = platformNames[i];
String platformName32 = platformName + "32";
String platformName64 = platformName + "64";
String platformNameArmv6hh = platformName + "-armv6hf";
// First check for things like 'application.macosx=' or 'application.windows32' in the export.txt file.
// These will override anything in the platform-specific subfolders.
String platformAll = exportTable.get("application." + platformName);
String[] platformList = platformAll == null ? null : PApplet.splitTokens(platformAll, ", ");
String platform32 = exportTable.get("application." + platformName + "32");
String[] platformList32 = platform32 == null ? null : PApplet.splitTokens(platform32, ", ");
String platform64 = exportTable.get("application." + platformName + "64");
String[] platformList64 = platform64 == null ? null : PApplet.splitTokens(platform64, ", ");
String platformArmv6hf = exportTable.get("application." + platformName + "-armv6hf");
String[] platformListArmv6hf = platformArmv6hf == null ? null : PApplet.splitTokens(platformArmv6hf, ", ");
// If nothing specified in the export.txt entries, look for the platform-specific folders.
if (platformAll == null) {
platformList = listPlatformEntries(libraryFolder, platformName, baseList);
}
if (platform32 == null) {
platformList32 = listPlatformEntries(libraryFolder, platformName32, baseList);
}
if (platform64 == null) {
platformList64 = listPlatformEntries(libraryFolder, platformName64, baseList);
}
if (platformListArmv6hf == null) {
platformListArmv6hf = listPlatformEntries(libraryFolder, platformNameArmv6hh, baseList);
}
if (platformList32 != null || platformList64 != null || platformListArmv6hf != null) {
multipleArch[i] = true;
}
// if there aren't any relevant imports specified or in their own folders,
// then use the baseList (root of the library folder) as the default.
if (platformList == null && platformList32 == null && platformList64 == null && platformListArmv6hf == null) {
exportList.put(platformName, baseList);
} else {
// once we've figured out which side our bread is buttered on, save it.
// (also concatenate the list of files in the root folder as well
if (platformList != null) {
exportList.put(platformName, platformList);
}
if (platformList32 != null) {
exportList.put(platformName32, platformList32);
}
if (platformList64 != null) {
exportList.put(platformName64, platformList64);
}
if (platformListArmv6hf != null) {
exportList.put(platformNameArmv6hh, platformListArmv6hf);
}
}
}
// for (String p : exportList.keySet()) {
// System.out.println(p + " -> ");
// PApplet.println(exportList.get(p));
// }
// get the path for all .jar files in this code folder
packageList = Util.packageListFromClassPath(getClassPath());
}
/**
* List who's inside a windows64, macosx, linux32, etc folder.
*/
static String[] listPlatformEntries(File libraryFolder, String folderName, String[] baseList) {
File folder = new File(libraryFolder, folderName);
if (folder.exists()) {
String[] entries = folder.list(standardFilter);
if (entries != null) {
String[] outgoing = new String[entries.length + baseList.length];
for (int i = 0; i < entries.length; i++) {
outgoing[i] = folderName + "/" + entries[i];
}
// Copy the base libraries in there as well
System.arraycopy(baseList, 0, outgoing, entries.length, baseList.length);
return outgoing;
}
}
return null;
}
static protected HashMap<String, Object> packageWarningMap = new HashMap<String, Object>();
/**
* Add the packages provided by this library to the master list that maps
* imports to specific libraries.
* @param importToLibraryTable mapping from package names to Library objects
*/
// public void addPackageList(HashMap<String,Library> importToLibraryTable) {
public void addPackageList(Map<String, List<Library>> importToLibraryTable) {
// PApplet.println(packages);
for (String pkg : packageList) {
// pw.println(pkg + "\t" + libraryFolder.getAbsolutePath());
// PApplet.println(pkg + "\t" + getName());
// Library library = importToLibraryTable.get(pkg);
List<Library> libraries = importToLibraryTable.get(pkg);
if (libraries == null) {
libraries = new ArrayList<Library>();
importToLibraryTable.put(pkg, libraries);
} else {
if (Base.DEBUG) {
System.err.println("The library found in");
System.err.println(getPath());
System.err.println("conflicts with");
for (Library library : libraries) {
System.err.println(library.getPath());
}
System.err.println("which already define(s) the package " + pkg);
System.err.println("If you have a line in your sketch that reads");
System.err.println("import " + pkg + ".*;");
System.err.println("Then you'll need to first remove one of those libraries.");
System.err.println();
}
}
libraries.add(this);
}
}
public boolean hasExamples() {
return examplesFolder.exists();
}
public File getExamplesFolder() {
return examplesFolder;
}
public String getGroup() {
return group;
}
public String getPath() {
return folder.getAbsolutePath();
}
public String getLibraryPath() {
return libraryFolder.getAbsolutePath();
}
public String getJarPath() {
//return new File(folder, "library/" + name + ".jar").getAbsolutePath();
return new File(libraryFolder, folder.getName() + ".jar").getAbsolutePath();
}
// this prepends a colon so that it can be appended to other paths safely
public String getClassPath() {
StringBuilder cp = new StringBuilder();
// PApplet.println(libraryFolder.getAbsolutePath());
// PApplet.println(libraryFolder.list());
String[] jarHeads = libraryFolder.list(jarFilter);
for (String jar : jarHeads) {
cp.append(File.pathSeparatorChar);
cp.append(new File(libraryFolder, jar).getAbsolutePath());
}
File nativeLibraryFolder = new File(nativeLibraryPath);
if (!libraryFolder.equals(nativeLibraryFolder)) {
jarHeads = new File(nativeLibraryPath).list(jarFilter);
for (String jar : jarHeads) {
cp.append(File.pathSeparatorChar);
cp.append(new File(nativeLibraryPath, jar).getAbsolutePath());
}
}
//cp.setLength(cp.length() - 1); // remove the last separator
return cp.toString();
}
public String getNativePath() {
// PApplet.println("native lib folder " + nativeLibraryPath);
return nativeLibraryPath;
}
// public String[] getAppletExports() {
// return appletExportList;
// }
protected File[] wrapFiles(String[] list) {
File[] outgoing = new File[list.length];
for (int i = 0; i < list.length; i++) {
outgoing[i] = new File(libraryFolder, list[i]);
}
return outgoing;
}
/**
* Applet exports don't go by platform, since by their nature applets are
* meant to be cross-platform. Technically, you could have a situation where
* you want to export applet code for different platforms, but it's too
* obscure a case that we're not interested in supporting it.
*/
public File[] getAppletExports() {
return wrapFiles(appletExportList);
}
public File[] getApplicationExports(int platform, String variant) {
String[] list = getApplicationExportList(platform, variant);
return wrapFiles(list);
}
/**
* Returns the necessary exports for the specified platform.
* If no 32 or 64-bit version of the exports exists, it returns the version
* that doesn't specify bit depth.
*/
public String[] getApplicationExportList(int platform, String variant) {
String platformName = PConstants.platformNames[platform];
if (variant.equals("32")) {
String[] pieces = exportList.get(platformName + "32");
if (pieces != null) return pieces;
} else if (variant.equals("64")) {
String[] pieces = exportList.get(platformName + "64");
if (pieces != null) return pieces;
} else if (variant.equals("armv6hf")) {
String[] pieces = exportList.get(platformName + "-armv6hf");
if (pieces != null) return pieces;
}
return exportList.get(platformName);
}
public File[] getAndroidExports() {
return wrapFiles(androidExportList);
}
// public boolean hasMultiplePlatforms() {
// return false;
// }
public boolean hasMultipleArch(int platform) {
return multipleArch[platform];
}
public boolean supportsArch(int platform, String variant) {
// If this is a universal library, or has no natives, then we're good.
if (multipleArch[platform] == false) {
return true;
}
return getApplicationExportList(platform, variant) != null;
}
static public boolean hasMultipleArch(int platform, List<Library> libraries) {
for (Library library : libraries) {
if (library.hasMultipleArch(platform)) {
return true;
}
}
return false;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static protected FilenameFilter junkFolderFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
// skip .DS_Store files, .svn folders, etc
if (name.charAt(0) == '.') return false;
if (name.equals("CVS")) return false;
return (new File(dir, name).isDirectory());
}
};
static public List<File> discover(File folder) {
List<File> libraries = new ArrayList<File>();
String[] folderNames = folder.list(junkFolderFilter);
// if a bad folder or something like that, this might come back null
if (folderNames != null) {
// alphabetize list, since it's not always alpha order
// replaced hella slow bubble sort with this feller for 0093
Arrays.sort(folderNames, String.CASE_INSENSITIVE_ORDER);
for (String potentialName : folderNames) {
File baseFolder = new File(folder, potentialName);
File libraryFolder = new File(baseFolder, "library");
File libraryJar = new File(libraryFolder, potentialName + ".jar");
// If a .jar file of the same prefix as the folder exists
// inside the 'library' subfolder of the sketch
if (libraryJar.exists()) {
String sanityCheck = Sketch.sanitizeName(potentialName);
if (sanityCheck.equals(potentialName)) {
libraries.add(baseFolder);
} else {
String mess = "The library \""
+ potentialName
+ "\" cannot be used.\n"
+ "Library names must contain only basic letters and numbers.\n"
+ "(ASCII only and no spaces, and it cannot start with a number)";
Messages.showMessage("Ignoring bad library name", mess);
continue;
}
/*
} else { // maybe it's a JS library
// TODO this should be in a better location
File jsLibrary = new File(libraryFolder, potentialName + ".js");
if (jsLibrary.exists()) {
libraries.add(baseFolder);
}
*/
}
}
}
return libraries;
}
static public List<Library> list(File folder) {
List<Library> libraries = new ArrayList<Library>();
List<File> librariesFolders = new ArrayList<File>();
librariesFolders.addAll(discover(folder));
for (File baseFolder : librariesFolders) {
libraries.add(new Library(baseFolder));
}
// Support libraries inside of one level of subfolders? I believe this was
// the compromise for supporting library groups, but probably a bad idea
// because it's not compatible with the Manager.
String[] folderNames = folder.list(junkFolderFilter);
if (folderNames != null) {
for (String subfolderName : folderNames) {
File subfolder = new File(folder, subfolderName);
if (!librariesFolders.contains(subfolder)) {
List<File> discoveredLibFolders = discover(subfolder);
for (File discoveredFolder : discoveredLibFolders) {
libraries.add(new Library(discoveredFolder, subfolderName));
}
}
}
}
return libraries;
}
public ContributionType getType() {
return ContributionType.LIBRARY;
}
/**
* Returns the object stored in the referenceFile field, which contains an
* instance of the file object representing the index file of the reference
*
* @return referenceFile
*/
public File getReferenceIndexFile() {
return referenceFile;
}
/**
* Tests whether the reference's index file indicated by referenceFile exists.
*
* @return true if and only if the file denoted by referenceFile exists; false
* otherwise.
*/
public boolean hasReference() {
return referenceFile.exists();
}
}