11package io .sloeber .core .api ;
22
3+ import java .io .File ;
4+ import java .io .FileReader ;
5+ import java .io .Reader ;
6+ import java .util .ArrayList ;
37import java .util .Collection ;
8+ import java .util .HashMap ;
9+ import java .util .List ;
10+ import java .util .Map ;
411import java .util .Set ;
512import java .util .TreeMap ;
613import java .util .TreeSet ;
714
815import org .eclipse .core .runtime .IProgressMonitor ;
916import org .eclipse .core .runtime .IStatus ;
1017import org .eclipse .core .runtime .MultiStatus ;
18+ import org .eclipse .core .runtime .NullProgressMonitor ;
1119import org .eclipse .core .runtime .Status ;
1220
21+ import com .google .gson .Gson ;
22+
23+ import io .sloeber .core .Activator ;
24+ import io .sloeber .core .common .Common ;
1325import io .sloeber .core .common .InstancePreferences ;
26+ import io .sloeber .core .managers .Library ;
1427import io .sloeber .core .managers .LibraryIndex ;
1528import io .sloeber .core .managers .Manager ;
29+ import io .sloeber .core .managers .Messages ;
30+ import io .sloeber .core .tools .Version ;
1631
1732public 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+
1842 public static LibraryTree getLibraryTree () {
1943 return new LibraryTree ();
2044
@@ -128,7 +152,7 @@ public Object getParent() {
128152 }
129153
130154 public LibraryTree () {
131- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
155+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
132156 for (String categoryName : libraryIndex .getCategories ()) {
133157 Category category = this .categories .get (categoryName );
134158 if (category == null ) {
@@ -166,7 +190,7 @@ public Collection<Library> getAllLibraries() {
166190 }
167191
168192 private static LibraryIndex findLibraryIndex (String name ) {
169- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
193+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
170194 if (libraryIndex .getName ().equals (name ))
171195 return libraryIndex ;
172196 }
@@ -188,7 +212,7 @@ public void reset() {
188212
189213 public static IStatus setLibraryTree (LibraryTree libs , IProgressMonitor monitor , MultiStatus status ) {
190214 for (LibraryTree .Library lib : libs .getAllLibraries ()) {
191- LibraryIndex libraryIndex = findLibraryIndex (lib .getIndexName ());
215+ LibraryIndex libraryIndex = getLibraryIndex (lib .getIndexName ());
192216
193217 if (libraryIndex != null ) {
194218 io .sloeber .core .managers .Library toRemove = libraryIndex .getInstalledLibrary (lib .getName ());
@@ -207,27 +231,10 @@ public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor,
207231 return status ;
208232 }
209233
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-
218234 public static String getPrivateLibraryPathsString () {
219235 return InstancePreferences .getPrivateLibraryPathsString ();
220236 }
221237
222- /**
223- * Wrapper method for Manager. installAllLatestLibraries()
224- *
225- * @param category
226- */
227- public static void installAllLatestLibraries () {
228- Manager .installAllLatestLibraries ();
229- }
230-
231238 /**
232239 * get all the categories for all libraries (installed or not)
233240 *
@@ -238,11 +245,93 @@ public static Set<String> getAllCategories() {
238245
239246 Set <String > ret = new TreeSet <>();
240247
241- for (LibraryIndex libraryIndex : Manager . getLibraryIndices ()) {
248+ for (LibraryIndex libraryIndex : getLibraryIndices ()) {
242249 for (String categoryName : libraryIndex .getCategories ()) {
243250 ret .add (categoryName );
244251 }
245252 }
246253 return ret ;
247254 }
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+
248337}
0 commit comments