Skip to content

Commit ae5febf

Browse files
committed
Added profile lib-deps install
1 parent 681d333 commit ae5febf

File tree

4 files changed

+739
-657
lines changed

4 files changed

+739
-657
lines changed

commands/service_profile_lib_add.go

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020

2121
"github.com/arduino/arduino-cli/commands/cmderrors"
2222
"github.com/arduino/arduino-cli/commands/internal/instances"
23+
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
2324
"github.com/arduino/arduino-cli/internal/arduino/sketch"
2425
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2526
paths "github.com/arduino/go-paths-helper"
27+
"go.bug.st/f"
2628
)
2729

2830
// ProfileLibAdd adds a library to the specified profile or to the default profile.
@@ -48,15 +50,17 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
4850
return nil, err
4951
}
5052

51-
var addedLib *sketch.ProfileLibraryReference
53+
var addedLibs []*sketch.ProfileLibraryReference
54+
var skippedLibs []*sketch.ProfileLibraryReference
5255
if reqLocalLib := req.GetLibrary().GetLocalLibrary(); reqLocalLib != nil {
5356
// Add a local library
5457
path := paths.New(reqLocalLib.GetPath())
5558
if path == nil {
5659
return nil, &cmderrors.InvalidArgumentError{Message: "invalid library path"}
5760
}
58-
addedLib = &sketch.ProfileLibraryReference{InstallDir: path}
61+
addedLib := &sketch.ProfileLibraryReference{InstallDir: path}
5962
profile.Libraries = append(profile.Libraries, addedLib)
63+
addedLibs = append(addedLibs, addedLib)
6064
} else if reqIndexLib := req.GetLibrary().GetIndexLibrary(); reqIndexLib != nil {
6165
// Obtain the library index from the manager
6266
li, err := instances.GetLibrariesIndex(req.GetInstance())
@@ -71,16 +75,42 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
7175
if err != nil {
7276
return nil, err
7377
}
74-
// If the library has been already added to the profile, just update the version
75-
if lib, _ := profile.GetLibrary(reqIndexLib.GetName()); lib != nil {
76-
lib.Version = libRelease.GetVersion()
77-
addedLib = lib
78-
} else {
79-
addedLib = &sketch.ProfileLibraryReference{
80-
Library: reqIndexLib.GetName(),
81-
Version: libRelease.GetVersion(),
78+
79+
add := func(libReleaseToAdd *librariesindex.Release) {
80+
libRefToAdd := &sketch.ProfileLibraryReference{
81+
Library: libReleaseToAdd.GetName(),
82+
Version: libReleaseToAdd.GetVersion(),
83+
}
84+
existingLibRef, _ := profile.GetLibrary(libReleaseToAdd.GetName())
85+
if existingLibRef == nil {
86+
profile.Libraries = append(profile.Libraries, libRefToAdd)
87+
addedLibs = append(addedLibs, libRefToAdd)
88+
return
89+
}
90+
// If the library has been already added to the profile, just update the version
91+
if req.GetNoOverwrite() {
92+
skippedLibs = append(skippedLibs, libRefToAdd)
93+
return
94+
}
95+
existingLibRef.Version = libReleaseToAdd.GetVersion()
96+
addedLibs = append(addedLibs, existingLibRef)
97+
}
98+
99+
add(libRelease)
100+
101+
if req.GetAddDependencies() {
102+
lme, release, err := instances.GetLibraryManagerExplorer(req.GetInstance())
103+
if err != nil {
104+
return nil, err
105+
}
106+
deps, err := libraryResolveDependencies(lme, li, libRelease.GetName(), libRelease.GetVersion().String(), false)
107+
release()
108+
if err != nil {
109+
return nil, err
110+
}
111+
for _, d := range deps {
112+
add(d)
82113
}
83-
profile.Libraries = append(profile.Libraries, addedLib)
84114
}
85115
} else {
86116
return nil, &cmderrors.InvalidArgumentError{Message: "library must be specified"}
@@ -91,5 +121,9 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
91121
return nil, err
92122
}
93123

94-
return &rpc.ProfileLibAddResponse{Library: addedLib.ToRpc(), ProfileName: profileName}, nil
124+
return &rpc.ProfileLibAddResponse{
125+
AddedLibraries: f.Map(addedLibs, (*sketch.ProfileLibraryReference).ToRpc),
126+
SkippedLibraries: f.Map(skippedLibs, (*sketch.ProfileLibraryReference).ToRpc),
127+
ProfileName: profileName,
128+
}, nil
95129
}

internal/cli/profile/lib.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/arduino/arduino-cli/internal/i18n"
2929
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3030
"github.com/spf13/cobra"
31+
"go.bug.st/f"
3132
)
3233

3334
func initLibCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
@@ -96,9 +97,12 @@ func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreSer
9697
if err != nil {
9798
feedback.Fatal(i18n.Tr("Error adding %s to the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric)
9899
}
100+
added := f.Map(resp.GetAddedLibraries(), func(l *rpc.ProfileLibraryReference) *result.ProfileLibraryReference_IndexLibraryResult {
101+
return result.NewProfileLibraryReference_IndexLibraryResult(l.GetIndexLibrary())
102+
})
99103
feedback.PrintResult(libAddResult{
100-
Library: result.NewProfileLibraryReference_IndexLibraryResult(resp.GetLibrary().GetIndexLibrary()),
101-
ProfileName: resp.ProfileName})
104+
AddedLibraries: added,
105+
ProfileName: resp.ProfileName})
102106
}
103107
}
104108

@@ -158,16 +162,24 @@ func runLibRemoveCommand(ctx context.Context, args []string, srv rpc.ArduinoCore
158162
}
159163

160164
type libAddResult struct {
161-
Library *result.ProfileLibraryReference_IndexLibraryResult `json:"library"`
162-
ProfileName string `json:"profile_name"`
165+
AddedLibraries []*result.ProfileLibraryReference_IndexLibraryResult `json:"added_libraries"`
166+
SkippedLibraries []*result.ProfileLibraryReference_IndexLibraryResult `json:"skipped_libraries"`
167+
ProfileName string `json:"profile_name"`
163168
}
164169

165170
func (lr libAddResult) Data() interface{} {
166171
return lr
167172
}
168173

169174
func (lr libAddResult) String() string {
170-
return i18n.Tr("Profile %s: %s@%s added successfully", lr.ProfileName, lr.Library.Name, lr.Library.Version)
175+
res := ""
176+
if len(lr.AddedLibraries) > 0 {
177+
res += fmt.Sprintln(i18n.Tr("The following libraries were added to the profile %s:", lr.ProfileName))
178+
for _, l := range lr.AddedLibraries {
179+
res += fmt.Sprintf(" - %s@%s\n", l.Name, l.Version)
180+
}
181+
}
182+
return res
171183
}
172184

173185
type libRemoveResult struct {

0 commit comments

Comments
 (0)