@@ -262,20 +262,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
262262 })
263263 }
264264
265- {
266- // We need to rebuild the PackageManager currently in use by this instance
267- // in case this is not the first Init on this instances, that might happen
268- // after reinitializing an instance after installing or uninstalling a core.
269- // If this is not done the information of the uninstall core is kept in memory,
270- // even if it should not.
271- pmb , commitPackageManager := instance .pm .NewBuilder ()
272-
273- // Load packages index
274- urls := []string {globals .DefaultIndexURL }
275- if profile == nil {
276- urls = append (urls , configuration .Settings .GetStringSlice ("board_manager.additional_urls" )... )
277- }
278- for _ , u := range urls {
265+ // Perform first-update of indexes if needed
266+ defaultIndexURL , _ := utils .URLParse (globals .DefaultIndexURL )
267+ allPackageIndexUrls := []* url.URL {defaultIndexURL }
268+ if profile == nil {
269+ for _ , u := range configuration .Settings .GetStringSlice ("board_manager.additional_urls" ) {
279270 URL , err := utils .URLParse (u )
280271 if err != nil {
281272 e := & arduino.InitFailedError {
@@ -286,7 +277,21 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
286277 responseError (e .ToRPCStatus ())
287278 continue
288279 }
280+ allPackageIndexUrls = append (allPackageIndexUrls , URL )
281+ }
282+ }
283+ firstUpdate (context .Background (), req .GetInstance (), downloadCallback , allPackageIndexUrls )
289284
285+ {
286+ // We need to rebuild the PackageManager currently in use by this instance
287+ // in case this is not the first Init on this instances, that might happen
288+ // after reinitializing an instance after installing or uninstalling a core.
289+ // If this is not done the information of the uninstall core is kept in memory,
290+ // even if it should not.
291+ pmb , commitPackageManager := instance .pm .NewBuilder ()
292+
293+ // Load packages index
294+ for _ , URL := range allPackageIndexUrls {
290295 if URL .Scheme == "file" {
291296 _ , err := pmb .LoadPackageIndexFromFile (paths .New (URL .Path ))
292297 if err != nil {
@@ -595,3 +600,41 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketc
595600 RootFolderFiles : rootFolderFiles ,
596601 }, nil
597602}
603+
604+ // firstUpdate downloads libraries and packages indexes if they don't exist.
605+ // This ideally is only executed the first time the CLI is run.
606+ func firstUpdate (ctx context.Context , instance * rpc.Instance , downloadCb func (msg * rpc.DownloadProgress ), externalPackageIndexes []* url.URL ) error {
607+ // Gets the data directory to verify if library_index.json and package_index.json exist
608+ dataDir := configuration .DataDir (configuration .Settings )
609+ libraryIndex := dataDir .Join ("library_index.json" )
610+
611+ if libraryIndex .NotExist () {
612+ // The library_index.json file doesn't exists, that means the CLI is run for the first time
613+ // so we proceed with the first update that downloads the file
614+ req := & rpc.UpdateLibrariesIndexRequest {Instance : instance }
615+ if err := UpdateLibrariesIndex (ctx , req , downloadCb ); err != nil {
616+ return err
617+ }
618+ }
619+
620+ for _ , URL := range externalPackageIndexes {
621+ if URL .Scheme == "file" {
622+ continue
623+ }
624+ packageIndexFileName := (& resources.IndexResource {URL : URL }).IndexFileName ()
625+ packageIndexFile := dataDir .Join (packageIndexFileName )
626+ if packageIndexFile .NotExist () {
627+ // The index file doesn't exists, that means the CLI is run for the first time,
628+ // or the 3rd party package index URL has just been added. Similarly to the
629+ // library update we download that file and all the other package indexes from
630+ // additional_urls
631+ req := & rpc.UpdateIndexRequest {Instance : instance }
632+ if err := UpdateIndex (ctx , req , downloadCb ); err != nil {
633+ return err
634+ }
635+ break
636+ }
637+ }
638+
639+ return nil
640+ }
0 commit comments