Skip to content

Commit

Permalink
Added vendored libraries to the Sketch object
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jan 24, 2024
1 parent 0dfb27e commit 6e9e305
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
48 changes: 41 additions & 7 deletions internal/arduino/sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ import (
"github.com/arduino/arduino-cli/commands/cmderrors"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/arduino/globals"
"github.com/arduino/arduino-cli/internal/arduino/libraries"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
)

// Sketch holds all the files composing a sketch
type Sketch struct {
Name string
MainFile *paths.Path
FullPath *paths.Path // FullPath is the path to the Sketch folder
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
AdditionalFiles paths.PathList
RootFolderFiles paths.PathList // All files that are in the Sketch root
Project *Project
Name string
MainFile *paths.Path
FullPath *paths.Path // FullPath is the path to the Sketch folder
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
AdditionalFiles paths.PathList
RootFolderFiles paths.PathList // All files that are in the Sketch root
vendoredLibraries []*libraries.Library // All libraries in the 'libraries' directory in the sketch
Project *Project
}

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

// Collect vedndored libraries
if librariesPath, ok := sketch.GetVendoredLibrariesDir(); ok {
libDirs, err := librariesPath.ReadDir()
if err != nil {
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
}
libDirs.FilterDirs()
for _, libDir := range libDirs {
lib, err := libraries.Load(libDir, libraries.Unmanaged)
if err != nil {
return nil, fmt.Errorf("%s: %w", tr("reading sketch libraries"), err)
}
sketch.vendoredLibraries = append(sketch.vendoredLibraries, lib)
}
}

return sketch, nil
}

// GetVendoredLibrariesDir returns the 'libraries' directory path.
// The result is in the res,ok format ok is true if the 'libraries' directory
// is present in the sketch, false otherwise.
func (s *Sketch) GetVendoredLibrariesDir() (res *paths.Path, ok bool) {
libsDir := s.FullPath.Join("libraries")
if libsDir.IsDir() {
return libsDir, true
}
return nil, false
}

// VendoredLibraries returns the libraries bundled in the sketch' 'libraries' directory.
func (s *Sketch) VendoredLibraries() []*libraries.Library {
return s.vendoredLibraries
}

// supportedFiles reads all files recursively contained in Sketch and
// filter out unneded or unsupported ones and returns them
func (s *Sketch) supportedFiles() (paths.PathList, error) {
Expand Down
8 changes: 8 additions & 0 deletions internal/arduino/sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,11 @@ func TestSketchWithMultipleSymlinkLoops(t *testing.T) {
require.Error(t, err)
require.Nil(t, sketch)
}

func TestSketchWithVendoredLibraries(t *testing.T) {
sketchPath := paths.New("testdata", "SketchWithLibraries")
sk, err := New(sketchPath)
require.NoError(t, err)
require.Len(t, sk.vendoredLibraries, 1)
require.Equal(t, "MyLib", sk.vendoredLibraries[0].Name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <MyLib.h>

void setup() {}
void loop() {
myFunction();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void myFunction() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name=MyLib

0 comments on commit 6e9e305

Please sign in to comment.