Skip to content

Commit df7a478

Browse files
committed
First implementation of 'core upgrade'
1 parent aead8c8 commit df7a478

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

commands/commands_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,32 @@ func TestCoreCommands(t *testing.T) {
369369
require.Zero(t, exitCode, "exit code")
370370
require.Contains(t, string(d), "arduino:avr")
371371

372+
// Upgrade platform
373+
exitCode, d = executeWithArgs(t, "core", "upgrade", "arduino:[email protected]")
374+
require.NotZero(t, exitCode, "exit code")
375+
require.Contains(t, string(d), "Invalid item arduino:[email protected]")
376+
377+
exitCode, d = executeWithArgs(t, "core", "upgrade", "other:avr")
378+
require.NotZero(t, exitCode, "exit code")
379+
require.Contains(t, string(d), "other:avr not found")
380+
381+
exitCode, d = executeWithArgs(t, "core", "upgrade", "arduino:samd")
382+
require.NotZero(t, exitCode, "exit code")
383+
require.Contains(t, string(d), "arduino:samd is not installed")
384+
385+
exitCode, d = executeWithArgs(t, "core", "upgrade", "arduino:avr")
386+
require.Zero(t, exitCode, "exit code")
387+
require.Contains(t, string(d), "Updating arduino:[email protected] with "+AVR)
388+
389+
// List updatable cores
390+
exitCode, d = executeWithArgs(t, "core", "list", "--updatable")
391+
require.Zero(t, exitCode, "exit code")
392+
require.NotContains(t, string(d), "arduino:avr")
393+
394+
exitCode, d = executeWithArgs(t, "core", "list")
395+
require.Zero(t, exitCode, "exit code")
396+
require.Contains(t, string(d), "arduino:avr")
397+
372398
// Build sketch for arduino:avr:uno
373399
exitCode, d = executeWithArgs(t, "sketch", "new", "Test1")
374400
require.Zero(t, exitCode, "exit code")
@@ -381,7 +407,7 @@ func TestCoreCommands(t *testing.T) {
381407
// Uninstall arduino:avr
382408
exitCode, d = executeWithArgs(t, "core", "uninstall", "arduino:avr")
383409
require.Zero(t, exitCode, "exit code")
384-
require.Contains(t, string(d), "arduino:[email protected] uninstalled")
410+
require.Contains(t, string(d), AVR+" uninstalled")
385411

386412
// Empty cores list
387413
exitCode, d = executeWithArgs(t, "core", "list")

commands/core/install.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
5252
downloadPlatformByRef(pm, platformRef)
5353
installPlatformByRef(pm, platformRef)
5454
}
55+
56+
// TODO: Cleanup unused tools
5557
}
5658

5759
func installPlatformByRef(pm *packagemanager.PackageManager, platformRef *packagemanager.PlatformReference) {
@@ -61,6 +63,10 @@ func installPlatformByRef(pm *packagemanager.PackageManager, platformRef *packag
6163
os.Exit(commands.ErrBadCall)
6264
}
6365

66+
// TODO: Check install prerequisites here
67+
68+
// TODO: Download here
69+
6470
for _, tool := range tools {
6571
InstallToolRelease(pm, tool)
6672
}
@@ -70,8 +76,15 @@ func installPlatformByRef(pm *packagemanager.PackageManager, platformRef *packag
7076
func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *cores.PlatformRelease) {
7177
log := pm.Log.WithField("platform", platformRelease)
7278

73-
log.Info("Installing platform")
74-
formatter.Print("Installing " + platformRelease.String() + "...")
79+
platform := platformRelease.Platform
80+
installed := platform.GetInstalled()
81+
if installed == nil {
82+
log.Info("Installing platform")
83+
formatter.Print("Installing " + platformRelease.String() + "...")
84+
} else {
85+
log.Info("Updating platform " + installed.String())
86+
formatter.Print("Updating " + installed.String() + " with " + platformRelease.String() + "...")
87+
}
7588

7689
err := pm.InstallPlatform(platformRelease)
7790
if os.IsExist(err) {
@@ -84,6 +97,24 @@ func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *
8497
os.Exit(commands.ErrGeneric)
8598
}
8699

100+
// If upgrading remove previous release
101+
if installed != nil {
102+
err := pm.UninstallPlatform(installed)
103+
104+
// In case of error try to rollback
105+
if err != nil {
106+
log.WithError(err).Error("Error updating platform.")
107+
formatter.PrintError(err, "Error updating platform")
108+
109+
// Rollback
110+
if err := pm.UninstallPlatform(platformRelease); err != nil {
111+
log.WithError(err).Error("Error rolling-back changes.")
112+
formatter.PrintError(err, "Error rolling-back changes.")
113+
}
114+
os.Exit(commands.ErrGeneric)
115+
}
116+
}
117+
87118
log.Info("Platform installed")
88119
formatter.Print(platformRelease.String() + " installed")
89120
}

commands/core/upgrade.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package core
1919

2020
import (
21-
"fmt"
2221
"os"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
@@ -112,8 +111,7 @@ func upgrade(pm *packagemanager.PackageManager, platformsRefs []*packagemanager.
112111
}
113112

114113
for _, platformRef := range toInstallRefs {
115-
fmt.Printf("Upgrading %s\n", platformRef)
116-
// downloadPlatformByRef(pm, platformRef)
117-
// installPlatformByRef(pm, platformRef)
114+
downloadPlatformByRef(pm, platformRef)
115+
installPlatformByRef(pm, platformRef)
118116
}
119117
}

0 commit comments

Comments
 (0)