Skip to content

Commit 6e9e305

Browse files
committed
Added vendored libraries to the Sketch object
1 parent 0dfb27e commit 6e9e305

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

Diff for: internal/arduino/sketch/sketch.go

+41-7
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ import (
2626
"github.com/arduino/arduino-cli/commands/cmderrors"
2727
f "github.com/arduino/arduino-cli/internal/algorithms"
2828
"github.com/arduino/arduino-cli/internal/arduino/globals"
29+
"github.com/arduino/arduino-cli/internal/arduino/libraries"
2930
"github.com/arduino/arduino-cli/internal/i18n"
3031
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3132
"github.com/arduino/go-paths-helper"
3233
)
3334

3435
// Sketch holds all the files composing a sketch
3536
type Sketch struct {
36-
Name string
37-
MainFile *paths.Path
38-
FullPath *paths.Path // FullPath is the path to the Sketch folder
39-
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
40-
AdditionalFiles paths.PathList
41-
RootFolderFiles paths.PathList // All files that are in the Sketch root
42-
Project *Project
37+
Name string
38+
MainFile *paths.Path
39+
FullPath *paths.Path // FullPath is the path to the Sketch folder
40+
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
41+
AdditionalFiles paths.PathList
42+
RootFolderFiles paths.PathList // All files that are in the Sketch root
43+
vendoredLibraries []*libraries.Library // All libraries in the 'libraries' directory in the sketch
44+
Project *Project
4345
}
4446

4547
var tr = i18n.Tr
@@ -148,9 +150,41 @@ func New(path *paths.Path) (*Sketch, error) {
148150
sort.Sort(&sketch.OtherSketchFiles)
149151
sort.Sort(&sketch.RootFolderFiles)
150152

153+
// Collect vedndored libraries
154+
if librariesPath, ok := sketch.GetVendoredLibrariesDir(); ok {
155+
libDirs, err := librariesPath.ReadDir()
156+
if err != nil {
157+
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
158+
}
159+
libDirs.FilterDirs()
160+
for _, libDir := range libDirs {
161+
lib, err := libraries.Load(libDir, libraries.Unmanaged)
162+
if err != nil {
163+
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
164+
}
165+
sketch.vendoredLibraries = append(sketch.vendoredLibraries, lib)
166+
}
167+
}
168+
151169
return sketch, nil
152170
}
153171

172+
// GetVendoredLibrariesDir returns the 'libraries' directory path.
173+
// The result is in the res,ok format ok is true if the 'libraries' directory
174+
// is present in the sketch, false otherwise.
175+
func (s *Sketch) GetVendoredLibrariesDir() (res *paths.Path, ok bool) {
176+
libsDir := s.FullPath.Join("libraries")
177+
if libsDir.IsDir() {
178+
return libsDir, true
179+
}
180+
return nil, false
181+
}
182+
183+
// VendoredLibraries returns the libraries bundled in the sketch' 'libraries' directory.
184+
func (s *Sketch) VendoredLibraries() []*libraries.Library {
185+
return s.vendoredLibraries
186+
}
187+
154188
// supportedFiles reads all files recursively contained in Sketch and
155189
// filter out unneded or unsupported ones and returns them
156190
func (s *Sketch) supportedFiles() (paths.PathList, error) {

Diff for: internal/arduino/sketch/sketch_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,11 @@ func TestSketchWithMultipleSymlinkLoops(t *testing.T) {
387387
require.Error(t, err)
388388
require.Nil(t, sketch)
389389
}
390+
391+
func TestSketchWithVendoredLibraries(t *testing.T) {
392+
sketchPath := paths.New("testdata", "SketchWithLibraries")
393+
sk, err := New(sketchPath)
394+
require.NoError(t, err)
395+
require.Len(t, sk.vendoredLibraries, 1)
396+
require.Equal(t, "MyLib", sk.vendoredLibraries[0].Name)
397+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <MyLib.h>
2+
3+
void setup() {}
4+
void loop() {
5+
myFunction();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void myFunction() {
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name=MyLib

0 commit comments

Comments
 (0)