1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-17 11:54:33 +01:00

Added installed lib priority comparator

This clearly defines the usage priority for installed libraries.
This commit is contained in:
Cristian Maglie 2018-01-18 17:19:29 +01:00
parent 8fa45a3a07
commit 246cf6edd8
3 changed files with 92 additions and 23 deletions

View File

@ -43,6 +43,7 @@ import processing.app.packages.LibraryList;
import processing.app.packages.UserLibrary;
import processing.app.packages.UserLibraryFolder;
import processing.app.packages.UserLibraryFolder.Location;
import processing.app.packages.UserLibraryPriorityComparator;
import java.io.File;
import java.io.FileInputStream;
@ -111,7 +112,25 @@ public class LibrariesIndexer {
return librariesFolders;
}
private UserLibraryPriorityComparator priorityComparator;
public void addToInstalledLibraries(UserLibrary lib) {
UserLibrary toReplace = installedLibraries.getByName(lib.getName());
if (toReplace == null) {
installedLibraries.add(lib);
return;
}
if (priorityComparator.compare(toReplace, lib) >= 0) {
// The current lib has priority, do nothing
return;
}
installedLibraries.remove(toReplace);
installedLibraries.add(lib);
}
public void rescanLibraries() {
priorityComparator = new UserLibraryPriorityComparator(BaseNoGui.getTargetPlatform().getId());
// Clear all installed flags
installedLibraries.clear();
@ -180,7 +199,7 @@ public class LibrariesIndexer {
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
}
installedLibraries.addOrReplace(lib);
addToInstalledLibraries(lib);
return;
}
@ -190,7 +209,7 @@ public class LibrariesIndexer {
if (headers.length == 0) {
throw new IOException(lib.getSrcFolder().getAbsolutePath());
}
installedLibraries.addOrReplaceArchAware(lib);
addToInstalledLibraries(lib);
Location loc = lib.getLocation();
if (loc != Location.CORE && loc != Location.REFERENCED_CORE) {

View File

@ -57,27 +57,6 @@ public class LibraryList extends LinkedList<UserLibrary> {
return null;
}
public synchronized void addOrReplaceArchAware(UserLibrary lib) {
addOrReplace(lib, true);
}
public synchronized void addOrReplace(UserLibrary lib) {
addOrReplace(lib, false);
}
public synchronized void addOrReplace(UserLibrary lib, boolean archAware) {
remove(lib, archAware);
add(lib);
}
public synchronized void remove(UserLibrary lib, boolean archAware) {
UserLibrary l = getByName(lib.getName());
if (l != null) {
if (!archAware || lib.getArchitectures().contains("*") || lib.getArchitectures().containsAll(l.getArchitectures()))
super.remove(l);
}
}
public synchronized void sort() {
Collections.sort(this, (x, y) -> x.getName().compareToIgnoreCase(y.getName()));
}

View File

@ -0,0 +1,71 @@
/*
* This file is part of Arduino.
*
* Copyright 2017 Arduino LLC (http://www.arduino.cc/)
*
* Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*/
package processing.app.packages;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import processing.app.packages.UserLibraryFolder.Location;
public class UserLibraryPriorityComparator implements Comparator<UserLibrary> {
private final static Map<Location, Integer> priorities = new HashMap<>();
static {
priorities.put(Location.SKETCHBOOK, 4);
priorities.put(Location.CORE, 3);
priorities.put(Location.REFERENCED_CORE, 2);
priorities.put(Location.IDE_BUILTIN, 1);
}
private String arch;
public UserLibraryPriorityComparator(String currentArch) {
arch = currentArch;
}
private boolean hasArchitecturePriority(UserLibrary x) {
return x.getArchitectures().contains(arch);
}
public int priority(UserLibrary l) {
int priority = priorities.get(l.getLocation());
if (hasArchitecturePriority(l))
priority += 10;
return priority;
}
@Override
public int compare(UserLibrary x, UserLibrary y) {
if (!x.getName().equals(y.getName())) {
throw new IllegalArgumentException("The compared libraries must have the same name");
}
return priority(x) - priority(y);
}
}