@@ -20,9 +20,11 @@ import (
20
20
21
21
"github.com/arduino/arduino-cli/commands/cmderrors"
22
22
"github.com/arduino/arduino-cli/commands/internal/instances"
23
+ "github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
23
24
"github.com/arduino/arduino-cli/internal/arduino/sketch"
24
25
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
25
26
paths "github.com/arduino/go-paths-helper"
27
+ "go.bug.st/f"
26
28
)
27
29
28
30
// 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
48
50
return nil , err
49
51
}
50
52
51
- var addedLib * sketch.ProfileLibraryReference
53
+ var addedLibs []* sketch.ProfileLibraryReference
54
+ var skippedLibs []* sketch.ProfileLibraryReference
52
55
if reqLocalLib := req .GetLibrary ().GetLocalLibrary (); reqLocalLib != nil {
53
56
// Add a local library
54
57
path := paths .New (reqLocalLib .GetPath ())
55
58
if path == nil {
56
59
return nil , & cmderrors.InvalidArgumentError {Message : "invalid library path" }
57
60
}
58
- addedLib = & sketch.ProfileLibraryReference {InstallDir : path }
61
+ addedLib : = & sketch.ProfileLibraryReference {InstallDir : path }
59
62
profile .Libraries = append (profile .Libraries , addedLib )
63
+ addedLibs = append (addedLibs , addedLib )
60
64
} else if reqIndexLib := req .GetLibrary ().GetIndexLibrary (); reqIndexLib != nil {
61
65
// Obtain the library index from the manager
62
66
li , err := instances .GetLibrariesIndex (req .GetInstance ())
@@ -71,16 +75,42 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
71
75
if err != nil {
72
76
return nil , err
73
77
}
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 )
82
113
}
83
- profile .Libraries = append (profile .Libraries , addedLib )
84
114
}
85
115
} else {
86
116
return nil , & cmderrors.InvalidArgumentError {Message : "library must be specified" }
@@ -91,5 +121,9 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
91
121
return nil , err
92
122
}
93
123
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
95
129
}
0 commit comments