@@ -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
@@ -54,39 +53,11 @@ type LibrariesDir struct {
5453// LibraryAlternatives is a list of different versions of the same library
5554// installed in the system
5655type LibraryAlternatives struct {
57- Alternatives libraries.List
56+ libraries.List
5857}
5958
6059var 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.
9162func (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}
0 commit comments