Skip to content

Commit

Permalink
Refactor legacy LibrariesLoader command
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Aug 31, 2023
1 parent d79e96a commit ad51a21
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 89 deletions.
24 changes: 12 additions & 12 deletions legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
return errors.New(tr("Sketch cannot be located in build path. Please specify a different build path"))
}

commands := []types.Command{
&LibrariesLoader{},
lm, libsResolver, verboseOut, err := LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
)
if err != nil {
return errors.WithStack(err)
}
ctx.Progress.AddSubSteps(len(commands))
defer ctx.Progress.RemoveSubSteps()
for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
return errors.WithStack(err)
}
ctx.Progress.CompleteStep()
ctx.PushProgress()

ctx.LibrariesManager = lm
ctx.LibrariesResolver = libsResolver
if ctx.Verbose {
ctx.Warn(string(verboseOut))
}
return nil
}
71 changes: 37 additions & 34 deletions legacy/builder/libraries_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,48 @@
package builder

import (
"bytes"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
)

type LibrariesLoader struct{}

func (s *LibrariesLoader) Run(ctx *types.Context) error {
if ctx.UseCachedLibrariesResolution {
func LibrariesLoader(
useCachedLibrariesResolution bool,
librariesManager *librariesmanager.LibrariesManager,
builtInLibrariesDirs *paths.Path, libraryDirs, otherLibrariesDirs paths.PathList,
actualPlatform, targetPlatform *cores.PlatformRelease,
) (*librariesmanager.LibrariesManager, *librariesresolver.Cpp, []byte, error) {
verboseOut := &bytes.Buffer{}
lm := librariesManager
if useCachedLibrariesResolution {
// Since we are using the cached libraries resolution
// the library manager is not needed.
lm := librariesmanager.NewLibraryManager(nil, nil)
ctx.LibrariesManager = lm
} else if ctx.LibrariesManager == nil {
lm := librariesmanager.NewLibraryManager(nil, nil)
ctx.LibrariesManager = lm
lm = librariesmanager.NewLibraryManager(nil, nil)
}
if librariesManager == nil {
lm = librariesmanager.NewLibraryManager(nil, nil)

builtInLibrariesFolders := ctx.BuiltInLibrariesDirs
builtInLibrariesFolders := builtInLibrariesDirs
if builtInLibrariesFolders != nil {
if err := builtInLibrariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
return nil, nil, nil, errors.WithStack(err)
}
lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn)
}

if ctx.ActualPlatform != ctx.TargetPlatform {
lm.AddPlatformReleaseLibrariesDir(ctx.ActualPlatform, libraries.ReferencedPlatformBuiltIn)
if actualPlatform != targetPlatform {
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
}
lm.AddPlatformReleaseLibrariesDir(ctx.TargetPlatform, libraries.PlatformBuiltIn)
lm.AddPlatformReleaseLibrariesDir(targetPlatform, libraries.PlatformBuiltIn)

librariesFolders := ctx.OtherLibrariesDirs
librariesFolders := otherLibrariesDirs
if err := librariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
return nil, nil, nil, errors.WithStack(err)
}
for _, folder := range librariesFolders {
lm.AddLibrariesDir(folder, libraries.User)
Expand All @@ -63,35 +70,31 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
// I have no intention right now to start a refactoring of the legacy package too, so
// here's this shitty solution for now.
// When we're gonna refactor the legacy package this will be gone.
if ctx.Verbose {
ctx.Warn(status.Message())
}
verboseOut.Write([]byte(status.Message()))
}

for _, dir := range ctx.LibraryDirs {
for _, dir := range libraryDirs {
// Libraries specified this way have top priority
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
return err
return nil, nil, nil, errors.WithStack(err)
}
}
}

resolver := librariesresolver.NewCppResolver()
if err := resolver.ScanIDEBuiltinLibraries(ctx.LibrariesManager); err != nil {
return errors.WithStack(err)
if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanUserAndUnmanagedLibraries(ctx.LibrariesManager); err != nil {
return errors.WithStack(err)
if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanPlatformLibraries(ctx.LibrariesManager, ctx.TargetPlatform); err != nil {
return errors.WithStack(err)
if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if ctx.ActualPlatform != ctx.TargetPlatform {
if err := resolver.ScanPlatformLibraries(ctx.LibrariesManager, ctx.ActualPlatform); err != nil {
return errors.WithStack(err)
if actualPlatform != targetPlatform {
if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
}
ctx.LibrariesResolver = resolver

return nil
return lm, resolver, verboseOut.Bytes(), nil
}
82 changes: 39 additions & 43 deletions legacy/builder/test/libraries_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/stretchr/testify/require"
)

func extractLibraries(ctx *types.Context) []*libraries.Library {
func extractLibraries(libs map[string]libraries.List) []*libraries.Library {
res := []*libraries.Library{}
for _, lib := range ctx.LibrariesManager.Libraries {
for _, lib := range libs {
for _, libAlternative := range lib {
res = append(res, libAlternative)
}
Expand All @@ -47,21 +47,20 @@ func TestLoadLibrariesAVR(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.LibrariesLoader{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}
lm, libsResolver, _, err := builder.LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
)
NoError(t, err)

librariesFolders := ctx.LibrariesManager.LibrariesDir
librariesFolders := lm.LibrariesDir
require.Equal(t, 3, len(librariesFolders))
require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path))
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path))
require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path))

libs := extractLibraries(ctx)
libs := extractLibraries(lm.Libraries)
require.Equal(t, 24, len(libs))

sort.Sort(ByLibraryName(libs))
Expand Down Expand Up @@ -124,21 +123,21 @@ func TestLoadLibrariesAVR(t *testing.T) {
idx++
require.Equal(t, "Wire", libs[idx].Name)

libs = ctx.LibrariesResolver.AlternativesFor("Audio.h")
libs = libsResolver.AlternativesFor("Audio.h")
require.Len(t, libs, 2)
sort.Sort(ByLibraryName(libs))
require.Equal(t, "Audio", libs[0].Name)
require.Equal(t, "FakeAudio", libs[1].Name)

libs = ctx.LibrariesResolver.AlternativesFor("FakeAudio.h")
libs = libsResolver.AlternativesFor("FakeAudio.h")
require.Len(t, libs, 1)
require.Equal(t, "FakeAudio", libs[0].Name)

libs = ctx.LibrariesResolver.AlternativesFor("Adafruit_PN532.h")
libs = libsResolver.AlternativesFor("Adafruit_PN532.h")
require.Len(t, libs, 1)
require.Equal(t, "Adafruit PN532", libs[0].Name)

libs = ctx.LibrariesResolver.AlternativesFor("IRremote.h")
libs = libsResolver.AlternativesFor("IRremote.h")
require.Len(t, libs, 1)
require.Equal(t, "IRremote", libs[0].Name)
}
Expand All @@ -152,21 +151,20 @@ func TestLoadLibrariesSAM(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:sam:arduino_due_x_dbg")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.LibrariesLoader{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}
lm, libsResolver, _, err := builder.LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
)
NoError(t, err)

librariesFolders := ctx.LibrariesManager.LibrariesDir
librariesFolders := lm.LibrariesDir
require.Equal(t, 3, len(librariesFolders))
require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path))
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "sam", "libraries")).EquivalentTo(librariesFolders[1].Path))
require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2].Path))

