Skip to content

Commit 40769a0

Browse files
cmagliefacchinm
authored andcommitted
When resolving dependencies consider installed contributions first
Consider a case where the user decides to install a library `A` that depends on library `B` and `B` is not up-to-date (i.e. is installed a version that is not the latest), then the user is asked to "install" both libraries `A` and `B`, effectively upgrading `B`. With this change the already installed library `B` is left untouched and not displayed in the missing dependencies.
1 parent e9f5639 commit 40769a0

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Diff for: arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.HashSet;
3636
import java.util.LinkedList;
3737
import java.util.List;
38+
import java.util.Optional;
3839
import java.util.stream.Collectors;
3940

4041
import cc.arduino.contributions.DownloadableContributionBuiltInAtTheBottomComparator;
@@ -145,13 +146,20 @@ public boolean resolveDependeciesOf(List<ContributedLibrary> solution,
145146
continue;
146147
}
147148

148-
// Pick the latest version among possible deps
149-
ContributedLibrary last = possibleDeps.stream()
150-
.reduce((a, b) -> b.isBefore(a) ? a : b).get();
149+
// Pick the installed version if available
150+
ContributedLibrary selected;
151+
Optional<ContributedLibrary> installed = possibleDeps.stream()
152+
.filter(l -> l.isInstalled()).findAny();
153+
if (installed.isPresent()) {
154+
selected = installed.get();
155+
} else {
156+
// otherwise pick the latest version
157+
selected = possibleDeps.stream().reduce((a, b) -> b.isBefore(a) ? a : b).get();
158+
}
151159

152-
// Add dependecy to the solution and process recursively
153-
solution.add(last);
154-
if (!resolveDependeciesOf(solution, last)) {
160+
// Add dependency to the solution and process recursively
161+
solution.add(selected);
162+
if (!resolveDependeciesOf(solution, selected)) {
155163
return false;
156164
}
157165
}

0 commit comments

Comments
 (0)