Skip to content

Commit a760b7c

Browse files
Improve library examples loading process by filtering out unneeded directories
1 parent 2a5c83a commit a760b7c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

arduino/libraries/loader.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"strings"
2121

2222
"github.com/arduino/arduino-cli/arduino/globals"
23-
"github.com/arduino/arduino-cli/arduino/sketch"
2423
"github.com/arduino/go-paths-helper"
2524
properties "github.com/arduino/go-properties-orderedmap"
2625
"github.com/pkg/errors"
@@ -176,20 +175,11 @@ func addExamples(lib *Library) error {
176175
}
177176

178177
func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error {
179-
files, err := examplesPath.ReadDir()
178+
files, err := examplesPath.ReadDirRecursiveFiltered(nil, paths.AndFilter(paths.FilterDirectories(), filterExamplesDirs))
180179
if err != nil {
181180
return err
182181
}
183-
for _, file := range files {
184-
_, err := sketch.New(file)
185-
if err == nil {
186-
list.Add(file)
187-
} else if file.IsDir() {
188-
if err := addExamplesToPathList(file, list); err != nil {
189-
return err
190-
}
191-
}
192-
}
182+
list.AddAll(files)
193183
return nil
194184
}
195185

@@ -208,3 +198,17 @@ func containsHeaderFile(d *paths.Path) (bool, error) {
208198
dirContent.FilterSuffix(headerExtensions...)
209199
return len(dirContent) > 0, nil
210200
}
201+
202+
// filterExamplesDirs filters out any directory that does not contain a ".ino" or ".pde" file
203+
// with the correct casing
204+
func filterExamplesDirs(dir *paths.Path) bool {
205+
if list, err := dir.ReadDir(); err == nil {
206+
list.FilterOutDirs()
207+
list.FilterPrefix(dir.Base()+".ino", dir.Base()+".pde")
208+
// accept only directories that contain a single correct sketch
209+
if list.Len() == 1 {
210+
return true
211+
}
212+
}
213+
return false
214+
}

0 commit comments

Comments
 (0)