Skip to content
16 changes: 16 additions & 0 deletions internal/orchestrator/sketch_libs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package orchestrator

import (
"context"
"fmt"
"log/slog"
"time"

"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
Expand All @@ -25,6 +28,8 @@ import (
"github.com/arduino/arduino-app-cli/internal/orchestrator/app"
)

const indexUpdateInterval = 10 * time.Minute

func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryReleaseID, addDeps bool) ([]LibraryReleaseID, error) {
srv := commands.NewArduinoCoreServer()
var inst *rpc.Instance
Expand All @@ -43,6 +48,16 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
return nil, err
}

// update the local library_index if it is older than a certain threshold, to ensure the library is found when added by the arduino-cli
stream, res := commands.UpdateLibrariesIndexStreamResponseToCallbackFunction(ctx, func(curr *rpc.DownloadProgress) {
slog.Debug("downloading library index", "progress", curr.Message)
})
req := &rpc.UpdateLibrariesIndexRequest{Instance: inst, UpdateIfOlderThanSecs: int64(indexUpdateInterval.Seconds())}
if err := srv.UpdateLibrariesIndex(req, stream); err != nil {
return []LibraryReleaseID{}, fmt.Errorf("error updating library index: %v", err)
}
slog.Debug("Library index update", "status", res().GetLibrariesIndex().GetStatus())

resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{
Instance: inst,
SketchPath: app.MainSketchPath.String(),
Expand All @@ -59,6 +74,7 @@ func AddSketchLibrary(ctx context.Context, app app.ArduinoApp, libRef LibraryRel
if err != nil {
return nil, err
}

return f.Map(resp.GetAddedLibraries(), rpcProfileLibReferenceToLibReleaseID), nil
}

Expand Down