Skip to content

Commit c666274

Browse files
bitroncmaglie
authored andcommitted
Moved libraries and importToLibraryTable (and related methods) from Base to BaseNoGui (work in progress).
1 parent 64c6fe5 commit c666274

File tree

4 files changed

+93
-52
lines changed

4 files changed

+93
-52
lines changed

app/src/processing/app/Base.java

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ public class Base {
8686

8787
static private List<File> librariesFolders;
8888

89-
// maps library name to their library folder
90-
static private LibraryList libraries;
91-
92-
// maps #included files to their library folder
93-
public static Map<String, Library> importToLibraryTable;
94-
9589
// classpath for all known libraries for p5
9690
// (both those in the p5/libs folder and those with lib subfolders
9791
// found in the sketchbook)
@@ -1224,17 +1218,15 @@ protected void rebuildSketchbookMenu(JMenu menu) {
12241218
}
12251219

12261220
public LibraryList getIDELibs() {
1227-
if (libraries == null)
1221+
if (getLibraries() == null)
12281222
return new LibraryList();
1229-
LibraryList res = new LibraryList(libraries);
1223+
LibraryList res = new LibraryList(getLibraries());
12301224
res.removeAll(getUserLibs());
12311225
return res;
12321226
}
12331227

12341228
public LibraryList getUserLibs() {
1235-
if (libraries == null)
1236-
return new LibraryList();
1237-
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
1229+
return BaseNoGui.getUserLibs();
12381230
}
12391231

12401232
public void rebuildImportMenu(JMenu importMenu) {
@@ -1311,42 +1303,11 @@ public void rebuildExamplesMenu(JMenu menu) {
13111303
}
13121304

13131305
public LibraryList scanLibraries(List<File> folders) throws IOException {
1314-
LibraryList res = new LibraryList();
1315-
for (File folder : folders)
1316-
res.addOrReplaceAll(scanLibraries(folder));
1317-
return res;
1306+
return BaseNoGui.scanLibraries(folders);
13181307
}
13191308

13201309
public LibraryList scanLibraries(File folder) throws IOException {
1321-
LibraryList res = new LibraryList();
1322-
1323-
String list[] = folder.list(new OnlyDirs());
1324-
// if a bad folder or something like that, this might come back null
1325-
if (list == null)
1326-
return res;
1327-
1328-
for (String libName : list) {
1329-
File subfolder = new File(folder, libName);
1330-
if (!Sketch.isSanitaryName(libName)) {
1331-
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
1332-
+ "Library names must contain only basic letters and numbers.\n"
1333-
+ "(ASCII only and no spaces, and it cannot start with a number)"),
1334-
libName);
1335-
Base.showMessage(_("Ignoring bad library name"), mess);
1336-
continue;
1337-
}
1338-
1339-
try {
1340-
Library lib = Library.create(subfolder);
1341-
// (also replace previously found libs with the same name)
1342-
if (lib != null)
1343-
res.addOrReplace(lib);
1344-
} catch (IOException e) {
1345-
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"),
1346-
subfolder, e.getMessage()));
1347-
}
1348-
}
1349-
return res;
1310+
return BaseNoGui.scanLibraries(folder);
13501311
}
13511312

