Skip to content

Commit ea93eec

Browse files
committed
Removed LibraryAlternatives object in favor of libraries.List
1 parent 7d1916d commit ea93eec

File tree

6 files changed

+51
-53
lines changed

6 files changed

+51
-53
lines changed

arduino/libraries/librarieslist.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package libraries
1717

1818
import (
1919
"sort"
20+
21+
semver "go.bug.st/relaxed-semver"
2022
)
2123

2224
// List is a list of Libraries
@@ -39,6 +41,16 @@ func (list *List) Add(libs ...*Library) {
3941
}
4042
}
4143

44+
// Remove removes the library from the list
45+
func (alts *List) Remove(library *Library) {
46+
for i, lib := range *alts {
47+
if lib == library {
48+
*alts = append((*alts)[:i], (*alts)[i+1:]...)
49+
return
50+
}
51+
}
52+
}
53+
4254
// FindByName returns the first library in the list that match
4355
// the specified name or nil if not found
4456
func (list *List) FindByName(name string) *Library {
@@ -50,6 +62,22 @@ func (list *List) FindByName(name string) *Library {
5062
return nil
5163
}
5264

65+
// FilterByVersionAndInstallLocation returns the libraries mathching the provided version and install location. If version
66+
// is nil all version are matched.
67+
func (list *List) FilterByVersionAndInstallLocation(version *semver.Version, installLocation LibraryLocation) List {
68+
var found List
69+
for _, lib := range *list {
70+
if lib.Location != installLocation {
71+
continue
72+
}
73+
if version != nil && !lib.Version.Equal(version) {
74+
continue
75+
}
76+
found.Add(lib)
77+
}
78+
return found
79+
}
80+
5381
// SortByName sorts the libraries by name
5482
func (list *List) SortByName() {
5583
sort.Slice(*list, func(i, j int) bool {

arduino/libraries/librariesmanager/install.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (lm *LibrariesManager) InstallPrerequisiteCheck(indexLibrary *librariesinde
5353

5454
var replaced *libraries.Library
5555
if installedLibs, have := lm.Libraries[saneName]; have {
56-
for _, installedLib := range installedLibs.Alternatives {
56+
for _, installedLib := range installedLibs {
5757
if installedLib.Location != installLocation {
5858
continue
5959
}
@@ -99,7 +99,9 @@ func (lm *LibrariesManager) Uninstall(lib *libraries.Library) error {
9999
return fmt.Errorf(tr("removing lib directory: %s"), err)
100100
}
101101

102-
lm.Libraries[lib.Name].Remove(lib)
102+
alternatives := lm.Libraries[lib.Name]
103+
alternatives.Remove(lib)
104+
lm.Libraries[lib.Name] = alternatives
103105
return nil
104106
}
105107

arduino/libraries/librariesmanager/librariesmanager.go

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
paths "github.com/arduino/go-paths-helper"
2828
"github.com/pmylund/sortutil"
2929
"github.com/sirupsen/logrus"
30-
semver "go.bug.st/relaxed-semver"
3130
"google.golang.org/grpc/codes"
3231
"google.golang.org/grpc/status"
3332
)
@@ -36,7 +35,7 @@ import (
3635
// (the list of libraries, revisions, installed paths, etc.)
3736
type LibrariesManager struct {
3837
LibrariesDir []*LibrariesDir
39-
Libraries map[string]*LibraryAlternatives `json:"libraries"`
38+
Libraries map[string]libraries.List `json:"libraries"`
4039

4140
Index *librariesindex.Index
4241
IndexFile *paths.Path
@@ -54,39 +53,11 @@ type LibrariesDir struct {
5453
// LibraryAlternatives is a list of different versions of the same library
5554
// installed in the system
5655
type LibraryAlternatives struct {
57-
Alternatives libraries.List
56+
libraries.List
5857
}
5958

6059
var tr = i18n.Tr
6160

62-
// Add adds a library to the alternatives
63-
func (alts *LibraryAlternatives) Add(library *libraries.Library) {
64-
if len(alts.Alternatives) > 0 && alts.Alternatives[0].Name != library.Name {
65-
panic(fmt.Sprintf("the library name is different from the set (%[1]s != %[2]s)", alts.Alternatives[0].Name, library.Name))
66-
}
67-
alts.Alternatives = append(alts.Alternatives, library)
68-
}
69-
70-
// Remove removes the library from the alternatives
71-
func (alts *LibraryAlternatives) Remove(library *libraries.Library) {
72-
for i, lib := range alts.Alternatives {
73-
if lib == library {
74-
alts.Alternatives = append(alts.Alternatives[:i], alts.Alternatives[i+1:]...)
75-
return
76-
}
77-
}
78-
}
79-
80-
// FindVersion returns the library mathching the provided version or nil if not found
81-
func (alts *LibraryAlternatives) FindVersion(version *semver.Version, installLocation libraries.LibraryLocation) *libraries.Library {
82-
for _, lib := range alts.Alternatives {
83-
if lib.Version.Equal(version) && lib.Location == installLocation {
84-
return lib
85-
}
86-
}
87-
return nil
88-
}
89-
9061
// Names returns an array with all the names of the installed libraries.
9162
func (lm LibrariesManager) Names() []string {
9263
res := make([]string, len(lm.Libraries))
@@ -107,7 +78,7 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
10778
indexFileSignature = indexDir.Join("library_index.json.sig")
10879
}
10980
return &LibrariesManager{
110-
Libraries: map[string]*LibraryAlternatives{},
81+
Libraries: map[string]libraries.List{},
11182
IndexFile: indexFile,
11283
IndexFileSignature: indexFileSignature,
11384
DownloadsDir: downloadsDir,
@@ -208,12 +179,9 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*
208179
continue
209180
}
210181
library.ContainerPlatform = librariesDir.PlatformRelease
211-
alternatives, ok := lm.Libraries[library.Name]
212-
if !ok {
213-
alternatives = &LibraryAlternatives{}
214-
lm.Libraries[library.Name] = alternatives
215-
}
182+
alternatives := lm.Libraries[library.Name]
216183
alternatives.Add(library)
184+
lm.Libraries[library.Name] = alternatives
217185
}
218186

219187
return statuses
@@ -232,13 +200,9 @@ func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location
232200
return fmt.Errorf(tr("loading library from %[1]s: %[2]s"), libRootDir, err)
233201
}
234202

235-
alternatives, ok := lm.Libraries[library.Name]
236-
if !ok {
237-
alternatives = &LibraryAlternatives{}
238-
lm.Libraries[library.Name] = alternatives
239-
}
203+
alternatives := lm.Libraries[library.Name]
240204
alternatives.Add(library)
241-
205+
lm.Libraries[library.Name] = alternatives
242206
return nil
243207
}
244208

@@ -253,12 +217,16 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, in
253217
}
254218
// TODO: Move "search into user" into another method...
255219
if libRef.Version == nil {
256-
for _, candidate := range alternatives.Alternatives {
220+
for _, candidate := range alternatives {
257221
if candidate.Location == installLocation {
258222
return candidate
259223
}
260224
}
261225
return nil
262226
}
263-
return alternatives.FindVersion(libRef.Version, installLocation)
227+
res := alternatives.FilterByVersionAndInstallLocation(libRef.Version, installLocation)
228+
if len(res) > 0 {
229+
return res[0]
230+
}
231+
return nil
264232
}

arduino/libraries/librariesresolver/cpp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewCppResolver() *Cpp {
4747
// and cache all C++ headers for later retrieval
4848
func (resolver *Cpp) ScanFromLibrariesManager(lm *librariesmanager.LibrariesManager) error {
4949
for _, libAlternatives := range lm.Libraries {
50-
for _, lib := range libAlternatives.Alternatives {
50+
for _, lib := range libAlternatives {
5151
resolver.ScanLibrary(lib)
5252
}
5353
}
@@ -58,7 +58,7 @@ func (resolver *Cpp) ScanFromLibrariesManager(lm *librariesmanager.LibrariesMana
5858
// and cache all C++ headers for later retrieval.
5959
func (resolver *Cpp) ScanIDEBuiltinLibraries(lm *librariesmanager.LibrariesManager) error {
6060
for _, libAlternatives := range lm.Libraries {
61-
for _, lib := range libAlternatives.Alternatives {
61+
for _, lib := range libAlternatives {
6262
if lib.Location == libraries.IDEBuiltIn {
6363
resolver.ScanLibrary(lib)
6464
}
@@ -71,7 +71,7 @@ func (resolver *Cpp) ScanIDEBuiltinLibraries(lm *librariesmanager.LibrariesManag
7171
// and cache all C++ headers for later retrieval.
7272
func (resolver *Cpp) ScanUserAndUnmanagedLibraries(lm *librariesmanager.LibrariesManager) error {
7373
for _, libAlternatives := range lm.Libraries {
74-
for _, lib := range libAlternatives.Alternatives {
74+
for _, lib := range libAlternatives {
7575
if lib.Location == libraries.User || lib.Location == libraries.Unmanaged {
7676
resolver.ScanLibrary(lib)
7777
}
@@ -84,7 +84,7 @@ func (resolver *Cpp) ScanUserAndUnmanagedLibraries(lm *librariesmanager.Librarie
8484
// to find and cache all C++ headers for later retrieval.
8585
func (resolver *Cpp) ScanPlatformLibraries(lm *librariesmanager.LibrariesManager, platform *cores.PlatformRelease) error {
8686
for _, libAlternatives := range lm.Libraries {
87-
for _, lib := range libAlternatives.Alternatives {
87+
for _, lib := range libAlternatives {
8888
if lib.Location != libraries.PlatformBuiltIn && lib.Location != libraries.ReferencedPlatformBuiltIn {
8989
continue
9090
}

commands/lib/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.Library
121121
func listLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bool) []*installedLib {
122122
res := []*installedLib{}
123123
for _, libAlternatives := range lm.Libraries {
124-
for _, lib := range libAlternatives.Alternatives {
124+
for _, lib := range libAlternatives {
125125
if !all {
126126
if lib.Location != libraries.User {
127127
continue

legacy/builder/test/libraries_loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
func extractLibraries(ctx *types.Context) []*libraries.Library {
3232
res := []*libraries.Library{}
3333
for _, lib := range ctx.LibrariesManager.Libraries {
34-
for _, libAlternative := range lib.Alternatives {
34+
for _, libAlternative := range lib {
3535
res = append(res, libAlternative)
3636
}
3737
}

0 commit comments

Comments
 (0)