Skip to content

Commit

Permalink
Inlining methods in ArduinoCoreServiceImpl (part 11: LibraryDownload,…
Browse files Browse the repository at this point in the history
… LibraryInstall, LibraryUpgrade, LibraryUninstall, LibraryUpgradeAll, LibraryResolveDependencies, LibrarySearch, LibraryList, ZipLibraryInstall, GitLibraryInstall)
  • Loading branch information
cmaglie committed Mar 18, 2024
1 parent 370337a commit 240b091
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 163 deletions.
83 changes: 0 additions & 83 deletions commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,94 +152,11 @@ func (s *arduinoCoreServerImpl) ListProgrammersAvailableForUpload(ctx context.Co
return ListProgrammersAvailableForUpload(ctx, req)
}

// LibraryDownload FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest, stream rpc.ArduinoCoreService_LibraryDownloadServer) error {
syncSend := NewSynchronizedSend(stream.Send)
resp, err := LibraryDownload(
stream.Context(), req,
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryDownloadResponse{Progress: p}) },
)
if err != nil {
return err
}
return syncSend.Send(resp)
}

// LibraryInstall FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return LibraryInstall(
stream.Context(), s, req,
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) },
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) },
)
}

// LibraryUpgrade FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, stream rpc.ArduinoCoreService_LibraryUpgradeServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return LibraryUpgrade(
stream.Context(), s, req,
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{Progress: p}) },
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{TaskProgress: p}) },
)
}

// LibraryUninstall FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return LibraryUninstall(stream.Context(), req,
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) },
)
}

// LibraryUpgradeAll FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, stream rpc.ArduinoCoreService_LibraryUpgradeAllServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return LibraryUpgradeAll(s, req,
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{Progress: p}) },
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) },
)
}

// LibraryResolveDependencies FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
return LibraryResolveDependencies(ctx, req)
}

// LibrarySearch FIXMEDOC
func (s *arduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
return LibrarySearch(ctx, req)
}

// LibraryList FIXMEDOC
func (s *arduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
return LibraryList(ctx, req)
}

// ArchiveSketch FIXMEDOC
func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
return ArchiveSketch(ctx, req)
}

// ZipLibraryInstall FIXMEDOC
func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequest, stream rpc.ArduinoCoreService_ZipLibraryInstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return ZipLibraryInstall(
stream.Context(), req,
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) },
)
}

// GitLibraryInstall FIXMEDOC
func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequest, stream rpc.ArduinoCoreService_GitLibraryInstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
return GitLibraryInstall(
stream.Context(), req,
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) },
)
}

// EnumerateMonitorPortSettings FIXMEDOC
func (s *arduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context, req *rpc.EnumerateMonitorPortSettingsRequest) (*rpc.EnumerateMonitorPortSettingsResponse, error) {
return EnumerateMonitorPortSettings(ctx, req)
Expand Down
40 changes: 25 additions & 15 deletions commands/service_library_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,54 +24,64 @@ import (
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
)

// LibraryDownload executes the download of the library.
// A DownloadProgressCB callback function must be passed to monitor download progress.
func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadRequest, downloadCB rpc.DownloadProgressCB) (*rpc.LibraryDownloadResponse, error) {
logrus.Info("Executing `arduino-cli lib download`")
// LibraryDownloadStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryDownload that sends
// all responses to the callback function.
func LibraryDownloadStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB) rpc.ArduinoCoreService_LibraryDownloadServer {
return streamResponseToCallback(ctx, func(r *rpc.LibraryDownloadResponse) error {
if r.GetProgress() != nil {
downloadCB(r.GetProgress())
}
return nil
})
}

// LibraryDownload downloads a library
func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest, stream rpc.ArduinoCoreService_LibraryDownloadServer) error {
syncSend := NewSynchronizedSend(stream.Send)
ctx := stream.Context()
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryDownloadResponse{Progress: p}) }

var downloadsDir *paths.Path
if pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance()); err != nil {
return nil, err
return err
} else {
downloadsDir = pme.DownloadDir
release()
}

li, err := instances.GetLibrariesIndex(req.GetInstance())
if err != nil {
return nil, err
return err
}

logrus.Info("Preparing download")

version, err := ParseVersion(req.GetVersion())
if err != nil {
return nil, err
return err
}

lib, err := li.FindRelease(req.GetName(), version)
if err != nil {
return nil, err
return err
}

if err := downloadLibrary(downloadsDir, lib, downloadCB, func(*rpc.TaskProgress) {}, "download"); err != nil {
return nil, err
if err := downloadLibrary(ctx, downloadsDir, lib, downloadCB, func(*rpc.TaskProgress) {}, "download"); err != nil {
return err
}

return &rpc.LibraryDownloadResponse{}, nil
return syncSend.Send(&rpc.LibraryDownloadResponse{})
}

