Skip to content

Commit 01b444c

Browse files
committed
Auto-download 3rd party indexes as part of the Init
1 parent d54f101 commit 01b444c

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

Diff for: commands/instances.go

+36-26
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
240240
})
241241
}
242242

243-
// Perform first-update of indexes if needed
244-
firstUpdate(context.Background(), req.GetInstance(), downloadCallback)
245-
246243
// Try to extract profile if specified
247244
var profile *sketch.Profile
248245
if req.GetProfile() != "" {
@@ -265,20 +262,11 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
265262
})
266263
}
267264

268-
{
269-
// We need to rebuild the PackageManager currently in use by this instance
270-
// in case this is not the first Init on this instances, that might happen
271-
// after reinitializing an instance after installing or uninstalling a core.
272-
// If this is not done the information of the uninstall core is kept in memory,
273-
// even if it should not.
274-
pmb, commitPackageManager := instance.pm.NewBuilder()
275-
276-
// Load packages index
277-
urls := []string{globals.DefaultIndexURL}
278-
if profile == nil {
279-
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
280-
}
281-
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") {
282270
URL, err := utils.URLParse(u)
283271
if err != nil {
284272
e := &arduino.InitFailedError{
@@ -289,7 +277,21 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
289277
responseError(e.ToRPCStatus())
290278
continue
291279
}
280+
allPackageIndexUrls = append(allPackageIndexUrls, URL)
281+
}
282+
}
283+
firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls)
292284

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 {
293295
if URL.Scheme == "file" {
294296
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
295297
if err != nil {
@@ -601,11 +603,10 @@ func LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketc
601603

602604
// firstUpdate downloads libraries and packages indexes if they don't exist.
603605
// This ideally is only executed the first time the CLI is run.
604-
func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(msg *rpc.DownloadProgress)) error {
606+
func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(msg *rpc.DownloadProgress), externalPackageIndexes []*url.URL) error {
605607
// Gets the data directory to verify if library_index.json and package_index.json exist
606608
dataDir := configuration.DataDir(configuration.Settings)
607609
libraryIndex := dataDir.Join("library_index.json")
608-
packageIndex := dataDir.Join("package_index.json")
609610

610611
if libraryIndex.NotExist() {
611612
// The library_index.json file doesn't exists, that means the CLI is run for the first time
@@ -616,13 +617,22 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
616617
}
617618
}
618619

619-
if packageIndex.NotExist() {
620-
// The package_index.json file doesn't exists, that means the CLI is run for the first time,
621-
// similarly to the library update we download that file and all the other package indexes
622-
// from additional_urls
623-
req := &rpc.UpdateIndexRequest{Instance: instance, IgnoreCustomPackageIndexes: true}
624-
if err := UpdateIndex(ctx, req, downloadCb); err != nil {
625-
return err
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
626636
}
627637
}
628638

0 commit comments

Comments
 (0)