Skip to content

Commit 9f48080

Browse files
move LibrariesLoader under arduino/builder
1 parent ad51a21 commit 9f48080

File tree

4 files changed

+103
-102
lines changed

4 files changed

+103
-102
lines changed

arduino/builder/libraries.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package builder
17+
18+
import (
19+
"bytes"
20+
21+
"github.com/arduino/arduino-cli/arduino/cores"
22+
"github.com/arduino/arduino-cli/arduino/libraries"
23+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
25+
"github.com/arduino/go-paths-helper"
26+
"github.com/pkg/errors"
27+
)
28+
29+
func LibrariesLoader(
30+
useCachedLibrariesResolution bool,
31+
librariesManager *librariesmanager.LibrariesManager,
32+
builtInLibrariesDirs *paths.Path, libraryDirs, otherLibrariesDirs paths.PathList,
33+
actualPlatform, targetPlatform *cores.PlatformRelease,
34+
) (*librariesmanager.LibrariesManager, *librariesresolver.Cpp, []byte, error) {
35+
verboseOut := &bytes.Buffer{}
36+
lm := librariesManager
37+
if useCachedLibrariesResolution {
38+
// Since we are using the cached libraries resolution
39+
// the library manager is not needed.
40+
lm = librariesmanager.NewLibraryManager(nil, nil)
41+
}
42+
if librariesManager == nil {
43+
lm = librariesmanager.NewLibraryManager(nil, nil)
44+
45+
builtInLibrariesFolders := builtInLibrariesDirs
46+
if builtInLibrariesFolders != nil {
47+
if err := builtInLibrariesFolders.ToAbs(); err != nil {
48+
return nil, nil, nil, errors.WithStack(err)
49+
}
50+
lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn)
51+
}
52+
53+
if actualPlatform != targetPlatform {
54+
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
55+
}
56+
lm.AddPlatformReleaseLibrariesDir(targetPlatform, libraries.PlatformBuiltIn)
57+
58+
librariesFolders := otherLibrariesDirs
59+
if err := librariesFolders.ToAbs(); err != nil {
60+
return nil, nil, nil, errors.WithStack(err)
61+
}
62+
for _, folder := range librariesFolders {
63+
lm.AddLibrariesDir(folder, libraries.User)
64+
}
65+
66+
for _, status := range lm.RescanLibraries() {
67+
// With the refactoring of the initialization step of the CLI we changed how
68+
// errors are returned when loading platforms and libraries, that meant returning a list of
69+
// errors instead of a single one to enhance the experience for the user.
70+
// I have no intention right now to start a refactoring of the legacy package too, so
71+
// here's this shitty solution for now.
72+
// When we're gonna refactor the legacy package this will be gone.
73+
verboseOut.Write([]byte(status.Message()))
74+
}
75+
76+
for _, dir := range libraryDirs {
77+
// Libraries specified this way have top priority
78+
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
79+
return nil, nil, nil, errors.WithStack(err)
80+
}
81+
}
82+
}
83+
84+
resolver := librariesresolver.NewCppResolver()
85+
if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil {
86+
return nil, nil, nil, errors.WithStack(err)
87+
}
88+
if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil {
89+
return nil, nil, nil, errors.WithStack(err)
90+
}
91+
if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil {
92+
return nil, nil, nil, errors.WithStack(err)
93+
}
94+
if actualPlatform != targetPlatform {
95+
if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil {
96+
return nil, nil, nil, errors.WithStack(err)
97+
}
98+
}
99+
return lm, resolver, verboseOut.Bytes(), nil
100+
}

legacy/builder/container_setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package builder
1717

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

