@@ -301,17 +301,13 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
301301 }
302302
303303 // 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 ()
309305
310306 // Load libraries
311307 for _ , pack := range pme .GetPackages () {
312308 for _ , platform := range pack .Platforms {
313309 if platformRelease := pme .GetInstalledPlatformRelease (platform ); platformRelease != nil {
314- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
310+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
315311 PlatformRelease : platformRelease ,
316312 Path : platformRelease .GetLibrariesDir (),
317313 Location : libraries .PlatformBuiltIn ,
@@ -320,22 +316,33 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
320316 }
321317 }
322318
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 {
324329 s := status .Newf (codes .FailedPrecondition , tr ("Loading index file: %v" ), err )
325330 responseError (s )
331+ li = librariesindex .EmptyIndex
326332 }
333+ instances .SetLibrariesIndex (instance , li )
327334
328335 if profile == nil {
329336 // Add directories of libraries bundled with IDE
330337 if bundledLibsDir := configuration .IDEBuiltinLibrariesDir (configuration .Settings ); bundledLibsDir != nil {
331- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
338+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
332339 Path : bundledLibsDir ,
333340 Location : libraries .IDEBuiltIn ,
334341 })
335342 }
336343
337344 // Add libraries directory from config file
338- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
345+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
339346 Path : configuration .LibrariesDir (configuration .Settings ),
340347 Location : libraries .User ,
341348 })
@@ -349,17 +356,14 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
349356 if ! libDir .IsDir () {
350357 // Download library
351358 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 {
357361 taskCallback (& rpc.TaskProgress {Name : tr ("Library %s not found" , libraryRef )})
358362 err := & cmderrors.LibraryNotFoundError {Library : libraryRef .Library }
359363 responseError (err .ToRPCStatus ())
360364 continue
361365 }
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 {
363367 taskCallback (& rpc.TaskProgress {Name : tr ("Error downloading library %s" , libraryRef )})
364368 e := & cmderrors.FailedLibraryInstallError {Cause : err }
365369 responseError (e .ToRPCStatus ())
@@ -369,7 +373,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
369373
370374 // Install library
371375 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 {
373377 taskCallback (& rpc.TaskProgress {Name : tr ("Error installing library %s" , libraryRef )})
374378 e := & cmderrors.FailedLibraryInstallError {Cause : err }
375379 responseError (e .ToRPCStatus ())
@@ -378,16 +382,23 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
378382 taskCallback (& rpc.TaskProgress {Completed : true })
379383 }
380384
381- lm .AddLibrariesDir (& librariesmanager.LibrariesDir {
385+ lmb .AddLibrariesDir (& librariesmanager.LibrariesDir {
382386 Path : libRoot ,
383387 Location : libraries .User ,
384388 })
385389 }
386390 }
387391
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 ()
391402 }
392403
393404 // Refreshes the locale used, this will change the
@@ -409,23 +420,18 @@ func Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse
409420// UpdateLibrariesIndex updates the library_index.json
410421func UpdateLibrariesIndex (ctx context.Context , req * rpc.UpdateLibrariesIndexRequest , downloadCB rpc.DownloadProgressCB ) error {
411422 logrus .Info ("Updating libraries index" )
412- lm , err := instances .GetLibraryManager (req .GetInstance ())
423+ pme , release , err := instances .GetPackageManagerExplorer (req .GetInstance ())
413424 if err != nil {
414425 return err
415426 }
427+ indexDir := pme .IndexDir
428+ release ()
416429
417- if err := lm . IndexFile . Parent () .MkdirAll (); err != nil {
430+ if err := indexDir .MkdirAll (); err != nil {
418431 return & cmderrors.PermissionDeniedError {Message : tr ("Could not create index directory" ), Cause : err }
419432 }
420433
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 {
429435 return err
430436 }
431437
0 commit comments