Skip to content

Commit e82d072

Browse files
committed
Use librarymanager to handle libraries loading
1 parent 81d2e00 commit e82d072

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

libraries_loader.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ package builder
3232
import (
3333
"os"
3434

35+
"github.com/bcmi-labs/arduino-cli/arduino/libraries/librariesmanager"
36+
3537
"github.com/arduino/arduino-builder/i18n"
3638
"github.com/arduino/arduino-builder/types"
3739
"github.com/bcmi-labs/arduino-cli/arduino/libraries"
@@ -69,38 +71,40 @@ func (s *LibrariesLoader) Run(ctx *types.Context) error {
6971

7072
ctx.LibrariesFolders = sortedLibrariesFolders
7173

72-
var libs []*libraries.Library
74+
lm := librariesmanager.NewLibraryManager()
7375
for _, libraryFolder := range sortedLibrariesFolders {
74-
libsInFolder, err := libraries.LoadLibrariesFromDir(libraryFolder)
75-
if err != nil {
76+
if err := lm.LoadLibrariesFromDir(libraryFolder); err != nil {
7677
return i18n.WrapError(err)
7778
}
78-
libs = append(libs, libsInFolder...)
7979
}
8080
if debugLevel > 0 {
81-
for _, lib := range libs {
82-
warnings, err := lib.Lint()
83-
if err != nil {
84-
return i18n.WrapError(err)
85-
}
86-
for _, warning := range warnings {
87-
logger.Fprintln(os.Stdout, "warn", warning)
81+
for _, lib := range lm.Libraries {
82+
for _, libAlt := range lib.Alternatives {
83+
warnings, err := libAlt.Lint()
84+
if err != nil {
85+
return i18n.WrapError(err)
86+
}
87+
for _, warning := range warnings {
88+
logger.Fprintln(os.Stdout, "warn", warning)
89+
}
8890
}
8991
}
9092
}
9193

92-
ctx.Libraries = libs
94+
ctx.LibrariesManager = lm
9395

9496
headerToLibraries := make(map[string][]*libraries.Library)
95-
for _, library := range libs {
96-
headers, err := library.SrcFolder.ReadDir()
97-
if err != nil {
98-
return i18n.WrapError(err)
99-
}
100-
headers.FilterSuffix(".h", ".hpp", ".hh")
101-
for _, header := range headers {
102-
headerFileName := header.Base()
103-
headerToLibraries[headerFileName] = append(headerToLibraries[headerFileName], library)
97+
for _, lib := range lm.Libraries {
98+
for _, library := range lib.Alternatives {
99+
headers, err := library.SrcFolder.ReadDir()
100+
if err != nil {
101+
return i18n.WrapError(err)
102+
}
103+
headers.FilterSuffix(".h", ".hpp", ".hh")
104+
for _, header := range headers {
105+
headerFileName := header.Base()
106+
headerToLibraries[headerFileName] = append(headerToLibraries[headerFileName], library)
107+
}
104108
}
105109
}
106110

test/libraries_loader_test.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,19 @@ import (
3838
"github.com/arduino/arduino-builder/constants"
3939
"github.com/arduino/arduino-builder/types"
4040
paths "github.com/arduino/go-paths-helper"
41+
"github.com/bcmi-labs/arduino-cli/arduino/libraries"
4142
"github.com/stretchr/testify/require"
4243
)
4344

45+
func extractLibraries(ctx *types.Context) []*libraries.Library {
46+
res := []*libraries.Library{}
47+
for _, lib := range ctx.LibrariesManager.Libraries {
48+
for _, libAlternative := range lib.Alternatives {
49+
res = append(res, libAlternative)
50+
}
51+
}
52+
return res
53+
}
4454
func TestLoadLibrariesAVR(t *testing.T) {
4555
DownloadCoresAndToolsAndLibraries(t)
4656

@@ -69,7 +79,7 @@ func TestLoadLibrariesAVR(t *testing.T) {
6979
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "avr", "libraries")).EquivalentTo(librariesFolders[1]))
7080
require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2]))
7181

72-
libs := ctx.Libraries
82+
libs := extractLibraries(ctx)
7383
require.Equal(t, 24, len(libs))
7484

7585
sort.Sort(ByLibraryName(libs))
@@ -134,18 +144,24 @@ func TestLoadLibrariesAVR(t *testing.T) {
134144

135145
headerToLibraries := ctx.HeaderToLibraries
136146
require.Equal(t, 2, len(headerToLibraries["Audio.h"]))
137-
require.Equal(t, "Audio", headerToLibraries["Audio.h"][0].Name)
138-
require.Equal(t, "FakeAudio", headerToLibraries["Audio.h"][1].Name)
139-
require.Equal(t, 1, len(headerToLibraries["FakeAudio.h"]))
147+
148+
libs = headerToLibraries["Audio.h"]
149+
require.Len(t, libs, 2)
150+
sort.Sort(ByLibraryName(libs))
151+
require.Equal(t, "Audio", libs[0].Name)
152+
require.Equal(t, "FakeAudio", libs[1].Name)
153+
154+
require.Len(t, headerToLibraries["FakeAudio.h"], 1)
140155
require.Equal(t, "FakeAudio", headerToLibraries["FakeAudio.h"][0].Name)
141-
require.Equal(t, 1, len(headerToLibraries["Adafruit_PN532.h"]))
156+
157+
require.Len(t, headerToLibraries["Adafruit_PN532.h"], 1)
142158
require.Equal(t, "Adafruit_PN532", headerToLibraries["Adafruit_PN532.h"][0].Name)
143159

144-
require.Equal(t, 2, len(headerToLibraries["IRremote.h"]))
160+
require.Len(t, headerToLibraries["IRremote.h"], 2)
145161

146162
libs = headerToLibraries["IRremote.h"]
163+
require.Len(t, libs, 2)
147164
sort.Sort(ByLibraryName(libs))
148-
149165
require.Equal(t, "IRremote", libs[0].Name)
150166
require.Equal(t, "Robot_IR_Remote", libs[1].Name)
151167
}
@@ -178,7 +194,7 @@ func TestLoadLibrariesSAM(t *testing.T) {
178194
require.True(t, Abs(t, paths.New("downloaded_hardware", "arduino", "sam", "libraries")).EquivalentTo(librariesFolders[1]))
179195
require.True(t, Abs(t, paths.New("libraries")).EquivalentTo(librariesFolders[2]))
180196

181-
libraries := ctx.Libraries
197+
libraries := extractLibraries(ctx)
182198
require.Equal(t, 22, len(libraries))
183199

184200
sort.Sort(ByLibraryName(libraries))
@@ -220,17 +236,20 @@ func TestLoadLibrariesSAM(t *testing.T) {
220236

221237
headerToLibraries := ctx.HeaderToLibraries
222238

223-
require.Equal(t, 2, len(headerToLibraries["Audio.h"]))
224-
libraries = headerToLibraries["Audio.h"]
225-
sort.Sort(ByLibraryName(libraries))
226-
require.Equal(t, "Audio", libraries[0].Name)
227-
require.Equal(t, "FakeAudio", libraries[1].Name)
239+
libs := headerToLibraries["Audio.h"]
240+
require.Len(t, libs, 2)
241+
sort.Sort(ByLibraryName(libs))
242+
require.Equal(t, "Audio", libs[0].Name)
243+
require.Equal(t, "FakeAudio", libs[1].Name)
228244

229245
require.Equal(t, 1, len(headerToLibraries["FakeAudio.h"]))
230246
require.Equal(t, "FakeAudio", headerToLibraries["FakeAudio.h"][0].Name)
231-
require.Equal(t, 2, len(headerToLibraries["IRremote.h"]))
232-
require.Equal(t, "Robot_IR_Remote", headerToLibraries["IRremote.h"][0].Name)
233-
require.Equal(t, "IRremote", headerToLibraries["IRremote.h"][1].Name)
247+
248+
libs = headerToLibraries["IRremote.h"]
249+
require.Len(t, libs, 2)
250+
sort.Sort(ByLibraryName(libs))
251+
require.Equal(t, "IRremote", libs[0].Name)
252+
require.Equal(t, "Robot_IR_Remote", libs[1].Name)
234253
}
235254

236255
func TestLoadLibrariesAVRNoDuplicateLibrariesFolders(t *testing.T) {

types/context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package types
33
import (
44
"strings"
55

6+
"github.com/bcmi-labs/arduino-cli/arduino/libraries/librariesmanager"
7+
68
"github.com/arduino/arduino-builder/i18n"
79
"github.com/arduino/go-paths-helper"
810
"github.com/arduino/go-properties-map"
@@ -73,7 +75,7 @@ type Context struct {
7375
WarningsLevel string
7476

7577
// Libraries handling
76-
Libraries []*libraries.Library
78+
LibrariesManager *librariesmanager.StatusContext
7779
HeaderToLibraries map[string][]*libraries.Library
7880
ImportedLibraries []*libraries.Library
7981
LibrariesResolutionResults map[string]LibraryResolutionResult

0 commit comments

Comments
 (0)