40-
lm, libsResolver, verboseOut, err := LibrariesLoader(
41+
lm, libsResolver, verboseOut, err := builder.LibrariesLoader(
4142
ctx.UseCachedLibrariesResolution, ctx.LibrariesManager,
4243
ctx.BuiltInLibrariesDirs, ctx.LibraryDirs, ctx.OtherLibrariesDirs,
4344
ctx.ActualPlatform, ctx.TargetPlatform,

legacy/builder/libraries_loader.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +0,0 @@
1-
// This file is part of arduino-cli.
2-
//
3-
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4-
//
5-
// This software is released under the GNU General Public License version 3,
6-
// which covers the main part of arduino-cli.
7-
// The terms of this license can be found at:
8-
// https://www.gnu.org/licenses/gpl-3.0.en.html
9-
//
10-
// You can be released from the requirements of the above licenses by purchasing
11-
// a commercial license. Buying such a license is mandatory if you want to
12-
// modify or otherwise use the software for commercial activities involving the
13-
// Arduino software without disclosing the source code of your own applications.
14-
// To purchase a commercial license, send an email to [email protected].
15-
16-
package builder
17-
18-
import (
19-
"bytes"
20-
21-
"github.com/arduino/arduino-cli/arduino/cores"
22-
"github.com/arduino/arduino-cli/arduino/libraries"
23-
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
24-
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
25-
"github.com/arduino/go-paths-helper"
26-
"github.com/pkg/errors"
27-
)
28-
29-
func LibrariesLoader(
30-
useCachedLibrariesResolution bool,
31-
librariesManager *librariesmanager.LibrariesManager,
32-
builtInLibrariesDirs *paths.Path, libraryDirs, otherLibrariesDirs paths.PathList,
33-
actualPlatform, targetPlatform *cores.PlatformRelease,
34-
) (*librariesmanager.LibrariesManager, *librariesresolver.Cpp, []byte, error) {
35-
verboseOut := &bytes.Buffer{}
36-
lm := librariesManager
37-
if useCachedLibrariesResolution {
38-
// Since we are using the cached libraries resolution
39-
// the library manager is not needed.
40-
lm = librariesmanager.NewLibraryManager(nil, nil)
41-
}
42-
if librariesManager == nil {
43-
lm = librariesmanager.NewLibraryManager(nil, nil)
44-
45-
builtInLibrariesFolders := builtInLibrariesDirs
46-
if builtInLibrariesFolders != nil {
47-
if err := builtInLibrariesFolders.ToAbs(); err != nil {
48-
return nil, nil, nil, errors.WithStack(err)
49-
}
50-
lm.AddLibrariesDir(builtInLibrariesFolders, libraries.IDEBuiltIn)
51-
}
52-
53-
if actualPlatform != targetPlatform {
54-
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
55-
}
56-
lm.AddPlatformReleaseLibrariesDir(targetPlatform, libraries.PlatformBuiltIn)
57-
58-
librariesFolders := otherLibrariesDirs
59-
if err := librariesFolders.ToAbs(); err != nil {
60-
return nil, nil, nil, errors.WithStack(err)
61-
}
62-
for _, folder := range librariesFolders {
63-
lm.AddLibrariesDir(folder, libraries.User)
64-
}
65-
66-
for _, status := range lm.RescanLibraries() {
67-
// With the refactoring of the initialization step of the CLI we changed how
68-
// errors are returned when loading platforms and libraries, that meant returning a list of
69-
// errors instead of a single one to enhance the experience for the user.
70-
// I have no intention right now to start a refactoring of the legacy package too, so
71-
// here's this shitty solution for now.
72-
// When we're gonna refactor the legacy package this will be gone.
73-
verboseOut.Write([]byte(status.Message()))
74-
}
75-
76-
for _, dir := range libraryDirs {
77-
// Libraries specified this way have top priority
78-
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
79-
return nil, nil, nil, errors.WithStack(err)
80-
}
81-
}
82-
}
83-
84-
resolver := librariesresolver.NewCppResolver()
85-
if err := resolver.ScanIDEBuiltinLibraries(lm); err != nil {
86-
return nil, nil, nil, errors.WithStack(err)
87-
}
88-
if err := resolver.ScanUserAndUnmanagedLibraries(lm); err != nil {
89-
return nil, nil, nil, errors.WithStack(err)
90-
}
91-
if err := resolver.ScanPlatformLibraries(lm, targetPlatform); err != nil {
92-
return nil, nil, nil, errors.WithStack(err)
93-
}
94-
if actualPlatform != targetPlatform {
95-
if err := resolver.ScanPlatformLibraries(lm, actualPlatform); err != nil {
96-
return nil, nil, nil, errors.WithStack(err)
97-
}
98-
}
99-
return lm, resolver, verboseOut.Bytes(), nil
100-
}

legacy/builder/test/libraries_loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/arduino/arduino-cli/arduino/libraries"
24-
"github.com/arduino/arduino-cli/legacy/builder"
24+
"github.com/arduino/arduino-cli/arduino/builder"
2525
"github.com/arduino/arduino-cli/legacy/builder/constants"
2626
"github.com/arduino/arduino-cli/legacy/builder/types"
2727
paths "github.com/arduino/go-paths-helper"

0 commit comments

Comments
 (0)