Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d02fc83

Browse files
committed
Added vendored libraries to the Sketch object
1 parent 54209cf commit d02fc83

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

‎internal/arduino/sketch/sketch.go‎

Lines changed: 41 additions & 7 deletions
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
// New creates an Sketch instance by reading all the files composing a sketch and grouping them
@@ -142,9 +144,41 @@ func New(path *paths.Path) (*Sketch, error) {
142144
sort.Sort(&sketch.OtherSketchFiles)
143145
sort.Sort(&sketch.RootFolderFiles)
144146

147+
// Collect vedndored libraries
148+
if librariesPath, ok := sketch.GetVendoredLibrariesDir(); ok {
149+
libDirs, err := librariesPath.ReadDir()
150+
if err != nil {
151+
return nil, fmt.Errorf("%s: %w", i18n.Tr("reading sketch libraries"), err)
152+
}
153+
libDirs.FilterDirs()
154+
for _, libDir := range libDirs {
155+
lib, err := libraries.Load(libDir, libraries.Unmanaged)
156+
if err != nil {
157+
return nil, fmt.Errorf("%s: %w", i18n.Tr("reading sketch libraries"), err)
158+
}
159+
sketch.vendoredLibraries = append(sketch.vendoredLibraries, lib)
160+
}
161+
}
162+
145163
return sketch, nil
146164
}
147165

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

‎internal/arduino/sketch/sketch_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,11 @@ func TestSketchWithMultipleSymlinkLoops(t *testing.T) {
381381
require.Error(t, err)
382382
require.Nil(t, sketch)
383383
}
384+
385+
func TestSketchWithVendoredLibraries(t *testing.T) {
386+
sketchPath := paths.New("testdata", "SketchWithLibraries")
387+
sk, err := New(sketchPath)
388+
require.NoError(t, err)
389+
require.Len(t, sk.vendoredLibraries, 1)
390+
require.Equal(t, "MyLib", sk.vendoredLibraries[0].Name)
391+
}
Lines changed: 6 additions & 0 deletions
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
void myFunction() {
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name=MyLib

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /