diff --git a/legacy/builder/container_setup.go b/legacy/builder/container_setup.go index 0b2f7103cc6..4d2d8a6d22d 100644 --- a/legacy/builder/container_setup.go +++ b/legacy/builder/container_setup.go @@ -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 } diff --git a/legacy/builder/libraries_loader.go b/legacy/builder/libraries_loader.go index b41c49edcb1..fcdf7ed89f9 100644 --- a/legacy/builder/libraries_loader.go +++ b/legacy/builder/libraries_loader.go @@ -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) @@ -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 } diff --git a/legacy/builder/test/libraries_loader_test.go b/legacy/builder/test/libraries_loader_test.go index a18880fed92..7b7fcb12750 100644 --- a/legacy/builder/test/libraries_loader_test.go +++ b/legacy/builder/test/libraries_loader_test.go @@ -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) } @@ -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)) @@ -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) } @@ -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)) @@ -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) } @@ -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)) @@ -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))