Search in sources :

Example 1 with IInstallLibraryHandler

use of io.sloeber.core.api.IInstallLibraryHandler in project arduino-eclipse-plugin by Sloeber.

the class Libraries method checkLibraries.

public static void checkLibraries(IProject affectedProject) {
    Map<String, String> includeHeaderReplacement = getIncludeHeaderReplacement();
    ICProjectDescriptionManager mngr = CoreModel.getDefault().getProjectDescriptionManager();
    if (mngr != null) {
        ICProjectDescription projectDescription = mngr.getProjectDescription(affectedProject, true);
        if (projectDescription != null) {
            ICConfigurationDescription configurationDescription = projectDescription.getActiveConfiguration();
            if (configurationDescription != null) {
                Set<String> UnresolvedIncludedHeaders = getUnresolvedProjectIncludes(affectedProject);
                Set<String> alreadyAddedLibs = getAllLibrariesFromProject(affectedProject);
                // remove pgmspace as it gives a problem
                // $NON-NLS-1$
                UnresolvedIncludedHeaders.remove("pgmspace");
                for (Map.Entry<String, String> entry : includeHeaderReplacement.entrySet()) {
                    if (UnresolvedIncludedHeaders.contains(entry.getKey())) {
                        UnresolvedIncludedHeaders.remove(entry.getKey());
                        UnresolvedIncludedHeaders.add(entry.getValue());
                    }
                }
                UnresolvedIncludedHeaders.removeAll(alreadyAddedLibs);
                IInstallLibraryHandler installHandler = LibraryManager.getInstallLibraryHandler();
                if (installHandler.autoInstall()) {
                    // Check if there are libraries that are not found in the installed libraries
                    Map<String, IPath> installedLibs = getAllInstalledLibraries(configurationDescription);
                    Set<String> uninstalledIncludedHeaders = new TreeSet<>(UnresolvedIncludedHeaders);
                    uninstalledIncludedHeaders.removeAll(installedLibs.keySet());
                    if (!uninstalledIncludedHeaders.isEmpty()) {
                        // some libraries may need to be installed
                        Map<String, LibraryDescriptor> availableLibs = LibraryManager.getLatestInstallableLibraries(uninstalledIncludedHeaders);
                        if (!availableLibs.isEmpty()) {
                            // We now know which libraries to install
                            // TODO for now I just install but there should be some user
                            // interaction
                            availableLibs = installHandler.selectLibrariesToInstall(availableLibs);
                            for (Entry<String, LibraryDescriptor> curLib : availableLibs.entrySet()) {
                                curLib.getValue().toLibrary().install(new NullProgressMonitor());
                            }
                        }
                    }
                }
                Map<String, IPath> installedLibs = getAllInstalledLibraries(configurationDescription);
                installedLibs.keySet().retainAll(UnresolvedIncludedHeaders);
                if (!installedLibs.isEmpty()) {
                    // there are possible libraries to add
                    Common.log(new Status(IStatus.INFO, Const.CORE_PLUGIN_ID, // $NON-NLS-1$
                    "list of libraries to add to project " + affectedProject.getName() + ": " + // $NON-NLS-1$
                    installedLibs.keySet().toString()));
                    addLibrariesToProject(affectedProject, configurationDescription, installedLibs);
                    try {
                        // TODO remove this logging code if this code is not causing the disrupts
                        long startTime = System.nanoTime();
                        mngr.setProjectDescription(affectedProject, projectDescription, true, null);
                        // in miliseconds
                        long duration = (System.nanoTime() - startTime) / 1000000;
                        if (duration > 45000) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID, "setProjectDescription took " + duration + " miliseconds!!!"));
                        }
                    } catch (CoreException e) {
                    // this can fail because the project may already
                    // be
                    // deleted
                    }
                }
            }
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ICProjectDescription(org.eclipse.cdt.core.settings.model.ICProjectDescription) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) ICProjectDescriptionManager(org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager) CoreException(org.eclipse.core.runtime.CoreException) IInstallLibraryHandler(io.sloeber.core.api.IInstallLibraryHandler) TreeSet(java.util.TreeSet) ICConfigurationDescription(org.eclipse.cdt.core.settings.model.ICConfigurationDescription) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) LibraryDescriptor(io.sloeber.core.api.LibraryDescriptor)

Aggregations

IInstallLibraryHandler (io.sloeber.core.api.IInstallLibraryHandler)1 LibraryDescriptor (io.sloeber.core.api.LibraryDescriptor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 ICConfigurationDescription (org.eclipse.cdt.core.settings.model.ICConfigurationDescription)1 ICProjectDescription (org.eclipse.cdt.core.settings.model.ICProjectDescription)1 ICProjectDescriptionManager (org.eclipse.cdt.core.settings.model.ICProjectDescriptionManager)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Status (org.eclipse.core.runtime.Status)1