Skip to content

Commit 8252c6b

Browse files
authored
[skip-changelog] Refactoring of LibraryManager libraries directory handling. (#2477)
* Factored some library_index.json URL globals * Removed unused parameter * findLibraryIndexRelease requires libraries index instead of libraries manager * LibrariesManager.AddLibrariesDir now accepts directly a LibraryDir struct This is preparatory for the next commit * AddLibrariesDir now can handle also single-lib directories * Removed weird hack in the library detector Previously the libraries detector had to load the single librares provided via '--library' flag after a Rescan, because the library manager wasn't able to handle a single folder library. Now it can so we removed all the tricky machinery.
1 parent fb1b9a0 commit 8252c6b

File tree

10 files changed

+96
-119
lines changed

10 files changed

+96
-119
lines changed

Diff for: commands/instances.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
311311
for _, pack := range pme.GetPackages() {
312312
for _, platform := range pack.Platforms {
313313
if platformRelease := pme.GetInstalledPlatformRelease(platform); platformRelease != nil {
314-
lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
314+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
315+
PlatformRelease: platformRelease,
316+
Path: platformRelease.GetLibrariesDir(),
317+
Location: libraries.PlatformBuiltIn,
318+
})
315319
}
316320
}
317321
}
@@ -324,11 +328,17 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
324328
if profile == nil {
325329
// Add directories of libraries bundled with IDE
326330
if bundledLibsDir := configuration.IDEBuiltinLibrariesDir(configuration.Settings); bundledLibsDir != nil {
327-
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
331+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
332+
Path: bundledLibsDir,
333+
Location: libraries.IDEBuiltIn,
334+
})
328335
}
329336

330337
// Add libraries directory from config file
331-
lm.AddLibrariesDir(configuration.LibrariesDir(configuration.Settings), libraries.User)
338+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
339+
Path: configuration.LibrariesDir(configuration.Settings),
340+
Location: libraries.User,
341+
})
332342
} else {
333343
// Load libraries required for profile
334344
for _, libraryRef := range profile.Libraries {
@@ -368,7 +378,10 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
368378
taskCallback(&rpc.TaskProgress{Completed: true})
369379
}
370380

371-
lm.AddLibrariesDir(libRoot, libraries.User)
381+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
382+
Path: libRoot,
383+
Location: libraries.User,
384+
})
372385
}
373386
}
374387

@@ -412,11 +425,7 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
412425
}
413426
defer tmp.RemoveAll()
414427

