Skip to content

Added provides_includes field in Library gRPC message #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package libraries

import (
"fmt"

"github.com/arduino/arduino-cli/arduino/cores"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
Expand Down Expand Up @@ -71,6 +73,7 @@ type Library struct {
License string
Properties *properties.Map
Examples paths.PathList
sourceHeaders []string
}

func (library *Library) String() string {
Expand Down Expand Up @@ -153,3 +156,20 @@ func (library *Library) LocationPriorityFor(platformRelease, refPlatformRelease
}
return 0
}

// SourceHeaders returns the C++ headers in the library.
func (library *Library) SourceHeaders() ([]string, error) {
if library.sourceHeaders == nil {
cppHeaders, err := library.SourceDir.ReadDir()
if err != nil {
return nil, fmt.Errorf("reading lib src dir: %s", err)
}
cppHeaders.FilterSuffix(".h", ".hpp", ".hh")
res := []string{}
for _, cppHeader := range cppHeaders {
res = append(res, cppHeader.Base())
}
library.sourceHeaders = res
}
return library.sourceHeaders, nil
}
8 changes: 3 additions & 5 deletions arduino/libraries/librariesresolver/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ func (resolver *Cpp) ScanFromLibrariesManager(lm *librariesmanager.LibrariesMana

// ScanLibrary reads a library to find and cache C++ headers for later retrieval
func (resolver *Cpp) ScanLibrary(lib *libraries.Library) error {
cppHeaders, err := lib.SourceDir.ReadDir()
cppHeaders, err := lib.SourceHeaders()
if err != nil {
return fmt.Errorf("reading lib src dir: %s", err)
return fmt.Errorf("reading lib headers: %s", err)
}
cppHeaders.FilterSuffix(".h", ".hpp", ".hh")
for _, cppHeaderPath := range cppHeaders {
cppHeader := cppHeaderPath.Base()
for _, cppHeader := range cppHeaders {
l := resolver.headers[cppHeader]
l.Add(lib)
resolver.headers[cppHeader] = l
Expand Down
17 changes: 13 additions & 4 deletions arduino/libraries/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
}
}

commaSeparatedToList := func(in string) []string {
res := []string{}
for _, e := range strings.Split(in, ",") {
res = append(res, strings.TrimSpace(e))
}
return res
}

library := &Library{}
library.Location = location
library.InstallDir = libraryDir
Expand All @@ -71,10 +79,7 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
if libProperties.Get("architectures") == "" {
libProperties.Set("architectures", "*")
}
library.Architectures = []string{}
for _, arch := range strings.Split(libProperties.Get("architectures"), ",") {
library.Architectures = append(library.Architectures, strings.TrimSpace(arch))
}
library.Architectures = commaSeparatedToList(libProperties.Get("architectures"))

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

if includes := libProperties.Get("includes"); includes != "" {
library.sourceHeaders = commaSeparatedToList(includes)
}

if err := addExamples(library); err != nil {
return nil, errors.Errorf("scanning examples: %s", err)
}
Expand Down
17 changes: 13 additions & 4 deletions commands/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package lib

import (
"context"
"errors"
"fmt"
"strings"

Expand All @@ -27,6 +26,7 @@ import (
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/commands"
"github.com/pkg/errors"
)

type installedLib struct {
Expand Down Expand Up @@ -87,7 +87,10 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
continue
}
libtmp := GetOutputLibrary(lib.Library)
libtmp, err := GetOutputLibrary(lib.Library)
if err != nil {
return nil, err
}
release := GetOutputRelease(lib.Available)
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
Library: libtmp,
Expand Down Expand Up @@ -125,7 +128,7 @@ func listLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bo
}

// GetOutputLibrary FIXMEDOC
func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
func GetOutputLibrary(lib *libraries.Library) (*rpc.Library, error) {
insdir := ""
if lib.InstallDir != nil {
insdir = lib.InstallDir.String()
Expand All @@ -143,6 +146,11 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
cntplat = lib.ContainerPlatform.String()
}

libHeaders, err := lib.SourceHeaders()
if err != nil {
return nil, errors.Errorf("getting library headers: %s", err)
}

return &rpc.Library{
Name: lib.Name,
Author: lib.Author,
Expand All @@ -167,7 +175,8 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
Version: lib.Version.String(),
License: lib.License,
Examples: lib.Examples.AsStrings(),
}
ProvidesIncludes: libHeaders,
}, nil
}

// GetOutputRelease FIXMEDOC
Expand Down
57 changes: 35 additions & 22 deletions rpc/commands/lib.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/commands/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ message Library {
LibraryLayout layout = 25;
// The example sketches provided by the library
repeated string examples = 26;
// Value of the `includes` field in library.properties or, if missing, the list of
// include files available on the library source root directory.
repeated string provides_includes = 27;
}

enum LibraryLayout {
Expand Down
15 changes: 15 additions & 0 deletions test/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ def test_list(run_command):
assert 1 == len(data)
# be sure data contains the available version
assert "" != data[0]["release"]["version"]
# be sure data contains the correct provides_includes field
assert "ArduinoJson.h" == data[0]["library"]["provides_includes"][0]
assert "ArduinoJson.hpp" == data[0]["library"]["provides_includes"][1]

# Install something we can list without provides_includes field given in library.properties
result = run_command("lib install [email protected]")
assert result.ok
# Look at the JSON output
result = run_command("lib list Braccio --format json")
assert result.ok
assert "" == result.stderr
data = json.loads(result.stdout)
assert 1 == len(data)
# be sure data contains the correct provides_includes field
assert "Braccio.h" == data[0]["library"]["provides_includes"][0]


def test_install(run_command):
Expand Down