@@ -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,43 @@ 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+ if req .GetAddDependencies () {
100+ lme , release , err := instances .GetLibraryManagerExplorer (req .GetInstance ())
101+ if err != nil {
102+ return nil , err
103+ }
104+ libWithDeps , err := libraryResolveDependencies (lme , li , libRelease .GetName (), libRelease .GetVersion ().String (), false )
105+ // deps contains the main library as well, so we skip it when adding dependencies
106+ release ()
107+ if err != nil {
108+ return nil , err
82109 }
83- profile .Libraries = append (profile .Libraries , addedLib )
110+ for _ , lib := range libWithDeps {
111+ add (lib )
112+ }
113+ } else {
114+ add (libRelease )
84115 }
85116 } else {
86117 return nil , & cmderrors.InvalidArgumentError {Message : "library must be specified" }
@@ -91,5 +122,9 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
91122 return nil , err
92123 }
93124
94- return & rpc.ProfileLibAddResponse {Library : addedLib .ToRpc (), ProfileName : profileName }, nil
125+ return & rpc.ProfileLibAddResponse {
126+ AddedLibraries : f .Map (addedLibs , (* sketch .ProfileLibraryReference ).ToRpc ),
127+ SkippedLibraries : f .Map (skippedLibs , (* sketch .ProfileLibraryReference ).ToRpc ),
128+ ProfileName : profileName ,
129+ }, nil
95130}
0 commit comments