415-
indexResource := resources.IndexResource{
416-
URL: librariesmanager.LibraryIndexWithSignatureArchiveURL,
417-
EnforceSignatureVerification: true,
418-
}
419-
if err := indexResource.Download(lm.IndexFile.Parent(), downloadCB); err != nil {
428+
if err := globals.LibrariesIndexResource.Download(lm.IndexFile.Parent(), downloadCB); err != nil {
420429
return err
421430
}
422431

Diff for: commands/lib/download.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadRequest, downl
4242

4343
logrus.Info("Preparing download")
4444

45-
lib, err := findLibraryIndexRelease(lm, req)
45+
lib, err := findLibraryIndexRelease(lm.Index, req)
4646
if err != nil {
4747
return nil, err
4848
}

Diff for: commands/lib/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest, downloa
7272
// Find the libReleasesToInstall to install
7373
libReleasesToInstall := map[*librariesindex.Release]*librariesmanager.LibraryInstallPlan{}
7474
for _, lib := range toInstall {
75-
libRelease, err := findLibraryIndexRelease(lm, &rpc.LibraryInstallRequest{
75+
libRelease, err := findLibraryIndexRelease(lm.Index, &rpc.LibraryInstallRequest{
7676
Name: lib.GetName(),
7777
Version: lib.GetVersionRequired(),
7878
})

Diff for: commands/lib/resolve_deps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDepe
3636
}
3737

3838
// Search the requested lib
39-
reqLibRelease, err := findLibraryIndexRelease(lm, req)
39+
reqLibRelease, err := findLibraryIndexRelease(lm.Index, req)
4040
if err != nil {
4141
return nil, err
4242
}

Diff for: commands/lib/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, tas
3232
return err
3333
}
3434

35-
ref, err := createLibIndexReference(lm, req)
35+
ref, err := createLibIndexReference(req)
3636
if err != nil {
3737
return &cmderrors.InvalidLibraryError{Cause: err}
3838
}

Diff for: commands/lib/utils.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ import (
1919
"github.com/arduino/arduino-cli/commands"
2020
"github.com/arduino/arduino-cli/commands/cmderrors"
2121
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
22-
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesmanager"
2322
)
2423

2524
type libraryReferencer interface {
2625
commands.Versioned
2726
GetName() string
2827
}
2928

30-
func createLibIndexReference(lm *librariesmanager.LibrariesManager, req libraryReferencer) (*librariesindex.Reference, error) {
29+
func createLibIndexReference(req libraryReferencer) (*librariesindex.Reference, error) {
3130
version, err := commands.ParseVersion(req)
3231
if err != nil {
3332
return nil, &cmderrors.InvalidVersionError{Cause: err}
@@ -36,12 +35,12 @@ func createLibIndexReference(lm *librariesmanager.LibrariesManager, req libraryR
3635
return &librariesindex.Reference{Name: req.GetName(), Version: version}, nil
3736
}
3837

39-
func findLibraryIndexRelease(lm *librariesmanager.LibrariesManager, req libraryReferencer) (*librariesindex.Release, error) {
40-
ref, err := createLibIndexReference(lm, req)
38+
func findLibraryIndexRelease(li *librariesindex.Index, req libraryReferencer) (*librariesindex.Release, error) {
39+
ref, err := createLibIndexReference(req)
4140
if err != nil {
4241
return nil, err
4342
}
44-
lib := lm.Index.FindRelease(ref)
43+
lib := li.FindRelease(ref)
4544
if lib == nil {
4645
return nil, &cmderrors.LibraryNotFoundError{Library: ref.String()}
4746
}

Diff for: internal/arduino/builder/internal/detector/detector.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -608,20 +608,42 @@ func LibrariesLoader(
608608
if err := builtInLibrariesFolders.ToAbs(); err != nil {
609609
return nil, nil, nil, err
610610
}
611-
lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn)
611+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
612+
Path: builtInLibrariesFolders,
613+
Location: libraries.IDEBuiltIn,
614+
})
612615
}
613616

614617
if actualPlatform != targetPlatform {
615-
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
618+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
619+
PlatformRelease: actualPlatform,
620+
Path: actualPlatform.GetLibrariesDir(),
621+
Location: libraries.ReferencedPlatformBuiltIn,
622+
})
616623
}
617-
lm.AddPlatformReleaseLibrariesDir(targetPlatform, libraries.PlatformBuiltIn)
624+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
625+
PlatformRelease: targetPlatform,
626+
Path: targetPlatform.GetLibrariesDir(),
627+
Location: libraries.PlatformBuiltIn,
628+
})
618629

619630
librariesFolders := otherLibrariesDirs
620631
if err := librariesFolders.ToAbs(); err != nil {
621632
return nil, nil, nil, err
622633
}
623634
for _, folder := range librariesFolders {
624-
lm.AddLibrariesDir(folder, libraries.User)
635+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
636+
Path: folder,
637+
Location: libraries.User, // XXX: Should be libraries.Unmanaged?
638+
})
639+
}
640+
641+
for _, dir := range libraryDirs {
642+
lm.AddLibrariesDir(&librariesmanager.LibrariesDir{
643+
Path: dir,
644+
Location: libraries.Unmanaged,
645+
IsSingleLibrary: true,
646+
})
625647
}
626648

