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 ) {
@@ -114,18 +115,16 @@ public List<UserLibraryFolder> getLibrariesFolders() {
114115 private Comparator <UserLibrary > priorityComparator = new UserLibraryPriorityComparator (
115116 null );
116117
117- public void addToInstalledLibraries (UserLibrary lib ) {
118+ public void addToInstalledLibraries (UserLibrary lib ) throws IOException {
118119 UserLibrary toReplace = installedLibraries .getByName (lib .getName ());
119120 if (toReplace == null ) {
120121 installedLibraries .add (lib );
121- return ;
122- }
123- if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
122+ } else if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
124123 // The current lib has priority, do nothing
125- return ;
124+ } else {
125+ installedLibraries .remove (toReplace );
126+ installedLibraries .add (lib );
126127 }
127- installedLibraries .remove (toReplace );
128- installedLibraries .add (lib );
129128 }
130129
131130 public void setArchitecturePriority (String arch ) {
@@ -140,17 +139,109 @@ public void rescanLibraries() {
140139 return ;
141140 }
142141
143- for (ContributedLibrary lib : index .getLibraries ()) {
144- for (ContributedLibraryRelease libRelease : lib .getReleases ()) {
145- libRelease .unsetInstalledUserLibrary ();
142+ index .getLibraries ().forEach (l -> {
143+ l .getReleases ().forEach (r -> {
144+ r .unsetInstalledUserLibrary ();
145+ });
146+ });
147+
148+ // Rescan libraries
149+ List <InstalledLibrary > installedLibsMeta ;
150+ try {
151+ installedLibsMeta = core .libraryList (true );
152+ } catch (StatusException e ) {
153+ e .printStackTrace ();
154+ return ;
155+ }
156+
157+ File coreLibsDir = null ;
158+ File refcoreLibsDir = null ;
159+ TargetPlatform targetPlatform = BaseNoGui .getTargetPlatform ();
160+ if (targetPlatform != null ) {
161+ String buildCore = BaseNoGui .getBoardPreferences ().get ("build.core" , "arduino" );
162+ if (buildCore .contains (":" )) {
163+ String referencedCore = buildCore .split (":" )[0 ];
164+ TargetPlatform referencedPlatform = BaseNoGui .getTargetPlatform (referencedCore , targetPlatform .getId ());
165+ if (referencedPlatform != null ) {
166+ File referencedPlatformFolder = referencedPlatform .getFolder ();
167+ // Add libraries folder for the referenced platform
168+ refcoreLibsDir = new File (referencedPlatformFolder , "libraries" );
169+ }
146170 }
171+ File platformFolder = targetPlatform .getFolder ();
172+ // Add libraries folder for the selected platform
173+ coreLibsDir = new File (platformFolder , "libraries" );
147174 }
148175
149- // Rescan libraries
150- for (UserLibraryFolder folderDesc : librariesFolders ) {
151- scanInstalledLibraries (folderDesc );
176+ for (InstalledLibrary meta : installedLibsMeta ) {
177+ Library l = meta .getLibrary ();
178+
179+ // Skip platform-related libraries that are not part of the currently
180+ // selected platform/board.
181+ if (l .getLocation ().equals ("platform" )) {
182+ File libDir = new File (l .getInstallDir ());
183+ boolean isCoreLib = (coreLibsDir != null )
184+ && FileUtils .isSubDirectory (coreLibsDir , libDir );
185+ boolean isRefCoreLib = (refcoreLibsDir != null ) //
186+ && FileUtils .isSubDirectory (refcoreLibsDir ,
187+ libDir );
188+ if (!isCoreLib && !isRefCoreLib ) {
189+ continue ;
190+ }
191+ }
192+
193+ UserLibrary lib = new UserLibrary ( //
194+ new File (l .getInstallDir ()), //
195+ l .getName (), //
196+ l .getVersion (), //
197+ l .getAuthor (), //
198+ l .getMaintainer (), //
199+ l .getSentence (), //
200+ l .getParagraph (), //
201+ l .getWebsite (), //
202+ l .getCategory (), //
203+ l .getLicense (), //
204+ l .getArchitecturesList (), //
205+ l .getLayout (), //
206+ l .getTypesList (), //
207+ false , // TODO: onGoingDevelopment
208+ null , // TODO: includes
209+ l .getLocation () //
210+ );
211+
212+ try {
213+ String [] headers = BaseNoGui
214+ .headerListFromIncludePath (lib .getSrcFolder ()); // TODO: Obtain from the CLI?
215+ if (headers .length == 0 ) {
216+ throw new IOException (format (tr ("no headers files (.h) found in {0}" ),
217+ lib .getSrcFolder ()));
218+ }
219+
220+ Location loc = lib .getLocation ();
221+ if (loc != Location .CORE && loc != Location .REFERENCED_CORE ) {
222+ // Check if we can find the same library in the index
223+ // and mark it as installed
224+ index .find (lib .getName (), lib .getVersion ()).ifPresent (foundLib -> {
225+ foundLib .setInstalledUserLibrary (lib );
226+ lib .setTypes (foundLib .getTypes ());
227+ });
228+ }
229+
230+ if (lib .getTypes ().isEmpty () && loc == Location .SKETCHBOOK ) {
231+ lib .setTypes (lib .getDeclaredTypes ());
232+ }
233+
234+ if (lib .getTypes ().isEmpty ()) {
235+ lib .setTypes (Collections .singletonList ("Contributed" ));
236+ }
237+
238+ addToInstalledLibraries (lib );
239+ } catch (Exception e ) {
240+ e .printStackTrace ();
241+ }
152242 }
153243
244+ // TODO: Should be done on the CLI?
154245 installedLibraries .stream () //
155246 .filter (l -> l .getTypes ().contains ("Contributed" )) //
156247 .filter (l -> l .getLocation () == Location .CORE
@@ -165,85 +256,15 @@ public void rescanLibraries() {
165256 });
166257 }
167258
168- private void scanInstalledLibraries (UserLibraryFolder folderDesc ) {
169- File list [] = folderDesc .folder .listFiles (OnlyDirs .ONLY_DIRS );
170- // if a bad folder or something like that, this might come back null
171- if (list == null )
172- return ;
173-
174- for (File subfolder : list ) {
175- String subfolderName = subfolder .getName ();
176- if (!BaseNoGui .isSanitaryName (subfolderName )) {
177-
178- // Detect whether the current folder name has already had a
179- // notification.
180- if (!badLibNotified .contains (subfolderName )) {
181-
182- badLibNotified .add (subfolderName );
259+ // String mess = I18n.format(
260+ // tr("The library \"{0}\" cannot be used.\n"
261+ // + "Library folder names must start with a letter or number, followed by letters,\n"
262+ // + "numbers, dashes, dots and underscores. Maximum length is 63 characters."),
263+ // subfolderName);
264+ // BaseNoGui.showMessage(tr("Ignoring library with bad name"), mess);
183265
184- String mess = I18n .format (
185- tr ("The library \" {0}\" cannot be used.\n "
186- + "Library folder names must start with a letter or number, followed by letters,\n "
187- + "numbers, dashes, dots and underscores. Maximum length is 63 characters." ),
188- subfolderName );
189- BaseNoGui .showMessage (tr ("Ignoring library with bad name" ), mess );
190- }
191- continue ;
192- }
193-
194- try {
195- scanLibrary (new UserLibraryFolder (subfolder , folderDesc .location ));
196- } catch (IOException e ) {
197- System .out .println (I18n .format (tr ("Invalid library found in {0}: {1}" ),
198- subfolder , e .getMessage ()));
199- }
200- }
201- }
202-
203- private void scanLibrary (UserLibraryFolder folderDesc ) throws IOException {
204- // A library is considered "legacy" if it doesn't contains
205- // a file called "library.properties"
206- File check = new File (folderDesc .folder , "library.properties" );
207- if (!check .exists () || !check .isFile ()) {
208- // Create a legacy library and exit
209- LegacyUserLibrary lib = LegacyUserLibrary .create (folderDesc );
210- String [] headers = BaseNoGui
211- .headerListFromIncludePath (lib .getSrcFolder ());
212- if (headers .length == 0 ) {
213- throw new IOException (format (tr ("no headers files (.h) found in {0}" ),
214- lib .getSrcFolder ()));
215- }
216- addToInstalledLibraries (lib );
217- return ;
218- }
219-
220- // Create a regular library
221- UserLibrary lib = UserLibrary .create (folderDesc );
222- String [] headers = BaseNoGui .headerListFromIncludePath (lib .getSrcFolder ());
223- if (headers .length == 0 ) {
224- throw new IOException (
225- format (tr ("no headers files (.h) found in {0}" ), lib .getSrcFolder ()));
226- }
227- addToInstalledLibraries (lib );
228-
229- Location loc = lib .getLocation ();
230- if (loc != Location .CORE && loc != Location .REFERENCED_CORE ) {
231- // Check if we can find the same library in the index
232- // and mark it as installed
233- index .find (lib .getName (), lib .getVersion ()).ifPresent (foundLib -> {
234- foundLib .setInstalledUserLibrary (lib );
235- lib .setTypes (foundLib .getTypes ());
236- });
237- }
238-
239- if (lib .getTypes ().isEmpty () && loc == Location .SKETCHBOOK ) {
240- lib .setTypes (lib .getDeclaredTypes ());
241- }
242-
243- if (lib .getTypes ().isEmpty ()) {
244- lib .setTypes (Collections .singletonList ("Contributed" ));
245- }
246- }
266+ // System.out.println(I18n.format(tr("Invalid library found in {0}: {1}"),
267+ // subfolder, e.getMessage()));
247268
248269 public LibrariesIndex getIndex () {
249270 return index ;
0 commit comments