|
| 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/packagemanager" |
| 25 | + |
| 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 initUpgradeCommand() *cobra.Command { |
| 33 | + upgradeCommand := &cobra.Command{ |
| 34 | + Use: "upgrade [PACKAGER:ARCH] ...", |
| 35 | + Short: "Upgrades one or all installed platforms to the latest version.", |
| 36 | + Long: "Upgrades one or all installed platforms to the latest version.", |
| 37 | + Example: "" + |
| 38 | + "# upgrade everything to the latest version\n" + |
| 39 | + "arduino core upgrade\n\n" + |
| 40 | + "# upgrade arduino:samd to the latest version\n" + |
| 41 | + "arduino core upgrade arduino:samd", |
| 42 | + Run: runUpgradeCommand, |
| 43 | + } |
| 44 | + return upgradeCommand |
| 45 | +} |
| 46 | + |
| 47 | +func runUpgradeCommand(cmd *cobra.Command, args []string) { |
| 48 | + logrus.Info("Executing `arduino core upgrade`") |
| 49 | + |
| 50 | + pm := commands.InitPackageManager() |
| 51 | + |
| 52 | + platformsRefs := parsePlatformReferenceArgs(args) |
| 53 | + if len(platformsRefs) == 0 { |
| 54 | + upgradeAllPlatforms(pm) |
| 55 | + } else { |
| 56 | + upgrade(pm, platformsRefs) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func upgradeAllPlatforms(pm *packagemanager.PackageManager) { |
| 61 | + // Extract all PlatformReference to platforms that have updates |
| 62 | + platformRefs := []*packagemanager.PlatformReference{} |
| 63 | + |
| 64 | + for _, targetPackage := range pm.GetPackages().Packages { |
| 65 | + for _, platform := range targetPackage.Platforms { |
| 66 | + installed := platform.GetInstalled() |
| 67 | + if installed == nil { |
| 68 | + continue |
| 69 | + } |
| 70 | + latest := platform.GetLatestRelease() |
| 71 | + if !latest.Version.GreaterThan(installed.Version) { |
| 72 | + continue |
| 73 | + } |
| 74 | + platformRefs = append(platformRefs, &packagemanager.PlatformReference{ |
| 75 | + Package: targetPackage.Name, |
| 76 | + PlatformArchitecture: platform.Architecture, |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + upgrade(pm, platformRefs) |
| 82 | +} |
| 83 | + |
| 84 | +func upgrade(pm *packagemanager.PackageManager, platformsRefs []*packagemanager.PlatformReference) { |
| 85 | + for _, platformRef := range platformsRefs { |
| 86 | + if platformRef.PlatformVersion != nil { |
| 87 | + formatter.PrintErrorMessage("Invalid item " + platformRef.String() + ", upgrade doesn't accept parameters with version") |
| 88 | + os.Exit(commands.ErrBadArgument) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Search the latest version for all specified platforms |
| 93 | + toInstallRefs := []*packagemanager.PlatformReference{} |
| 94 | + for _, platformRef := range platformsRefs { |
| 95 | + platform := pm.FindPlatform(platformRef) |
| 96 | + if platform == nil { |
| 97 | + formatter.PrintErrorMessage("Platform " + platformRef.String() + " not found") |
| 98 | + os.Exit(commands.ErrBadArgument) |
| 99 | + } |
| 100 | + installed := platform.GetInstalled() |
| 101 | + if installed == nil { |
| 102 | + formatter.PrintErrorMessage("Platform " + platformRef.String() + " is not installed") |
| 103 | + os.Exit(commands.ErrBadArgument) |
| 104 | + } |
| 105 | + latest := platform.GetLatestRelease() |
| 106 | + if !latest.Version.GreaterThan(installed.Version) { |
| 107 | + formatter.PrintResult("Platform " + platformRef.String() + " is already at the latest version.") |
| 108 | + } else { |
| 109 | + platformRef.PlatformVersion = latest.Version |
| 110 | + toInstallRefs = append(toInstallRefs, platformRef) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + for _, platformRef := range toInstallRefs { |
| 115 | + fmt.Printf("Upgrading %s\n", platformRef) |
| 116 | + // downloadPlatformByRef(pm, platformRef) |
| 117 | + // installPlatformByRef(pm, platformRef) |
| 118 | + } |
| 119 | +} |
0 commit comments