func downloadLibrary(downloadsDir *paths.Path, libRelease *librariesindex.Release,
func downloadLibrary(_ context.Context, downloadsDir *paths.Path, libRelease *librariesindex.Release,
downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB, queryParameter string) error {

taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", libRelease)})
config, err := httpclient.GetDownloaderConfig()
if err != nil {
return &cmderrors.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
}
// TODO: Pass context
if err := libRelease.Resource.Download(downloadsDir, config, libRelease.String(), downloadCB, queryParameter); err != nil {
return &cmderrors.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
}
Expand Down
64 changes: 58 additions & 6 deletions commands/service_library_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,27 @@ import (
"github.com/sirupsen/logrus"
)

// LibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryInstall that sends
// all responses to the callback function.
func LibraryInstallStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_LibraryInstallServer {
return streamResponseToCallback(ctx, func(r *rpc.LibraryInstallResponse) error {
if r.GetProgress() != nil {
downloadCB(r.GetProgress())
}
if r.GetTaskProgress() != nil {
taskCB(r.GetTaskProgress())
}
return nil
})
}

// LibraryInstall resolves the library dependencies, then downloads and installs the libraries into the install location.
func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.LibraryInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) }
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) }

// Obtain the library index from the manager
li, err := instances.GetLibrariesIndex(req.GetInstance())
if err != nil {
Expand Down Expand Up @@ -128,16 +147,18 @@ func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *
downloadReason += "-builtin"
}
}
if err := downloadLibrary(downloadsDir, libRelease, downloadCB, taskCB, downloadReason); err != nil {
if err := downloadLibrary(ctx, downloadsDir, libRelease, downloadCB, taskCB, downloadReason); err != nil {
return err
}
if err := installLibrary(lmi, downloadsDir, libRelease, installTask, taskCB); err != nil {
return err
}
}

stream := InitStreamResponseToCallbackFunction(ctx, nil)
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
err = s.Init(
&rpc.InitRequest{Instance: req.GetInstance()},
InitStreamResponseToCallbackFunction(ctx, nil))
if err != nil {
return err
}

Expand Down Expand Up @@ -166,8 +187,23 @@ func installLibrary(lmi *librariesmanager.Installer, downloadsDir *paths.Path, l
return nil
}

// ZipLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in ZipLibraryInstall that sends
// all responses to the callback function.
func ZipLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_ZipLibraryInstallServer {
return streamResponseToCallback(ctx, func(r *rpc.ZipLibraryInstallResponse) error {
if r.GetTaskProgress() != nil {
taskCB(r.GetTaskProgress())
}
return nil
})
}

// ZipLibraryInstall FIXMEDOC
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, taskCB rpc.TaskProgressCB) error {
func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequest, stream rpc.ArduinoCoreService_ZipLibraryInstallServer) error {
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) }

lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
return err
Expand All @@ -181,14 +217,30 @@ func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, t
return nil
}

// GitLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in GitLibraryInstall that sends
// all responses to the callback function.
func GitLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_GitLibraryInstallServer {
return streamResponseToCallback(ctx, func(r *rpc.GitLibraryInstallResponse) error {
if r.GetTaskProgress() != nil {
taskCB(r.GetTaskProgress())
}
return nil
})
}

// GitLibraryInstall FIXMEDOC
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallRequest, taskCB rpc.TaskProgressCB) error {
func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequest, stream rpc.ArduinoCoreService_GitLibraryInstallServer) error {
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) }
lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
return err
}
lmi, release := lm.NewInstaller()
defer release()

// TODO: pass context
// ctx := stream.Context()
if err := lmi.InstallGitLib(req.GetUrl(), req.GetOverwrite()); err != nil {
return &cmderrors.FailedLibraryInstallError{Cause: err}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/service_library_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type installedLib struct {
}

// LibraryList FIXMEDOC
func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
func (s *arduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion commands/service_library_resolve_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// LibraryResolveDependencies FIXMEDOC
func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
func (s *arduinoCoreServerImpl) LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
lme, release, err := instances.GetLibraryManagerExplorer(req.GetInstance())
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion commands/service_library_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// LibrarySearch FIXMEDOC
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
func (s *arduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
li, err := instances.GetLibrariesIndex(req.GetInstance())
if err != nil {
return nil, err
Expand Down
20 changes: 18 additions & 2 deletions commands/service_library_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,23 @@ import (
"github.com/arduino/go-paths-helper"
)

// LibraryUninstall FIXMEDOC
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, taskCB rpc.TaskProgressCB) error {
// LibraryUninstallStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryUninstall that sends
// all responses to the callback function.
func LibraryUninstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_LibraryUninstallServer {
return streamResponseToCallback(ctx, func(r *rpc.LibraryUninstallResponse) error {
if r.GetTaskProgress() != nil {
taskCB(r.GetTaskProgress())
}
return nil
})
}

// LibraryUninstall uninstalls a library
func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
// ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) }

lm, err := instances.GetLibraryManager(req.GetInstance())
if err != nil {
return err
Expand All @@ -47,6 +62,7 @@ func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, tas

if len(libs) == 1 {
taskCB(&rpc.TaskProgress{Name: tr("Uninstalling %s", libs)})
// TODO: pass context
lmi.Uninstall(libs[0])
taskCB(&rpc.TaskProgress{Completed: true})
return nil
Expand Down
Loading

0 comments on commit 240b091

Please sign in to comment.