@@ -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,41 @@ 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
+ }
89
+ // If the library has been already added to the profile, just update the version
90
+ if req .GetNoOverwrite () {
91
+ skippedLibs = append (skippedLibs , libRefToAdd )
92
+ return
93
+ }
94
+ existingLibRef .Version = libReleaseToAdd .GetVersion ()
95
+ addedLibs = append (addedLibs , existingLibRef )
96
+ }
97
+
98
+ add (libRelease )
99
+
100
+ if req .GetAddDependencies () {
101
+ lme , release , err := instances .GetLibraryManagerExplorer (req .GetInstance ())
102
+ if err != nil {
103
+ return nil , err
104
+ }
105
+ deps , err := libraryResolveDependencies (lme , li , libRelease .GetName (), libRelease .GetVersion ().String (), false )
106
+ release ()
107
+ if err != nil {
108
+ return nil , err
109
+ }
110
+ for _ , d := range deps {
111
+ add (d )
82
112
}
83
- profile .Libraries = append (profile .Libraries , addedLib )
84
113
}
85
114
} else {
86
115
return nil , & cmderrors.InvalidArgumentError {Message : "library must be specified" }
@@ -91,5 +120,9 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
91
120
return nil , err
92
121
}
93
122
94
- return & rpc.ProfileLibAddResponse {Library : addedLib .ToRpc (), ProfileName : profileName }, nil
123
+ return & rpc.ProfileLibAddResponse {
124
+ AddedLibraries : f .Map (addedLibs , (* sketch .ProfileLibraryReference ).ToRpc ),
125
+ SkippedLibraries : f .Map (skippedLibs , (* sketch .ProfileLibraryReference ).ToRpc ),
126
+ ProfileName : profileName ,
127
+ }, nil
95
128
}
0 commit comments