Skip to content

Commit

Permalink
move LibrariesLoader under arduino/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Aug 31, 2023
1 parent ad51a21 commit 9f48080
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 102 deletions.
100 changes: 100 additions & 0 deletions arduino/builder/libraries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

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/go-paths-helper"
"github.com/pkg/errors"
)

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)
}
if librariesManager == nil {
lm = librariesmanager.NewLibraryManager(nil, nil)

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

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

librariesFolders := otherLibrariesDirs
if err := librariesFolders.ToAbs(); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
for _, folder := range librariesFolders {
lm.AddLibrariesDir(folder, libraries.User)
}

for _, status := range lm.RescanLibraries() {
// With the refactoring of the initialization step of the CLI we changed how
// errors are returned when loading platforms and libraries, that meant returning a list of
// errors instead of a single one to enhance the experience for the user.
// 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.
verboseOut.Write([]byte(status.Message()))
}

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

resolver := librariesresolver.NewCppResolver()
if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if actualPlatform != targetPlatform {
if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
}
return lm, resolver, verboseOut.Bytes(), nil
}
3 changes: 2 additions & 1 deletion legacy/builder/container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package builder

import (
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/types"
"github.com/pkg/errors"
)
Expand All @@ -37,7 +38,7 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
return errors.New(tr("Sketch cannot be located in build path. Please specify a different build path"))
}

lm, libsResolver, verboseOut, err := LibrariesLoader(
lm, libsResolver, verboseOut, err := builder.LibrariesLoader(
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
ctx.ActualPlatform, ctx.TargetPlatform,
Expand Down
100 changes: 0 additions & 100 deletions legacy/builder/libraries_loader.go
Original file line number Diff line number Diff line change
@@ -1,100 +0,0 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

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/go-paths-helper"
"github.com/pkg/errors"
)

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)
}
if librariesManager == nil {
lm = librariesmanager.NewLibraryManager(nil, nil)

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

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

librariesFolders := otherLibrariesDirs
if err := librariesFolders.ToAbs(); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
for _, folder := range librariesFolders {
lm.AddLibrariesDir(folder, libraries.User)
}

for _, status := range lm.RescanLibraries() {
// With the refactoring of the initialization step of the CLI we changed how
// errors are returned when loading platforms and libraries, that meant returning a list of
// errors instead of a single one to enhance the experience for the user.
// 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.
verboseOut.Write([]byte(status.Message()))
}

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

resolver := librariesresolver.NewCppResolver()
if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
if actualPlatform != targetPlatform {
if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil {
return nil, nil, nil, errors.WithStack(err)
}
}
return lm, resolver, verboseOut.Bytes(), nil
}
2 changes: 1 addition & 1 deletion legacy/builder/test/libraries_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/arduino/builder"
"github.com/arduino/arduino-cli/legacy/builder/constants"
"github.com/arduino/arduino-cli/legacy/builder/types"
paths "github.com/arduino/go-paths-helper"
Expand Down

0 comments on commit 9f48080

Please sign in to comment.