From 246cf6edd8fd18a81963062181c89189e31cd48a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 18 Jan 2018 17:19:29 +0100 Subject: [PATCH] Added installed lib priority comparator This clearly defines the usage priority for installed libraries. --- .../libraries/LibrariesIndexer.java | 23 +++++- .../processing/app/packages/LibraryList.java | 21 ------ .../UserLibraryPriorityComparator.java | 71 +++++++++++++++++++ 3 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 arduino-core/src/processing/app/packages/UserLibraryPriorityComparator.java diff --git a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java index 8e90cc388..12d7f1bb8 100644 --- a/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java +++ b/arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java @@ -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) { diff --git a/arduino-core/src/processing/app/packages/LibraryList.java b/arduino-core/src/processing/app/packages/LibraryList.java index 5cf7a5c8c..33c1e0230 100644 --- a/arduino-core/src/processing/app/packages/LibraryList.java +++ b/arduino-core/src/processing/app/packages/LibraryList.java @@ -57,27 +57,6 @@ public class LibraryList extends LinkedList { 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())); } diff --git a/arduino-core/src/processing/app/packages/UserLibraryPriorityComparator.java b/arduino-core/src/processing/app/packages/UserLibraryPriorityComparator.java new file mode 100644 index 000000000..fe64ba1aa --- /dev/null +++ b/arduino-core/src/processing/app/packages/UserLibraryPriorityComparator.java @@ -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 { + + private final static Map 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); + } +}