@@ -30,8 +30,27 @@ import (
30
30
"github.com/sirupsen/logrus"
31
31
)
32
32
33
+ // LibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryInstall that sends
34
+ // all responses to the callback function.
35
+ func LibraryInstallStreamResponseToCallbackFunction (ctx context.Context , downloadCB rpc.DownloadProgressCB , taskCB rpc.TaskProgressCB ) rpc.ArduinoCoreService_LibraryInstallServer {
36
+ return streamResponseToCallback (ctx , func (r * rpc.LibraryInstallResponse ) error {
37
+ if r .GetProgress () != nil {
38
+ downloadCB (r .GetProgress ())
39
+ }
40
+ if r .GetTaskProgress () != nil {
41
+ taskCB (r .GetTaskProgress ())
42
+ }
43
+ return nil
44
+ })
45
+ }
46
+
33
47
// LibraryInstall resolves the library dependencies, then downloads and installs the libraries into the install location.
34
- func LibraryInstall (ctx context.Context , srv rpc.ArduinoCoreServiceServer , req * rpc.LibraryInstallRequest , downloadCB rpc.DownloadProgressCB , taskCB rpc.TaskProgressCB ) error {
48
+ func (s * arduinoCoreServerImpl ) LibraryInstall (req * rpc.LibraryInstallRequest , stream rpc.ArduinoCoreService_LibraryInstallServer ) error {
49
+ ctx := stream .Context ()
50
+ syncSend := NewSynchronizedSend (stream .Send )
51
+ downloadCB := func (p * rpc.DownloadProgress ) { syncSend .Send (& rpc.LibraryInstallResponse {Progress : p }) }
52
+ taskCB := func (p * rpc.TaskProgress ) { syncSend .Send (& rpc.LibraryInstallResponse {TaskProgress : p }) }
53
+
35
54
// Obtain the library index from the manager
36
55
li , err := instances .GetLibrariesIndex (req .GetInstance ())
37
56
if err != nil {
@@ -128,16 +147,18 @@ func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *
128
147
downloadReason += "-builtin"
129
148
}
130
149
}
131
- if err := downloadLibrary (downloadsDir , libRelease , downloadCB , taskCB , downloadReason ); err != nil {
150
+ if err := downloadLibrary (ctx , downloadsDir , libRelease , downloadCB , taskCB , downloadReason ); err != nil {
132
151
return err
133
152
}
134
153
if err := installLibrary (lmi , downloadsDir , libRelease , installTask , taskCB ); err != nil {
135
154
return err
136
155
}
137
156
}
138
157
139
- stream := InitStreamResponseToCallbackFunction (ctx , nil )
140
- if err := srv .Init (& rpc.InitRequest {Instance : req .GetInstance ()}, stream ); err != nil {
158
+ err = s .Init (
159
+ & rpc.InitRequest {Instance : req .GetInstance ()},
160
+ InitStreamResponseToCallbackFunction (ctx , nil ))
161
+ if err != nil {
141
162
return err
142
163
}
143
164
@@ -166,8 +187,23 @@ func installLibrary(lmi *librariesmanager.Installer, downloadsDir *paths.Path, l
166
187
return nil
167
188
}
168
189
190
+ // ZipLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in ZipLibraryInstall that sends
191
+ // all responses to the callback function.
192
+ func ZipLibraryInstallStreamResponseToCallbackFunction (ctx context.Context , taskCB rpc.TaskProgressCB ) rpc.ArduinoCoreService_ZipLibraryInstallServer {
193
+ return streamResponseToCallback (ctx , func (r * rpc.ZipLibraryInstallResponse ) error {
194
+ if r .GetTaskProgress () != nil {
195
+ taskCB (r .GetTaskProgress ())
196
+ }
197
+ return nil
198
+ })
199
+ }
200
+
169
201
// ZipLibraryInstall FIXMEDOC
170
- func ZipLibraryInstall (ctx context.Context , req * rpc.ZipLibraryInstallRequest , taskCB rpc.TaskProgressCB ) error {
202
+ func (s * arduinoCoreServerImpl ) ZipLibraryInstall (req * rpc.ZipLibraryInstallRequest , stream rpc.ArduinoCoreService_ZipLibraryInstallServer ) error {
203
+ ctx := stream .Context ()
204
+ syncSend := NewSynchronizedSend (stream .Send )
205
+ taskCB := func (p * rpc.TaskProgress ) { syncSend .Send (& rpc.ZipLibraryInstallResponse {TaskProgress : p }) }
206
+
171
207
lm , err := instances .GetLibraryManager (req .GetInstance ())
172
208
if err != nil {
173
209
return err
@@ -181,14 +217,30 @@ func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, t
181
217
return nil
182
218
}
183
219
220
+ // GitLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in GitLibraryInstall that sends
221
+ // all responses to the callback function.
222
+ func GitLibraryInstallStreamResponseToCallbackFunction (ctx context.Context , taskCB rpc.TaskProgressCB ) rpc.ArduinoCoreService_GitLibraryInstallServer {
223
+ return streamResponseToCallback (ctx , func (r * rpc.GitLibraryInstallResponse ) error {
224
+ if r .GetTaskProgress () != nil {
225
+ taskCB (r .GetTaskProgress ())
226
+ }
227
+ return nil
228
+ })
229
+ }
230
+
184
231
// GitLibraryInstall FIXMEDOC
185
- func GitLibraryInstall (ctx context.Context , req * rpc.GitLibraryInstallRequest , taskCB rpc.TaskProgressCB ) error {
232
+ func (s * arduinoCoreServerImpl ) GitLibraryInstall (req * rpc.GitLibraryInstallRequest , stream rpc.ArduinoCoreService_GitLibraryInstallServer ) error {
233
+ syncSend := NewSynchronizedSend (stream .Send )
234
+ taskCB := func (p * rpc.TaskProgress ) { syncSend .Send (& rpc.GitLibraryInstallResponse {TaskProgress : p }) }
186
235
lm , err := instances .GetLibraryManager (req .GetInstance ())
187
236
if err != nil {
188
237
return err
189
238
}
190
239
lmi , release := lm .NewInstaller ()
191
240
defer release ()
241
+
242
+ // TODO: pass context
243
+ // ctx := stream.Context()
192
244
if err := lmi .InstallGitLib (req .GetUrl (), req .GetOverwrite ()); err != nil {
193
245
return & cmderrors.FailedLibraryInstallError {Cause : err }
194
246
}
0 commit comments