Skip to content

Commit e6f1947

Browse files
authored
Added provides_includes field in Library gRPC message (#934)
* Refactored commaSeparatedToList function * Added 'provides_includes' field in 'lib list' output structures * Added integration tests for 'provides_includes' field * Report errors while populating library output
1 parent 08a6042 commit e6f1947

File tree

7 files changed

+102
-35
lines changed

7 files changed

+102
-35
lines changed

Diff for: arduino/libraries/libraries.go

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package libraries
1717

1818
import (
19+
"fmt"
20+
1921
"github.com/arduino/arduino-cli/arduino/cores"
2022
paths "github.com/arduino/go-paths-helper"
2123
properties "github.com/arduino/go-properties-orderedmap"
@@ -71,6 +73,7 @@ type Library struct {
7173
License string
7274
Properties *properties.Map
7375
Examples paths.PathList
76+
sourceHeaders []string
7477
}
7578

7679
func (library *Library) String() string {
@@ -153,3 +156,20 @@ func (library *Library) LocationPriorityFor(platformRelease, refPlatformRelease
153156
}
154157
return 0
155158
}
159+
160+
// SourceHeaders returns the C++ headers in the library.
161+
func (library *Library) SourceHeaders() ([]string, error) {
162+
if library.sourceHeaders == nil {
163+
cppHeaders, err := library.SourceDir.ReadDir()
164+
if err != nil {
165+
return nil, fmt.Errorf("reading lib src dir: %s", err)
166+
}
167+
cppHeaders.FilterSuffix(".h", ".hpp", ".hh")
168+
res := []string{}
169+
for _, cppHeader := range cppHeaders {
170+
res = append(res, cppHeader.Base())
171+
}
172+
library.sourceHeaders = res
173+
}
174+
return library.sourceHeaders, nil
175+
}

Diff for: arduino/libraries/librariesresolver/cpp.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ func (resolver *Cpp) ScanFromLibrariesManager(lm *librariesmanager.LibrariesMana
5252

5353
// ScanLibrary reads a library to find and cache C++ headers for later retrieval
5454
func (resolver *Cpp) ScanLibrary(lib *libraries.Library) error {
55-
cppHeaders, err := lib.SourceDir.ReadDir()
55+
cppHeaders, err := lib.SourceHeaders()
5656
if err != nil {
57-
return fmt.Errorf("reading lib src dir: %s", err)
57+
return fmt.Errorf("reading lib headers: %s", err)
5858
}
59-
cppHeaders.FilterSuffix(".h", ".hpp", ".hh")
60-
for _, cppHeaderPath := range cppHeaders {
61-
cppHeader := cppHeaderPath.Base()
59+
for _, cppHeader := range cppHeaders {
6260
l := resolver.headers[cppHeader]
6361
l.Add(lib)
6462
resolver.headers[cppHeader] = l

Diff for: arduino/libraries/loader.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
5656
}
5757
}
5858

59+
commaSeparatedToList := func(in string) []string {
60+
res := []string{}
61+
for _, e := range strings.Split(in, ",") {
62+
res = append(res, strings.TrimSpace(e))
63+
}
64+
return res
65+
}
66+
5967
library := &Library{}
6068
library.Location = location
6169
library.InstallDir = libraryDir
@@ -71,10 +79,7 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
7179
if libProperties.Get("architectures") == "" {
7280
libProperties.Set("architectures", "*")
7381
}
74-
library.Architectures = []string{}
75-
for _, arch := range strings.Split(libProperties.Get("architectures"), ",") {
76-
library.Architectures = append(library.Architectures, strings.TrimSpace(arch))
77-
}
82+
library.Architectures = commaSeparatedToList(libProperties.Get("architectures"))
7883

7984
libProperties.Set("category", strings.TrimSpace(libProperties.Get("category")))
8085
if !ValidCategories[libProperties.Get("category")] {
@@ -95,6 +100,10 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
95100
library.Version = v
96101
}
97102

103+
if includes := libProperties.Get("includes"); includes != "" {
104+
library.sourceHeaders = commaSeparatedToList(includes)
105+
}
106+
98107
if err := addExamples(library); err != nil {
99108
return nil, errors.Errorf("scanning examples: %s", err)
100109
}

Diff for: commands/lib/list.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package lib
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"strings"
2322

@@ -27,6 +26,7 @@ import (
2726
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2827
"github.com/arduino/arduino-cli/commands"
2928
rpc "github.com/arduino/arduino-cli/rpc/commands"
29+
"github.com/pkg/errors"
3030
)
3131

3232
type installedLib struct {
@@ -87,7 +87,10 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
8787
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
8888
continue
8989
}
90-
libtmp := GetOutputLibrary(lib.Library)
90+
libtmp, err := GetOutputLibrary(lib.Library)
91+
if err != nil {
92+
return nil, err
93+
}
9194
release := GetOutputRelease(lib.Available)
9295
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
9396
Library: libtmp,
@@ -125,7 +128,7 @@ func listLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bo
125128
}
126129

127130
// GetOutputLibrary FIXMEDOC
128-
func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
131+
func GetOutputLibrary(lib *libraries.Library) (*rpc.Library, error) {
129132
insdir := ""
130133
if lib.InstallDir != nil {
131134
insdir = lib.InstallDir.String()
@@ -143,6 +146,11 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
143146
cntplat = lib.ContainerPlatform.String()
144147
}
145148

149+
libHeaders, err := lib.SourceHeaders()
150+
if err != nil {
151+
return nil, errors.Errorf("getting library headers: %s", err)
152+
}
153+
146154
return &rpc.Library{
147155
Name: lib.Name,
148156
Author: lib.Author,
@@ -167,7 +175,8 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
167175
Version: lib.Version.String(),
168176
License: lib.License,
169177
Examples: lib.Examples.AsStrings(),
170-
}
178+
ProvidesIncludes: libHeaders,
179+
}, nil
171180
}
172181

173182
// GetOutputRelease FIXMEDOC

Diff for: rpc/commands/lib.pb.go

+35-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/commands/lib.proto

+3
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ message Library {
268268
LibraryLayout layout = 25;
269269
// The example sketches provided by the library
270270
repeated string examples = 26;
271+
// Value of the `includes` field in library.properties or, if missing, the list of
272+
// include files available on the library source root directory.
273+
repeated string provides_includes = 27;
271274
}
272275

273276
enum LibraryLayout {

Diff for: test/test_lib.py

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ def test_list(run_command):
5656
assert 1 == len(data)
5757
# be sure data contains the available version
5858
assert "" != data[0]["release"]["version"]
59+
# be sure data contains the correct provides_includes field
60+
assert "ArduinoJson.h" == data[0]["library"]["provides_includes"][0]
61+
assert "ArduinoJson.hpp" == data[0]["library"]["provides_includes"][1]
62+
63+
# Install something we can list without provides_includes field given in library.properties
64+
result = run_command("lib install [email protected]")
65+
assert result.ok
66+
# Look at the JSON output
67+
result = run_command("lib list Braccio --format json")
68+
assert result.ok
69+
assert "" == result.stderr
70+
data = json.loads(result.stdout)
71+
assert 1 == len(data)
72+
# be sure data contains the correct provides_includes field
73+
assert "Braccio.h" == data[0]["library"]["provides_includes"][0]
5974

6075

6176
def test_install(run_command):

0 commit comments

Comments
 (0)