libraries := extractLibraries(ctx)
libraries := extractLibraries(lm.Libraries)
require.Equal(t, 22, len(libraries))

sort.Sort(ByLibraryName(libraries))
Expand Down Expand Up @@ -206,17 +204,17 @@ func TestLoadLibrariesSAM(t *testing.T) {
idx++
require.Equal(t, "Wire", libraries[idx].Name)

libs := ctx.LibrariesResolver.AlternativesFor("Audio.h")
libs := libsResolver.AlternativesFor("Audio.h")
require.Len(t, libs, 2)
sort.Sort(ByLibraryName(libs))
require.Equal(t, "Audio", libs[0].Name)
require.Equal(t, "FakeAudio", libs[1].Name)

libs = ctx.LibrariesResolver.AlternativesFor("FakeAudio.h")
libs = libsResolver.AlternativesFor("FakeAudio.h")
require.Len(t, libs, 1)
require.Equal(t, "FakeAudio", libs[0].Name)

libs = ctx.LibrariesResolver.AlternativesFor("IRremote.h")
libs = libsResolver.AlternativesFor("IRremote.h")
require.Len(t, libs, 1)
require.Equal(t, "IRremote", libs[0].Name)
}
Expand All @@ -230,15 +228,14 @@ func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, nil, "arduino:avr:leonardo")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.LibrariesLoader{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}
lm, _, _, err := builder.LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
)
NoError(t, err)

librariesFolders := ctx.LibrariesManager.LibrariesDir
librariesFolders := lm.LibrariesDir
require.Equal(t, 3, len(librariesFolders))
require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path))
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path))
Expand All @@ -254,15 +251,14 @@ func TestLoadLibrariesMyAVRPlatform(t *testing.T) {
ctx = prepareBuilderTestContext(t, ctx, nil, "my_avr_platform:avr:custom_yun")
defer cleanUpBuilderTestContext(t, ctx)

commands := []types.Command{
&builder.LibrariesLoader{},
}
for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}
lm, _, _, err := builder.LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
)
NoError(t, err)

librariesFolders := ctx.LibrariesManager.LibrariesDir
librariesFolders := lm.LibrariesDir
require.Equal(t, 4, len(librariesFolders))
require.True(t, Abs(t, paths.New("downloaded_libraries")).EquivalentTo(librariesFolders[0].Path))
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1].Path))
Expand Down

0 comments on commit ad51a21

Please sign in to comment.