3434
3535import java .io .File ;
3636import java .io .IOException ;
37- import java .util .ArrayList ;
3837import java .util .Collections ;
38+ import java .util .Comparator ;
3939import java .util .List ;
4040import java .util .Optional ;
4141
4242import cc .arduino .cli .ArduinoCoreInstance ;
43+ import cc .arduino .cli .commands .Lib .InstalledLibrary ;
44+ import cc .arduino .cli .commands .Lib .Library ;
4345import cc .arduino .contributions .packages .ContributedPlatform ;
4446import io .grpc .StatusException ;
4547import processing .app .BaseNoGui ;
46- import processing .app .I18n ;
47- import processing .app .helpers .filefilters .OnlyDirs ;
48- import processing .app .packages .LegacyUserLibrary ;
48+ import processing .app .debug .TargetPlatform ;
49+ import processing .app .helpers .FileUtils ;
4950import processing .app .packages .LibraryList ;
5051import processing .app .packages .UserLibrary ;
5152import processing .app .packages .UserLibraryFolder ;
@@ -58,7 +59,6 @@ public class LibrariesIndexer {
5859 private final LibraryList installedLibraries = new LibraryList ();
5960 private List <UserLibraryFolder > librariesFolders ;
6061
61- private final List <String > badLibNotified = new ArrayList <>();
6262 private ArduinoCoreInstance core ;
6363
6464 public LibrariesIndexer (ArduinoCoreInstance core ) {
@@ -100,6 +100,7 @@ public void regenerateIndex() {
100100
101101// format(tr("Error parsing libraries index: {0}\nTry to open the Library Manager to update the libraries index."),
102102// System.err.println(format(tr("Error reading libraries index: {0}"),
103+ rescanLibraries ();
103104 }
104105
105106 public void setLibrariesFolders (List <UserLibraryFolder > folders ) {
@@ -118,18 +119,16 @@ public List<UserLibraryFolder> getLibrariesFolders() {
118119 private Comparator <UserLibrary > priorityComparator = new UserLibraryPriorityComparator (
119120 null );
120121
121- public void addToInstalledLibraries (UserLibrary lib ) {
122+ public void addToInstalledLibraries (UserLibrary lib ) throws IOException {
122123 UserLibrary toReplace = installedLibraries .getByName (lib .getName ());
123124 if (toReplace == null ) {
124125 installedLibraries .add (lib );
125- return ;
126- }
127- if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
126+ } else if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
128127 // The current lib has priority, do nothing
129- return ;
128+ } else {
129+ installedLibraries .remove (toReplace );
130+ installedLibraries .add (lib );
130131 }
131- installedLibraries .remove (toReplace );
132- installedLibraries .add (lib );
133132 }
134133
135134 public void setArchitecturePriority (String arch ) {
@@ -144,17 +143,109 @@ public void rescanLibraries() {
144143 return ;
145144 }
146145
147- for (ContributedLibrary lib : index .getLibraries ()) {
148- for (ContributedLibraryRelease libRelease : lib .getReleases ()) {
149- libRelease .unsetInstalledUserLibrary ();
146+ index .getLibraries ().forEach (l -> {
147+ l .getReleases ().forEach (r -> {
148+ r .unsetInstalledUserLibrary ();
149+ });
150+ });
151+
152+ // Rescan libraries
153+ List <InstalledLibrary > installedLibsMeta ;
154+ try {
155+ installedLibsMeta = core .libraryList (true );
156+ } catch (StatusException e ) {
157+ e .printStackTrace ();
158+ return ;
159+ }
160+
161+ File coreLibsDir = null ;
162+ File refcoreLibsDir = null ;
163+ TargetPlatform targetPlatform = BaseNoGui .getTargetPlatform ();
164+ if (targetPlatform != null ) {
165+ String buildCore = BaseNoGui .getBoardPreferences ().get ("build.core" , "arduino" );
166+ if (buildCore .contains (":" )) {
167+ String referencedCore = buildCore .split (":" )[0 ];
168+ TargetPlatform referencedPlatform = BaseNoGui .getTargetPlatform (referencedCore , targetPlatform .getId ());
169+ if (referencedPlatform != null ) {
170+ File referencedPlatformFolder = referencedPlatform .getFolder ();
171+ // Add libraries folder for the referenced platform
172+ refcoreLibsDir = new File (referencedPlatformFolder , "libraries" );
173+ }
150174 }
175+ File platformFolder = targetPlatform .getFolder ();
176+ // Add libraries folder for the selected platform
177+ coreLibsDir = new File (platformFolder , "libraries" );
151178 }
152179
153- // Rescan libraries
154- for (UserLibraryFolder folderDesc : librariesFolders ) {
155- scanInstalledLibraries (folderDesc );
180+ for (InstalledLibrary meta : installedLibsMeta ) {
181+ Library l = meta .getLibrary ();
182+
183+ // Skip platform-related libraries that are not part of the currently
184+ // selected platform/board.
185+ if (l .getLocation ().equals ("platform" )) {
186+ File libDir = new File (l .getInstallDir ());
187+ boolean isCoreLib = (coreLibsDir != null )
188+ && FileUtils .isSubDirectory (coreLibsDir , libDir );
189+ boolean isRefCoreLib = (refcoreLibsDir != null ) //
190+ && FileUtils .isSubDirectory (refcoreLibsDir ,
191+ libDir );
192+ if (!isCoreLib && !isRefCoreLib ) {
193+ continue ;
194+ }
195+ }
196+
197+ UserLibrary lib = new UserLibrary ( //
198+ new File (l .getInstallDir ()), //
199+ l .getName (), //
200+ l .getVersion (), //
201+ l .getAuthor (), //
202+ l .getMaintainer (), //
203+ l .getSentence (), //
204+ l .getParagraph (), //
205+ l .getWebsite (), //
206+ l .getCategory (), //
207+ l .getLicense (), //
208+ l .getArchitecturesList (), //
209+ l .getLayout (), //
210+ l .getTypesList (), //
211+ false , // TODO: onGoingDevelopment
212+ null , // TODO: includes
213+ l .getLocation () //
214+ );
215+
216+ try {
217+ String [] headers = BaseNoGui
218+ .headerListFromIncludePath (lib .getSrcFolder ()); // TODO: Obtain from the CLI?
219+ if (headers .length == 0 ) {
220+ throw new IOException (format (tr ("no headers files (.h) found in {0}" ),
221+ lib .getSrcFolder ()));
222+ }
223+
224+ Location loc = lib .getLocation ();
225+ if (loc != Location .CORE && loc != Location .REFERENCED_CORE ) {
226+ // Check if we can find the same library in the index
227+ // and mark it as installed
228+ index .find (lib .getName (), lib .getVersion ()).ifPresent (foundLib -> {
229+ foundLib .setInstalledUserLibrary (lib );
230+ lib .setTypes (foundLib .getTypes ());
231+ });
232+ }
233+
234+ if (lib .getTypes ().isEmpty () && loc == Location .SKETCHBOOK ) {
235+ lib .setTypes (lib .getDeclaredTypes ());
236+ }
237+
238+ if (lib .getTypes ().isEmpty ()) {
239+ lib .setTypes (Collections .singletonList ("Contributed" ));
240+ }
241+
242+ addToInstalledLibraries (lib );
243+ } catch (Exception e ) {
244+ e .printStackTrace ();
245+ }
156246 }
157247
248+ // TODO: Should be done on the CLI?
158249 installedLibraries .stream () //
159250 .filter (l -> l .getTypes ().contains ("Contributed" )) //
160251 .filter (l -> l .getLocation () == Location .CORE
@@ -169,85 +260,15 @@ public void rescanLibraries() {
169260 });
170261 }
171262
172- private void scanInstalledLibraries (UserLibraryFolder folderDesc ) {
173- File list [] = folderDesc .folder .listFiles (OnlyDirs .ONLY_DIRS );
174- // if a bad folder or something like that, this might come back null
175- if (list == null )
176- return ;
177-
178- for (File subfolder : list ) {
179- String subfolderName = subfolder .getName ();
180- if (!BaseNoGui .isSanitaryName (subfolderName )) {
181-
182- // Detect whether the current folder name has already had a
183- // notification.
184- if (!badLibNotified .contains (subfolderName )) {
185-
186- badLibNotified .add (subfolderName );
263+ // String mess = I18n.format(
264+ // tr("The library \"{0}\" cannot be used.\n"
265+ // + "Library folder names must start with a letter or number, followed by letters,\n"
266+ // + "numbers, dashes, dots and underscores. Maximum length is 63 characters."),
267+ // subfolderName);
268+ // BaseNoGui.showMessage(tr("Ignoring library with bad name"), mess);
187269
188- String mess = I18n .format (
189- tr ("The library \" {0}\" cannot be used.\n "
190- + "Library folder names must start with a letter or number, followed by letters,\n "
191- + "numbers, dashes, dots and underscores. Maximum length is 63 characters." ),
192- subfolderName );
193- BaseNoGui .showMessage (tr ("Ignoring library with bad name" ), mess );
194- }
195- continue ;
196- }
197-
198- try {
199- scanLibrary (new UserLibraryFolder (subfolder , folderDesc .location ));
200- } catch (IOException e ) {
201- System .out .println (I18n .format (tr ("Invalid library found in {0}: {1}" ),
202- subfolder , e .getMessage ()));
203- }
204- }
205- }
206-
207- private void scanLibrary (UserLibraryFolder folderDesc ) throws IOException {
208- // A library is considered "legacy" if it doesn't contains
209- // a file called "library.properties"
210- File check = new File (folderDesc .folder , "library.properties" );
211- if (!check .exists () || !check .isFile ()) {
212- // Create a legacy library and exit
213- LegacyUserLibrary lib = LegacyUserLibrary .create (folderDesc );
214- String [] headers = BaseNoGui
215- .headerListFromIncludePath (lib .getSrcFolder ());
216- if (headers .length == 0 ) {
217- throw new IOException (format (tr ("no headers files (.h) found in {0}" ),
218- lib .getSrcFolder ()));
219- }
220- addToInstalledLibraries (lib );
221- return ;
222- }
223-
224- // Create a regular library
225- UserLibrary lib = UserLibrary .create (folderDesc );
226- String [] headers = BaseNoGui .headerListFromIncludePath (lib .getSrcFolder ());
227- if (headers .length == 0 ) {
228- throw new IOException (
229- format (tr ("no headers files (.h) found in {0}" ), lib .getSrcFolder ()));
230- }
231- addToInstalledLibraries (lib );
232-
233- Location loc = lib .getLocation ();
234- if (loc != Location .CORE && loc != Location .REFERENCED_CORE ) {
235- // Check if we can find the same library in the index
236- // and mark it as installed
237- index .find (lib .getName (), lib .getVersion ()).ifPresent (foundLib -> {
238- foundLib .setInstalledUserLibrary (lib );
239- lib .setTypes (foundLib .getTypes ());
240- });
241- }
242-
243- if (lib .getTypes ().isEmpty () && loc == Location .SKETCHBOOK ) {
244- lib .setTypes (lib .getDeclaredTypes ());
245- }
246-
247- if (lib .getTypes ().isEmpty ()) {
248- lib .setTypes (Collections .singletonList ("Contributed" ));
249- }
250- }
270+ // System.out.println(I18n.format(tr("Invalid library found in {0}: {1}"),
271+ // subfolder, e.getMessage()));
251272
252273 public LibrariesIndex getIndex () {
253274 return index ;
0 commit comments