4
4
5
5
import java .io .File ;
6
6
import java .io .FileInputStream ;
7
+ import java .io .FileWriter ;
7
8
import java .io .IOException ;
8
9
import java .io .InputStream ;
10
+ import java .util .ArrayList ;
9
11
import java .util .Arrays ;
10
12
import java .util .Date ;
11
13
import java .util .HashMap ;
@@ -47,12 +49,18 @@ public class BaseNoGui {
47
49
48
50
private static DiscoveryManager discoveryManager = new DiscoveryManager ();
49
51
52
+ // these are static because they're used by Sketch
53
+ static private File examplesFolder ;
54
+ static private File toolsFolder ;
55
+
50
56
// maps #included files to their library folder
51
57
public static Map <String , Library > importToLibraryTable ;
52
58
53
59
// maps library name to their library folder
54
60
static private LibraryList libraries ;
55
61
62
+ static private List <File > librariesFolders ;
63
+
56
64
static UserNotifier notifier = new BasicUserNotifier ();
57
65
58
66
static public Map <String , TargetPackage > packages ;
@@ -137,6 +145,14 @@ public static DiscoveryManager getDiscoveryManager() {
137
145
return discoveryManager ;
138
146
}
139
147
148
+ static public File getExamplesFolder () {
149
+ return examplesFolder ;
150
+ }
151
+
152
+ static public String getExamplesPath () {
153
+ return examplesFolder .getAbsolutePath ();
154
+ }
155
+
140
156
static public File getHardwareFolder () {
141
157
// calculate on the fly because it's needed by Preferences.init() to find
142
158
// the boards.txt and programmers.txt preferences files (which happens
@@ -152,6 +168,10 @@ static public LibraryList getLibraries() {
152
168
return libraries ;
153
169
}
154
170
171
+ static public List <File > getLibrariesPath () {
172
+ return librariesFolders ;
173
+ }
174
+
155
175
/**
156
176
* Return an InputStream for a file inside the Processing lib folder.
157
177
*/
@@ -218,6 +238,22 @@ static public File getSketchbookHardwareFolder() {
218
238
return new File (getSketchbookFolder (), "hardware" );
219
239
}
220
240
241
+ static public File getSketchbookLibrariesFolder () {
242
+ File libdir = new File (getSketchbookFolder (), "libraries" );
243
+ if (!libdir .exists ()) {
244
+ try {
245
+ libdir .mkdirs ();
246
+ File readme = new File (libdir , "readme.txt" );
247
+ FileWriter freadme = new FileWriter (readme );
248
+ freadme .write (_ ("For information on installing libraries, see: " +
249
+ "http://arduino.cc/en/Guide/Libraries\n " ));
250
+ freadme .close ();
251
+ } catch (Exception e ) {
252
+ }
253
+ }
254
+ return libdir ;
255
+ }
256
+
221
257
public static TargetBoard getTargetBoard () {
222
258
String boardId = PreferencesData .get ("board" );
223
259
return getTargetPlatform ().getBoard (boardId );
@@ -249,6 +285,14 @@ static public TargetPlatform getTargetPlatform(String packageName,
249
285
return p .get (platformName );
250
286
}
251
287
288
+ static public File getToolsFolder () {
289
+ return toolsFolder ;
290
+ }
291
+
292
+ static public String getToolsPath () {
293
+ return toolsFolder .getAbsolutePath ();
294
+ }
295
+
252
296
static public LibraryList getUserLibs () {
253
297
if (libraries == null )
254
298
return new LibraryList ();
@@ -370,6 +414,42 @@ static public String loadFile(File file) throws IOException {
370
414
return PApplet .join (contents , "\n " );
371
415
}
372
416
417
+ static public void onBoardOrPortChange () {
418
+ TargetPlatform targetPlatform = getTargetPlatform ();
419
+ if (targetPlatform == null )
420
+ return ;
421
+
422
+ // Calculate paths for libraries and examples
423
+ examplesFolder = getContentFile ("examples" );
424
+ toolsFolder = getContentFile ("tools" );
425
+
426
+ File platformFolder = targetPlatform .getFolder ();
427
+ librariesFolders = new ArrayList <File >();
428
+ librariesFolders .add (getContentFile ("libraries" ));
429
+ String core = getBoardPreferences ().get ("build.core" );
430
+ if (core .contains (":" )) {
431
+ String referencedCore = core .split (":" )[0 ];
432
+ TargetPlatform referencedPlatform = getTargetPlatform (referencedCore , targetPlatform .getId ());
433
+ if (referencedPlatform != null ) {
434
+ File referencedPlatformFolder = referencedPlatform .getFolder ();
435
+ librariesFolders .add (new File (referencedPlatformFolder , "libraries" ));
436
+ }
437
+ }
438
+ librariesFolders .add (new File (platformFolder , "libraries" ));
439
+ librariesFolders .add (getSketchbookLibrariesFolder ());
440
+
441
+ // Scan for libraries in each library folder.
442
+ // Libraries located in the latest folders on the list can override
443
+ // other libraries with the same name.
444
+ try {
445
+ scanAndUpdateLibraries (librariesFolders );
446
+ } catch (IOException e ) {
447
+ showWarning (_ ("Error" ), _ ("Error loading libraries" ), e );
448
+ }
449
+
450
+ populateImportToLibraryTable ();
451
+ }
452
+
373
453
static public void populateImportToLibraryTable () {
374
454
// Populate importToLibraryTable
375
455
importToLibraryTable = new HashMap <String , Library >();
0 commit comments