13521313
public void onBoardOrPortChange() {
@@ -1377,26 +1338,26 @@ public void onBoardOrPortChange() {
13771338
// Libraries located in the latest folders on the list can override
13781339
// other libraries with the same name.
13791340
try {
1380-
libraries = scanLibraries(librariesFolders);
1341+
BaseNoGui.scanAndUpdateLibraries(librariesFolders);
13811342
} catch (IOException e) {
13821343
showWarning(_("Error"), _("Error loading libraries"), e);
13831344
}
13841345

13851346
// Populate importToLibraryTable
1386-
importToLibraryTable = new HashMap<String, Library>();
1387-
for (Library lib : libraries) {
1347+
BaseNoGui.newImportToLibraryTable();
1348+
for (Library lib : getLibraries()) {
13881349
try {
13891350
String headers[] = headerListFromIncludePath(lib.getSrcFolder());
13901351
for (String header : headers) {
1391-
Library old = importToLibraryTable.get(header);
1352+
Library old = BaseNoGui.importToLibraryTable.get(header);
13921353
if (old != null) {
13931354
// If a library was already found with this header, keep
13941355
// it if the library's name matches the header name.
13951356
String name = header.substring(0, header.length() - 2);
13961357
if (old.getFolder().getPath().endsWith(name))
13971358
continue;
13981359
}
1399-
importToLibraryTable.put(header, lib);
1360+
BaseNoGui.importToLibraryTable.put(header, lib);
14001361
}
14011362
} catch (IOException e) {
14021363
showWarning(_("Error"), I18n
@@ -2013,7 +1974,7 @@ static public File createTempFolder(String name) {
20131974

20141975

20151976
static public LibraryList getLibraries() {
2016-
return libraries;
1977+
return BaseNoGui.getLibraries();
20171978
}
20181979

20191980

app/src/processing/app/BaseNoGui.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.util.Arrays;
88
import java.util.HashMap;
9+
import java.util.List;
910
import java.util.Map;
1011

1112
import processing.app.debug.TargetBoard;
@@ -15,7 +16,10 @@
1516
import processing.app.helpers.OSUtils;
1617
import processing.app.helpers.PreferencesMap;
1718
import processing.app.helpers.filefilters.OnlyDirs;
19+
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
1820
import processing.app.legacy.PApplet;
21+
import processing.app.packages.Library;
22+
import processing.app.packages.LibraryList;
1923

2024
public class BaseNoGui {
2125

@@ -29,6 +33,12 @@ public class BaseNoGui {
2933
// commandline
3034
static String currentDirectory = System.getProperty("user.dir");
3135

36+
// maps #included files to their library folder
37+
public static Map<String, Library> importToLibraryTable;
38+
39+
// maps library name to their library folder
40+
static private LibraryList libraries;
41+
3242
static public Map<String, TargetPackage> packages;
3343

3444
static Platform platform;
@@ -102,6 +112,10 @@ static public String getHardwarePath() {
102112
return getHardwareFolder().getAbsolutePath();
103113
}
104114

115+
static public LibraryList getLibraries() {
116+
return libraries;
117+
}
118+
105119
static public Platform getPlatform() {
106120
return platform;
107121
}
@@ -151,6 +165,25 @@ static public TargetPlatform getTargetPlatform(String packageName,
151165
return p.get(platformName);
152166
}
153167

168+
static public LibraryList getUserLibs() {
169+
if (libraries == null)
170+
return new LibraryList();
171+
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
172+
}
173+
174+
/**
175+
* Given a folder, return a list of the header files in that folder (but not
176+
* the header files in its sub-folders, as those should be included from
177+
* within the header files at the top-level).
178+
*/
179+
static public String[] headerListFromIncludePath(File path) throws IOException {
180+
String[] list = path.list(new OnlyFilesWithExtension(".h"));
181+
if (list == null) {
182+
throw new IOException();
183+
}
184+
return list;
185+
}
186+
154187
static public void initPackages() {
155188
packages = new HashMap<String, TargetPackage>();
156189
loadHardware(getHardwareFolder());
@@ -213,6 +246,10 @@ static protected void loadHardware(File folder) {
213246
}
214247
}
215248

249+
static public void newImportToLibraryTable() {
250+
importToLibraryTable = new HashMap<String, Library>();
251+
}
252+
216253
/**
217254
* Spew the contents of a String object out to a file.
218255
*/
@@ -237,4 +274,47 @@ static public void saveFile(String str, File file) throws IOException {
237274
}
238275
}
239276

277+
static public void scanAndUpdateLibraries(List<File> folders) throws IOException {
278+
libraries = scanLibraries(folders);
279+
}
280+
281+
static public LibraryList scanLibraries(List<File> folders) throws IOException {
282+
LibraryList res = new LibraryList();
283+
for (File folder : folders)
284+
res.addOrReplaceAll(scanLibraries(folder));
285+
return res;
286+
}
287+
288+
static public LibraryList scanLibraries(File folder) throws IOException {
289+
LibraryList res = new LibraryList();
290+
291+
String list[] = folder.list(new OnlyDirs());
292+
// if a bad folder or something like that, this might come back null
293+
if (list == null)
294+
return res;
295+
296+
for (String libName : list) {
297+
File subfolder = new File(folder, libName);
298+
if (!Sketch.isSanitaryName(libName)) {
299+
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
300+
+ "Library names must contain only basic letters and numbers.\n"
301+
+ "(ASCII only and no spaces, and it cannot start with a number)"),
302+
libName);
303+
Base.showMessage(_("Ignoring bad library name"), mess);
304+
continue;
305+
}
306+
307+
try {
308+
Library lib = Library.create(subfolder);
309+
// (also replace previously found libs with the same name)
310+
if (lib != null)
311+
res.addOrReplace(lib);
312+
} catch (IOException e) {
313+
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"),
314+
subfolder, e.getMessage()));
315+
}
316+
}
317+
return res;
318+
}
319+
240320
}

app/src/processing/app/debug/Compiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ public void preprocess(String buildPath, PdePreprocessor preprocessor) throws Ru
953953

954954
importedLibraries = new LibraryList();
955955
for (String item : preprocessor.getExtraImports()) {
956-
Library lib = Base.importToLibraryTable.get(item);
956+
Library lib = BaseNoGui.importToLibraryTable.get(item);
957957
if (lib != null && !importedLibraries.contains(lib)) {
958958
importedLibraries.add(lib);
959959
}

app/test/processing/app/AbstractGUITest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void startUpTheIDE() throws Exception {
2121
Preferences.init(null);
2222
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
2323
Theme.init();
24-
Base.platform.setLookAndFeel();
24+
Base.getPlatform().setLookAndFeel();
2525
Base.untitledFolder = Base.createTempFolder("untitled");
2626
Base.untitledFolder.deleteOnExit();
2727

0 commit comments

Comments
 (0)