627649
for _, status := range lm.RescanLibraries() {
@@ -633,13 +655,6 @@ func LibrariesLoader(
633655
// When we're gonna refactor the legacy package this will be gone.
634656
verboseOut.Write([]byte(status.Message()))
635657
}
636-
637-
for _, dir := range libraryDirs {
638-
// Libraries specified this way have top priority
639-
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
640-
return nil, nil, nil, err
641-
}
642-
}
643658
}
644659

645660
resolver := librariesresolver.NewCppResolver()

Diff for: internal/arduino/globals/globals.go

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
package globals
1717

18+
import (
19+
"net/url"
20+
21+
"github.com/arduino/arduino-cli/internal/arduino/resources"
22+
)
23+
1824
var (
1925
// MainFileValidExtension is the extension that must be used for files in new sketches
2026
MainFileValidExtension = ".ino"
@@ -63,4 +69,13 @@ var (
6369

6470
// DefaultIndexURL is the default index url
6571
DefaultIndexURL = "https://downloads.arduino.cc/packages/package_index.tar.bz2"
72+
73+
// LibrariesIndexURL is the URL where to get the libraries index.
74+
LibrariesIndexURL, _ = url.Parse("https://downloads.arduino.cc/libraries/library_index.tar.bz2")
75+
76+
// LibrariesIndexResource is the IndexResource to get the libraries index.
77+
LibrariesIndexResource = resources.IndexResource{
78+
URL: LibrariesIndexURL,
79+
EnforceSignatureVerification: true,
80+
}
6681
)

Diff for: internal/arduino/libraries/librariesmanager/download.go

-32
This file was deleted.

Diff for: internal/arduino/libraries/librariesmanager/librariesmanager.go

+29-58
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type LibrariesDir struct {
4949
Path *paths.Path
5050
Location libraries.LibraryLocation
5151
PlatformRelease *cores.PlatformRelease
52+
IsSingleLibrary bool // true if Path points directly to a library instad of a dir of libraries
5253
}
5354

5455
var tr = i18n.Tr
@@ -102,46 +103,28 @@ func (lm *LibrariesManager) LoadIndex() error {
102103
// AddLibrariesDir adds path to the list of directories
103104
// to scan when searching for libraries. If a path is already
104105
// in the list it is ignored.
105-
func (lm *LibrariesManager) AddLibrariesDir(path *paths.Path, location libraries.LibraryLocation) {
106-
for _, dir := range lm.LibrariesDir {
107-
if dir.Path.EquivalentTo(path) {
108-
return
109-
}
110-
}
111-
logrus.WithField("dir", path).WithField("location", location.String()).Info("Adding libraries dir")
112-
lm.LibrariesDir = append(lm.LibrariesDir, &LibrariesDir{
113-
Path: path,
114-
Location: location,
115-
})
116-
}
117-
118-
// AddPlatformReleaseLibrariesDir add the libraries directory in the
119-
// specified PlatformRelease to the list of directories to scan when
120-
// searching for libraries.
121-
func (lm *LibrariesManager) AddPlatformReleaseLibrariesDir(plaftormRelease *cores.PlatformRelease, location libraries.LibraryLocation) {
122-
path := plaftormRelease.GetLibrariesDir()
123-
if path == nil {
106+
func (lm *LibrariesManager) AddLibrariesDir(libDir *LibrariesDir) {
107+
if libDir.Path == nil {
124108
return
125109
}
126110
for _, dir := range lm.LibrariesDir {
127-
if dir.Path.EquivalentTo(path) {
111+
if dir.Path.EquivalentTo(libDir.Path) {
128112
return
129113
}
130114
}
131-
logrus.WithField("dir", path).WithField("location", location.String()).Info("Adding libraries dir")
132-
lm.LibrariesDir = append(lm.LibrariesDir, &LibrariesDir{
133-
Path: path,
134-
Location: location,
135-
PlatformRelease: plaftormRelease,
136-
})
115+
logrus.WithField("dir", libDir.Path).
116+
WithField("location", libDir.Location.String()).
117+
WithField("isSingleLibrary", libDir.IsSingleLibrary).
118+
Info("Adding libraries dir")
119+
lm.LibrariesDir = append(lm.LibrariesDir, libDir)
137120
}
138121

139122
// RescanLibraries reload all installed libraries in the system.
140123
func (lm *LibrariesManager) RescanLibraries() []*status.Status {
141124
lm.clearLibraries()
142125
statuses := []*status.Status{}
143126
for _, dir := range lm.LibrariesDir {
144-
if errs := lm.LoadLibrariesFromDir(dir); len(errs) > 0 {
127+
if errs := lm.loadLibrariesFromDir(dir); len(errs) > 0 {
145128
statuses = append(statuses, errs...)
146129
}
147130
}
@@ -164,22 +147,29 @@ func (lm *LibrariesManager) getLibrariesDir(installLocation libraries.LibraryLoc
164147
}
165148
}
166149

167-
// LoadLibrariesFromDir loads all libraries in the given directory. Returns
150+
// loadLibrariesFromDir loads all libraries in the given directory. Returns
168151
// nil if the directory doesn't exists.
169-
func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*status.Status {
152+
func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*status.Status {
170153
statuses := []*status.Status{}
171-
subDirs, err := librariesDir.Path.ReadDir()
172-
if os.IsNotExist(err) {
173-
return statuses
174-
}
175-
if err != nil {
176-
s := status.Newf(codes.FailedPrecondition, tr("reading dir %[1]s: %[2]s"), librariesDir.Path, err)
177-
return append(statuses, s)
154+
155+
var libDirs paths.PathList
156+
if librariesDir.IsSingleLibrary {
157+
libDirs.Add(librariesDir.Path)
158+
} else {
159+
d, err := librariesDir.Path.ReadDir()
160+
if os.IsNotExist(err) {
161+
return statuses
162+
}
163+
if err != nil {
164+
s := status.Newf(codes.FailedPrecondition, tr("reading dir %[1]s: %[2]s"), librariesDir.Path, err)
165+
return append(statuses, s)
166+
}
167+
d.FilterDirs()
168+
d.FilterOutHiddenFiles()
169+
libDirs = d
178170
}
179-
subDirs.FilterDirs()
180-
subDirs.FilterOutHiddenFiles()
181171

182-
for _, subDir := range subDirs {
172+
for _, subDir := range libDirs {
183173
library, err := libraries.Load(subDir, librariesDir.Location)
184174
if err != nil {
185175
s := status.Newf(codes.Internal, tr("loading library from %[1]s: %[2]s"), subDir, err)
@@ -195,25 +185,6 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*
195185
return statuses
196186
}
197187

198-
// LoadLibraryFromDir loads one single library from the libRootDir.
199-
// libRootDir must point to the root of a valid library.
200-
// An error is returned if the path doesn't exist or loading of the library fails.
201-
func (lm *LibrariesManager) LoadLibraryFromDir(libRootDir *paths.Path, location libraries.LibraryLocation) error {
202-
if libRootDir.NotExist() {
203-
return fmt.Errorf(tr("library path does not exist: %s"), libRootDir)
204-
}
205-
206-
library, err := libraries.Load(libRootDir, location)
207-
if err != nil {
208-
return fmt.Errorf(tr("loading library from %[1]s: %[2]s"), libRootDir, err)
209-
}
210-
211-
alternatives := lm.Libraries[library.Name]
212-
alternatives.Add(library)
213-
lm.Libraries[library.Name] = alternatives
214-
return nil
215-
}
216-
217188
// FindByReference return the installed libraries matching the Reference
218189
// name and version or, if the version is nil, the libraries installed
219190
// in the installLocation.

0 commit comments

Comments
 (0)