Skip to content

Commit

Permalink
porting of lib/list command to daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Rocketct committed May 7, 2019
1 parent 9910ea7 commit af3eae7
Show file tree
Hide file tree
Showing 13 changed files with 846 additions and 304 deletions.
2 changes: 1 addition & 1 deletion arduino/libraries/libraries_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type LibraryLayout uint16

const (
// FlatLayout is a library without a `src` directory
FlatLayout LibraryLayout = 1 << iota
FlatLayout LibraryLayout = iota
// RecursiveLayout is a library with `src` directory (that allows recursive build)
RecursiveLayout
)
Expand Down
2 changes: 1 addition & 1 deletion arduino/libraries/libraries_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type LibraryLocation int
// The enumeration is listed in ascending order of priority
const (
// IDEBuiltIn are libraries bundled in the IDE
IDEBuiltIn = 1 << iota
IDEBuiltIn = iota
// PlatformBuiltIn are libraries bundled in a PlatformRelease
PlatformBuiltIn
// ReferencedPlatformBuiltIn are libraries bundled in a PlatformRelease referenced for build
Expand Down
77 changes: 68 additions & 9 deletions cli/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
package lib

import (
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"fmt"
"os"

"github.com/arduino/arduino-cli/cli"
"github.com/arduino/arduino-cli/commands/lib"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/rpc"
"github.com/gosuri/uitable"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

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

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

var lm *librariesmanager.LibrariesManager
if listFlags.all {
_, lm = cli.InitPackageAndLibraryManager()
res, err := lib.LibraryList(context.Background(), &rpc.LibraryListReq{
Instance: instance,
All: listFlags.all,
Updatable: listFlags.updatable,
})
if err != nil {
formatter.PrintError(err, "Error listing Libraries")
os.Exit(cli.ErrGeneric)
}
if len(res.GetInstalledLibrary()) > 0 {
results := res.GetInstalledLibrary()
if cli.OutputJSONOrElse(results) {
if len(results) > 0 {
fmt.Println(outputListLibrary(results))
} else {
formatter.Print("Error listing Libraries")
}
}
}
logrus.Info("Done")
}

func outputListLibrary(il []*rpc.InstalledLibrary) string {
table := uitable.New()
table.MaxColWidth = 100
table.Wrap = true

hasUpdates := false
for _, libMeta := range il {
if libMeta.GetRelease() != nil {
hasUpdates = true
}
}

if hasUpdates {
table.AddRow("Name", "Installed", "Available", "Location")
} else {
lm = cli.InitLibraryManager(cli.Config)
table.AddRow("Name", "Installed", "Location")
}

res := lib.ListLibraries(lm, listFlags.updatable)
if len(res.Libraries) > 0 {
formatter.Print(res)
lastName := ""
for _, libMeta := range il {
lib := libMeta.GetLibrary()
name := lib.Name
if name == lastName {
name = ` "`
} else {
lastName = name
}

location := lib.GetLocation()
if lib.ContainerPlatform != "" {
location = lib.GetContainerPlatform()
}
if hasUpdates {
var available string
if libMeta.GetRelease() != nil {
available = libMeta.GetRelease().GetVersion()
}
table.AddRow(name, lib.Version, available, location)
} else {
table.AddRow(name, lib.Version, location)
}
}
logrus.Info("Done")
return fmt.Sprintln(table)
}
104 changes: 100 additions & 4 deletions commands/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,125 @@
package lib

import (
"context"

"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
"github.com/arduino/arduino-cli/common/formatter/output"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/rpc"
)

type InstalledLib struct {
Library *libraries.Library `json:"library"`
Available *librariesindex.Release `omitempy,json:"available"`
}

func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryListResp, error) {

lm := commands.GetLibraryManager(req)
instaledLib := []*rpc.InstalledLibrary{}
res := ListLibraries(lm, req.GetUpdatable(), req.GetAll())
if len(res) > 0 {
for _, lib := range res {
libtmp := GetOutputLibrary(lib.Library)
release := GetOutputRelease(lib.Available)
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
Library: libtmp,
Release: release,
})
}

return &rpc.LibraryListResp{InstalledLibrary: instaledLib}, nil
}
return &rpc.LibraryListResp{}, nil
}

