@@ -166,172 +166,170 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, *status.Status) {
166
166
// Failures don't stop the loading process, in case of loading failure the Platform or library
167
167
// is simply skipped and an error gRPC status is sent in the returned channel.
168
168
// Channel is closed when loading is finished.
169
- func Init (req * rpc.InitRequest ) (<- chan * rpc.InitResponse , * status.Status ) {
169
+ func Init (req * rpc.InitRequest , resp func (r * rpc.InitResponse )) * status.Status {
170
+ if resp == nil {
171
+ resp = func (r * rpc.InitResponse ) {}
172
+ }
170
173
instance := instances [req .Instance .Id ]
171
174
if instance == nil {
172
- return nil , status .Newf (codes .InvalidArgument , "Invalid instance ID" )
175
+ return status .Newf (codes .InvalidArgument , "Invalid instance ID" )
173
176
}
174
177
175
- outChan := make (chan * rpc.InitResponse )
176
- go func () {
177
- defer close (outChan )
178
-
179
- // Load Platforms
180
- urls := []string {globals .DefaultIndexURL }
181
- urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
182
- for _ , u := range urls {
183
- URL , err := utils .URLParse (u )
184
- if err != nil {
185
- s := status .Newf (codes .InvalidArgument , "Invalid additional URL: %v" , err )
186
- outChan <- & rpc.InitResponse {
187
- Message : & rpc.InitResponse_Error {
188
- Error : s .Proto (),
189
- },
190
- }
191
- continue
192
- }
193
-
194
- if URL .Scheme == "file" {
195
- indexFile := paths .New (URL .Path )
178
+ // Load Platforms
179
+ urls := []string {globals .DefaultIndexURL }
180
+ urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
181
+ for _ , u := range urls {
182
+ URL , err := utils .URLParse (u )
183
+ if err != nil {
184
+ s := status .Newf (codes .InvalidArgument , "Invalid additional URL: %v" , err )
185
+ resp (& rpc.InitResponse {
186
+ Message : & rpc.InitResponse_Error {
187
+ Error : s .Proto (),
188
+ },
189
+ })
190
+ continue
191
+ }
196
192
197
- _ , err := instance .PackageManager .LoadPackageIndexFromFile (indexFile )
198
- if err != nil {
199
- s := status .Newf (codes .FailedPrecondition , "Loading index file: %v" , err )
200
- outChan <- & rpc.InitResponse {
201
- Message : & rpc.InitResponse_Error {
202
- Error : s .Proto (),
203
- },
204
- }
205
- }
206
- continue
207
- }
193
+ if URL .Scheme == "file" {
194
+ indexFile := paths .New (URL .Path )
208
195
209
- if err := instance .PackageManager .LoadPackageIndex (URL ); err != nil {
196
+ _ , err := instance .PackageManager .LoadPackageIndexFromFile (indexFile )
197
+ if err != nil {
210
198
s := status .Newf (codes .FailedPrecondition , "Loading index file: %v" , err )
211
- outChan <- & rpc.InitResponse {
199
+ resp ( & rpc.InitResponse {
212
200
Message : & rpc.InitResponse_Error {
213
201
Error : s .Proto (),
214
202
},
215
- }
203
+ })
216
204
}
205
+ continue
217
206
}
218
207
219
- // We load hardware before verifying builtin tools are installed
220
- // otherwise we wouldn't find them and reinstall them each time
221
- // and they would never get reloaded.
222
- for _ , err := range instance .PackageManager .LoadHardware () {
223
- outChan <- & rpc.InitResponse {
208
+ if err := instance .PackageManager .LoadPackageIndex (URL ); err != nil {
209
+ s := status .Newf (codes .FailedPrecondition , "Loading index file: %v" , err )
210
+ resp (& rpc.InitResponse {
224
211
Message : & rpc.InitResponse_Error {
225
- Error : err .Proto (),
212
+ Error : s .Proto (),
226
213
},
227
- }
214
+ })
228
215
}
216
+ }
229
217
230
- taskCallback := func (msg * rpc.TaskProgress ) {
231
- outChan <- & rpc.InitResponse {
232
- Message : & rpc.InitResponse_InitProgress {
233
- InitProgress : & rpc.InitResponse_Progress {
234
- TaskProgress : msg ,
235
- },
218
+ // We load hardware before verifying builtin tools are installed
219
+ // otherwise we wouldn't find them and reinstall them each time
220
+ // and they would never get reloaded.
221
+ for _ , err := range instance .PackageManager .LoadHardware () {
222
+ resp (& rpc.InitResponse {
223
+ Message : & rpc.InitResponse_Error {
224
+ Error : err .Proto (),
225
+ },
226
+ })
227
+ }
228
+
229
+ taskCallback := func (msg * rpc.TaskProgress ) {
230
+ resp (& rpc.InitResponse {
231
+ Message : & rpc.InitResponse_InitProgress {
232
+ InitProgress : & rpc.InitResponse_Progress {
233
+ TaskProgress : msg ,
236
234
},
237
- }
238
- }
235
+ },
236
+ })
237
+ }
239
238
240
- downloadCallback := func (msg * rpc.DownloadProgress ) {
241
- outChan <- & rpc.InitResponse {
242
- Message : & rpc.InitResponse_InitProgress {
243
- InitProgress : & rpc.InitResponse_Progress {
244
- DownloadProgress : msg ,
245
- },
239
+ downloadCallback := func (msg * rpc.DownloadProgress ) {
240
+ resp (& rpc.InitResponse {
241
+ Message : & rpc.InitResponse_InitProgress {
242
+ InitProgress : & rpc.InitResponse_Progress {
243
+ DownloadProgress : msg ,
246
244
},
247
- }
248
- }
245
+ },
246
+ })
247
+ }
249
248
250
- // Install tools if necessary
251
- toolHasBeenInstalled := false
252
- ctagsTool , err := getBuiltinCtagsTool (instance .PackageManager )
249
+ // Install tools if necessary
250
+ toolHasBeenInstalled := false
251
+ ctagsTool , err := getBuiltinCtagsTool (instance .PackageManager )
252
+ if err != nil {
253
+ s := status .Newf (codes .Internal , err .Error ())
254
+ resp (& rpc.InitResponse {
255
+ Message : & rpc.InitResponse_Error {
256
+ Error : s .Proto (),
257
+ },
258
+ })
259
+ } else {
260
+ ctagsInstalled , err := instance .installToolIfMissing (ctagsTool , downloadCallback , taskCallback )
261
+ toolHasBeenInstalled = toolHasBeenInstalled || ctagsInstalled
253
262
if err != nil {
254
263
s := status .Newf (codes .Internal , err .Error ())
255
- outChan <- & rpc.InitResponse {
264
+ resp ( & rpc.InitResponse {
256
265
Message : & rpc.InitResponse_Error {
257
266
Error : s .Proto (),
258
267
},
259
- }
260
- } else {
261
- ctagsInstalled , err := instance .installToolIfMissing (ctagsTool , downloadCallback , taskCallback )
262
- toolHasBeenInstalled = toolHasBeenInstalled || ctagsInstalled
263
- if err != nil {
264
- s := status .Newf (codes .Internal , err .Error ())
265
- outChan <- & rpc.InitResponse {
266
- Message : & rpc.InitResponse_Error {
267
- Error : s .Proto (),
268
- },
269
- }
270
- }
268
+ })
271
269
}
270
+ }
272
271
273
- serialDiscoveryTool , _ := getBuiltinSerialDiscoveryTool (instance .PackageManager )
272
+ serialDiscoveryTool , _ := getBuiltinSerialDiscoveryTool (instance .PackageManager )
273
+ if err != nil {
274
+ s := status .Newf (codes .Internal , err .Error ())
275
+ resp (& rpc.InitResponse {
276
+ Message : & rpc.InitResponse_Error {
277
+ Error : s .Proto (),
278
+ },
279
+ })
280
+ } else {
281
+ serialDiscoveryToolInstalled , err := instance .installToolIfMissing (serialDiscoveryTool , downloadCallback , taskCallback )
282
+ toolHasBeenInstalled = toolHasBeenInstalled || serialDiscoveryToolInstalled
274
283
if err != nil {
275
284
s := status .Newf (codes .Internal , err .Error ())
276
- outChan <- & rpc.InitResponse {
285
+ resp ( & rpc.InitResponse {
277
286
Message : & rpc.InitResponse_Error {
278
287
Error : s .Proto (),
279
288
},
280
- }
281
- } else {
282
- serialDiscoveryToolInstalled , err := instance .installToolIfMissing (serialDiscoveryTool , downloadCallback , taskCallback )
283
- toolHasBeenInstalled = toolHasBeenInstalled || serialDiscoveryToolInstalled
284
- if err != nil {
285
- s := status .Newf (codes .Internal , err .Error ())
286
- outChan <- & rpc.InitResponse {
287
- Message : & rpc.InitResponse_Error {
288
- Error : s .Proto (),
289
- },
290
- }
291
- }
292
- }
293
-
294
- if toolHasBeenInstalled {
295
- // We installed at least one new tool after loading hardware
296
- // so we must reload again otherwise we would never found them.
297
- for _ , err := range instance .PackageManager .LoadHardware () {
298
- outChan <- & rpc.InitResponse {
299
- Message : & rpc.InitResponse_Error {
300
- Error : err .Proto (),
301
- },
302
- }
303
- }
304
- }
305
-
306
- // Load libraries
307
- for _ , pack := range instance .PackageManager .Packages {
308
- for _ , platform := range pack .Platforms {
309
- if platformRelease := instance .PackageManager .GetInstalledPlatformRelease (platform ); platformRelease != nil {
310
- instance .lm .AddPlatformReleaseLibrariesDir (platformRelease , libraries .PlatformBuiltIn )
311
- }
312
- }
289
+ })
313
290
}
291
+ }
314
292
315
- if err := instance .lm .LoadIndex (); err != nil {
316
- s := status .Newf (codes .FailedPrecondition , "Loading index file: %v" , err )
317
- outChan <- & rpc.InitResponse {
293
+ if toolHasBeenInstalled {
294
+ // We installed at least one new tool after loading hardware
295
+ // so we must reload again otherwise we would never found them.
296
+ for _ , err := range instance .PackageManager .LoadHardware () {
297
+ resp (& rpc.InitResponse {
318
298
Message : & rpc.InitResponse_Error {
319
- Error : s .Proto (),
299
+ Error : err .Proto (),
320
300
},
321
- }
301
+ })
322
302
}
303
+ }
323
304
324
- for _ , err := range instance .lm .RescanLibraries () {
325
- s := status .Newf (codes .FailedPrecondition , "Loading libraries: %v" , err )
326
- outChan <- & rpc.InitResponse {
327
- Message : & rpc.InitResponse_Error {
328
- Error : s .Proto (),
329
- },
305
+ // Load libraries
306
+ for _ , pack := range instance .PackageManager .Packages {
307
+ for _ , platform := range pack .Platforms {
308
+ if platformRelease := instance .PackageManager .GetInstalledPlatformRelease (platform ); platformRelease != nil {
309
+ instance .lm .AddPlatformReleaseLibrariesDir (platformRelease , libraries .PlatformBuiltIn )
330
310
}
331
311
}
332
- }()
312
+ }
333
313
334
- return outChan , nil
314
+ if err := instance .lm .LoadIndex (); err != nil {
315
+ s := status .Newf (codes .FailedPrecondition , "Loading index file: %v" , err )
316
+ resp (& rpc.InitResponse {
317
+ Message : & rpc.InitResponse_Error {
318
+ Error : s .Proto (),
319
+ },
320
+ })
321
+ }
322
+
323
+ for _ , err := range instance .lm .RescanLibraries () {
324
+ s := status .Newf (codes .FailedPrecondition , "Loading libraries: %v" , err )
325
+ resp (& rpc.InitResponse {
326
+ Message : & rpc.InitResponse_Error {
327
+ Error : s .Proto (),
328
+ },
329
+ })
330
+ }
331
+
332
+ return nil
335
333
}
336
334
337
335
// Destroy FIXMEDOC
0 commit comments