Skip to content

Commit 122bca5

Browse files
committed
Implemented core uninstall
1 parent 7e2872d commit 122bca5

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

arduino/cores/packagemanager/install_uninstall.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ func (pm *PackageManager) InstallPlatform(platformRelease *cores.PlatformRelease
3333
return platformRelease.Resource.Install(pm.DownloadDir, pm.TempDir, destDir)
3434
}
3535

36+
// UninstallPlatform remove a PlatformRelease.
37+
func (pm *PackageManager) UninstallPlatform(platformRelease *cores.PlatformRelease) error {
38+
if platformRelease.InstallDir == nil {
39+
return fmt.Errorf("platform not installed")
40+
}
41+
42+
// Safety measure
43+
if safe, err := platformRelease.InstallDir.IsInsideDir(pm.PackagesDir); err != nil {
44+
return fmt.Errorf("checking if plaform is installed in data dir: %s", err)
45+
} else if !safe {
46+
return fmt.Errorf("platform is not installed inside data dir")
47+
}
48+
49+
if err := platformRelease.InstallDir.RemoveAll(); err != nil {
50+
return fmt.Errorf("removing platform files: %s", err)
51+
}
52+
platformRelease.InstallDir = nil
53+
return nil
54+
}
55+
3656
// InstallTool installs a specific release of a tool.
3757
func (pm *PackageManager) InstallTool(toolRelease *cores.ToolRelease) error {
3858
toolResource := toolRelease.GetCompatibleFlavour()
@@ -47,6 +67,26 @@ func (pm *PackageManager) InstallTool(toolRelease *cores.ToolRelease) error {
4767
return toolResource.Install(pm.DownloadDir, pm.TempDir, destDir)
4868
}
4969

70+
// UninstallTool remove a ToolRelease.
71+
func (pm *PackageManager) UninstallTool(toolRelease *cores.ToolRelease) error {
72+
if toolRelease.InstallDir == nil {
73+
return fmt.Errorf("tool not installed")
74+
}
75+
76+
// Safety measure
77+
if safe, err := toolRelease.InstallDir.IsInsideDir(pm.PackagesDir); err != nil {
78+
return fmt.Errorf("checking if tool is installed in data dir: %s", err)
79+
} else if !safe {
80+
return fmt.Errorf("tool is not installed inside data dir")
81+
}
82+
83+
if err := toolRelease.InstallDir.RemoveAll(); err != nil {
84+
return fmt.Errorf("removing tool files: %s", err)
85+
}
86+
toolRelease.InstallDir = nil
87+
return nil
88+
}
89+
5090
// IsToolRequired returns true if any of the installed platforms requires the toolRelease
5191
// passed as parameter
5292
func (pm *PackageManager) IsToolRequired(toolRelease *cores.ToolRelease) bool {

commands/commands_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,14 @@ func TestCoreCommands(t *testing.T) {
372372
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:uno", currSketchbookDir.Join("Test1").String())
373373
require.Zero(t, exitCode, "exit code")
374374
require.Contains(t, string(d), "Sketch uses")
375+
376+
// Uninstall arduino:avr
377+
exitCode, d = executeWithArgs(t, "core", "uninstall", "arduino:avr")
378+
require.Zero(t, exitCode, "exit code")
379+
require.Contains(t, string(d), AVR+" uninstalled")
380+
381+
// Empty cores list
382+
exitCode, d = executeWithArgs(t, "core", "list")
383+
require.Zero(t, exitCode, "exit code")
384+
require.Empty(t, strings.TrimSpace(string(d)))
375385
}

commands/core/core.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func InitCommand() *cobra.Command {
3434
coreCommand.AddCommand(initInstallCommand())
3535
coreCommand.AddCommand(initListCommand())
3636
coreCommand.AddCommand(initUpdateIndexCommand())
37+
coreCommand.AddCommand(initUninstallCommand())
3738
coreCommand.AddCommand(initSearchCommand())
3839
return coreCommand
3940
}

commands/core/uninstall.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
package core
19+
20+
import (
21+
"fmt"
22+
"os"
23+
24+
"github.com/arduino/arduino-cli/arduino/cores"
25+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
26+
"github.com/arduino/arduino-cli/commands"
27+
"github.com/arduino/arduino-cli/common/formatter"
28+
"github.com/sirupsen/logrus"
29+
"github.com/spf13/cobra"
30+
)
31+
32+
func initUninstallCommand() *cobra.Command {
33+
return &cobra.Command{
34+
Use: "uninstall PACKAGER:ARCH[@VERSION] ...",
35+
Short: "Uninstalls one or more cores and corresponding tool dependencies if no more used.",
36+
Long: "Uninstalls one or more cores and corresponding tool dependencies if no more used.",
37+
Example: " " + commands.AppName + " core uninstall arduino:samd\n",
38+
Args: cobra.MinimumNArgs(1),
39+
Run: runUninstallCommand,
40+
}
41+
}
42+
43+
func runUninstallCommand(cmd *cobra.Command, args []string) {
44+
logrus.Info("Executing `arduino core download`")
45+
46+
platformsRefs := parsePlatformReferenceArgs(args)
47+
pm := commands.InitPackageManager()
48+
49+
for _, platformRef := range platformsRefs {
50+
uninstallPlatformByRef(pm, platformRef)
51+
}
52+
}
53+
54+
func uninstallPlatformByRef(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) {
55+
platform, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
56+
if err != nil {
57+
formatter.PrintError(err, "Could not determine platform dependencies")
58+
os.Exit(commands.ErrBadCall)
59+
}
60+
61+
uninstallPlatformRelease(pm, platform)
62+
63+
for _, tool := range tools {
64+
if !pm.IsToolRequired(tool) {
65+
fmt.Printf("Tool %s is no more required\n", tool)
66+
uninstallToolRelease(pm, tool)
67+
}
68+
}
69+
}
70+
71+
func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
72+
log := pm.Log.WithField("platform", platformRelease)
73+
74+
log.Info("Uninstalling platform")
75+
formatter.Print("Uninstalling " + platformRelease.String() + "...")
76+
77+
if err := pm.UninstallPlatform(platformRelease); err != nil {
78+
log.WithError(err).Error("Error uninstalling")
79+
formatter.PrintError(err, "Error uninstalling "+platformRelease.String())
80+
os.Exit(commands.ErrGeneric)
81+
}
82+
83+
log.Info("Platform uninstalled")
84+
formatter.Print(platformRelease.String() + " uninstalled")
85+
}
86+
87+
func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
88+
log := pm.Log.WithField("Tool", toolRelease)
89+
90+
log.Info("Uninstalling tool")
91+
formatter.Print("Uninstalling " + toolRelease.String())
92+
93+
if err := pm.UninstallTool(toolRelease); err != nil {
94+
log.WithError(err).Error("Error uninstalling")
95+
formatter.PrintError(err, "Error uninstalling "+toolRelease.String())
96+
os.Exit(commands.ErrGeneric)
97+
}
98+
99+
log.Info("Tool uninstalled")
100+
formatter.Print(toolRelease.String() + " uninstalled")
101+
}

0 commit comments

Comments
 (0)