Skip to content

Commit 496d03d

Browse files
committed
Improved core install/uninstall precodition checks
1 parent dc790dc commit 496d03d

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

arduino/cores/cores.go

+5
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ func (release *PlatformRelease) GetLibrariesDir() *paths.Path {
214214
return nil
215215
}
216216

217+
// IsInstalled returns true if the PlatformRelease is installed
218+
func (release *PlatformRelease) IsInstalled() bool {
219+
return release.InstallDir != nil
220+
}
221+
217222
func (release *PlatformRelease) String() string {
218223
version := ""
219224
if release.Version != nil {

commands/core/download.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,30 @@ func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *pack
6060
formatter.PrintError(err, "Could not determine platform dependencies")
6161
os.Exit(commands.ErrBadCall)
6262
}
63-
64-
// Check if all tools have a flavor available for the current OS
63+
downloadPlatform(pm, platform)
6564
for _, tool := range tools {
66-
if tool.GetCompatibleFlavour() == nil {
67-
formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS")
68-
os.Exit(commands.ErrGeneric)
69-
}
65+
downloadTool(pm, tool)
7066
}
67+
}
7168

72-
// Download tools
73-
for _, tool := range tools {
74-
DownloadToolRelease(pm, tool)
75-
}
69+
func downloadPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
70+
// Download platform
71+
resp, err := pm.DownloadPlatformRelease(platformRelease)
72+
download(resp, err, platformRelease.String())
73+
}
7674

77-
// Download cores
78-
formatter.Print("Downloading " + platform.String() + "...")
79-
resp, err := pm.DownloadPlatformRelease(platform)
80-
download(resp, err, platform.String())
75+
func downloadTool(pm *packagemanager.PackageManager, tool *cores.ToolRelease) {
76+
// Check if tool has a flavor available for the current OS
77+
if tool.GetCompatibleFlavour() == nil {
78+
formatter.PrintErrorMessage("The tool " + tool.String() + " is not available for the current OS")
79+
os.Exit(commands.ErrGeneric)
80+
}
8181

82-
logrus.Info("Done")
82+
DownloadToolRelease(pm, tool)
8383
}
8484

8585
// DownloadToolRelease downloads a ToolRelease
8686
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
87-
formatter.Print("Downloading " + toolRelease.String() + "...")
8887
resp, err := pm.DownloadToolRelease(toolRelease)
8988
download(resp, err, toolRelease.String())
9089
}
@@ -98,6 +97,7 @@ func download(d *downloader.Downloader, err error, label string) {
9897
formatter.Print(label + " already downloaded")
9998
return
10099
}
100+
formatter.Print("Downloading " + label + "...")
101101
formatter.DownloadProgressBar(d, label)
102102
if d.Error() != nil {
103103
formatter.PrintError(d.Error(), "Error downloading "+label)

commands/core/install.go

+34-19
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5050
pm := commands.InitPackageManagerWithoutBundles()
5151

5252
for _, platformRef := range platformsRefs {
53-
downloadPlatformByRef(pm, platformRef)
5453
installPlatformByRef(pm, platformRef)
5554
}
5655

@@ -64,19 +63,39 @@ func installPlatformByRef(pm *packagemanager.PackageManager, platformRef *packag
6463
os.Exit(commands.ErrBadCall)
6564
}
6665

67-
// TODO: Check install prerequisites here
66+
installPlatform(pm, platform, tools)
67+
}
6868

69-
// TODO: Download here
69+
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) {
70+
log := pm.Log.WithField("platform", platformRelease)
7071

71-
for _, tool := range tools {
72-
InstallToolRelease(pm, tool)
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+
}
7386
}
74-
installPlatformRelease(pm, platform)
75-
}
7687

77-
func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
78-
log := pm.Log.WithField("platform", platformRelease)
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+
}
7997

98+
// Are we installing or upgrading?
8099
platform := platformRelease.Platform
81100
installed := pm.GetInstalledPlatformRelease(platform)
82101
if installed == nil {
@@ -87,12 +106,8 @@ func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *
87106
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...")
88107
}
89108

109+
// Install
90110
err := pm.InstallPlatform(platformRelease)
91-
if os.IsExist(err) {
92-
log.Warn("Platform already installed")
93-
formatter.Print("Platform " + platformRelease.String() + " already installed")
94-
return
95-
}
96111
if err != nil {
97112
log.WithError(err).Error("Cannot install platform")
98113
formatter.PrintError(err, "Cannot install platform")
@@ -125,15 +140,15 @@ func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *
125140
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
126141
log := pm.Log.WithField("Tool", toolRelease)
127142

128-
log.Info("Installing tool")
129-
formatter.Print("Installing " + toolRelease.String())
130-
131-
err := pm.InstallTool(toolRelease)
132-
if os.IsExist(err) {
143+
if toolRelease.IsInstalled() {
133144
log.Warn("Tool already installed")
134145
formatter.Print("Tool " + toolRelease.String() + " already installed")
135146
return
136147
}
148+
149+
log.Info("Installing tool")
150+
formatter.Print("Installing " + toolRelease.String() + "...")
151+
err := pm.InstallTool(toolRelease)
137152
if err != nil {
138153
log.WithError(err).Warn("Cannot install tool")
139154
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String())

commands/core/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.
103103
log := pm.Log.WithField("Tool", toolRelease)
104104

105105
log.Info("Uninstalling tool")
106-
formatter.Print("Uninstalling " + toolRelease.String())
106+
formatter.Print("Uninstalling " + toolRelease.String() + "...")
107107

108108
if err := pm.UninstallTool(toolRelease); err != nil {
109109
log.WithError(err).Error("Error uninstalling")

0 commit comments

Comments
 (0)