Skip to content

Commit af3eae7

Browse files
committed
porting of lib/list command to daemon
1 parent 9910ea7 commit af3eae7

File tree

13 files changed

+846
-304
lines changed

13 files changed

+846
-304
lines changed

arduino/libraries/libraries_layout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type LibraryLayout uint16
2727

2828
const (
2929
// FlatLayout is a library without a `src` directory
30-
FlatLayout LibraryLayout = 1 << iota
30+
FlatLayout LibraryLayout = iota
3131
// RecursiveLayout is a library with `src` directory (that allows recursive build)
3232
RecursiveLayout
3333
)

arduino/libraries/libraries_location.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type LibraryLocation int
2828
// The enumeration is listed in ascending order of priority
2929
const (
3030
// IDEBuiltIn are libraries bundled in the IDE
31-
IDEBuiltIn = 1 << iota
31+
IDEBuiltIn = iota
3232
// PlatformBuiltIn are libraries bundled in a PlatformRelease
3333
PlatformBuiltIn
3434
// ReferencedPlatformBuiltIn are libraries bundled in a PlatformRelease referenced for build

cli/lib/list.go

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
package lib
1919

2020
import (
21-
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
21+
"fmt"
22+
"os"
23+
2224
"github.com/arduino/arduino-cli/cli"
2325
"github.com/arduino/arduino-cli/commands/lib"
2426
"github.com/arduino/arduino-cli/common/formatter"
27+
"github.com/arduino/arduino-cli/rpc"
28+
"github.com/gosuri/uitable"
2529
"github.com/sirupsen/logrus"
2630
"github.com/spf13/cobra"
31+
"golang.org/x/net/context"
2732
)
2833

2934
func initListCommand() *cobra.Command {
@@ -46,18 +51,72 @@ var listFlags struct {
4651
}
4752

4853
func runListCommand(cmd *cobra.Command, args []string) {
54+
instance := cli.CreateInstance()
4955
logrus.Info("Listing")
5056

51-
var lm *librariesmanager.LibrariesManager
52-
if listFlags.all {
53-
_, lm = cli.InitPackageAndLibraryManager()
57+
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
58+
Instance: instance,
59+
All: listFlags.all,
60+
Updatable: listFlags.updatable,
61+
})
62+
if err != nil {
63+
formatter.PrintError(err, "Error listing Libraries")
64+
os.Exit(cli.ErrGeneric)
65+
}
66+
if len(res.GetInstalledLibrary()) > 0 {
67+
results := res.GetInstalledLibrary()
68+
if cli.OutputJSONOrElse(results) {
69+
if len(results) > 0 {
70+
fmt.Println(outputListLibrary(results))
71+
} else {
72+
formatter.Print("Error listing Libraries")
73+
}
74+
}
75+
}
76+
logrus.Info("Done")
77+
}
78+
79+
func outputListLibrary(il []*rpc.InstalledLibrary) string {
80+
table := uitable.New()
81+
table.MaxColWidth = 100
82+
table.Wrap = true
83+
84+
hasUpdates := false
85+
for _, libMeta := range il {
86+
if libMeta.GetRelease() != nil {
87+
hasUpdates = true
88+
}
89+
}
90+
91+
if hasUpdates {
92+
table.AddRow("Name", "Installed", "Available", "Location")
5493
} else {
55-
lm = cli.InitLibraryManager(cli.Config)
94+
table.AddRow("Name", "Installed", "Location")
5695
}
5796

58-
res := lib.ListLibraries(lm, listFlags.updatable)
59-
if len(res.Libraries) > 0 {
60-
formatter.Print(res)
97+
lastName := ""
98+
for _, libMeta := range il {
99+
lib := libMeta.GetLibrary()
100+
name := lib.Name
101+
if name == lastName {
102+
name = ` "`
103+
} else {
104+
lastName = name
105+
}
106+
107+
location := lib.GetLocation()
108+
if lib.ContainerPlatform != "" {
109+
location = lib.GetContainerPlatform()
110+
}
111+
if hasUpdates {
112+
var available string
113+
if libMeta.GetRelease() != nil {
114+
available = libMeta.GetRelease().GetVersion()
115+
}
116+
table.AddRow(name, lib.Version, available, location)
117+
} else {
118+
table.AddRow(name, lib.Version, location)
119+
}
61120
}
62-
logrus.Info("Done")
121+
return fmt.Sprintln(table)
63122
}

commands/lib/list.go

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,125 @@
1818
package lib
1919

2020
import (
21+
"context"
22+
23+
"github.com/arduino/arduino-cli/arduino/libraries"
2124
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2225
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
23-
"github.com/arduino/arduino-cli/common/formatter/output"
26+
"github.com/arduino/arduino-cli/commands"
27+
"github.com/arduino/arduino-cli/rpc"
2428
)
2529

30+
type InstalledLib struct {
31+
Library *libraries.Library `json:"library"`
32+
Available *librariesindex.Release `omitempy,json:"available"`
33+
}
34+
35+
func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {
36+
37+
lm := commands.GetLibraryManager(req)
38+
instaledLib := []*rpc.InstalledLibrary{}
39+
res := ListLibraries(lm, req.GetUpdatable(), req.GetAll())
40+
if len(res) > 0 {
41+
for _, lib := range res {
42+
libtmp := GetOutputLibrary(lib.Library)
43+
release := GetOutputRelease(lib.Available)
44+
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
45+
Library: libtmp,
46+
Release: release,
47+
})
48+
}
49+
50+
return &rpc.LibraryListResp{InstalledLibrary: instaledLib}, nil
51+
}
52+
return &rpc.LibraryListResp{}, nil
53+
}
54+
2655
// ListLibraries returns the list of installed libraries. If updatable is true it
2756
// returns only the libraries that may be updated.
28-
func ListLibraries(lm *librariesmanager.LibrariesManager, updatable bool) *output.InstalledLibraries {
29-
res := &output.InstalledLibraries{}
57+
func ListLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bool) []*InstalledLib {
58+
59+
res := []*InstalledLib{}
3060
for _, libAlternatives := range lm.Libraries {
3161
for _, lib := range libAlternatives.Alternatives {
62+
if !all {
63+
if lib.Location != libraries.Sketchbook {
64+
continue
65+
}
66+
}
3267
var available *librariesindex.Release
3368
if updatable {
3469
available = lm.Index.FindLibraryUpdate(lib)
3570
if available == nil {
3671
continue
3772
}
3873
}
39-
res.Libraries = append(res.Libraries, &output.InstalledLibary{
74+
res = append(res, &InstalledLib{
4075
Library: lib,
4176
Available: available,
4277
})
4378
}
4479
}
4580
return res
4681
}
82+
83+
func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
84+
insdir := ""
85+
if lib.InstallDir != nil {
86+
insdir = lib.InstallDir.String()
87+
}
88+
srcdir := ""
89+
if lib.SourceDir != nil {
90+
srcdir = lib.SourceDir.String()
91+
}
92+
utldir := ""
93+
if lib.UtilityDir != nil {
94+
utldir = lib.UtilityDir.String()
95+
}
96+
cntplat := ""
97+
if lib.ContainerPlatform != nil {
98+
cntplat = lib.ContainerPlatform.String()
99+
}
100+
101+
return &rpc.Library{
102+
Name: lib.Name,
103+
Author: lib.Author,
104+
Maintainer: lib.Maintainer,
105+
Sentence: lib.Sentence,
106+
Paragraph: lib.Paragraph,
107+
Website: lib.Website,
108+
Category: lib.Category,
109+
Architectures: lib.Architectures,
110+
Types: lib.Types,
111+
InstallDir: insdir,
112+
SourceDir: srcdir,
113+
UtilityDir: utldir,
114+
Location: lib.Location.String(),
115+
ContainerPlatform: cntplat,
116+
Layout: lib.Layout.String(),
117+
RealName: lib.RealName,
118+
DotALinkage: lib.DotALinkage,
119+
Precompiled: lib.Precompiled,
120+
LDflags: lib.LDflags,
121+
IsLegacy: lib.IsLegacy,
122+
Version: lib.Version.String(),
123+
License: lib.LDflags,
124+
}
125+
}
126+
127+
func GetOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease { //
128+
if lib != nil {
129+
return &rpc.LibraryRelease{
130+
Author: lib.Author,
131+
Version: lib.Version.String(),
132+
Maintainer: lib.Maintainer,
133+
Sentence: lib.Sentence,
134+
Paragraph: lib.Paragraph,
135+
Website: lib.Website,
136+
Category: lib.Category,
137+
Architectures: lib.Architectures,
138+
Types: lib.Types,
139+
}
140+
}
141+
return &rpc.LibraryRelease{}
142+
}

commands/lib/upgrade.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downl
2929
lm := commands.GetLibraryManager(req)
3030

3131
// Obtain the list of upgradable libraries
32-
list := ListLibraries(lm, true)
32+
list := ListLibraries(lm, true, true)
3333

34-
for _, upgradeDesc := range list.Libraries {
34+
for _, upgradeDesc := range list {
3535
if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB); err != nil {
3636
return err
3737
}
3838
}
39-
for _, upgradeDesc := range list.Libraries {
39+
for _, upgradeDesc := range list {
4040
installLibrary(lm, upgradeDesc.Available, taskCB)
4141
}
4242

common/formatter/output/lib_structs.go

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717

1818
package output
1919

20-
import (
21-
"fmt"
22-
"strings"
23-
24-
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
25-
)
20+
import "fmt"
2621

2722
// VersionResult represents the output of the version commands.
2823
type VersionResult struct {
@@ -33,65 +28,3 @@ type VersionResult struct {
3328
func (vr VersionResult) String() string {
3429
return fmt.Sprintf("%s version %s", vr.CommandName, vr.Version)
3530
}
36-
37-
// LibProcessResults represent the result of a process on libraries.
38-
type LibProcessResults struct {
39-
Libraries map[string]ProcessResult `json:"libraries,required"`
40-
}
41-
42-
// CoreProcessResults represent the result of a process on cores or tools.
43-
type CoreProcessResults struct {
44-
Cores map[string]ProcessResult `json:"cores,omitempty"`
45-
Tools map[string]ProcessResult `json:"tools,omitempty"`
46-
}
47-
48-
// String returns a string representation of the object.
49-
func (cpr CoreProcessResults) String() string {
50-
ret := ""
51-
for _, cr := range cpr.Cores {
52-
ret += fmt.Sprintln(cr)
53-
}
54-
for _, tr := range cpr.Tools {
55-
ret += fmt.Sprintln(tr)
56-
}
57-
return ret
58-
}
59-
60-
// LibSearchResults represents a set of results of a search of libraries.
61-
type LibSearchResults struct {
62-
Libraries []*librariesindex.Library `json:"libraries,required"`
63-
}
64-
65-
// String returns a string representation of the object.
66-
func (lpr LibProcessResults) String() string {
67-
ret := ""
68-
for _, lr := range lpr.Libraries {
69-
ret += fmt.Sprintln(lr)
70-
}
71-
return strings.TrimSpace(ret)
72-
}
73-
74-
// String returns a string representation of the object.
75-
func (lsr LibSearchResults) String() string {
76-
ret := ""
77-
for _, l := range lsr.Libraries {
78-
ret += fmt.Sprintf("Name: \"%s\"\n", l.Name) +
79-
fmt.Sprintln(" Author: ", l.Latest.Author) +
80-
fmt.Sprintln(" Maintainer: ", l.Latest.Maintainer) +
81-
fmt.Sprintln(" Sentence: ", l.Latest.Sentence) +
82-
fmt.Sprintln(" Paragraph: ", l.Latest.Paragraph) +
83-
fmt.Sprintln(" Website: ", l.Latest.Website) +
84-
fmt.Sprintln(" Category: ", l.Latest.Category) +
85-
fmt.Sprintln(" Architecture: ", strings.Join(l.Latest.Architectures, ", ")) +
86-
fmt.Sprintln(" Types: ", strings.Join(l.Latest.Types, ", ")) +
87-
fmt.Sprintln(" Versions: ", strings.Replace(fmt.Sprint(l.Versions()), " ", ", ", -1))
88-
}
89-
return strings.TrimSpace(ret)
90-
}
91-
92-
// Results returns a set of generic results, to allow them to be modified externally.
93-
//
94-
// -> ProcessResults interface.
95-
func (lpr LibProcessResults) Results() map[string]ProcessResult {
96-
return lpr.Libraries
97-
}

0 commit comments

Comments
 (0)