Commit 122bca5b authored by Cristian Maglie's avatar Cristian Maglie

Implemented core uninstall

parent 7e2872d0
......@@ -33,6 +33,26 @@ func (pm *PackageManager) InstallPlatform(platformRelease *cores.PlatformRelease
return platformRelease.Resource.Install(pm.DownloadDir, pm.TempDir, destDir)
}
// UninstallPlatform remove a PlatformRelease.
func (pm *PackageManager) UninstallPlatform(platformRelease *cores.PlatformRelease) error {
if platformRelease.InstallDir == nil {
return fmt.Errorf("platform not installed")
}
// Safety measure
if safe, err := platformRelease.InstallDir.IsInsideDir(pm.PackagesDir); err != nil {
return fmt.Errorf("checking if plaform is installed in data dir: %s", err)
} else if !safe {
return fmt.Errorf("platform is not installed inside data dir")
}
if err := platformRelease.InstallDir.RemoveAll(); err != nil {
return fmt.Errorf("removing platform files: %s", err)
}
platformRelease.InstallDir = nil
return nil
}
// InstallTool installs a specific release of a tool.
func (pm *PackageManager) InstallTool(toolRelease *cores.ToolRelease) error {
toolResource := toolRelease.GetCompatibleFlavour()
......@@ -47,6 +67,26 @@ func (pm *PackageManager) InstallTool(toolRelease *cores.ToolRelease) error {
return toolResource.Install(pm.DownloadDir, pm.TempDir, destDir)
}
// UninstallTool remove a ToolRelease.
func (pm *PackageManager) UninstallTool(toolRelease *cores.ToolRelease) error {
if toolRelease.InstallDir == nil {
return fmt.Errorf("tool not installed")
}
// Safety measure
if safe, err := toolRelease.InstallDir.IsInsideDir(pm.PackagesDir); err != nil {
return fmt.Errorf("checking if tool is installed in data dir: %s", err)
} else if !safe {
return fmt.Errorf("tool is not installed inside data dir")
}
if err := toolRelease.InstallDir.RemoveAll(); err != nil {
return fmt.Errorf("removing tool files: %s", err)
}
toolRelease.InstallDir = nil
return nil
}
// IsToolRequired returns true if any of the installed platforms requires the toolRelease
// passed as parameter
func (pm *PackageManager) IsToolRequired(toolRelease *cores.ToolRelease) bool {
......
......@@ -372,4 +372,14 @@ func TestCoreCommands(t *testing.T) {
exitCode, d = executeWithArgs(t, "compile", "-b", "arduino:avr:uno", currSketchbookDir.Join("Test1").String())
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), "Sketch uses")
// Uninstall arduino:avr
exitCode, d = executeWithArgs(t, "core", "uninstall", "arduino:avr")
require.Zero(t, exitCode, "exit code")
require.Contains(t, string(d), AVR+" uninstalled")
// Empty cores list
exitCode, d = executeWithArgs(t, "core", "list")
require.Zero(t, exitCode, "exit code")
require.Empty(t, strings.TrimSpace(string(d)))
}
......@@ -34,6 +34,7 @@ func InitCommand() *cobra.Command {
coreCommand.AddCommand(initInstallCommand())
coreCommand.AddCommand(initListCommand())
coreCommand.AddCommand(initUpdateIndexCommand())
coreCommand.AddCommand(initUninstallCommand())
coreCommand.AddCommand(initSearchCommand())
return coreCommand
}
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to license@arduino.cc.
*/
package core
import (
"fmt"
"os"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func initUninstallCommand() *cobra.Command {
return &cobra.Command{
Use: "uninstall PACKAGER:ARCH[@VERSION] ...",
Short: "Uninstalls one or more cores and corresponding tool dependencies if no more used.",
Long: "Uninstalls one or more cores and corresponding tool dependencies if no more used.",
Example: " " + commands.AppName + " core uninstall arduino:samd\n",
Args: cobra.MinimumNArgs(1),
Run: runUninstallCommand,
}
}
func runUninstallCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core download`")
platformsRefs := parsePlatformReferenceArgs(args)
pm := commands.InitPackageManager()
for _, platformRef := range platformsRefs {
uninstallPlatformByRef(pm, platformRef)
}
}
func uninstallPlatformByRef(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) {
platform, tools, err := pm.FindPlatformReleaseDependencies(platformRef)
if err != nil {
formatter.PrintError(err, "Could not determine platform dependencies")
os.Exit(commands.ErrBadCall)
}
uninstallPlatformRelease(pm, platform)
for _, tool := range tools {
if !pm.IsToolRequired(tool) {
fmt.Printf("Tool %s is no more required\n", tool)
uninstallToolRelease(pm, tool)
}
}
}
func uninstallPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
log := pm.Log.WithField("platform", platformRelease)
log.Info("Uninstalling platform")
formatter.Print("Uninstalling " + platformRelease.String() + "...")
if err := pm.UninstallPlatform(platformRelease); err != nil {
log.WithError(err).Error("Error uninstalling")
formatter.PrintError(err, "Error uninstalling "+platformRelease.String())
os.Exit(commands.ErrGeneric)
}
log.Info("Platform uninstalled")
formatter.Print(platformRelease.String() + " uninstalled")
}
func uninstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
log := pm.Log.WithField("Tool", toolRelease)
log.Info("Uninstalling tool")
formatter.Print("Uninstalling " + toolRelease.String())
if err := pm.UninstallTool(toolRelease); err != nil {
log.WithError(err).Error("Error uninstalling")
formatter.PrintError(err, "Error uninstalling "+toolRelease.String())
os.Exit(commands.ErrGeneric)
}
log.Info("Tool uninstalled")
formatter.Print(toolRelease.String() + " uninstalled")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment