Skip to content

Issue/#679 #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions io.sloeber.core/src/io/sloeber/core/api/BoardDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private void ParseSection(Map<String, String> boardInfo) {
String refArchitecture = valueSplit[1];
String refVersion = valueSplit[2];
String actualValue = valueSplit[3];
this.myUploadTool = actualValue;
this.myBoardsCore = actualValue;
this.myReferencedCorePlatformPath = Manager.getPlatformInstallPath(refVendor, refArchitecture,
refVersion);
if (this.myReferencedCorePlatformPath == null) {
Expand Down Expand Up @@ -296,7 +296,7 @@ private void ParseSection(Map<String, String> boardInfo) {
String refArchitecture = valueSplit[1];
String refVersion = valueSplit[2];
String actualValue = valueSplit[3];
this.myUploadTool = actualValue;
this.myBoardsVariant = actualValue;
this.myReferencedBoardVariantPlatformPath = Manager.getPlatformInstallPath(refVendor, refArchitecture,
refVersion);
if (this.myReferencedBoardVariantPlatformPath == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.sloeber.core.api;

import java.util.Map;

/**
* this interface is to allow the ui to handle the automatic installation
* of libraries.
* If you do not register your implementation of this interface there will be
* no automatic install of libraries
*
* registering your inmplementation is done in the library manager
*
* @author jan
*
*/

public interface IInstallLibraryHandler {
/**
* The core will call this method to find out if you want to install
* the libraries automatically or not
*
* @return true if you want libraries to beinstalled automatically
*/
abstract boolean autoInstall();
/**
* given the set of proposed libraries to install
* let the user decide on what to install
* @param proposedLibsToInstall the libraries Sloeber proposes to install
*
* @return The libraries the user wants to install
*/
abstract Map<String, LibraryDescriptor> selectLibrariesToInstall(Map<String, LibraryDescriptor> proposedLibsToInstall);
}
17 changes: 17 additions & 0 deletions io.sloeber.core/src/io/sloeber/core/api/LibraryDescriptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sloeber.core.api;

import io.sloeber.core.managers.Library;

public class LibraryDescriptor {
private Library library=null;

public LibraryDescriptor(Library value) {
this.library=value;
}

public Library toLibrary() {
// TODO Auto-generated method stub
return this.library;
}

}
54 changes: 50 additions & 4 deletions io.sloeber.core/src/io/sloeber/core/api/LibraryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@
import io.sloeber.core.common.Common;
import io.sloeber.core.common.ConfigurationPreferences;
import io.sloeber.core.common.InstancePreferences;
import io.sloeber.core.core.DefaultInstallHandler;
import io.sloeber.core.managers.Library;
import io.sloeber.core.managers.LibraryIndex;
import io.sloeber.core.managers.Manager;
import io.sloeber.core.managers.Messages;
import io.sloeber.core.tools.Version;

/**
* This class is the main entry point for libraries.
* It handles
* private libraries
* hardware libraries
* installed libraries
*
* @author jan
*
*/
public class LibraryManager {
static private List<LibraryIndex> libraryIndices;
private static IInstallLibraryHandler myInstallLibraryHandler = new DefaultInstallHandler();

static public List<LibraryIndex> getLibraryIndices() {
if (libraryIndices == null) {
Expand Down Expand Up @@ -237,6 +248,7 @@ public static IStatus setLibraryTree(LibraryTree libs, IProgressMonitor monitor,
public static String getPrivateLibraryPathsString() {
return InstancePreferences.getPrivateLibraryPathsString();
}

public static void setPrivateLibraryPaths(String[] libraryPaths) {
InstancePreferences.setPrivateLibraryPaths(libraryPaths);

Expand Down Expand Up @@ -296,9 +308,9 @@ static public void loadJson(File jsonFile) {
}

/**
* Install the latest version of all the libraries belonging to this
* category If a earlier version is installed this version will be removed
* before installation of the newer version
* Install the latest version of all the libraries belonging to this category If
* a earlier version is installed this version will be removed before
* installation of the newer version
*
* @param category
*/
Expand Down Expand Up @@ -351,6 +363,40 @@ public static void removeAllLibs() {
}
}

/**
* Searches for all libraries that can be installed but are not yet installed. A
* library is considered installed when 1 version of the library is installed.
*
* @return a map of all instalable libraries
*/
public static Map<String, LibraryDescriptor> getAllInstallableLibraries() {
Map<String, LibraryDescriptor> ret = new HashMap<>();
for (LibraryIndex libraryIndex : libraryIndices) {
ret.putAll(libraryIndex.getLatestInstallableLibraries());
}

return ret;
}

public static Map<String, LibraryDescriptor> getLatestInstallableLibraries(Set<String> libnames) {
Set<String> remainingLibNames = new TreeSet<>(libnames);
Map<String, LibraryDescriptor> ret = new HashMap<>();
for (LibraryIndex libraryIndex : libraryIndices) {
ret.putAll(libraryIndex.getLatestInstallableLibraries(remainingLibNames));
remainingLibNames.removeAll(ret.keySet());
}

return ret;
}

public static void registerInstallLibraryHandler(IInstallLibraryHandler installLibraryHandler) {
myInstallLibraryHandler = installLibraryHandler;
}

public static IInstallLibraryHandler getInstallLibraryHandler() {
return myInstallLibraryHandler;
}

/**
* Check wether a library is installed.
* The check looks at the library installation place at the disk.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.sloeber.core.core;

import java.util.Map;

import io.sloeber.core.api.IInstallLibraryHandler;
import io.sloeber.core.api.LibraryDescriptor;

public class DefaultInstallHandler implements IInstallLibraryHandler {

@Override
public boolean autoInstall() {
return false;
}


@Override
public Map<String, LibraryDescriptor> selectLibrariesToInstall(Map<String, LibraryDescriptor> proposedLibsToInstall) {

return proposedLibsToInstall;
}

}
33 changes: 30 additions & 3 deletions io.sloeber.core/src/io/sloeber/core/managers/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
import org.eclipse.core.runtime.Status;

import io.sloeber.core.Activator;
import io.sloeber.core.common.Common;
import io.sloeber.core.common.ConfigurationPreferences;
import io.sloeber.core.tools.FileModifiers;

/**
* This class represents an entry ina a library json file
*
* @author jan
*
*/
public class Library implements Comparable<Library> {

private String name;
Expand Down Expand Up @@ -159,6 +166,26 @@ public boolean isInstalled() {
return getInstallPath().toFile().exists();
}

/**
* checks if any version of this library is installed. Can popup a window if
* there is something wrong with the folder structure
*
* @return false if any version is installed. true in case of error and in case
* no version is installed
*/
public boolean isAVersionInstalled() {
if (!getInstallPath().getParent().toFile().exists()) {
return false;
}
if (getInstallPath().getParent().toFile().isFile()) {
// something is wrong here
Common.log(new Status(IStatus.ERROR, Activator.getId(),
getInstallPath().getParent() + " is a file but it should be a directory.")); //$NON-NLS-1$
return false;
}
return getInstallPath().getParent().toFile().list().length > 0;
}

public IStatus install(IProgressMonitor monitor) {
monitor.setTaskName("Downloading and installing " + getName() + " library."); //$NON-NLS-1$ //$NON-NLS-2$
if (isInstalled()) {
Expand Down Expand Up @@ -215,9 +242,9 @@ public int compareTo(Library other) {
}

/**
* delete the library This will delete all installed versions of the
* library. Normally only 1 version can be installed so deleting all
* versions should be delete 1 version
* delete the library This will delete all installed versions of the library.
* Normally only 1 version can be installed so deleting all versions should be
* delete 1 version
*
* @param monitor
* @return Status.OK_STATUS if delete is successful otherwise IStatus.ERROR
Expand Down
47 changes: 47 additions & 0 deletions io.sloeber.core/src/io/sloeber/core/managers/LibraryIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;

import io.sloeber.core.api.Defaults;
import io.sloeber.core.api.LibraryDescriptor;
import io.sloeber.core.tools.Version;

/**
* This class represents a json file that references libraries
*
* @author jan
*
*/
public class LibraryIndex {
private String jsonFileName;
private List<Library> libraries;
Expand Down Expand Up @@ -93,6 +101,23 @@ public Map<String, Library> getLatestLibraries() {
return this.latestLibs;
}

/**
* get all the latest versions of alll libraries that can be installed but are
* not yet installed To do so I find all latest libraries and I remove the once
* that are installed.
*
* @return
*/
public Map<String, LibraryDescriptor> getLatestInstallableLibraries() {
Map<String, LibraryDescriptor> ret = new HashMap<>();
for (Entry<String, Library> curLibrary : this.latestLibs.entrySet()) {
if (!curLibrary.getValue().isAVersionInstalled()) {
ret.put(curLibrary.getKey(),new LibraryDescriptor( curLibrary.getValue()));
}
}
return ret;
}

public Collection<Library> getLibraries(String category) {
Set<String> categoryLibs = this.categories.get(category);
if (categoryLibs == null) {
Expand Down Expand Up @@ -121,4 +146,26 @@ public void setJsonFile(File packageFile) {
public String getName() {
return this.jsonFileName;
}

/**
* get all the latest versions of alll the libraries provided that can be
* installed but are not yet installed To do so I find all latest libraries and
* I remove the once that are installed.
*
* @return
*/
public Map<String, LibraryDescriptor> getLatestInstallableLibraries(Set<String> libNames) {
Map<String, LibraryDescriptor> ret = new HashMap<>();
if (libNames.isEmpty()) {
return ret;
}
for (Entry<String, Library> curLibrary : this.latestLibs.entrySet()) {
if (libNames.contains(curLibrary.getKey())) {
if (!curLibrary.getValue().isAVersionInstalled()) {
ret.put(curLibrary.getKey(), new LibraryDescriptor(curLibrary.getValue()));
}
}
}
return ret;
}
}
43 changes: 38 additions & 5 deletions io.sloeber.core/src/io/sloeber/core/tools/Libraries.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import io.sloeber.core.InternalBoardDescriptor;
import io.sloeber.core.api.BoardDescriptor;
import io.sloeber.core.api.IInstallLibraryHandler;
import io.sloeber.core.api.LibraryDescriptor;
import io.sloeber.core.api.LibraryManager;
import io.sloeber.core.common.Common;
import io.sloeber.core.common.ConfigurationPreferences;
import io.sloeber.core.common.Const;
Expand Down Expand Up @@ -390,17 +394,46 @@ public static void checkLibraries(IProject affectedProject) {
UnresolvedIncludedHeaders.add(entry.getValue());
}
}
Map<String, IPath> availableLibs = getAllInstalledLibraries(configurationDescription);
UnresolvedIncludedHeaders.removeAll(alreadyAddedLibs);

availableLibs.keySet().retainAll(UnresolvedIncludedHeaders);
if (!availableLibs.isEmpty()) {
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, "list of libraries to add to project " //$NON-NLS-1$
+ affectedProject.getName() + ": " + availableLibs.keySet().toString())); //$NON-NLS-1$
addLibrariesToProject(affectedProject, configurationDescription, availableLibs);
+ affectedProject.getName() + ": " + installedLibs.keySet().toString())); //$NON-NLS-1$
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);
long duration = (System.nanoTime() - startTime)/ 1000000; //in miliseconds
if (duration>45000) {
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID,"setProjectDescription took "+duration+" miliseconds!!!")); //$NON-NLS-1$ //$NON-NLS-2$
}
} catch (CoreException e) {
// this can fail because the project may already
// be
Expand Down
Loading