Skip to content

Commit 370337a

Browse files
committed
Inlining methods in ArduinoCoreServiceImpl (part 10: PlatformInstall, PlatformDownload, PlatformUninstall, PlatformUpgrade, PlatformSearch)
1 parent 827eb6e commit 370337a

19 files changed

+159
-132
lines changed

Diff for: commands/service.go

-59
Original file line numberDiff line numberDiff line change
@@ -64,65 +64,6 @@ func (s *arduinoCoreServerImpl) SetSketchDefaults(ctx context.Context, req *rpc.
6464
return SetSketchDefaults(ctx, req)
6565
}
6666

67-
// PlatformInstall FIXMEDOC
68-
func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, stream rpc.ArduinoCoreService_PlatformInstallServer) error {
69-
syncSend := NewSynchronizedSend(stream.Send)
70-
resp, err := PlatformInstall(
71-
stream.Context(), s, req,
72-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformInstallResponse{Progress: p}) },
73-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformInstallResponse{TaskProgress: p}) },
74-
)
75-
if err != nil {
76-
return err
77-
}
78-
return syncSend.Send(resp)
79-
}
80-
81-
// PlatformDownload FIXMEDOC
82-
func (s *arduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadRequest, stream rpc.ArduinoCoreService_PlatformDownloadServer) error {
83-
syncSend := NewSynchronizedSend(stream.Send)
84-
resp, err := PlatformDownload(
85-
stream.Context(), req,
86-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformDownloadResponse{Progress: p}) },
87-
)
88-
if err != nil {
89-
return err
90-
}
91-
return syncSend.Send(resp)
92-
}
93-
94-
// PlatformUninstall FIXMEDOC
95-
func (s *arduinoCoreServerImpl) PlatformUninstall(req *rpc.PlatformUninstallRequest, stream rpc.ArduinoCoreService_PlatformUninstallServer) error {
96-
syncSend := NewSynchronizedSend(stream.Send)
97-
resp, err := PlatformUninstall(
98-
stream.Context(), s, req,
99-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUninstallResponse{TaskProgress: p}) },
100-
)
101-
if err != nil {
102-
return err
103-
}
104-
return syncSend.Send(resp)
105-
}
106-
107-
// PlatformUpgrade FIXMEDOC
108-
func (s *arduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeRequest, stream rpc.ArduinoCoreService_PlatformUpgradeServer) error {
109-
syncSend := NewSynchronizedSend(stream.Send)
110-
resp, err := PlatformUpgrade(
111-
stream.Context(), s, req,
112-
func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{Progress: p}) },
113-
func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{TaskProgress: p}) },
114-
)
115-
if err2 := syncSend.Send(resp); err2 != nil {
116-
return err2
117-
}
118-
return err
119-
}
120-
121-
// PlatformSearch FIXMEDOC
122-
func (s *arduinoCoreServerImpl) PlatformSearch(ctx context.Context, req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
123-
return PlatformSearch(req)
124-
}
125-
12667
// Upload FIXMEDOC
12768
func (s *arduinoCoreServerImpl) Upload(req *rpc.UploadRequest, stream rpc.ArduinoCoreService_UploadServer) error {
12869
syncSend := NewSynchronizedSend(stream.Send)

Diff for: commands/service_platform_download.go

+26-8
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ import (
2727

2828
var tr = i18n.Tr
2929

30-
// PlatformDownload FIXMEDOC
31-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB rpc.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
30+
// PlatformDownloadStreamResponseToCallbackFunction returns a gRPC stream to be used in PlatformDownload that sends
31+
// all responses to the callback function.
32+
func PlatformDownloadStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB) rpc.ArduinoCoreService_PlatformDownloadServer {
33+
return streamResponseToCallback(ctx, func(r *rpc.PlatformDownloadResponse) error {
34+
if r.GetProgress() != nil {
35+
downloadCB(r.GetProgress())
36+
}
37+
return nil
38+
})
39+
}
40+
41+
// PlatformDownload downloads a platform package
42+
func (s *arduinoCoreServerImpl) PlatformDownload(req *rpc.PlatformDownloadRequest, stream rpc.ArduinoCoreService_PlatformDownloadServer) error {
43+
syncSend := NewSynchronizedSend(stream.Send)
44+
3245
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3346
if err != nil {
34-
return nil, err
47+
return err
3548
}
3649
defer release()
3750

3851
version, err := ParseVersion(req.GetVersion())
3952
if err != nil {
40-
return nil, &cmderrors.InvalidVersionError{Cause: err}
53+
return &cmderrors.InvalidVersionError{Cause: err}
4154
}
4255

4356
ref := &packagemanager.PlatformReference{
@@ -47,18 +60,23 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
4760
}
4861
platform, tools, err := pme.FindPlatformReleaseDependencies(ref)
4962
if err != nil {
50-
return nil, &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err}
63+
return &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err}
5164
}
5265

66+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformDownloadResponse{Progress: p}) }
67+
68+
// TODO: pass context
69+
// ctx := stream.Context()
5370
if err := pme.DownloadPlatformRelease(platform, nil, downloadCB); err != nil {
54-
return nil, err
71+
return err
5572
}
5673

5774
for _, tool := range tools {
75+
// TODO: pass context
5876
if err := pme.DownloadToolRelease(tool, nil, downloadCB); err != nil {
59-
return nil, err
77+
return err
6078
}
6179
}
6280

63-
return &rpc.PlatformDownloadResponse{}, nil
81+
return syncSend.Send(&rpc.PlatformDownloadResponse{})
6482
}

Diff for: commands/service_platform_install.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,27 @@ import (
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2626
)
2727

28-
// PlatformInstall FIXMEDOC
29-
func PlatformInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformInstallRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
28+
// UpdateIndexStreamResponseToCallbackFunction returns a gRPC stream to be used in PlatformInstall that sends
29+
// all responses to the callback function.
30+
func PlatformInstallStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_PlatformInstallServer {
31+
return streamResponseToCallback(ctx, func(r *rpc.PlatformInstallResponse) error {
32+
if r.GetProgress() != nil {
33+
downloadCB(r.GetProgress())
34+
}
35+
if r.GetTaskProgress() != nil {
36+
taskCB(r.GetTaskProgress())
37+
}
38+
return nil
39+
})
40+
}
41+
42+
// PlatformInstall installs a platform package
43+
func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest, stream rpc.ArduinoCoreService_PlatformInstallServer) error {
44+
ctx := stream.Context()
45+
syncSend := NewSynchronizedSend(stream.Send)
46+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformInstallResponse{TaskProgress: p}) }
47+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformInstallResponse{Progress: p}) }
48+
3049
install := func() error {
3150
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3251
if err != nil {
@@ -63,6 +82,7 @@ func PlatformInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req
6382
}
6483
}
6584

85+
// TODO: Pass context
6686
if err := pme.DownloadAndInstallPlatformAndTools(platformRelease, tools, downloadCB, taskCB, req.GetSkipPostInstall(), req.GetSkipPreUninstall()); err != nil {
6787
return err
6888
}
@@ -71,12 +91,16 @@ func PlatformInstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req
7191
}
7292

7393
if err := install(); err != nil {
74-
return nil, err
94+
return err
7595
}
7696

77-
stream := InitStreamResponseToCallbackFunction(ctx, nil)
78-
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, stream); err != nil {
79-
return nil, err
97+
err := s.Init(
98+
&rpc.InitRequest{Instance: req.GetInstance()},
99+
InitStreamResponseToCallbackFunction(ctx, nil),
100+
)
101+
if err != nil {
102+
return err
80103
}
81-
return &rpc.PlatformInstallResponse{}, nil
104+
105+
return syncSend.Send(&rpc.PlatformInstallResponse{})
82106
}

Diff for: commands/service_platform_search.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package commands
1717

1818
import (
19+
"context"
1920
"regexp"
2021
"sort"
2122
"strings"
@@ -27,7 +28,7 @@ import (
2728
)
2829

2930
// PlatformSearch FIXMEDOC
30-
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
31+
func (s *arduinoCoreServerImpl) PlatformSearch(_ context.Context, req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
3132
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3233
if err != nil {
3334
return nil, err

Diff for: commands/service_platform_search_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestPlatformSearch(t *testing.T) {
5050
require.NoError(t, err)
5151

5252
t.Run("SearchAllVersions", func(t *testing.T) {
53-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
53+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
5454
Instance: inst,
5555
SearchArgs: "retrokit",
5656
})
@@ -91,7 +91,7 @@ func TestPlatformSearch(t *testing.T) {
9191
})
9292

9393
t.Run("SearchThePackageMaintainer", func(t *testing.T) {
94-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
94+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
9595
Instance: inst,
9696
SearchArgs: "Retrokits (www.retrokits.com)",
9797
})
@@ -131,7 +131,7 @@ func TestPlatformSearch(t *testing.T) {
131131
})
132132

133133
t.Run("SearchPackageName", func(t *testing.T) {
134-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
134+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
135135
Instance: inst,
136136
SearchArgs: "Retrokits-RK002",
137137
})
@@ -171,7 +171,7 @@ func TestPlatformSearch(t *testing.T) {
171171
})
172172

173173
t.Run("SearchPlatformName", func(t *testing.T) {
174-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
174+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
175175
Instance: inst,
176176
SearchArgs: "rk002",
177177
})
@@ -211,7 +211,7 @@ func TestPlatformSearch(t *testing.T) {
211211
})
212212

213213
t.Run("SearchBoardName", func(t *testing.T) {
214-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
214+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
215215
Instance: inst,
216216
SearchArgs: "Yún",
217217
})
@@ -269,7 +269,7 @@ func TestPlatformSearch(t *testing.T) {
269269
})
270270

271271
t.Run("SearchBoardName2", func(t *testing.T) {
272-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
272+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
273273
Instance: inst,
274274
SearchArgs: "yun",
275275
})
@@ -350,7 +350,7 @@ func TestPlatformSearchSorting(t *testing.T) {
350350
err = srv.Init(&rpc.InitRequest{Instance: inst}, InitStreamResponseToCallbackFunction(ctx, nil))
351351
require.NoError(t, err)
352352

353-
res, stat := PlatformSearch(&rpc.PlatformSearchRequest{
353+
res, stat := srv.PlatformSearch(ctx, &rpc.PlatformSearchRequest{
354354
Instance: inst,
355355
SearchArgs: "",
356356
})

Diff for: commands/service_platform_uninstall.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,33 @@ import (
2424
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2525
)
2626

27-
// PlatformUninstall FIXMEDOC
28-
func PlatformUninstall(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) (*rpc.PlatformUninstallResponse, error) {
29-
if err := platformUninstall(req, taskCB); err != nil {
30-
return nil, err
27+
// PlatformUninstallStreamResponseToCallbackFunction returns a gRPC stream to be used in PlatformUninstall that sends
28+
// all responses to the callback function.
29+
func PlatformUninstallStreamResponseToCallbackFunction(ctx context.Context, taskCB rpc.TaskProgressCB) rpc.ArduinoCoreService_PlatformUninstallServer {
30+
return streamResponseToCallback(ctx, func(r *rpc.PlatformUninstallResponse) error {
31+
if r.GetTaskProgress() != nil {
32+
taskCB(r.GetTaskProgress())
33+
}
34+
return nil
35+
})
36+
}
37+
38+
// PlatformUninstall uninstalls a platform package
39+
func (s *arduinoCoreServerImpl) PlatformUninstall(req *rpc.PlatformUninstallRequest, stream rpc.ArduinoCoreService_PlatformUninstallServer) error {
40+
syncSend := NewSynchronizedSend(stream.Send)
41+
ctx := stream.Context()
42+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUninstallResponse{TaskProgress: p}) }
43+
if err := platformUninstall(ctx, req, taskCB); err != nil {
44+
return err
3145
}
32-
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
33-
return nil, err
46+
if err := s.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
47+
return err
3448
}
35-
return &rpc.PlatformUninstallResponse{}, nil
49+
return syncSend.Send(&rpc.PlatformUninstallResponse{})
3650
}
3751

3852
// platformUninstall is the implementation of platform unistaller
39-
func platformUninstall(req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) error {
53+
func platformUninstall(_ context.Context, req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgressCB) error {
4054
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
4155
if err != nil {
4256
return &cmderrors.InvalidInstanceError{}
@@ -64,6 +78,7 @@ func platformUninstall(req *rpc.PlatformUninstallRequest, taskCB rpc.TaskProgres
6478
return &cmderrors.NotFoundError{Message: tr("Can't find dependencies for platform %s", ref), Cause: err}
6579
}
6680

81+
// TODO: pass context
6782
if err := pme.UninstallPlatform(platform, taskCB, req.GetSkipPreUninstall()); err != nil {
6883
return err
6984
}

Diff for: commands/service_platform_upgrade.go

+39-11
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,34 @@ import (
2424
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2525
)
2626

27-
// PlatformUpgrade FIXMEDOC
28-
func PlatformUpgrade(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req *rpc.PlatformUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (*rpc.PlatformUpgradeResponse, error) {
27+
// PlatformUpgradeStreamResponseToCallbackFunction returns a gRPC stream to be used in PlatformUpgrade that sends
28+
// all responses to the callback function.
29+
func PlatformUpgradeStreamResponseToCallbackFunction(ctx context.Context, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) (rpc.ArduinoCoreService_PlatformUpgradeServer, func() *rpc.Platform) {
30+
var resp *rpc.Platform
31+
return streamResponseToCallback(ctx, func(r *rpc.PlatformUpgradeResponse) error {
32+
// TODO: use oneof in protoc files?
33+
if r.GetProgress() != nil {
34+
downloadCB(r.GetProgress())
35+
}
36+
if r.GetTaskProgress() != nil {
37+
taskCB(r.GetTaskProgress())
38+
}
39+
if r.GetPlatform() != nil {
40+
resp = r.GetPlatform()
41+
}
42+
return nil
43+
}), func() *rpc.Platform {
44+
return resp
45+
}
46+
}
47+
48+
// PlatformUpgrade upgrades a platform package
49+
func (s *arduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeRequest, stream rpc.ArduinoCoreService_PlatformUpgradeServer) error {
50+
syncSend := NewSynchronizedSend(stream.Send)
51+
ctx := stream.Context()
52+
downloadCB := func(p *rpc.DownloadProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{Progress: p}) }
53+
taskCB := func(p *rpc.TaskProgress) { syncSend.Send(&rpc.PlatformUpgradeResponse{TaskProgress: p}) }
54+
2955
upgrade := func() (*cores.PlatformRelease, error) {
3056
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance())
3157
if err != nil {
@@ -46,20 +72,22 @@ func PlatformUpgrade(ctx context.Context, srv rpc.ArduinoCoreServiceServer, req
4672
return platform, nil
4773
}
4874

49-
var rpcPlatform *rpc.Platform
5075
platformRelease, err := upgrade()
5176
if platformRelease != nil {
52-
rpcPlatform = &rpc.Platform{
53-
Metadata: PlatformToRPCPlatformMetadata(platformRelease.Platform),
54-
Release: PlatformReleaseToRPC(platformRelease),
55-
}
77+
syncSend.Send(&rpc.PlatformUpgradeResponse{
78+
Platform: &rpc.Platform{
79+
Metadata: PlatformToRPCPlatformMetadata(platformRelease.Platform),
80+
Release: PlatformReleaseToRPC(platformRelease),
81+
},
82+
})
5683
}
5784
if err != nil {
58-
return &rpc.PlatformUpgradeResponse{Platform: rpcPlatform}, err
85+
return err
5986
}
60-
if err := srv.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
61-
return nil, err
87+
88+
if err := s.Init(&rpc.InitRequest{Instance: req.GetInstance()}, InitStreamResponseToCallbackFunction(ctx, nil)); err != nil {
89+
return err
6290
}
6391

64-
return &rpc.PlatformUpgradeResponse{Platform: rpcPlatform}, nil
92+
return nil
6593
}

0 commit comments

Comments
 (0)