34
34
35
35
import java .io .File ;
36
36
import java .io .IOException ;
37
- import java .util .ArrayList ;
38
37
import java .util .Collections ;
38
+ import java .util .Comparator ;
39
39
import java .util .List ;
40
40
import java .util .Optional ;
41
41
42
42
import cc .arduino .cli .ArduinoCoreInstance ;
43
+ import cc .arduino .cli .commands .Lib .InstalledLibrary ;
44
+ import cc .arduino .cli .commands .Lib .Library ;
43
45
import cc .arduino .contributions .packages .ContributedPlatform ;
44
46
import io .grpc .StatusException ;
45
47
import 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 ;
49
50
import processing .app .packages .LibraryList ;
50
51
import processing .app .packages .UserLibrary ;
51
52
import processing .app .packages .UserLibraryFolder ;
@@ -58,7 +59,6 @@ public class LibrariesIndexer {
58
59
private final LibraryList installedLibraries = new LibraryList ();
59
60
private List <UserLibraryFolder > librariesFolders ;
60
61
61
- private final List <String > badLibNotified = new ArrayList <>();
62
62
private ArduinoCoreInstance core ;
63
63
64
64
public LibrariesIndexer (ArduinoCoreInstance core ) {
@@ -100,6 +100,7 @@ public void regenerateIndex() {
100
100
101
101
// format(tr("Error parsing libraries index: {0}\nTry to open the Library Manager to update the libraries index."),
102
102
// System.err.println(format(tr("Error reading libraries index: {0}"),
103
+ rescanLibraries ();
103
104
}
104
105
105
106
public void setLibrariesFolders (List <UserLibraryFolder > folders ) {
@@ -118,18 +119,16 @@ public List<UserLibraryFolder> getLibrariesFolders() {
118
119
private Comparator <UserLibrary > priorityComparator = new UserLibraryPriorityComparator (
119
120
null );
120
121
121
- public void addToInstalledLibraries (UserLibrary lib ) {
122
+ public void addToInstalledLibraries (UserLibrary lib ) throws IOException {
122
123
UserLibrary toReplace = installedLibraries .getByName (lib .getName ());
123
124
if (toReplace == null ) {
124
125
installedLibraries .add (lib );
125
- return ;
126
- }
127
- if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
126
+ } else if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
128
127
// The current lib has priority, do nothing
129
- return ;
128
+ } else {
129
+ installedLibraries .remove (toReplace );
130
+ installedLibraries .add (lib );
130
131
}
131
- installedLibraries .remove (toReplace );
132
- installedLibraries .add (lib );
133
132
}
134
133
135
134
public void setArchitecturePriority (String arch ) {
@@ -144,17 +143,109 @@ public void rescanLibraries() {
144
143
return ;
145
144
}
146
145
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
+ Optional <TargetPlatform > targetPlatform = BaseNoGui .getTargetPlatform ();
164
+ if (targetPlatform .isPresent ()) {
165
+ String buildCore = BaseNoGui .getBoardPreferences ().get ("build.core" , "arduino" );
166
+ if (buildCore .contains (":" )) {
167
+ String referencedCore = buildCore .split (":" )[0 ];
168
+ Optional <TargetPlatform > referencedPlatform = BaseNoGui .getTargetPlatform (referencedCore , targetPlatform .get ().getId ());
169
+ if (referencedPlatform .isPresent ()) {
170
+ File referencedPlatformFolder = referencedPlatform .get ().getFolder ();
171
+ // Add libraries folder for the referenced platform
172
+ refcoreLibsDir = new File (referencedPlatformFolder , "libraries" );
173
+ }
150
174
}
175
+ File platformFolder = targetPlatform .get ().getFolder ();
176
+ // Add libraries folder for the selected platform
177
+ coreLibsDir = new File (platformFolder , "libraries" );
151
178
}
152
179
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
+ }
156
246
}
157
247
248
+ // TODO: Should be done on the CLI?
158
249
installedLibraries .stream () //
159
250
.filter (l -> l .getTypes ().contains ("Contributed" )) //
160
251
.filter (l -> l .getLocation () == Location .CORE
@@ -169,85 +260,15 @@ public void rescanLibraries() {
169
260
});
170
261
}
171
262
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);
187
269
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()));
251
272
252
273
public LibrariesIndex getIndex () {
253
274
return index ;
0 commit comments