Skip to content

Commit acde186

Browse files
committed
Added 'examples' field in rpc.Library
1 parent 1785314 commit acde186

File tree

5 files changed

+156
-88
lines changed

5 files changed

+156
-88
lines changed

arduino/libraries/libraries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type Library struct {
7070
Version *semver.Version
7171
License string
7272
Properties *properties.Map
73+
Examples paths.PathList
7374
}
7475

7576
func (library *Library) String() string {

arduino/libraries/loader.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/arduino/go-paths-helper"
2323
properties "github.com/arduino/go-properties-orderedmap"
24+
"github.com/pkg/errors"
2425
semver "go.bug.st/relaxed-semver"
2526
)
2627

@@ -94,6 +95,9 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
9495
library.Version = v
9596
}
9697

98+
if err := addExamples(library); err != nil {
99+
return nil, errors.Errorf("scanning examples: %s", err)
100+
}
97101
library.Name = libraryDir.Base()
98102
library.RealName = strings.TrimSpace(libProperties.Get("name"))
99103
library.Author = strings.TrimSpace(libProperties.Get("author"))
@@ -122,6 +126,56 @@ func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, er
122126
IsLegacy: true,
123127
Version: semver.MustParse(""),
124128
}
129+
if err := addExamples(library); err != nil {
130+
return nil, errors.Errorf("scanning examples: %s", err)
131+
}
125132
addUtilityDirectory(library)
126133
return library, nil
127134
}
135+
136+
func addExamples(lib *Library) error {
137+
files, err := lib.InstallDir.ReadDir()
138+
if err != nil {
139+
return err
140+
}
141+
examples := paths.NewPathList()
142+
for _, file := range files {
143+
name := strings.ToLower(file.Base())
144+
if name != "example" && name != "examples" {
145+
continue
146+
}
147+
if !file.IsDir() {
148+
continue
149+
}
150+
if err := addExamplesToPathList(file, &examples); err != nil {
151+
return err
152+
}
153+
break
154+
}
155+
156+
lib.Examples = examples
157+
return nil
158+
}
159+
160+
func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error {
161+
files, err := examplesPath.ReadDir()
162+
if err != nil {
163+
return err
164+
}
165+
for _, file := range files {
166+
if isExample(file) {
167+
list.Add(file)
168+
} else if file.IsDir() {
169+
if err := addExamplesToPathList(file, list); err != nil {
170+
return err
171+
}
172+
}
173+
}
174+
return nil
175+
}
176+
177+
// isExample returns true if examplePath contains an example
178+
func isExample(examplePath *paths.Path) bool {
179+
mainIno := examplePath.Join(examplePath.Base() + ".ino")
180+
return mainIno.Exist() && mainIno.IsNotDir()
181+
}

commands/lib/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
117117
IsLegacy: lib.IsLegacy,
118118
Version: lib.Version.String(),
119119
License: lib.License,
120+
Examples: lib.Examples.AsStrings(),
120121
}
121122
}
122123

0 commit comments

Comments
 (0)