Skip to content

Commit 6f0d6d8

Browse files
committed
Factored out methods to convert Libraries into their rpc counterpart
1 parent 05adc5c commit 6f0d6d8

File tree

3 files changed

+64
-71
lines changed

3 files changed

+64
-71
lines changed

arduino/libraries/libraries.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020

2121
"github.com/arduino/arduino-cli/arduino/cores"
22+
rpc "github.com/arduino/arduino-cli/rpc/commands"
2223
paths "github.com/arduino/go-paths-helper"
2324
properties "github.com/arduino/go-properties-orderedmap"
2425
semver "go.bug.st/relaxed-semver"
@@ -85,6 +86,49 @@ func (library *Library) String() string {
8586
return library.Name + "@" + library.Version.String()
8687
}
8788

89+
// ToRPCLibrary converts this library into an rpc.Library
90+
func (library *Library) ToRPCLibrary() *rpc.Library {
91+
pathOrEmpty := func(p *paths.Path) string {
92+
if p == nil {
93+
return ""
94+
}
95+
return p.String()
96+
}
97+
platformOrEmpty := func(p *cores.PlatformRelease) string {
98+
if p == nil {
99+
return ""
100+
}
101+
return p.String()
102+
}
103+
return &rpc.Library{
104+
Name: library.Name,
105+
Author: library.Author,
106+
Maintainer: library.Maintainer,
107+
Sentence: library.Sentence,
108+
Paragraph: library.Paragraph,
109+
Website: library.Website,
110+
Category: library.Category,
111+
Architectures: library.Architectures,
112+
Types: library.Types,
113+
InstallDir: pathOrEmpty(library.InstallDir),
114+
SourceDir: pathOrEmpty(library.SourceDir),
115+
UtilityDir: pathOrEmpty(library.UtilityDir),
116+
Location: library.Location.ToRPCLibraryLocation(),
117+
ContainerPlatform: platformOrEmpty(library.ContainerPlatform),
118+
Layout: library.Layout.ToRPCLibraryLayout(),
119+
RealName: library.RealName,
120+
DotALinkage: library.DotALinkage,
121+
Precompiled: library.Precompiled,
122+
LdFlags: library.LDflags,
123+
IsLegacy: library.IsLegacy,
124+
Version: library.Version.String(),
125+
License: library.License,
126+
Examples: library.Examples.AsStrings(),
127+
ProvidesIncludes: library.DeclaredHeaders(),
128+
CompatibleWith: library.CompatibleWith,
129+
}
130+
}
131+
88132
// SupportsAnyArchitectureIn returns true if any of the following is true:
89133
// - the library supports at least one of the given architectures
90134
// - the library is architecture independent

arduino/libraries/librariesindex/index.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/arduino/arduino-cli/arduino/libraries"
2222
"github.com/arduino/arduino-cli/arduino/resources"
23+
rpc "github.com/arduino/arduino-cli/rpc/commands"
2324
semver "go.bug.st/relaxed-semver"
2425
)
2526

@@ -58,6 +59,21 @@ type Release struct {
5859
Library *Library `json:"-"`
5960
}
6061

62+
// ToRPCLibraryRelease transform this Release into a rpc.LibraryRelease
63+
func (r *Release) ToRPCLibraryRelease() *rpc.LibraryRelease {
64+
return &rpc.LibraryRelease{
65+
Author: r.Author,
66+
Version: r.Version.String(),
67+
Maintainer: r.Maintainer,
68+
Sentence: r.Sentence,
69+
Paragraph: r.Paragraph,
70+
Website: r.Website,
71+
Category: r.Category,
72+
Architectures: r.Architectures,
73+
Types: r.Types,
74+
}
75+
}
76+
6177
// GetName returns the name of this library.
6278
func (r *Release) GetName() string {
6379
return r.Library.Name

commands/lib/list.go

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,12 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
9999
if nameFilter != "" && strings.ToLower(lib.Library.Name) != nameFilter {
100100
continue
101101
}
102-
libtmp, err := GetOutputLibrary(lib.Library)
103-
if err != nil {
104-
return nil, err
102+
var release *rpc.LibraryRelease
103+
if lib.Available != nil {
104+
release = lib.Available.ToRPCLibraryRelease()
105105
}
106-
release := GetOutputRelease(lib.Available)
107106
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
108-
Library: libtmp,
107+
Library: lib.Library.ToRPCLibrary(),
109108
Release: release,
110109
})
111110
}
@@ -136,69 +135,3 @@ func listLibraries(lm *librariesmanager.LibrariesManager, updatable bool, all bo
136135
}
137136
return res
138137
}
139-
140-
// GetOutputLibrary FIXMEDOC
141-
func GetOutputLibrary(lib *libraries.Library) (*rpc.Library, error) {
142-
insdir := ""
143-
if lib.InstallDir != nil {
144-
insdir = lib.InstallDir.String()
145-
}
146-
srcdir := ""
147-
if lib.SourceDir != nil {
148-
srcdir = lib.SourceDir.String()
149-
}
150-
utldir := ""
151-
if lib.UtilityDir != nil {
152-
utldir = lib.UtilityDir.String()
153-
}
154-
cntplat := ""
155-
if lib.ContainerPlatform != nil {
156-
cntplat = lib.ContainerPlatform.String()
157-
}
158-
159-
return &rpc.Library{
160-
Name: lib.Name,
161-
Author: lib.Author,
162-
Maintainer: lib.Maintainer,
163-
Sentence: lib.Sentence,
164-
Paragraph: lib.Paragraph,
165-
Website: lib.Website,
166-
Category: lib.Category,
167-
Architectures: lib.Architectures,
168-
Types: lib.Types,
169-
InstallDir: insdir,
170-
SourceDir: srcdir,
171-
UtilityDir: utldir,
172-
Location: lib.Location.ToRPCLibraryLocation(),
173-
ContainerPlatform: cntplat,
174-
Layout: lib.Layout.ToRPCLibraryLayout(),
175-
RealName: lib.RealName,
176-
DotALinkage: lib.DotALinkage,
177-
Precompiled: lib.Precompiled,
178-
LdFlags: lib.LDflags,
179-
IsLegacy: lib.IsLegacy,
180-
Version: lib.Version.String(),
181-
License: lib.License,
182-
Examples: lib.Examples.AsStrings(),
183-
ProvidesIncludes: lib.DeclaredHeaders(),
184-
CompatibleWith: lib.CompatibleWith,
185-
}, nil
186-
}
187-
188-
// GetOutputRelease FIXMEDOC
189-
func GetOutputRelease(lib *librariesindex.Release) *rpc.LibraryRelease { //
190-
if lib != nil {
191-
return &rpc.LibraryRelease{
192-
Author: lib.Author,
193-
Version: lib.Version.String(),
194-
Maintainer: lib.Maintainer,
195-
Sentence: lib.Sentence,
196-
Paragraph: lib.Paragraph,
197-
Website: lib.Website,
198-
Category: lib.Category,
199-
Architectures: lib.Architectures,
200-
Types: lib.Types,
201-
}
202-
}
203-
return &rpc.LibraryRelease{}
204-
}

0 commit comments

Comments
 (0)