@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/arduino/go-paths-helper"
23
23
properties "github.com/arduino/go-properties-orderedmap"
24
+ "github.com/pkg/errors"
24
25
semver "go.bug.st/relaxed-semver"
25
26
)
26
27
@@ -94,6 +95,9 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
94
95
library .Version = v
95
96
}
96
97
98
+ if err := addExamples (library ); err != nil {
99
+ return nil , errors .Errorf ("scanning examples: %s" , err )
100
+ }
97
101
library .Name = libraryDir .Base ()
98
102
library .RealName = strings .TrimSpace (libProperties .Get ("name" ))
99
103
library .Author = strings .TrimSpace (libProperties .Get ("author" ))
@@ -122,6 +126,56 @@ func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, er
122
126
IsLegacy : true ,
123
127
Version : semver .MustParse ("" ),
124
128
}
129
+ if err := addExamples (library ); err != nil {
130
+ return nil , errors .Errorf ("scanning examples: %s" , err )
131
+ }
125
132
addUtilityDirectory (library )
126
133
return library , nil
127
134
}
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
+ }
0 commit comments