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 ) {
@@ -114,18 +115,16 @@ public List<UserLibraryFolder> getLibrariesFolders() {
114
115
private Comparator <UserLibrary > priorityComparator = new UserLibraryPriorityComparator (
115
116
null );
116
117
117
- public void addToInstalledLibraries (UserLibrary lib ) {
118
+ public void addToInstalledLibraries (UserLibrary lib ) throws IOException {
118
119
UserLibrary toReplace = installedLibraries .getByName (lib .getName ());
119
120
if (toReplace == null ) {
120
121
installedLibraries .add (lib );
121
- return ;
122
- }
123
- if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
122
+ } else if (priorityComparator .compare (toReplace , lib ) >= 0 ) {
124
123
// The current lib has priority, do nothing
125
- return ;
124
+ } else {
125
+ installedLibraries .remove (toReplace );
126
+ installedLibraries .add (lib );
126
127
}
127
- installedLibraries .remove (toReplace );
128
- installedLibraries .add (lib );
129
128
}
130
129
131
130
public void setArchitecturePriority (String arch ) {
@@ -140,17 +139,109 @@ public void rescanLibraries() {
140
139
return ;
141
140
}
142
141
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
+ }
146
170
}
171
+ File platformFolder = targetPlatform .getFolder ();
172
+ // Add libraries folder for the selected platform
173
+ coreLibsDir = new File (platformFolder , "libraries" );
147
174
}
148
175
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
+ }
152
242
}
153
243
244
+ // TODO: Should be done on the CLI?
154
245
installedLibraries .stream () //
155
246
.filter (l -> l .getTypes ().contains ("Contributed" )) //
156
247
.filter (l -> l .getLocation () == Location .CORE
@@ -165,85 +256,15 @@ public void rescanLibraries() {
165
256
});
166
257
}
167
258
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);
183
265
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()));
247
268
248
269
public LibrariesIndex getIndex () {
249
270
return index ;
0 commit comments