@@ -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.)
3736type 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
@@ -51,42 +50,8 @@ type LibrariesDir struct {
5150 PlatformRelease * cores.PlatformRelease
5251}
5352
54- // LibraryAlternatives is a list of different versions of the same library
55- // installed in the system
56- type LibraryAlternatives struct {
57- Alternatives libraries.List
58- }
59-
6053var tr = i18n .Tr
6154
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-
9055// Names returns an array with all the names of the installed libraries.
9156func (lm LibrariesManager ) Names () []string {
9257 res := make ([]string , len (lm .Libraries ))
@@ -107,7 +72,7 @@ func NewLibraryManager(indexDir *paths.Path, downloadsDir *paths.Path) *Librarie
10772 indexFileSignature = indexDir .Join ("library_index.json.sig" )
10873 }
10974 return & LibrariesManager {
110- Libraries : map [string ]* LibraryAlternatives {},
75+ Libraries : map [string ]libraries. List {},
11176 IndexFile : indexFile ,
11277 IndexFileSignature : indexFileSignature ,
11378 DownloadsDir : downloadsDir ,
@@ -208,12 +173,9 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*
208173 continue
209174 }
210175 library .ContainerPlatform = librariesDir .PlatformRelease
211- alternatives , ok := lm .Libraries [library .Name ]
212- if ! ok {
213- alternatives = & LibraryAlternatives {}
214- lm .Libraries [library .Name ] = alternatives
215- }
176+ alternatives := lm .Libraries [library .Name ]
216177 alternatives .Add (library )
178+ lm .Libraries [library .Name ] = alternatives
217179 }
218180
219181 return statuses
@@ -232,13 +194,9 @@ func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location
232194 return fmt .Errorf (tr ("loading library from %[1]s: %[2]s" ), libRootDir , err )
233195 }
234196
235- alternatives , ok := lm .Libraries [library .Name ]
236- if ! ok {
237- alternatives = & LibraryAlternatives {}
238- lm .Libraries [library .Name ] = alternatives
239- }
197+ alternatives := lm .Libraries [library .Name ]
240198 alternatives .Add (library )
241-
199+ lm . Libraries [ library . Name ] = alternatives
242200 return nil
243201}
244202
@@ -253,12 +211,16 @@ func (lm *LibrariesManager) FindByReference(libRef *librariesindex.Reference, in
253211 }
254212 // TODO: Move "search into user" into another method...
255213 if libRef .Version == nil {
256- for _ , candidate := range alternatives . Alternatives {
214+ for _ , candidate := range alternatives {
257215 if candidate .Location == installLocation {
258216 return candidate
259217 }
260218 }
261219 return nil
262220 }
263- return alternatives .FindVersion (libRef .Version , installLocation )
221+ res := alternatives .FilterByVersionAndInstallLocation (libRef .Version , installLocation )
222+ if len (res ) > 0 {
223+ return res [0 ]
224+ }
225+ return nil
264226}
0 commit comments