// ListLibraries returns the list of installed libraries. If updatable is true it
// returns only the libraries that may be updated.
func ListLibraries(lm *librariesmanager.LibrariesManager, updatable bool) *output.InstalledLibraries {
res := &output.InstalledLibraries{}
func ListLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bool) []*InstalledLib {

res := []*InstalledLib{}
for _, libAlternatives := range lm.Libraries {
for _, lib := range libAlternatives.Alternatives {
if !all {
if lib.Location != libraries.Sketchbook {
continue
}
}
var available *librariesindex.Release
if updatable {
available = lm.Index.FindLibraryUpdate(lib)
if available == nil {
continue
}
}
res.Libraries = append(res.Libraries, &output.InstalledLibary{
res = append(res, &InstalledLib{
Library: lib,
Available: available,
})
}
}
return res
}

func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
insdir := ""
if lib.InstallDir != nil {
insdir = lib.InstallDir.String()
}
srcdir := ""
if lib.SourceDir != nil {
srcdir = lib.SourceDir.String()
}
utldir := ""
if lib.UtilityDir != nil {
utldir = lib.UtilityDir.String()
}
cntplat := ""
if lib.ContainerPlatform != nil {
cntplat = lib.ContainerPlatform.String()
}

return &rpc.Library{
Name: lib.Name,
Author: lib.Author,
Maintainer: lib.Maintainer,
Sentence: lib.Sentence,
Paragraph: lib.Paragraph,
Website: lib.Website,
Category: lib.Category,
Architectures: lib.Architectures,
Types: lib.Types,
InstallDir: insdir,
SourceDir: srcdir,
UtilityDir: utldir,
Location: lib.Location.String(),
ContainerPlatform: cntplat,
Layout: lib.Layout.String(),
RealName: lib.RealName,
DotALinkage: lib.DotALinkage,
Precompiled: lib.Precompiled,
LDflags: lib.LDflags,
IsLegacy: lib.IsLegacy,
Version: lib.Version.String(),
License: lib.LDflags,
}
}

func GetOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease { //
if lib != nil {
return &rpc.LibraryRelease{
Author: lib.Author,
Version: lib.Version.String(),
Maintainer: lib.Maintainer,
Sentence: lib.Sentence,
Paragraph: lib.Paragraph,
Website: lib.Website,
Category: lib.Category,
Architectures: lib.Architectures,
Types: lib.Types,
}
}
return &rpc.LibraryRelease{}
}
6 changes: 3 additions & 3 deletions commands/lib/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func LibraryUpgradeAll(ctx context.Context, req *rpc.LibraryUpgradeAllReq, downl
lm := commands.GetLibraryManager(req)

// Obtain the list of upgradable libraries
list := ListLibraries(lm, true)
list := ListLibraries(lm, true, true)

for _, upgradeDesc := range list.Libraries {
for _, upgradeDesc := range list {
if err := downloadLibrary(lm, upgradeDesc.Available, downloadCB, taskCB); err != nil {
return err
}
}
for _, upgradeDesc := range list.Libraries {
for _, upgradeDesc := range list {
installLibrary(lm, upgradeDesc.Available, taskCB)
}

Expand Down
69 changes: 1 addition & 68 deletions common/formatter/output/lib_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

package output

import (
"fmt"
"strings"

"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
)
import "fmt"

// VersionResult represents the output of the version commands.
type VersionResult struct {
Expand All @@ -33,65 +28,3 @@ type VersionResult struct {
func (vr VersionResult) String() string {
return fmt.Sprintf("%s version %s", vr.CommandName, vr.Version)
}

// LibProcessResults represent the result of a process on libraries.
type LibProcessResults struct {
Libraries map[string]ProcessResult `json:"libraries,required"`
}

// CoreProcessResults represent the result of a process on cores or tools.
type CoreProcessResults struct {
Cores map[string]ProcessResult `json:"cores,omitempty"`
Tools map[string]ProcessResult `json:"tools,omitempty"`
}

// String returns a string representation of the object.
func (cpr CoreProcessResults) String() string {
ret := ""
for _, cr := range cpr.Cores {
ret += fmt.Sprintln(cr)
}
for _, tr := range cpr.Tools {
ret += fmt.Sprintln(tr)
}
return ret
}

// LibSearchResults represents a set of results of a search of libraries.
type LibSearchResults struct {
Libraries []*librariesindex.Library `json:"libraries,required"`
}

// String returns a string representation of the object.
func (lpr LibProcessResults) String() string {
ret := ""
for _, lr := range lpr.Libraries {
ret += fmt.Sprintln(lr)
}
return strings.TrimSpace(ret)
}

// String returns a string representation of the object.
func (lsr LibSearchResults) String() string {
ret := ""
for _, l := range lsr.Libraries {
ret += fmt.Sprintf("Name: \"%s\"\n", l.Name) +
fmt.Sprintln(" Author: ", l.Latest.Author) +
fmt.Sprintln(" Maintainer: ", l.Latest.Maintainer) +
fmt.Sprintln(" Sentence: ", l.Latest.Sentence) +
fmt.Sprintln(" Paragraph: ", l.Latest.Paragraph) +
fmt.Sprintln(" Website: ", l.Latest.Website) +
fmt.Sprintln(" Category: ", l.Latest.Category) +
fmt.Sprintln(" Architecture: ", strings.Join(l.Latest.Architectures, ", ")) +
fmt.Sprintln(" Types: ", strings.Join(l.Latest.Types, ", ")) +
fmt.Sprintln(" Versions: ", strings.Replace(fmt.Sprint(l.Versions()), " ", ", ", -1))
}
return strings.TrimSpace(ret)
}

// Results returns a set of generic results, to allow them to be modified externally.
//
// -> ProcessResults interface.
func (lpr LibProcessResults) Results() map[string]ProcessResult {
return lpr.Libraries
}
Loading

0 comments on commit af3eae7

Please sign in to comment.