Skip to content

Commit 8e7db44

Browse files
committed
Moved "PlatformInstall" implementation from cli to commands (WIP)
- Renamed install in stall platform; - Regenerated the Pb file from core.proto; - Modified demon callback;
1 parent 74998fe commit 8e7db44

File tree

10 files changed

+329
-313
lines changed

10 files changed

+329
-313
lines changed

cli/core/install.go

Lines changed: 15 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@
1818
package core
1919

2020
import (
21-
"os"
21+
"context"
2222

23-
"github.com/arduino/arduino-cli/arduino/cores"
24-
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2523
"github.com/arduino/arduino-cli/cli"
26-
"github.com/arduino/arduino-cli/common/formatter"
24+
"github.com/arduino/arduino-cli/commands/core"
25+
"github.com/arduino/arduino-cli/rpc"
2726
"github.com/sirupsen/logrus"
2827
"github.com/spf13/cobra"
2928
)
@@ -44,117 +43,24 @@ func initInstallCommand() *cobra.Command {
4443
}
4544

4645
func runInstallCommand(cmd *cobra.Command, args []string) {
46+
instance := cli.CreateInstance()
4747
logrus.Info("Executing `arduino core download`")
4848

4949
platformsRefs := parsePlatformReferenceArgs(args)
50-
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
5150

5251
for _, platformRef := range platformsRefs {
53-
installPlatformByRef(pm, platformRef)
52+
core.PlatformInstall(context.Background(), &rpc.PlatformInstallReq{
53+
Instance: instance,
54+
PlatformPackage: platformRef.Package,
55+
Architecture: platformRef.PlatformArchitecture,
56+
Version: platformRef.PlatformVersion.String(),
57+
})
58+
// if err != nil {
59+
// formatter.PrintError(err, "Error during build")
60+
// os.Exit(cli.ErrGeneric)
61+
// }
62+
// }
5463
}
5564

5665
// TODO: Cleanup unused tools
5766
}
58-
59-
func installPlatformByRef(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) {
60-
platform, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
61-
if err != nil {
62-
formatter.PrintError(err, "Could not determine platform dependencies")
63-
os.Exit(cli.ErrBadCall)
64-
}
65-
66-
installPlatform(pm, platform, tools)
67-
}
68-
69-
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) {
70-
log := pm.Log.WithField("platform", platformRelease)
71-
72-
// Prerequisite checks before install
73-
if platformRelease.IsInstalled() {
74-
log.Warn("Platform already installed")
75-
formatter.Print("Platform " + platformRelease.String() + " already installed")
76-
return
77-
}
78-
toolsToInstall := []*cores.ToolRelease{}
79-
for _, tool := range requiredTools {
80-
if tool.IsInstalled() {
81-
log.WithField("tool", tool).Warn("Tool already installed")
82-
formatter.Print("Tool " + tool.String() + " already installed")
83-
} else {
84-
toolsToInstall = append(toolsToInstall, tool)
85-
}
86-
}
87-
88-
// Package download
89-
for _, tool := range toolsToInstall {
90-
downloadTool(pm, tool)
91-
}
92-
downloadPlatform(pm, platformRelease)
93-
94-
for _, tool := range toolsToInstall {
95-
InstallToolRelease(pm, tool)
96-
}
97-
98-
// Are we installing or upgrading?
99-
platform := platformRelease.Platform
100-
installed := pm.GetInstalledPlatformRelease(platform)
101-
if installed == nil {
102-
log.Info("Installing platform")
103-
formatter.Print("Installing " + platformRelease.String() + "...")
104-
} else {
105-
log.Info("Updating platform " + installed.String())
106-
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...")
107-
}
108-
109-
// Install
110-
err := pm.InstallPlatform(platformRelease)
111-
if err != nil {
112-
log.WithError(err).Error("Cannot install platform")
113-
formatter.PrintError(err, "Cannot install platform")
114-
os.Exit(cli.ErrGeneric)
115-
}
116-
117-
// If upgrading remove previous release
118-
if installed != nil {
119-
err := pm.UninstallPlatform(installed)
120-
121-
// In case of error try to rollback
122-
if err != nil {
123-
log.WithError(err).Error("Error updating platform.")
124-
formatter.PrintError(err, "Error updating platform")
125-
126-
// Rollback
127-
if err := pm.UninstallPlatform(platformRelease); err != nil {
128-
log.WithError(err).Error("Error rolling-back changes.")
129-
formatter.PrintError(err, "Error rolling-back changes.")
130-
}
131-
os.Exit(cli.ErrGeneric)
132-
}
133-
}
134-
135-
log.Info("Platform installed")
136-
formatter.Print(platformRelease.String() + " installed")
137-
}
138-
139-
// InstallToolRelease installs a ToolRelease
140-
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
141-
log := pm.Log.WithField("Tool", toolRelease)
142-
143-
if toolRelease.IsInstalled() {
144-
log.Warn("Tool already installed")
145-
formatter.Print("Tool " + toolRelease.String() + " already installed")
146-
return
147-
}
148-
149-
log.Info("Installing tool")
150-
formatter.Print("Installing " + toolRelease.String() + "...")
151-
err := pm.InstallTool(toolRelease)
152-
if err != nil {
153-
log.WithError(err).Warn("Cannot install tool")
154-
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())
155-
os.Exit(cli.ErrGeneric)
156-
}
157-
158-
log.Info("Tool installed")
159-
formatter.Print(toolRelease.String() + " installed")
160-
}

commands/core/core.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

commands/core/install.go

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,128 @@ package core
22

33
import (
44
"context"
5+
"os"
56

7+
"github.com/arduino/arduino-cli/arduino/cores"
8+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
9+
"github.com/arduino/arduino-cli/cli"
10+
"github.com/arduino/arduino-cli/common/formatter"
611
"github.com/arduino/arduino-cli/rpc"
12+
semver "go.bug.st/relaxed-semver"
713
)
814

9-
func Install(ctx context.Context, req *rpc.InstallReq) (*rpc.InstallResp, error) {
15+
//"packager:arch@version
16+
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.PlatformInstallResp, error) {
17+
version, err := semver.Parse(req.Version)
18+
if err != nil {
19+
formatter.PrintError(err, "version not readable")
20+
os.Exit(cli.ErrBadCall)
21+
}
22+
ref := &packagemanager.PlatformReference{
23+
Package: req.PlatformPackage,
24+
PlatformArchitecture: req.Architecture,
25+
PlatformVersion: version}
26+
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles()
27+
platform, tools, err := pm.FindPlatformReleaseDependencies(ref)
28+
if err != nil {
29+
formatter.PrintError(err, "Could not determine platform dependencies")
30+
os.Exit(cli.ErrBadCall)
31+
}
32+
33+
installPlatform(pm, platform, tools)
34+
1035
return nil, nil
1136
}
37+
38+
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) {
39+
log := pm.Log.WithField("platform", platformRelease)
40+
41+
// Prerequisite checks before install
42+
if platformRelease.IsInstalled() {
43+
log.Warn("Platform already installed")
44+
formatter.Print("Platform " + platformRelease.String() + " already installed")
45+
return
46+
}
47+
toolsToInstall := []*cores.ToolRelease{}
48+
for _, tool := range requiredTools {
49+
if tool.IsInstalled() {
50+
log.WithField("tool", tool).Warn("Tool already installed")
51+
formatter.Print("Tool " + tool.String() + " already installed")
52+
} else {
53+
toolsToInstall = append(toolsToInstall, tool)
54+
}
55+
}
56+
57+
// Package download
58+
for _, tool := range toolsToInstall {
59+
downloadTool(pm, tool)
60+
}
61+
downloadPlatform(pm, platformRelease)
62+
63+
for _, tool := range toolsToInstall {
64+
InstallToolRelease(pm, tool)
65+
}
66+
67+
// Are we installing or upgrading?
68+
platform := platformRelease.Platform
69+
installed := pm.GetInstalledPlatformRelease(platform)
70+
if installed == nil {
71+
log.Info("Installing platform")
72+
formatter.Print("Installing " + platformRelease.String() + "...")
73+
} else {
74+
log.Info("Updating platform " + installed.String())
75+
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...")
76+
}
77+
78+
// Install
79+
err := pm.InstallPlatform(platformRelease)
80+
if err != nil {
81+
log.WithError(err).Error("Cannot install platform")
82+
formatter.PrintError(err, "Cannot install platform")
83+
os.Exit(cli.ErrGeneric)
84+
}
85+
86+
// If upgrading remove previous release
87+
if installed != nil {
88+
err := pm.UninstallPlatform(installed)
89+
90+
// In case of error try to rollback
91+
if err != nil {
92+
log.WithError(err).Error("Error updating platform.")
93+
formatter.PrintError(err, "Error updating platform")
94+
95+
// Rollback
96+
if err := pm.UninstallPlatform(platformRelease); err != nil {
97+
log.WithError(err).Error("Error rolling-back changes.")
98+
formatter.PrintError(err, "Error rolling-back changes.")
99+
}
100+
os.Exit(cli.ErrGeneric)
101+
}
102+
}
103+
104+
log.Info("Platform installed")
105+
formatter.Print(platformRelease.String() + " installed")
106+
}
107+
108+
// InstallToolRelease installs a ToolRelease
109+
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
110+
log := pm.Log.WithField("Tool", toolRelease)
111+
112+
if toolRelease.IsInstalled() {
113+
log.Warn("Tool already installed")
114+
formatter.Print("Tool " + toolRelease.String() + " already installed")
115+
return
116+
}
117+
118+
log.Info("Installing tool")
119+
formatter.Print("Installing " + toolRelease.String() + "...")
120+
err := pm.InstallTool(toolRelease)
121+
if err != nil {
122+
log.WithError(err).Warn("Cannot install tool")
123+
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())
124+
os.Exit(cli.ErrGeneric)
125+
}
126+
127+
log.Info("Tool installed")
128+
formatter.Print(toolRelease.String() + " installed")
129+
}

daemon/daemon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoC
6868
return err
6969
}
7070

71-
func (s *ArduinoCoreServerImpl) Install(ctx context.Context, req *rpc.InstallReq) (*rpc.InstallResp, error) {
72-
return core.Install(ctx, req)
71+
func (s *ArduinoCoreServerImpl) PlatformInstall(ctx context.Context, req *rpc.PlatformInstallReq) (*rpc.PlatformInstallResp, error) {
72+
return core.PlatformInstall(ctx, req)
7373
}

0 commit comments

Comments
 (0)