Skip to content

Commit 50ebe37

Browse files
committed
Started refactoring/rationalization of 'core' commands
1 parent 967ad6a commit 50ebe37

File tree

6 files changed

+101
-178
lines changed

6 files changed

+101
-178
lines changed

arduino/cores/packagemanager/download.go

Lines changed: 25 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package packagemanager
1919

2020
import (
2121
"fmt"
22-
"os"
2322

2423
"github.com/arduino/arduino-cli/arduino/cores"
25-
"github.com/arduino/arduino-cli/common/formatter/output"
2624
"github.com/cavaliercoder/grab"
2725
"go.bug.st/relaxed-semver"
2826
)
@@ -65,58 +63,37 @@ func (pm *PackageManager) FindPlatformRelease(ref *PlatformReference) *cores.Pla
6563
return platformRelease
6664
}
6765

68-
// FindItemsToDownload takes a set of PlatformReference and returns a set of items to download and
66+
// FindPlatformReleaseDependencies takes a PlatformReference and returns a set of items to download and
6967
// a set of outputs for non existing platforms.
70-
func (pm *PackageManager) FindItemsToDownload(items []PlatformReference) (
71-
[]*cores.PlatformRelease, []*cores.ToolRelease, error) {
72-
73-
retPlatforms := []*cores.PlatformRelease{}
74-
retTools := []*cores.ToolRelease{}
75-
added := map[string]bool{}
76-
77-
for _, item := range items {
78-
targetPackage, exists := pm.packages.Packages[item.Package]
79-
if !exists {
80-
return nil, nil, fmt.Errorf("package %s not found", item.Package)
81-
}
82-
platform, exists := targetPackage.Platforms[item.PlatformArchitecture]
83-
if !exists {
84-
return nil, nil, fmt.Errorf("platform %s not found in package %s", item.PlatformArchitecture, targetPackage.String())
85-
}
68+
func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReference) (*cores.PlatformRelease, []*cores.ToolRelease, error) {
69+
targetPackage, exists := pm.packages.Packages[item.Package]
70+
if !exists {
71+
return nil, nil, fmt.Errorf("package %s not found", item.Package)
72+
}
73+
platform, exists := targetPackage.Platforms[item.PlatformArchitecture]
74+
if !exists {
75+
return nil, nil, fmt.Errorf("platform %s not found in package %s", item.PlatformArchitecture, targetPackage.String())
76+
}
8677

87-
if added[platform.String()] {
88-
continue
78+
var release *cores.PlatformRelease
79+
if item.PlatformVersion != nil {
80+
release = platform.GetRelease(item.PlatformVersion)
81+
if release == nil {
82+
return nil, nil, fmt.Errorf("required version %s not found for platform %s", item.PlatformVersion, platform.String())
8983
}
90-
added[platform.String()] = true
91-
92-
var release *cores.PlatformRelease
93-
if item.PlatformVersion != nil {
94-
release = platform.GetRelease(item.PlatformVersion)
95-
if release == nil {
96-
return nil, nil, fmt.Errorf("required version %s not found for platform %s", item.PlatformVersion, platform.String())
97-
}
98-
} else {
99-
release = platform.GetLatestRelease()
100-
if release == nil {
101-
return nil, nil, fmt.Errorf("platform %s has no available releases", platform.String())
102-
}
84+
} else {
85+
release = platform.GetLatestRelease()
86+
if release == nil {
87+
return nil, nil, fmt.Errorf("platform %s has no available releases", platform.String())
10388
}
104-
retPlatforms = append(retPlatforms, release)
89+
}
10590

106-
// replaces "latest" with latest version too
107-
toolDeps, err := pm.packages.GetDepsOfPlatformRelease(release)
108-
if err != nil {
109-
return nil, nil, fmt.Errorf("getting tool dependencies for platform %s: %s", release.String(), err)
110-
}
111-
for _, tool := range toolDeps {
112-
if added[tool.String()] {
113-
continue
114-
}
115-
added[tool.String()] = true
116-
retTools = append(retTools, tool)
117-
}
91+
// replaces "latest" with latest version too
92+
toolDeps, err := pm.packages.GetDepsOfPlatformRelease(release)
93+
if err != nil {
94+
return nil, nil, fmt.Errorf("getting tool dependencies for platform %s: %s", release.String(), err)
11895
}
119-
return retPlatforms, retTools, nil
96+
return release, toolDeps, nil
12097
}
12198

12299
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Response
@@ -134,79 +111,3 @@ func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*grab.Re
134111
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*grab.Response, error) {
135112
return platform.Resource.Download(pm.DownloadDir)
136113
}
137-
138-
// FIXME: Make more generic and decouple the error print logic (that list should not exists;
139-
// rather a failure @ the first package)
140-
141-
func (pm *PackageManager) InstallToolReleases(toolReleases []*cores.ToolRelease,
142-
result *output.CoreProcessResults) error {
143-
144-
for _, toolRelease := range toolReleases {
145-
pm.Log.WithField("Package", toolRelease.Tool.Package.Name).
146-
WithField("Name", toolRelease.Tool.Name).
147-
WithField("Version", toolRelease.Version).
148-
Info("Installing tool")
149-
150-
err := pm.InstallTool(toolRelease)
151-
var processResult output.ProcessResult
152-
if err != nil {
153-
if os.IsExist(err) {
154-
pm.Log.WithError(err).Warnf("Cannot install tool `%s`, it is already installed", toolRelease.Tool.Name)
155-
processResult = output.ProcessResult{
156-
Status: "Already Installed",
157-
}
158-
} else {
159-
pm.Log.WithError(err).Warnf("Cannot install tool `%s`", toolRelease.Tool.Name)
160-
processResult = output.ProcessResult{
161-
Error: err.Error(),
162-
}
163-
}
164-
} else {
165-
pm.Log.Info("Adding installed tool to final result")
166-
processResult = output.ProcessResult{
167-
Status: "Installed",
168-
}
169-
}
170-
name := toolRelease.String()
171-
processResult.ItemName = name
172-
result.Tools[name] = processResult
173-
}
174-
return nil
175-
}
176-
177-
func (pm *PackageManager) InstallPlatformReleases(platformReleases []*cores.PlatformRelease,
178-
outputResults *output.CoreProcessResults) error {
179-
180-
for _, platformRelease := range platformReleases {
181-
pm.Log.WithField("Package", platformRelease.Platform.Package.Name).
182-
WithField("Name", platformRelease.Platform.Name).
183-
WithField("Version", platformRelease.Version).
184-
Info("Installing platform")
185-
186-
err := pm.InstallPlatform(platformRelease)
187-
var result output.ProcessResult
188-
if err != nil {
189-
if os.IsExist(err) {
190-
pm.Log.WithError(err).Warnf("Cannot install platform `%s`, it is already installed", platformRelease.Platform.Name)
191-
result = output.ProcessResult{
192-
Status: "Already Installed",
193-
}
194-
} else {
195-
pm.Log.WithError(err).Warnf("Cannot install platform `%s`", platformRelease.Platform.Name)
196-
result = output.ProcessResult{
197-
Error: err.Error(),
198-
}
199-
}
200-
} else {
201-
pm.Log.Info("Adding installed platform to final result")
202-
203-
result = output.ProcessResult{
204-
Status: "Installed",
205-
}
206-
}
207-
name := platformRelease.String()
208-
result.ItemName = name
209-
outputResults.Cores[name] = result
210-
}
211-
return nil
212-
}

commands/commands_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func detectLatestAVRCore(t *testing.T) string {
312312
return latest.String()
313313
}
314314

315-
func TestCoreDownload(t *testing.T) {
315+
func TestCoreCommands(t *testing.T) {
316316
defer makeTempDataDir(t)()
317317
defer makeTempSketchbookDir(t)()
318318

@@ -358,7 +358,7 @@ func TestCoreDownload(t *testing.T) {
358358
// Install avr
359359
exitCode, d = executeWithArgs(t, "core", "install", "arduino:avr")
360360
require.Zero(t, exitCode, "exit code")
361-
require.Contains(t, string(d), AVR+" - Installed")
361+
require.Contains(t, string(d), AVR+" installed")
362362

363363
exitCode, d = executeWithArgs(t, "core", "list")
364364
require.Zero(t, exitCode, "exit code")

commands/compile/compile.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
"github.com/arduino/arduino-cli/arduino/cores"
3030
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
3131
"github.com/arduino/arduino-cli/commands"
32+
"github.com/arduino/arduino-cli/commands/core"
3233
"github.com/arduino/arduino-cli/common/formatter"
33-
"github.com/arduino/arduino-cli/common/formatter/output"
3434
"github.com/arduino/arduino-cli/configs"
3535
"github.com/arduino/go-paths-helper"
3636
properties "github.com/arduino/go-properties-map"
@@ -108,24 +108,9 @@ func run(cmd *cobra.Command, args []string) {
108108
loadBuiltinCtagsMetadata(pm)
109109
ctags, err := getBuiltinCtagsTool(pm)
110110
if !ctags.IsInstalled() {
111-
formatter.Print("Downloading missing tool: " + ctags.String())
112-
resp, err := pm.DownloadToolRelease(ctags)
113-
if err != nil {
114-
formatter.PrintError(err, "Error downloading ctags")
115-
os.Exit(commands.ErrNetwork)
116-
}
117-
formatter.DownloadProgressBar(resp, ctags.String())
118-
if resp.Err() != nil {
119-
formatter.PrintError(resp.Err(), "Error downloading ctags")
120-
os.Exit(commands.ErrNetwork)
121-
}
122-
formatter.Print("Installing " + ctags.String())
123-
res := &output.CoreProcessResults{Tools: map[string]output.ProcessResult{}}
124-
if err := pm.InstallToolReleases([]*cores.ToolRelease{ctags}, res); err != nil {
125-
formatter.PrintError(err, "Error installing ctags")
126-
formatter.PrintErrorMessage("Missing ctags tool.")
127-
os.Exit(commands.ErrCoreConfig)
128-
}
111+
formatter.Print("Downloading and installing missing tool: " + ctags.String())
112+
core.DownloadToolRelease(pm, ctags)
113+
core.InstallToolRelease(pm, ctags)
129114

130115
if err := pm.LoadHardware(commands.Config); err != nil {
131116
formatter.PrintError(err, "Could not load hardware packages.")

commands/core/args.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
)
3030

3131
// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice.
32-
func parsePlatformReferenceArgs(args []string) []packagemanager.PlatformReference {
33-
ret := []packagemanager.PlatformReference{}
32+
func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference {
33+
ret := []*packagemanager.PlatformReference{}
3434

3535
for _, arg := range args {
3636
var version *semver.Version
@@ -48,7 +48,7 @@ func parsePlatformReferenceArgs(args []string) []packagemanager.PlatformReferenc
4848
formatter.PrintErrorMessage(fmt.Sprintf("'%s' is an invalid item (does not match the syntax 'PACKAGER:ARCH[@VERSION]')", arg))
4949
os.Exit(commands.ErrBadArgument)
5050
}
51-
ret = append(ret, packagemanager.PlatformReference{
51+
ret = append(ret, &packagemanager.PlatformReference{
5252
Package: split[0],
5353
PlatformArchitecture: split[1],
5454
PlatformVersion: version,

commands/core/download.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package core
2020
import (
2121
"os"
2222

23+
"github.com/arduino/arduino-cli/arduino/cores"
2324
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2425
"github.com/arduino/arduino-cli/commands"
2526
"github.com/arduino/arduino-cli/common/formatter"
@@ -45,13 +46,15 @@ func initDownloadCommand() *cobra.Command {
4546
func runDownloadCommand(cmd *cobra.Command, args []string) {
4647
logrus.Info("Executing `arduino core download`")
4748

48-
pm := commands.InitPackageManager()
4949
platformsRefs := parsePlatformReferenceArgs(args)
50-
downloadPlatforms(pm, platformsRefs)
50+
pm := commands.InitPackageManager()
51+
for _, platformRef := range platformsRefs {
52+
downloadPlatformByRef(pm, platformRef)
53+
}
5154
}
5255

53-
func downloadPlatforms(pm *packagemanager.PackageManager, platformsRefs []packagemanager.PlatformReference) {
54-
platforms, tools, err := pm.FindItemsToDownload(platformsRefs)
56+
func downloadPlatformByRef(pm *packagemanager.PackageManager, platformsRef *packagemanager.PlatformReference) {
57+
platform, tools, err := pm.FindPlatformReleaseDependencies(platformsRef)
5558
if err != nil {
5659
formatter.PrintError(err, "Could not determine platform dependencies")
5760
os.Exit(commands.ErrBadCall)
@@ -66,22 +69,24 @@ func downloadPlatforms(pm *packagemanager.PackageManager, platformsRefs []packag
6669
}
6770

6871
// Download tools
69-
formatter.Print("Downloading tools...")
7072
for _, tool := range tools {
71-
resp, err := pm.DownloadToolRelease(tool)
72-
download(resp, err, tool.String())
73+
DownloadToolRelease(pm, tool)
7374
}
7475

7576
// Download cores
76-
formatter.Print("Downloading cores...")
77-
for _, platform := range platforms {
78-
resp, err := pm.DownloadPlatformRelease(platform)
79-
download(resp, err, platform.String())
80-
}
77+
resp, err := pm.DownloadPlatformRelease(platform)
78+
download(resp, err, platform.String())
8179

8280
logrus.Info("Done")
8381
}
8482

83+
// DownloadToolRelease downloads a ToolRelease
84+
func DownloadToolRelease(pm *packagemanager.PackageManager, toolRelease *cores.ToolRelease) {
85+
formatter.Print("Downloading " + toolRelease.String() + "...")
86+
resp, err := pm.DownloadToolRelease(toolRelease)
87+
download(resp, err, toolRelease.String())
88+
}
89+
8590
func download(resp *grab.Response, err error, label string) {
8691
if err != nil {
8792
formatter.PrintError(err, "Error downloading "+label)

0 commit comments

Comments
 (0)