forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added daemon callback for Install rpc call
- Loading branch information
Showing
2 changed files
with
9 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,160 +1,11 @@ | ||
/* | ||
* 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 [email protected]. | ||
*/ | ||
|
||
package core | ||
|
||
import ( | ||
"os" | ||
"context" | ||
|
||
"github.com/arduino/arduino-cli/arduino/cores" | ||
"github.com/arduino/arduino-cli/arduino/cores/packagemanager" | ||
"github.com/arduino/arduino-cli/cli" | ||
"github.com/arduino/arduino-cli/common/formatter" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/arduino/arduino-cli/rpc" | ||
) | ||
|
||
func initInstallCommand() *cobra.Command { | ||
installCommand := &cobra.Command{ | ||
Use: "install PACKAGER:ARCH[@VERSION] ...", | ||
Short: "Installs one or more cores and corresponding tool dependencies.", | ||
Long: "Installs one or more cores and corresponding tool dependencies.", | ||
Example: " # download the latest version of arduino SAMD core.\n" + | ||
" " + cli.AppName + " core install arduino:samd\n\n" + | ||
" # download a specific version (in this case 1.6.9).\n" + | ||
" " + cli.AppName + " core install arduino:samd=1.6.9", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: runInstallCommand, | ||
} | ||
return installCommand | ||
} | ||
|
||
func runInstallCommand(cmd *cobra.Command, args []string) { | ||
logrus.Info("Executing `arduino core download`") | ||
|
||
platformsRefs := parsePlatformReferenceArgs(args) | ||
pm, _ := cli.InitPackageAndLibraryManagerWithoutBundles() | ||
|
||
for _, platformRef := range platformsRefs { | ||
installPlatformByRef(pm, platformRef) | ||
} | ||
|
||
// TODO: Cleanup unused tools | ||
} | ||
|
||
func installPlatformByRef(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(cli.ErrBadCall) | ||
} | ||
|
||
installPlatform(pm, platform, tools) | ||
} | ||
|
||
func installPlatform(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease, requiredTools []*cores.ToolRelease) { | ||
log := pm.Log.WithField("platform", platformRelease) | ||
|
||
// Prerequisite checks before install | ||
if platformRelease.IsInstalled() { | ||
log.Warn("Platform already installed") | ||
formatter.Print("Platform " + platformRelease.String() + " already installed") | ||
return | ||
} | ||
toolsToInstall := []*cores.ToolRelease{} | ||
for _, tool := range requiredTools { | ||
if tool.IsInstalled() { | ||
log.WithField("tool", tool).Warn("Tool already installed") | ||
formatter.Print("Tool " + tool.String() + " already installed") | ||
} else { | ||
toolsToInstall = append(toolsToInstall, tool) | ||
} | ||
} | ||
|
||
// Package download | ||
for _, tool := range toolsToInstall { | ||
downloadTool(pm, tool) | ||
} | ||
downloadPlatform(pm, platformRelease) | ||
|
||
for _, tool := range toolsToInstall { | ||
InstallToolRelease(pm, tool) | ||
} | ||
|
||
// Are we installing or upgrading? | ||
platform := platformRelease.Platform | ||
installed := pm.GetInstalledPlatformRelease(platform) | ||
if installed == nil { | ||
log.Info("Installing platform") | ||
formatter.Print("Installing " + platformRelease.String() + "...") | ||
} else { | ||
log.Info("Updating platform " + installed.String()) | ||
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...") | ||
} | ||
|
||
// Install | ||
err := pm.InstallPlatform(platformRelease) | ||
if err != nil { | ||
log.WithError(err).Error("Cannot install platform") | ||
formatter.PrintError(err, "Cannot install platform") | ||
os.Exit(cli.ErrGeneric) | ||
} | ||
|
||
// If upgrading remove previous release | ||
if installed != nil { | ||
err := pm.UninstallPlatform(installed) | ||
|
||
// In case of error try to rollback | ||
if err != nil { | ||
log.WithError(err).Error("Error updating platform.") | ||
formatter.PrintError(err, "Error updating platform") | ||
|
||
// Rollback | ||
if err := pm.UninstallPlatform(platformRelease); err != nil { | ||
log.WithError(err).Error("Error rolling-back changes.") | ||
formatter.PrintError(err, "Error rolling-back changes.") | ||
} | ||
os.Exit(cli.ErrGeneric) | ||
} | ||
} | ||
|
||
log.Info("Platform installed") | ||
formatter.Print(platformRelease.String() + " installed") | ||
} | ||
|
||
// InstallToolRelease installs a ToolRelease | ||
func InstallToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) { | ||
log := pm.Log.WithField("Tool", toolRelease) | ||
|
||
if toolRelease.IsInstalled() { | ||
log.Warn("Tool already installed") | ||
formatter.Print("Tool " + toolRelease.String() + " already installed") | ||
return | ||
} | ||
|
||
log.Info("Installing tool") | ||
formatter.Print("Installing " + toolRelease.String() + "...") | ||
err := pm.InstallTool(toolRelease) | ||
if err != nil { | ||
log.WithError(err).Warn("Cannot install tool") | ||
formatter.PrintError(err, "Cannot install tool: "+toolRelease.String()) | ||
os.Exit(cli.ErrGeneric) | ||
} | ||
|
||
log.Info("Tool installed") | ||
formatter.Print(toolRelease.String() + " installed") | ||
func Install(ctx context.Context, req *rpc.InstallReq) (*rpc.InstallResp, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters