Skip to content

Commit 461b2b1

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 11: LibraryDownload, LibraryInstall, LibraryUpgrade, LibraryUninstall, LibraryUpgradeAll, LibraryResolveDependencies, LibrarySearch, LibraryList, ZipLibraryInstall, GitLibraryInstall)
1 parent 149b956 commit 461b2b1

19 files changed

+193
-163
lines changed

commands/service.go

-83
Original file line numberDiff line numberDiff line change
@@ -152,94 +152,11 @@ func (s *arduinoCoreServerImpl) ListProgrammersAvailableForUpload(ctx context.Co
152152
return ListProgrammersAvailableForUpload(ctx, req)
153153
}
154154

155-
// LibraryDownload FIXMEDOC
156-
func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest, stream rpc.ArduinoCoreService_LibraryDownloadServer) error {
157-
syncSend := NewSynchronizedSend(stream.Send)
158-
resp, err := LibraryDownload(
159-
stream.Context(), req,
160-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryDownloadResponse{Progress: p}) },
161-
)
162-
if err != nil {
163-
return err
164-
}
165-
return syncSend.Send(resp)
166-
}
167-
168-
// LibraryInstall FIXMEDOC
169-
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
170-
syncSend := NewSynchronizedSend(stream.Send)
171-
return LibraryInstall(
172-
stream.Context(), s, req,
173-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) },
174-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) },
175-
)
176-
}
177-
178-
// LibraryUpgrade FIXMEDOC
179-
func (s *arduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, stream rpc.ArduinoCoreService_LibraryUpgradeServer) error {
180-
syncSend := NewSynchronizedSend(stream.Send)
181-
return LibraryUpgrade(
182-
stream.Context(), s, req,
183-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{Progress: p}) },
184-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeResponse{TaskProgress: p}) },
185-
)
186-
}
187-
188-
// LibraryUninstall FIXMEDOC
189-
func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
190-
syncSend := NewSynchronizedSend(stream.Send)
191-
return LibraryUninstall(stream.Context(), req,
192-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) },
193-
)
194-
}
195-
196-
// LibraryUpgradeAll FIXMEDOC
197-
func (s *arduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, stream rpc.ArduinoCoreService_LibraryUpgradeAllServer) error {
198-
syncSend := NewSynchronizedSend(stream.Send)
199-
return LibraryUpgradeAll(s, req,
200-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{Progress: p}) },
201-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) },
202-
)
203-
}
204-
205-
// LibraryResolveDependencies FIXMEDOC
206-
func (s *arduinoCoreServerImpl) LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
207-
return LibraryResolveDependencies(ctx, req)
208-
}
209-
210-
// LibrarySearch FIXMEDOC
211-
func (s *arduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
212-
return LibrarySearch(ctx, req)
213-
}
214-
215-
// LibraryList FIXMEDOC
216-
func (s *arduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
217-
return LibraryList(ctx, req)
218-
}
219-
220155
// ArchiveSketch FIXMEDOC
221156
func (s *arduinoCoreServerImpl) ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchRequest) (*rpc.ArchiveSketchResponse, error) {
222157
return ArchiveSketch(ctx, req)
223158
}
224159

225-
// ZipLibraryInstall FIXMEDOC
226-
func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequest, stream rpc.ArduinoCoreService_ZipLibraryInstallServer) error {
227-
syncSend := NewSynchronizedSend(stream.Send)
228-
return ZipLibraryInstall(
229-
stream.Context(), req,
230-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) },
231-
)
232-
}
233-
234-
// GitLibraryInstall FIXMEDOC
235-
func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequest, stream rpc.ArduinoCoreService_GitLibraryInstallServer) error {
236-
syncSend := NewSynchronizedSend(stream.Send)
237-
return GitLibraryInstall(
238-
stream.Context(), req,
239-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) },
240-
)
241-
}
242-
243160
// EnumerateMonitorPortSettings FIXMEDOC
244161
func (s *arduinoCoreServerImpl) EnumerateMonitorPortSettings(ctx context.Context, req *rpc.EnumerateMonitorPortSettingsRequest) (*rpc.EnumerateMonitorPortSettingsResponse, error) {
245162
return EnumerateMonitorPortSettings(ctx, req)

commands/service_library_download.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -24,54 +24,64 @@ import (
2424
"github.com/arduino/arduino-cli/internal/arduino/libraries/librariesindex"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
"github.com/arduino/go-paths-helper"
27-
"github.com/sirupsen/logrus"
2827
)
2928

30-
// LibraryDownload executes the download of the library.
31-
// A DownloadProgressCB callback function must be passed to monitor download progress.
32-
func LibraryDownload(ctx context.Context, req *rpc.LibraryDownloadRequest, downloadCB rpc.DownloadProgressCB) (*rpc.LibraryDownloadResponse, error) {
33-
logrus.Info("Executing `arduino-cli lib download`")
29+
// LibraryDownloadStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryDownload that sends
30+
// all responses to the callback function.
31+
func LibraryDownloadStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB) rpc.ArduinoCoreService_LibraryDownloadServer {
32+
return streamResponseToCallback(ctx, func(r *rpc.LibraryDownloadResponse) error {
33+
if r.GetProgress() != nil {
34+
downloadCB(r.GetProgress())
35+
}
36+
return nil
37+
})
38+
}
39+
40+
// LibraryDownload downloads a library
41+
func (s *arduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadRequest, stream rpc.ArduinoCoreService_LibraryDownloadServer) error {
42+
syncSend := NewSynchronizedSend(stream.Send)
43+
ctx := stream.Context()
44+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryDownloadResponse{Progress: p}) }
3445

3546
var downloadsDir *paths.Path
3647
if pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance()); err != nil {
37-
return nil, err
48+
return err
3849
} else {
3950
downloadsDir = pme.DownloadDir
4051
release()
4152
}
4253

4354
li, err := instances.GetLibrariesIndex(req.GetInstance())
4455
if err != nil {
45-
return nil, err
56+
return err
4657
}
4758

48-
logrus.Info("Preparing download")
49-
5059
version, err := ParseVersion(req.GetVersion())
5160
if err != nil {
52-
return nil, err
61+
return err
5362
}
5463

5564
lib, err := li.FindRelease(req.GetName(), version)
5665
if err != nil {
57-
return nil, err
66+
return err
5867
}
5968

60-
if err := downloadLibrary(downloadsDir, lib, downloadCB, func(*rpc.TaskProgress) {}, "download"); err != nil {
61-
return nil, err
69+
if err := downloadLibrary(ctx, downloadsDir, lib, downloadCB, func(*rpc.TaskProgress) {}, "download"); err != nil {
70+
return err
6271
}
6372

64-
return &rpc.LibraryDownloadResponse{}, nil
73+
return syncSend.Send(&rpc.LibraryDownloadResponse{})
6574
}
6675

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

7079
taskCB(&rpc.TaskProgress{Name: tr("Downloading %s", libRelease)})
7180
config, err := httpclient.GetDownloaderConfig()
7281
if err != nil {
7382
return &cmderrors.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
7483
}
84+
// TODO: Pass context
7585
if err := libRelease.Resource.Download(downloadsDir, config, libRelease.String(), downloadCB, queryParameter); err != nil {
7686
return &cmderrors.FailedDownloadError{Message: tr("Can't download library"), Cause: err}
7787
}

commands/service_library_install.go

+58-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,27 @@ import (
3030
"github.com/sirupsen/logrus"
3131
)
3232

33+
// LibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryInstall that sends
34+
// all responses to the callback function.
35+
func LibraryInstallStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_LibraryInstallServer {
36+
return streamResponseToCallback(ctx, func(r *rpc.LibraryInstallResponse) error {
37+
if r.GetProgress() != nil {
38+
downloadCB(r.GetProgress())
39+
}
40+
if r.GetTaskProgress() != nil {
41+
taskCB(r.GetTaskProgress())
42+
}
43+
return nil
44+
})
45+
}
46+
3347
// LibraryInstall resolves the library dependencies, then downloads and installs the libraries into the install location.
34-
func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.LibraryInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
48+
func (s *arduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, stream rpc.ArduinoCoreService_LibraryInstallServer) error {
49+
ctx := stream.Context()
50+
syncSend := NewSynchronizedSend(stream.Send)
51+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.LibraryInstallResponse{Progress: p}) }
52+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) }
53+
3554
// Obtain the library index from the manager
3655
li, err := instances.GetLibrariesIndex(req.GetInstance())
3756
if err != nil {
@@ -128,16 +147,18 @@ func LibraryInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *
128147
downloadReason += "-builtin"
129148
}
130149
}
131-
if err := downloadLibrary(downloadsDir, libRelease, downloadCB, taskCB, downloadReason); err != nil {
150+
if err := downloadLibrary(ctx, downloadsDir, libRelease, downloadCB, taskCB, downloadReason); err != nil {
132151
return err
133152
}
134153
if err := installLibrary(lmi, downloadsDir, libRelease, installTask, taskCB); err != nil {
135154
return err
136155
}
137156
}
138157

139-
stream := InitStreamResponseToCallbackFunction(ctx, nil)
140-
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
158+
err = s.Init(
159+
&rpc.InitRequest{Instance: req.GetInstance()},
160+
InitStreamResponseToCallbackFunction(ctx, nil))
161+
if err != nil {
141162
return err
142163
}
143164

@@ -166,8 +187,23 @@ func installLibrary(lmi *librariesmanager.Installer, downloadsDir *paths.Path, l
166187
return nil
167188
}
168189

