1
1
package io .sloeber .core .api ;
2
2
3
+ import java .io .File ;
4
+ import java .io .FileReader ;
5
+ import java .io .Reader ;
6
+ import java .util .ArrayList ;
3
7
import java .util .Collection ;
8
+ import java .util .HashMap ;
9
+ import java .util .List ;
10
+ import java .util .Map ;
4
11
import java .util .Set ;
5
12
import java .util .TreeMap ;
6
13
import java .util .TreeSet ;
7
14
8
15
import org .eclipse .core .runtime .IProgressMonitor ;
9
16
import org .eclipse .core .runtime .IStatus ;
10
17
import org .eclipse .core .runtime .MultiStatus ;
18
+ import org .eclipse .core .runtime .NullProgressMonitor ;
11
19
import org .eclipse .core .runtime .Status ;
12
20
21
+ import com .google .gson .Gson ;
22
+
23
+ import io .sloeber .core .Activator ;
24
+ import io .sloeber .core .common .Common ;
13
25
import io .sloeber .core .common .InstancePreferences ;
26
+ import io .sloeber .core .managers .Library ;
14
27
import io .sloeber .core .managers .LibraryIndex ;
15
28
import io .sloeber .core .managers .Manager ;
29
+ import io .sloeber .core .managers .Messages ;
30
+ import io .sloeber .core .tools .Version ;
16
31
17
32
public class LibraryManager {
33
+ static private List <LibraryIndex > libraryIndices ;
34
+
35
+ static public List <LibraryIndex > getLibraryIndices () {
36
+ if (libraryIndices == null ) {
37
+ Manager .getPackageIndices ();
38
+ }
39
+ return libraryIndices ;
40
+ }
41
+
18
42
public static LibraryTree getLibraryTree () {
19
43
return new LibraryTree ();
20
44
@@ -128,7 +152,7 @@ public Object getParent() {
128
152
}
129
153
130
154
public LibraryTree () {
131
- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
155
+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
132
156
for (String categoryName : libraryIndex .getCategories ()) {
133
157
Category category = this .categories .get (categoryName );
134
158
if (category == null ) {
@@ -166,7 +190,7 @@ public Collection<Library> getAllLibraries() {
166
190
}
167
191
168
192
private static LibraryIndex findLibraryIndex (String name ) {
169
- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
193
+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
170
194
if (libraryIndex .getName ().equals (name ))
171
195
return libraryIndex ;
172
196
}
@@ -188,7 +212,7 @@ public void reset() {
188
212
189
213
public static IStatus setLibraryTree (LibraryTree libs , IProgressMonitor monitor , MultiStatus status ) {
190
214
for (LibraryTree .Library lib : libs .getAllLibraries ()) {
191
- LibraryIndex libraryIndex = findLibraryIndex (lib .getIndexName ());
215
+ LibraryIndex libraryIndex = getLibraryIndex (lib .getIndexName ());
192
216
193
217
if (libraryIndex != null ) {
194
218
io .sloeber .core .managers .Library toRemove = libraryIndex .getInstalledLibrary (lib .getName ());
@@ -207,27 +231,10 @@ public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor,
207
231
return status ;
208
232
}
209
233
210
- private static LibraryIndex findLibraryIndex (String indexName ) {
211
- for (LibraryIndex libraryIndex : Manager .getLibraryIndices ()) {
212
- if (libraryIndex .getName ().equals (indexName ))
213
- return libraryIndex ;
214
- }
215
- return null ;
216
- }
217
-
218
234
public static String getPrivateLibraryPathsString () {
219
235
return InstancePreferences .getPrivateLibraryPathsString ();
220
236
}
221
237
222
- /**
223
- * Wrapper method for Manager. installAllLatestLibraries()
224
- *
225
- * @param category
226
- */
227
- public static void installAllLatestLibraries () {
228
- Manager .installAllLatestLibraries ();
229
- }
230
-
231
238
/**
232
239
* get all the categories for all libraries (installed or not)
233
240
*
@@ -238,11 +245,93 @@ public static Set<String> getAllCategories() {
238
245
239
246
Set <String > ret = new TreeSet <>();
240
247
241
- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
248
+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
242
249
for (String categoryName : libraryIndex .getCategories ()) {
243
250
ret .add (categoryName );
244
251
}
245
252
}
246
253
return ret ;
247
254
}
255
+
256
+ public static void InstallDefaultLibraries (IProgressMonitor monitor ) {
257
+ LibraryIndex libindex = getLibraryIndex (Defaults .DEFAULT );
258
+ if (libindex == null )
259
+ return ;
260
+
261
+ for (String library : Defaults .INSTALLED_LIBRARIES ) {
262
+ Library toInstalLib = libindex .getLatestLibrary (library );
263
+ if (toInstalLib != null ) {
264
+ toInstalLib .install (monitor );
265
+ }
266
+ }
267
+ }
268
+
269
+ static private LibraryIndex getLibraryIndex (String name ) {
270
+ for (LibraryIndex index : getLibraryIndices ()) {
271
+ if (index .getName ().equals (name )) {
272
+ return index ;
273
+ }
274
+ }
275
+ return null ;
276
+ }
277
+
278
+ static public void loadJson (File jsonFile ) {
279
+ try (Reader reader = new FileReader (jsonFile )) {
280
+ LibraryIndex index = new Gson ().fromJson (reader , LibraryIndex .class );
281
+ index .resolve ();
282
+ index .setJsonFile (jsonFile );
283
+ libraryIndices .add (index );
284
+ } catch (Exception e ) {
285
+ Common .log (new Status (IStatus .ERROR , Activator .getId (),
286
+ Messages .Manager_Failed_to_parse .replace ("${FILE}" , jsonFile .getAbsolutePath ()), e )); //$NON-NLS-1$
287
+ jsonFile .delete ();// Delete the file so it stops damaging
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Install the latest version of all the libraries belonging to this
293
+ * category If a earlier version is installed this version will be removed
294
+ * before installation of the newer version
295
+ *
296
+ * @param category
297
+ */
298
+ public static void installAllLatestLibraries () {
299
+ List <LibraryIndex > libraryIndices1 = getLibraryIndices ();
300
+ Map <String , Library > latestLibs = new HashMap <>();
301
+ for (LibraryIndex libraryIndex : libraryIndices1 ) {
302
+ Map <String , Library > libraries = libraryIndex .getLatestLibraries ();
303
+ for (Map .Entry <String , Library > entry : libraries .entrySet ()) {
304
+ String curLibName = entry .getKey ();
305
+ Library curLibrary = entry .getValue ();
306
+ Library current = latestLibs .get (curLibName );
307
+ if (current != null ) {
308
+ if (Version .compare (curLibrary .getVersion (), current .getVersion ()) > 0 ) {
309
+ latestLibs .put (curLibName , curLibrary );
310
+ }
311
+ } else {
312
+ latestLibs .put (curLibName , curLibrary );
313
+ }
314
+ }
315
+ }
316
+ for (Map .Entry <String , Library > entry : latestLibs .entrySet ()) {
317
+ String curLibName = entry .getKey ();
318
+ Library curLibrary = entry .getValue ();
319
+ for (LibraryIndex libraryIndex : libraryIndices1 ) {
320
+
321
+ Library previousVersion = libraryIndex .getInstalledLibrary (curLibName );
322
+ if ((previousVersion != null ) && (previousVersion != curLibrary )) {
323
+ previousVersion .remove (new NullProgressMonitor ());
324
+ }
325
+ }
326
+ if (!curLibrary .isInstalled ()) {
327
+ curLibrary .install (new NullProgressMonitor ());
328
+ }
329
+ }
330
+
331
+ }
332
+
333
+ public static void flushIndices () {
334
+ libraryIndices = new ArrayList <>();
335
+ }
336
+
248
337
}
0 commit comments