Skip to content

Commit d6f561b

Browse files
cmagliefacchinm
authored andcommitted
Added library-dependency resolver
1 parent 1ebdb02 commit d6f561b

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java

+71
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package cc.arduino.contributions.libraries;
3131

32+
import java.util.ArrayList;
3233
import java.util.Collection;
3334
import java.util.Collections;
3435
import java.util.HashSet;
@@ -98,4 +99,74 @@ public Optional<ContributedLibrary> getInstalled(String name) {
9899
ContributedLibraryReleases rel = new ContributedLibraryReleases(find(name));
99100
return rel.getInstalled();
100101
}
102+
103+
public List<ContributedLibrary> resolveDependeciesOf(ContributedLibrary library) {
104+
List<ContributedLibrary> solution = new ArrayList<>();
105+
solution.add(library);
106+
if (resolveDependeciesOf(solution, library)) {
107+
return solution;
108+
} else {
109+
return null;
110+
}
111+
}
112+
113+
public boolean resolveDependeciesOf(List<ContributedLibrary> solution,
114+
ContributedLibrary library) {
115+
List<ContributedLibraryDependency> requirements = library.getRequires();
116+
if (requirements == null) {
117+
// No deps for this library, great!
118+
return true;
119+
}
120+
121+
for (ContributedLibraryDependency dep : requirements) {
122+
123+
// If the current solution already contains this dependency, skip over
124+
boolean alreadyInSolution = false;
125+
for (ContributedLibrary c : solution) {
126+
if (c.getName().equals(dep.getName()))
127+
alreadyInSolution = true;
128+
}
129+
if (alreadyInSolution)
130+
continue;
131+
132+
// Generate possible matching dependencies
133+
List<ContributedLibrary> possibleDeps = findMatchingDependencies(dep);
134+
135+
// If there are no dependencies available add as "missing" lib
136+
if (possibleDeps.isEmpty()) {
137+
solution.add(new UnavailableContributedLibrary(dep));
138+
continue;
139+
}
140+
141+
// Pick the latest version among possible deps
142+
ContributedLibrary last = possibleDeps.stream()
143+
.reduce((a, b) -> b.isBefore(a) ? a : b).get();
144+
145+
// Add dependecy to the solution and process recursively
146+
solution.add(last);
147+
if (!resolveDependeciesOf(solution, last)) {
148+
return false;
149+
}
150+
}
151+
return true;
152+
}
153+
154+
private List<ContributedLibrary> findMatchingDependencies(ContributedLibraryDependency dep) {
155+
List<ContributedLibrary> available = find(dep.getName());
156+
if (dep.getVersionRequired() == null || dep.getVersionRequired().isEmpty())
157+
return available;
158+
159+
// XXX: The following part is actually never reached. The use of version
160+
// constraints requires a much complex backtracking algorithm, the following
161+
// is just a draft placeholder.
162+
163+
// List<ContributedLibrary> match = available.stream()
164+
// // TODO: add more complex version comparators (> >= < <= ~ 1.0.* 1.*...)
165+
// .filter(candidate -> candidate.getParsedVersion()
166+
// .equals(dep.getVersionRequired()))
167+
// .collect(Collectors.toList());
168+
// return match;
169+
170+
return available;
171+
}
101172
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2017 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.contributions.libraries;
31+
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class UnavailableContributedLibrary extends ContributedLibrary {
36+
37+
private String name;
38+
private String version;
39+
40+
public UnavailableContributedLibrary(ContributedLibraryDependency dependency) {
41+
this(dependency.getName(), dependency.getVersionRequired());
42+
}
43+
44+
public UnavailableContributedLibrary(String _name, String _version) {
45+
name = _name;
46+
version = _version;
47+
}
48+
49+
@Override
50+
public String getName() {
51+
return name;
52+
}
53+
54+
@Override
55+
public String getMaintainer() {
56+
return "Unknown";
57+
}
58+
59+
@Override
60+
public String getAuthor() {
61+
return "Unknown";
62+
}
63+
64+
@Override
65+
public String getWebsite() {
66+
return "Unknown";
67+
}
68+
69+
@Override
70+
public String getCategory() {
71+
return "Uncategorized";
72+
}
73+
74+
@Override
75+
public void setCategory(String category) {
76+
}
77+
78+
@Override
79+
public String getLicense() {
80+
return "Unknown";
81+
}
82+
83+
@Override
84+
public String getParagraph() {
85+
return "";
86+
}
87+
88+
@Override
89+
public String getSentence() {
90+
return "";
91+
}
92+
93+
@Override
94+
public List<String> getArchitectures() {
95+
return new ArrayList<>();
96+
}
97+
98+
@Override
99+
public List<String> getTypes() {
100+
return new ArrayList<>();
101+
}
102+
103+
@Override
104+
public List<ContributedLibraryDependency> getRequires() {
105+
return new ArrayList<>();
106+
}
107+
108+
@Override
109+
public String getUrl() {
110+
return "";
111+
}
112+
113+
@Override
114+
public String getVersion() {
115+
return version;
116+
}
117+
118+
@Override
119+
public String getChecksum() {
120+
return "";
121+
}
122+
123+
@Override
124+
public long getSize() {
125+
return 0;
126+
}
127+
128+
@Override
129+
public String getArchiveFileName() {
130+
return "";
131+
}
132+
133+
@Override
134+
public String toString() {
135+
return "!" + super.toString();
136+
}
137+
138+
@Override
139+
public List<String> getProvidesIncludes() {
140+
return new ArrayList<>();
141+
}
142+
}

0 commit comments

Comments
 (0)