@@ -301,17 +301,13 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
301
301
}
302
302
303
303
// Create library manager and add libraries directories
304
- lm := librariesmanager .NewLibraryManager (
305
- pme .IndexDir ,
306
- pme .DownloadDir ,
307
- )
308
- _ = instances .SetLibraryManager (instance , lm ) // should never fail
304
+ lmb := librariesmanager .NewBuilder ()
309
305
310
306
// Load libraries
311
307
for _ , pack := range pme .GetPackages () {
312
308
for _ , platform := range pack .Platforms {
313
309
if platformRelease := pme .GetInstalledPlatformRelease (platform ); platformRelease != nil {
314
- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
310
+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
315
311
PlatformRelease : platformRelease ,
316
312
Path : platformRelease .GetLibrariesDir (),
317
313
Location : libraries .PlatformBuiltIn ,
@@ -320,22 +316,33 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
320
316
}
321
317
}
322
318
323
- if err := lm .LoadIndex (); err != nil {
319
+ indexFileName , err := globals .LibrariesIndexResource .IndexFileName ()
320
+ if err != nil {
321
+ // should never happen
322
+ panic ("failed getting libraries index file name: " + err .Error ())
323
+ }
324
+ indexFile := pme .IndexDir .Join (indexFileName )
325
+
326
+ logrus .WithField ("index" , indexFile ).Info ("Loading libraries index file" )
327
+ li , err := librariesindex .LoadIndex (indexFile )
328
+ if err != nil {
324
329
s := status .Newf (codes .FailedPrecondition , tr ("Loading index file: %v" ), err )
325
330
responseError (s )
331
+ li = librariesindex .EmptyIndex
326
332
}
333
+ instances .SetLibrariesIndex (instance , li )
327
334
328
335
if profile == nil {
329
336
// Add directories of libraries bundled with IDE
330
337
if bundledLibsDir := configuration .IDEBuiltinLibrariesDir (configuration .Settings ); bundledLibsDir != nil {
331
- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
338
+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
332
339
Path : bundledLibsDir ,
333
340
Location : libraries .IDEBuiltIn ,
334
341
})
335
342
}
336
343
337
344
// Add libraries directory from config file
338
- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
345
+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
339
346
Path : configuration .LibrariesDir (configuration .Settings ),
340
347
Location : libraries .User ,
341
348
})
@@ -349,17 +356,14 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
349
356
if ! libDir .IsDir () {
350
357
// Download library
351
358
taskCallback (& rpc.TaskProgress {Name : tr ("Downloading library %s" , libraryRef )})
352
- libRelease := lm .Index .FindRelease (& librariesindex.Reference {
353
- Name : libraryRef .Library ,
354
- Version : libraryRef .Version ,
355
- })
356
- if libRelease == nil {
359
+ libRelease , err := li .FindRelease (libraryRef .Library , libraryRef .Version )
360
+ if err != nil {
357
361
taskCallback (& rpc.TaskProgress {Name : tr ("Library %s not found" , libraryRef )})
358
362
err := & cmderrors.LibraryNotFoundError {Library : libraryRef .Library }
359
363
responseError (err .ToRPCStatus ())
360
364
continue
361
365
}
362
- if err := libRelease .Resource .Download (lm . DownloadsDir , nil , libRelease .String (), downloadCallback , "" ); err != nil {
366
+ if err := libRelease .Resource .Download (pme . DownloadDir , nil , libRelease .String (), downloadCallback , "" ); err != nil {
363
367
taskCallback (& rpc.TaskProgress {Name : tr ("Error downloading library %s" , libraryRef )})
364
368
e := & cmderrors.FailedLibraryInstallError {Cause : err }
365
369
responseError (e .ToRPCStatus ())
@@ -369,7 +373,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
369
373
370
374
// Install library
371
375
taskCallback (& rpc.TaskProgress {Name : tr ("Installing library %s" , libraryRef )})
372
- if err := libRelease .Resource .Install (lm . DownloadsDir , libRoot , libDir ); err != nil {
376
+ if err := libRelease .Resource .Install (pme . DownloadDir , libRoot , libDir ); err != nil {
373
377
taskCallback (& rpc.TaskProgress {Name : tr ("Error installing library %s" , libraryRef )})
374
378
e := & cmderrors.FailedLibraryInstallError {Cause : err }
375
379
responseError (e .ToRPCStatus ())
@@ -378,16 +382,23 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
378
382
taskCallback (& rpc.TaskProgress {Completed : true })
379
383
}
380
384
381
- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
385
+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
382
386
Path : libRoot ,
383
387
Location : libraries .User ,
384
388
})
385
389
}
386
390
}
387
391
388
- for _ , status := range lm .RescanLibraries () {
389
- logrus .WithError (status .Err ()).Warnf ("Error loading library" )
390
- // TODO: report as warning: responseError(err)
392
+ lm := lmb .Build ()
393
+ _ = instances .SetLibraryManager (instance , lm ) // should never fail
394
+
395
+ {
396
+ lmi , release := lm .NewInstaller ()
397
+ for _ , status := range lmi .RescanLibraries () {
398
+ logrus .WithError (status .Err ()).Warnf ("Error loading library" )
399
+ // TODO: report as warning: responseError(err)
400
+ }
401
+ release ()
391
402
}
392
403
393
404
// Refreshes the locale used, this will change the
@@ -409,23 +420,18 @@ func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse
409
420
// UpdateLibrariesIndex updates the library_index.json
410
421
func UpdateLibrariesIndex (ctx context.Context , req * rpc.UpdateLibrariesIndexRequest , downloadCB rpc.DownloadProgressCB ) error {
411
422
logrus .Info ("Updating libraries index" )
412
- lm , err := instances .GetLibraryManager (req .GetInstance ())
423
+ pme , release , err := instances .GetPackageManagerExplorer (req .GetInstance ())
413
424
if err != nil {
414
425
return err
415
426
}
427
+ indexDir := pme .IndexDir
428
+ release ()
416
429
417
- if err := lm . IndexFile . Parent () .MkdirAll (); err != nil {
430
+ if err := indexDir .MkdirAll (); err != nil {
418
431
return & cmderrors.PermissionDeniedError {Message : tr ("Could not create index directory" ), Cause : err }
419
432
}
420
433
421
- // Create a temp dir to stage all downloads
422
- tmp , err := paths .MkTempDir ("" , "library_index_download" )
423
- if err != nil {
424
- return & cmderrors.TempDirCreationFailedError {Cause : err }
425
- }
426
- defer tmp .RemoveAll ()
427
-
428
- if err := globals .LibrariesIndexResource .Download (lm .IndexFile .Parent (), downloadCB ); err != nil {
434
+ if err := globals .LibrariesIndexResource .Download (indexDir , downloadCB ); err != nil {
429
435
return err
430
436
}
431
437
0 commit comments