Skip to content

Commit 693036c

Browse files
committed
Implemented TaskProgress, replacing formatter module
1 parent 684a37b commit 693036c

File tree

13 files changed

+220
-115
lines changed

13 files changed

+220
-115
lines changed

cli/compile/compile.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
"context"
2222
"os"
2323

24+
"github.com/arduino/arduino-cli/cli"
2425
"github.com/arduino/arduino-cli/commands/compile"
2526
"github.com/arduino/arduino-cli/common/formatter"
27+
"github.com/arduino/arduino-cli/output"
2628
"github.com/arduino/arduino-cli/rpc"
27-
28-
"github.com/arduino/arduino-cli/cli"
2929
"github.com/spf13/cobra"
3030
)
3131

@@ -111,7 +111,7 @@ func run(cmd *cobra.Command, args []string) {
111111
Quiet: flags.quiet,
112112
VidPid: flags.vidPid,
113113
ExportFile: flags.exportFile,
114-
}, os.Stdout)
114+
}, os.Stdout, output.NewTaskProgressCB())
115115
if err == nil {
116116
outputCompileResp(compRes)
117117
} else {

cli/core/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5555
PlatformPackage: platformRef.Package,
5656
Architecture: platformRef.PlatformArchitecture,
5757
Version: platformRef.PlatformVersion.String(),
58-
}, output.DownloadProgressBar())
58+
}, output.DownloadProgressBar(), output.NewTaskProgressCB())
5959
// if err != nil {
6060
// formatter.PrintError(err, "Error during build")
6161
// os.Exit(cli.ErrGeneric)

cli/core/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
5454
PlatformPackage: platformRef.Package,
5555
Architecture: platformRef.PlatformArchitecture,
5656
Version: platformRef.PlatformVersion.String(),
57-
}, output.DownloadProgressBar())
57+
}, output.DownloadProgressBar(), output.NewTaskProgressCB())
5858
}
5959
}

commands/compile/compile.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/arduino/arduino-cli/arduino/cores"
1515
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
1616
"github.com/arduino/arduino-cli/cli"
17+
"github.com/arduino/arduino-cli/commands"
1718
"github.com/arduino/arduino-cli/commands/core"
1819
"github.com/arduino/arduino-cli/common/formatter"
1920
"github.com/arduino/arduino-cli/rpc"
@@ -22,7 +23,8 @@ import (
2223
"github.com/sirupsen/logrus"
2324
)
2425

25-
func Compile(ctx context.Context, req *rpc.CompileReq, output io.Writer) (*rpc.CompileResp, error) {
26+
func Compile(ctx context.Context, req *rpc.CompileReq,
27+
output io.Writer, taskCB commands.TaskProgressCB) (*rpc.CompileResp, error) {
2628
logrus.Info("Executing `arduino compile`")
2729
var sketchPath *paths.Path
2830
if req.GetSketchPath() != "" {
@@ -55,7 +57,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, output io.Writer) (*rpc.C
5557
core.DownloadToolRelease(pm, ctags, func(curr *rpc.DownloadProgress) {
5658
fmt.Printf(">> %+v\n", curr)
5759
})
58-
core.InstallToolRelease(pm, ctags)
60+
core.InstallToolRelease(pm, ctags, taskCB)
5961

6062
if err := pm.LoadHardware(cli.Config); err != nil {
6163
return nil, fmt.Errorf("loading hardware packages: %s", err)

commands/core/download.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
semver "go.bug.st/relaxed-semver"
3131
)
3232

33-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, progressCallback func(*rpc.DownloadProgress)) (*rpc.PlatformDownloadResp, error) {
33+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, progress commands.ProgressCB) (*rpc.PlatformDownloadResp, error) {
3434
var version *semver.Version
3535
if v, err := semver.Parse(req.Version); err == nil {
3636
version = v
@@ -48,13 +48,13 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, progres
4848
return nil, fmt.Errorf("find platform dependencies: %s", err)
4949
}
5050

51-
err = downloadPlatform(pm, platform, progressCallback)
51+
err = downloadPlatform(pm, platform, progress)
5252
if err != nil {
5353
return nil, err
5454
}
5555

5656
for _, tool := range tools {
57-
err := downloadTool(pm, tool, progressCallback)
57+
err := downloadTool(pm, tool, progress)
5858
if err != nil {
5959
return nil, fmt.Errorf("downloading tool %s: %s", tool, err)
6060
}
@@ -63,53 +63,53 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadReq, progres
6363
return &rpc.PlatformDownloadResp{}, nil
6464
}
6565

66-
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, progressCallback func(*rpc.DownloadProgress)) error {
66+
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, progress commands.ProgressCB) error {
6767
// Download platform
6868
resp, err := pm.DownloadPlatformRelease(platformRelease)
6969
if err != nil {
7070
return err
7171
}
72-
return download(resp, platformRelease.String(), progressCallback)
72+
return download(resp, platformRelease.String(), progress)
7373
}
7474

75-
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, progressCallback func(*rpc.DownloadProgress)) error {
75+
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease, progress commands.ProgressCB) error {
7676
// Check if tool has a flavor available for the current OS
7777
if tool.GetCompatibleFlavour() == nil {
7878
return fmt.Errorf("tool %s not available for the current OS", tool)
7979
}
8080

81-
return DownloadToolRelease(pm, tool, progressCallback)
81+
return DownloadToolRelease(pm, tool, progress)
8282
}
8383

8484
// DownloadToolRelease downloads a ToolRelease
85-
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, progressCallback func(*rpc.DownloadProgress)) error {
85+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, progress commands.ProgressCB) error {
8686
resp, err := pm.DownloadToolRelease(toolRelease)
8787
if err != nil {
8888
return err
8989
}
90-
return download(resp, toolRelease.String(), progressCallback)
90+
return download(resp, toolRelease.String(), progress)
9191
}
9292

93-
func download(d *downloader.Downloader, label string, progressCallback func(*rpc.DownloadProgress)) error {
93+
func download(d *downloader.Downloader, label string, progress commands.ProgressCB) error {
9494
if d == nil {
9595
// This signal means that the file is already downloaded
96-
progressCallback(&rpc.DownloadProgress{
96+
progress(&rpc.DownloadProgress{
9797
File: label,
9898
Completed: true,
9999
})
100100
return nil
101101
}
102-
progressCallback(&rpc.DownloadProgress{
102+
progress(&rpc.DownloadProgress{
103103
File: label,
104104
Url: d.URL,
105105
TotalSize: d.Size(),
106106
})
107107
d.RunAndPoll(func(downloaded int64) {
108-
progressCallback(&rpc.DownloadProgress{Downloaded: downloaded})
108+
progress(&rpc.DownloadProgress{Downloaded: downloaded})
109109
}, 250*time.Millisecond)
110110
if d.Error() != nil {
111111
return d.Error()
112112
}
113-
progressCallback(&rpc.DownloadProgress{Completed: true})
113+
progress(&rpc.DownloadProgress{Completed: true})
114114
return nil
115115
}

commands/core/install.go

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
semver "go.bug.st/relaxed-semver"
1313
)
1414

15-
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, progress commands.ProgressCB) (*rpc.PlatformInstallResp, error) {
15+
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq,
16+
progress commands.ProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResp, error) {
1617
var version *semver.Version
1718
if req.Version != "" {
1819
if v, err := semver.Parse(req.Version); err == nil {
@@ -21,18 +22,18 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, progress
2122
return nil, fmt.Errorf("invalid version: %s", err)
2223
}
2324
}
24-
ref := &packagemanager.PlatformReference{
25+
26+
pm := commands.GetPackageManager(req)
27+
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
2528
Package: req.PlatformPackage,
2629
PlatformArchitecture: req.Architecture,
27-
PlatformVersion: version}
28-
pm := commands.GetPackageManager(req)
29-
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
30+
PlatformVersion: version,
31+
})
3032
if err != nil {
31-
formatter.PrintError(err, "Could not determine platform dependencies")
32-
return nil, fmt.Errorf("Could not determine platform dependencies: %s", err)
33+
return nil, fmt.Errorf("finding platform dependencies: %s", err)
3334
}
3435

35-
err = installPlatform(pm, platform, tools, progress)
36+
err = installPlatform(pm, platform, tools, progress, taskCB)
3637
if err != nil {
3738
return nil, err
3839
}
@@ -47,51 +48,56 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq, progress
4748

4849
func installPlatform(pm *packagemanager.PackageManager,
4950
platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease,
50-
progress commands.ProgressCB) error {
51+
progress commands.ProgressCB, taskCB commands.TaskProgressCB) error {
5152
log := pm.Log.WithField("platform", platformRelease)
5253

5354
// Prerequisite checks before install
5455
if platformRelease.IsInstalled() {
5556
log.Warn("Platform already installed")
56-
formatter.Print("Platform " + platformRelease.String() + " already installed")
57-
return fmt.Errorf("Platform " + platformRelease.String() + " already installed")
57+
taskCB(&rpc.TaskProgress{Name: "Platform " + platformRelease.String() + " already installed", Completed: true})
58+
return nil
5859
}
5960
toolsToInstall := []*cores.ToolRelease{}
6061
for _, tool := range requiredTools {
6162
if tool.IsInstalled() {
6263
log.WithField("tool", tool).Warn("Tool already installed")
63-
formatter.Print("Tool " + tool.String() + " already installed")
64+
taskCB(&rpc.TaskProgress{Name: "Tool " + tool.String() + " already installed", Completed: true})
6465
} else {
6566
toolsToInstall = append(toolsToInstall, tool)
6667
}
6768
}
6869

6970
// Package download
71+
taskCB(&rpc.TaskProgress{Name: "Downloading packages"})
7072
for _, tool := range toolsToInstall {
7173
downloadTool(pm, tool, progress)
7274
}
7375
downloadPlatform(pm, platformRelease, progress)
76+
taskCB(&rpc.TaskProgress{Completed: true})
7477

78+
// Install tools first
7579
for _, tool := range toolsToInstall {
76-
InstallToolRelease(pm, tool)
80+
err := InstallToolRelease(pm, tool, taskCB)
81+
if err != nil {
82+
// TODO: handle error
83+
}
7784
}
7885

7986
// Are we installing or upgrading?
8087
platform := platformRelease.Platform
8188
installed := pm.GetInstalledPlatformRelease(platform)
8289
if installed == nil {
8390
log.Info("Installing platform")
84-
formatter.Print("Installing " + platformRelease.String() + "...")
91+
taskCB(&rpc.TaskProgress{Name: "Installing " + platformRelease.String()})
8592
} else {
8693
log.Info("Updating platform " + installed.String())
87-
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...")
94+
taskCB(&rpc.TaskProgress{Name: "Updating " + installed.String() + " with " + platformRelease.String()})
8895
}
8996

9097
// Install
9198
err := pm.InstallPlatform(platformRelease)
9299
if err != nil {
93100
log.WithError(err).Error("Cannot install platform")
94-
formatter.PrintError(err, "Cannot install platform")
95101
return err
96102
}
97103

@@ -102,43 +108,44 @@ func installPlatform(pm *packagemanager.PackageManager,
102108
// In case of error try to rollback
103109
if errUn != nil {
104110
log.WithError(errUn).Error("Error updating platform.")
105-
formatter.PrintError(errUn, "Error updating platform")
111+
taskCB(&rpc.TaskProgress{Message: "Error updating platform: " + err.Error()})
106112

107113
// Rollback
108114
if err := pm.UninstallPlatform(platformRelease); err != nil {
109115
log.WithError(err).Error("Error rolling-back changes.")
110-
formatter.PrintError(err, "Error rolling-back changes.")
111-
return fmt.Errorf("rolling-back changes: %s", err)
116+
taskCB(&rpc.TaskProgress{Message: "Error rolling-back changes: " + err.Error()})
117+
//return fmt.Errorf("rolling-back changes: %s", err)
112118
}
119+
113120
return fmt.Errorf("updating platform: %s", errUn)
114121
}
115122
}
116123

117124
log.Info("Platform installed")
118-
formatter.Print(platformRelease.String() + " installed")
125+
taskCB(&rpc.TaskProgress{Message: platformRelease.String() + " installed", Completed: true})
119126
return nil
120127
}
121128

122129
// InstallToolRelease installs a ToolRelease
123-
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) error {
130+
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease, taskCB commands.TaskProgressCB) error {
124131
log := pm.Log.WithField("Tool", toolRelease)
125132

126133
if toolRelease.IsInstalled() {
127134
log.Warn("Tool already installed")
128-
formatter.Print("Tool " + toolRelease.String() + " already installed")
129-
return fmt.Errorf("Tool " + toolRelease.String() + " already installed")
135+
taskCB(&rpc.TaskProgress{Name: "Tool " + toolRelease.String() + " already installed", Completed: true})
136+
return nil
130137
}
131138

132139
log.Info("Installing tool")
133-
formatter.Print("Installing " + toolRelease.String() + "...")
140+
taskCB(&rpc.TaskProgress{Name: "Installing " + toolRelease.String()})
134141
err := pm.InstallTool(toolRelease)
135142
if err != nil {
136143
log.WithError(err).Warn("Cannot install tool")
137144
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())
138145
return fmt.Errorf("Cannot install tool: "+toolRelease.String(), err)
139146
}
140-
141147
log.Info("Tool installed")
142-
formatter.Print(toolRelease.String() + " installed")
148+
taskCB(&rpc.TaskProgress{Message: toolRelease.String() + " installed", Completed: true})
149+
143150
return nil
144151
}

commands/core/upgrade.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import (
2828
semver "go.bug.st/relaxed-semver"
2929
)
3030

31-
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, progress commands.ProgressCB) (*rpc.PlatformUpgradeResp, error) {
31+
func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq,
32+
progress commands.ProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformUpgradeResp, error) {
3233
// Extract all PlatformReference to platforms that have updates
3334
var version *semver.Version
3435
if req.Version != "" {
@@ -44,7 +45,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, progress
4445
PlatformVersion: version}
4546
pm := commands.GetPackageManager(req)
4647

47-
err := upgradePlatform(pm, ref, progress)
48+
err := upgradePlatform(pm, ref, progress, taskCB)
4849
if err != nil {
4950
return nil, err
5051
}
@@ -57,7 +58,8 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeReq, progress
5758
return &rpc.PlatformUpgradeResp{}, nil
5859
}
5960

60-
func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference, progress commands.ProgressCB) error {
61+
func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference,
62+
progress commands.ProgressCB, taskCB commands.TaskProgressCB) error {
6163
if platformRef.PlatformVersion != nil {
6264
formatter.PrintErrorMessage("Invalid item " + platformRef.String() + ", upgrade doesn't accept parameters with version")
6365
return fmt.Errorf("Invalid item " + platformRef.String() + ", upgrade doesn't accept parameters with version")
@@ -88,7 +90,7 @@ func upgradePlatform(pm *packagemanager.PackageManager, platformRef *packagemana
8890
if err != nil {
8991
return fmt.Errorf("Platform " + platformRef.String() + " is not installed")
9092
}
91-
err = installPlatform(pm, platform, tools, progress)
93+
err = installPlatform(pm, platform, tools, progress, taskCB)
9294
if err != nil {
9395
return err
9496
}

commands/progress.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ import "github.com/arduino/arduino-cli/rpc"
44

55
// ProgressCB is a callback to get updates on download progress
66
type ProgressCB func(curr *rpc.DownloadProgress)
7+
8+
// TaskProgressCB is a callback to receive progress messages
9+
type TaskProgressCB func(msg *rpc.TaskProgress)

daemon/daemon.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoC
6767
}
6868
}
6969
}()
70-
resp, err := compile.Compile(stream.Context(), req, w)
70+
resp, err := compile.Compile(stream.Context(), req, w, func(taskProgress *rpc.TaskProgress) {
71+
// TODO
72+
})
7173
stream.Send(resp)
7274
return err
7375
}
7476

7577
func (s *ArduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallReq, stream rpc.ArduinoCore_PlatformInstallServer) error {
7678
resp, err := core.PlatformInstall(stream.Context(), req, func(progress *rpc.DownloadProgress) {
7779
stream.Send(&rpc.PlatformInstallResp{Progress: progress})
80+
}, func(taskProgress *rpc.TaskProgress) {
81+
// TODO
7882
})
7983
if err != nil {
8084
return err
@@ -99,6 +103,8 @@ func (s *ArduinoCoreServerImpl) PlatformUninstall(ctx context.Context, req *rpc.
99103
func (s *ArduinoCoreServerImpl) PlatformUpgrade(req *rpc.PlatformUpgradeReq, stream rpc.ArduinoCore_PlatformUpgradeServer) error {
100104
resp, err := core.PlatformUpgrade(stream.Context(), req, func(progress *rpc.DownloadProgress) {
101105
stream.Send(&rpc.PlatformUpgradeResp{Progress: progress})
106+
}, func(taskProgress *rpc.TaskProgress) {
107+
// TODO
102108
})
103109
if err != nil {
104110
return err

0 commit comments

Comments
 (0)