Unverified Commit c3c7c739 authored by Silvano Cerza's avatar Silvano Cerza Committed by GitHub

lib list command now only shows compatible libraries when specifying fqbn (#978)

parent 27316e33
......@@ -75,6 +75,7 @@ type Library struct {
Examples paths.PathList
declaredHeaders []string
sourceHeaders []string
CompatibleWith map[string]bool
}
func (library *Library) String() string {
......
......@@ -76,7 +76,16 @@ func runListCommand(cmd *cobra.Command, args []string) {
os.Exit(errorcodes.ErrGeneric)
}
libs := res.GetInstalledLibrary()
libs := []*rpc.InstalledLibrary{}
if listFlags.fqbn == "" {
libs = res.GetInstalledLibrary()
} else {
for _, lib := range res.GetInstalledLibrary() {
if lib.Library.CompatibleWith[listFlags.fqbn] {
libs = append(libs, lib)
}
}
}
// To uniform the output to other commands, when there are no result
// print out an empty slice.
......@@ -139,7 +148,6 @@ func (ir installedResult) String() string {
} else if len(sentence) > 40 {
sentence = sentence[:37] + "..."
}
t.AddRow(name, lib.Version, available, location, sentence)
}
}
......
......@@ -50,57 +50,67 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
instaledLib := []*rpc.InstalledLibrary{}
res := listLibraries(lm, req.GetUpdatable(), req.GetAll())
if len(res) > 0 {
if f := req.GetFqbn(); f != "" {
fqbn, err := cores.ParseFQBN(req.GetFqbn())
if err != nil {
return nil, fmt.Errorf("parsing fqbn: %s", err)
}
_, boardPlatform, _, _, refBoardPlatform, err := pm.ResolveFQBN(fqbn)
if err != nil {
return nil, fmt.Errorf("loading board data: %s", err)
}
if f := req.GetFqbn(); f != "" {
fqbn, err := cores.ParseFQBN(req.GetFqbn())
if err != nil {
return nil, fmt.Errorf("parsing fqbn: %s", err)
}
_, boardPlatform, _, _, refBoardPlatform, err := pm.ResolveFQBN(fqbn)
if err != nil {
return nil, fmt.Errorf("loading board data: %s", err)
}
filteredRes := map[string]*installedLib{}
for _, lib := range res {
if cp := lib.Library.ContainerPlatform; cp != nil {
if cp != boardPlatform && cp != refBoardPlatform {
// Filter all libraries from extraneous platforms
continue
}
filteredRes := map[string]*installedLib{}
for _, lib := range res {
if cp := lib.Library.ContainerPlatform; cp != nil {
if cp != boardPlatform && cp != refBoardPlatform {
// Filter all libraries from extraneous platforms
continue
}
if latest, has := filteredRes[lib.Library.Name]; has {
if latest.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) >= lib.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) {
continue
}
}
if latest, has := filteredRes[lib.Library.Name]; has {
if latest.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) >= lib.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) {
continue
}
filteredRes[lib.Library.Name] = lib
}
res = []*installedLib{}
for _, lib := range filteredRes {
res = append(res, lib)
// Check if library is compatible with board specified by FBQN
compatible := false
for _, arch := range lib.Library.Architectures {
compatible = (arch == fqbn.PlatformArch || arch == "*")
if compatible {
break
}
}
lib.Library.CompatibleWith = map[string]bool{
f: compatible,
}
filteredRes[lib.Library.Name] = lib
}
for _, lib := range res {
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
continue
}
libtmp, err := GetOutputLibrary(lib.Library)
if err != nil {
return nil, err
}
release := GetOutputRelease(lib.Available)
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
Library: libtmp,
Release: release,
})
res = []*installedLib{}
for _, lib := range filteredRes {
res = append(res, lib)
}
}
return &rpc.LibraryListResp{InstalledLibrary: instaledLib}, nil
for _, lib := range res {
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
continue
}
libtmp, err := GetOutputLibrary(lib.Library)
if err != nil {
return nil, err
}
release := GetOutputRelease(lib.Available)
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
Library: libtmp,
Release: release,
})
}
return &rpc.LibraryListResp{}, nil
return &rpc.LibraryListResp{InstalledLibrary: instaledLib}, nil
}
// listLibraries returns the list of installed libraries. If updatable is true it
......@@ -171,6 +181,7 @@ func GetOutputLibrary(lib *libraries.Library) (*rpc.Library, error) {
License: lib.License,
Examples: lib.Examples.AsStrings(),
ProvidesIncludes: lib.DeclaredHeaders(),
CompatibleWith: lib.CompatibleWith,
}, nil
}
......
This diff is collapsed.
......@@ -271,6 +271,8 @@ message Library {
// 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;
// Map of FQBNs that specifies if library is compatible with this library
map<string, bool> compatible_with = 28;
}
enum LibraryLayout {
......@@ -287,7 +289,7 @@ enum LibraryLocation {
user = 1;
// In the `libraries` subdirectory of a platform.
platform_builtin = 2;
// When `LibraryLocation` is used in a context where a board is specified,
// When `LibraryLocation` is used in a context where a board is specified,
// this indicates the library is in the `libraries` subdirectory of a
// platform referenced by the board's platform.
referenced_platform_builtin = 3;
......
......@@ -70,6 +70,76 @@ def test_list(run_command):
assert "Arduino_APDS9960.h" == data[0]["library"]["provides_includes"][0]
def test_list_exit_code(run_command):
# Init the environment explicitly
assert run_command("core update-index")
assert run_command("core list")
# Verifies lib list doesn't fail when platform is not specified
result = run_command("lib list")
assert result.ok
assert result.stderr.strip() == ""
# Verify lib list command fails because specified platform is not installed
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.failed
assert (
result.stderr.strip() == "Error listing Libraries: loading board data: platform arduino:samd is not installed"
)
assert run_command('lib install "AllThingsTalk LoRaWAN SDK"')
# Verifies lib list command keeps failing
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.failed
assert (
result.stderr.strip() == "Error listing Libraries: loading board data: platform arduino:samd is not installed"
)
assert run_command("core install arduino:samd")
# Verifies lib list command now works since platform has been installed
result = run_command("lib list -b arduino:samd:mkr1000")
assert result.ok
assert result.stderr.strip() == ""
def test_list_with_fqbn(run_command):
# Init the environment explicitly
assert run_command("core update-index")
# Install core
assert run_command("core install arduino:avr")
# Install some library
assert run_command("lib install ArduinoJson")
assert run_command("lib install wm8978-esp32")
# Look at the plain text output
result = run_command("lib list -b arduino:avr:uno")
assert result.ok
assert "" == result.stderr
lines = result.stdout.strip().splitlines()
assert 2 == len(lines)
# Verifies library is compatible
toks = [t.strip() for t in lines[1].split(maxsplit=4)]
assert 5 == len(toks)
assert "ArduinoJson" == toks[0]
# Look at the JSON output
result = run_command("lib list -b arduino:avr:uno --format json")
assert result.ok
assert "" == result.stderr
data = json.loads(result.stdout)
assert 1 == len(data)
# Verifies library is compatible
assert data[0]["library"]["name"] == "ArduinoJson"
assert data[0]["library"]["compatible_with"]["arduino:avr:uno"]
def test_install(run_command):
libs = ['"AzureIoTProtocol_MQTT"', '"CMMC MQTT Connector"', '"WiFiNINA"']
# Should be safe to run install multiple times
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment