Skip to content

Commit dd55eb7

Browse files
committed
Auto-download 3rd party indexes as part of the Init
1 parent ebde920 commit dd55eb7

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

Diff for: commands/instances.go

+38-28
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,6 +262,26 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
265262
})
266263
}
267264

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") {
270+
URL, err := utils.URLParse(u)
271+
if err != nil {
272+
e := &arduino.InitFailedError{
273+
Code: codes.InvalidArgument,
274+
Cause: fmt.Errorf(tr("Invalid additional URL: %v", err)),
275+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
276+
}
277+
responseError(e.ToRPCStatus())
278+
continue
279+
}
280+
allPackageIndexUrls = append(allPackageIndexUrls, URL)
281+
}
282+
}
283+
firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls)
284+
268285
{
269286
// We need to rebuild the PackageManager currently in use by this instance
270287
// in case this is not the first Init on this instances, that might happen
@@ -299,22 +316,7 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
299316
}
300317

301318
// Load packages index
302-
urls := []string{globals.DefaultIndexURL}
303-
if profile == nil {
304-
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
305-
}
306-
for _, u := range urls {
307-
URL, err := utils.URLParse(u)
308-
if err != nil {
309-
e := &arduino.InitFailedError{
310-
Code: codes.InvalidArgument,
311-
Cause: fmt.Errorf(tr("Invalid additional URL: %v", err)),
312-
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INVALID_INDEX_URL,
313-
}
314-
responseError(e.ToRPCStatus())
315-
continue
316-
}
317-
319+
for _, URL := range allPackageIndexUrls {
318320
if URL.Scheme == "file" {
319321
_, err := pmb.LoadPackageIndexFromFile(paths.New(URL.Path))
320322
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)