Skip to content

Commit f97fdd0

Browse files
committed
Added fqbn filtering for libraries
1 parent 1a9066d commit f97fdd0

File tree

5 files changed

+166
-95
lines changed

5 files changed

+166
-95
lines changed

Diff for: cli/lib/examples.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,35 @@ import (
3535

3636
func initExamplesCommand() *cobra.Command {
3737
examplesCommand := &cobra.Command{
38-
Use: "examples LIBRARY_NAME",
39-
Short: "Shows the list of the examples for the given library.",
40-
Long: "Shows the list of the examples for the given library.",
38+
Use: "examples [LIBRARY_NAME]",
39+
Short: "Shows the list of the examples for libraries.",
40+
Long: "Shows the list of the examples for libraries. A name may be given as argument to search a specific library.",
4141
Example: " " + os.Args[0] + " lib examples Wire",
42-
Args: cobra.ExactArgs(1),
42+
Args: cobra.MaximumNArgs(1),
4343
Run: runExamplesCommand,
4444
}
45+
examplesCommand.Flags().StringVarP(&examplesFlags.fqbn, "fqbn", "b", "", "Show libraries for the specified board FQBN.")
4546
return examplesCommand
4647
}
4748

49+
var examplesFlags struct {
50+
fqbn string
51+
}
52+
4853
func runExamplesCommand(cmd *cobra.Command, args []string) {
4954
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
5055
logrus.Info("Show examples for library")
5156

57+
name := ""
58+
if len(args) > 0 {
59+
name = args[0]
60+
}
61+
5262
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
5363
Instance: instance,
5464
All: true,
55-
Name: args[0],
65+
Name: name,
66+
Fqbn: examplesFlags.fqbn,
5667
})
5768
if err != nil {
5869
feedback.Errorf("Error getting libraries info: %v", err)

Diff for: cli/lib/list.go

+3
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ func initListCommand() *cobra.Command {
4242
Run: runListCommand,
4343
}
4444
listCommand.Flags().BoolVar(&listFlags.all, "all", false, "Include built-in libraries (from platforms and IDE) in listing.")
45+
listCommand.Flags().StringVarP(&listFlags.fqbn, "fqbn", "b", "", "Show libraries for the specified board FQBN.")
4546
listCommand.Flags().BoolVar(&listFlags.updatable, "updatable", false, "List updatable libraries.")
4647
return listCommand
4748
}
4849

4950
var listFlags struct {
5051
all bool
5152
updatable bool
53+
fqbn string
5254
}
5355

5456
func runListCommand(cmd *cobra.Command, args []string) {
@@ -65,6 +67,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
6567
All: listFlags.all,
6668
Updatable: listFlags.updatable,
6769
Name: name,
70+
Fqbn: listFlags.fqbn,
6871
})
6972
if err != nil {
7073
feedback.Errorf("Error listing Libraries: %v", err)

Diff for: commands/lib/list.go

+43
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package lib
1717

1818
import (
1919
"context"
20+
"errors"
21+
"fmt"
2022
"strings"
2123

24+
"github.com/arduino/arduino-cli/arduino/cores"
2225
"github.com/arduino/arduino-cli/arduino/libraries"
2326
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2427
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -33,13 +36,53 @@ type installedLib struct {
3336

3437
// LibraryList FIXMEDOC
3538
func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {
39+
pm := commands.GetPackageManager(req.GetInstance().GetId())
40+
if pm == nil {
41+
return nil, errors.New("invalid instance")
42+
}
43+
3644
lm := commands.GetLibraryManager(req.GetInstance().GetId())
45+
if lm == nil {
46+
return nil, errors.New("invalid instance")
47+
}
3748

3849
nameFilter := strings.ToLower(req.GetName())
3950

4051
instaledLib := []*rpc.InstalledLibrary{}
4152
res := listLibraries(lm, req.GetUpdatable(), req.GetAll())
4253
if len(res) > 0 {
54+
if f := req.GetFqbn(); f != "" {
55+
fqbn, err := cores.ParseFQBN(req.GetFqbn())
56+
if err != nil {
57+
return nil, fmt.Errorf("parsing fqbn: %s", err)
58+
}
59+
_, boardPlatform, _, _, refBoardPlatform, err := pm.ResolveFQBN(fqbn)
60+
if err != nil {
61+
return nil, fmt.Errorf("loading board data: %s", err)
62+
}
63+
64+
filteredRes := map[string]*installedLib{}
65+
for _, lib := range res {
66+
if cp := lib.Library.ContainerPlatform; cp != nil {
67+
if cp != boardPlatform && cp != refBoardPlatform {
68+
// Filter all libraries from extraneous platforms
69+
continue
70+
}
71+
}
72+
if latest, has := filteredRes[lib.Library.Name]; has {
73+
if latest.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) >= lib.Library.LocationPriorityFor(boardPlatform, refBoardPlatform) {
74+
continue
75+
}
76+
}
77+
filteredRes[lib.Library.Name] = lib
78+
}
79+
80+
res = []*installedLib{}
81+
for _, lib := range filteredRes {
82+
res = append(res, lib)
83+
}
84+
}
85+
4386
for _, lib := range res {
4487
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
4588
continue

0 commit comments

Comments
 (0)