190+
// ZipLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in ZipLibraryInstall that sends
191+
// all responses to the callback function.
192+
func ZipLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_ZipLibraryInstallServer {
193+
return streamResponseToCallback(ctx, func(r *rpc.ZipLibraryInstallResponse) error {
194+
if r.GetTaskProgress() != nil {
195+
taskCB(r.GetTaskProgress())
196+
}
197+
return nil
198+
})
199+
}
200+
169201
// ZipLibraryInstall FIXMEDOC
170-
func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, taskCB rpc.TaskProgressCB) error {
202+
func (s *arduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequest, stream rpc.ArduinoCoreService_ZipLibraryInstallServer) error {
203+
ctx := stream.Context()
204+
syncSend := NewSynchronizedSend(stream.Send)
205+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) }
206+
171207
lm, err := instances.GetLibraryManager(req.GetInstance())
172208
if err != nil {
173209
return err
@@ -181,14 +217,30 @@ func ZipLibraryInstall(ctx context.Context, req *rpc.ZipLibraryInstallRequest, t
181217
return nil
182218
}
183219

220+
// GitLibraryInstallStreamResponseToCallbackFunction returns a gRPC stream to be used in GitLibraryInstall that sends
221+
// all responses to the callback function.
222+
func GitLibraryInstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_GitLibraryInstallServer {
223+
return streamResponseToCallback(ctx, func(r *rpc.GitLibraryInstallResponse) error {
224+
if r.GetTaskProgress() != nil {
225+
taskCB(r.GetTaskProgress())
226+
}
227+
return nil
228+
})
229+
}
230+
184231
// GitLibraryInstall FIXMEDOC
185-
func GitLibraryInstall(ctx context.Context, req *rpc.GitLibraryInstallRequest, taskCB rpc.TaskProgressCB) error {
232+
func (s *arduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequest, stream rpc.ArduinoCoreService_GitLibraryInstallServer) error {
233+
syncSend := NewSynchronizedSend(stream.Send)
234+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) }
186235
lm, err := instances.GetLibraryManager(req.GetInstance())
187236
if err != nil {
188237
return err
189238
}
190239
lmi, release := lm.NewInstaller()
191240
defer release()
241+
242+
// TODO: pass context
243+
// ctx := stream.Context()
192244
if err := lmi.InstallGitLib(req.GetUrl(), req.GetOverwrite()); err != nil {
193245
return &cmderrors.FailedLibraryInstallError{Cause: err}
194246
}

commands/service_library_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type installedLib struct {
3535
}
3636

3737
// LibraryList FIXMEDOC
38-
func LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
38+
func (s *arduinoCoreServerImpl) LibraryList(ctx context.Context, req *rpc.LibraryListRequest) (*rpc.LibraryListResponse, error) {
3939
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
4040
if err != nil {
4141
return nil, err

commands/service_library_resolve_deps.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
// LibraryResolveDependencies FIXMEDOC
33-
func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
33+
func (s *arduinoCoreServerImpl) LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDependenciesRequest) (*rpc.LibraryResolveDependenciesResponse, error) {
3434
lme, release, err := instances.GetLibraryManagerExplorer(req.GetInstance())
3535
if err != nil {
3636
return nil, err

commands/service_library_search.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
// LibrarySearch FIXMEDOC
30-
func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
30+
func (s *arduinoCoreServerImpl) LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.LibrarySearchResponse, error) {
3131
li, err := instances.GetLibrariesIndex(req.GetInstance())
3232
if err != nil {
3333
return nil, err

commands/service_library_uninstall.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,23 @@ import (
2525
"github.com/arduino/go-paths-helper"
2626
)
2727

28-
// LibraryUninstall FIXMEDOC
29-
func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, taskCB rpc.TaskProgressCB) error {
28+
// LibraryUninstallStreamResponseToCallbackFunction returns a gRPC stream to be used in LibraryUninstall that sends
29+
// all responses to the callback function.
30+
func LibraryUninstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_LibraryUninstallServer {
31+
return streamResponseToCallback(ctx, func(r *rpc.LibraryUninstallResponse) error {
32+
if r.GetTaskProgress() != nil {
33+
taskCB(r.GetTaskProgress())
34+
}
35+
return nil
36+
})
37+
}
38+
39+
// LibraryUninstall uninstalls a library
40+
func (s *arduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
41+
// ctx := stream.Context()
42+
syncSend := NewSynchronizedSend(stream.Send)
43+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) }
44+
3045
lm, err := instances.GetLibraryManager(req.GetInstance())
3146
if err != nil {
3247
return err
@@ -47,6 +62,7 @@ func LibraryUninstall(ctx context.Context, req *rpc.LibraryUninstallRequest, tas
4762

4863
if len(libs) == 1 {
4964
taskCB(&rpc.TaskProgress{Name: tr("Uninstalling %s", libs)})
65+
// TODO: pass context
5066
lmi.Uninstall(libs[0])
5167
taskCB(&rpc.TaskProgress{Completed: true})
5268
return nil

0 commit comments

Comments
 (0)