Skip to content

Commit e814a42

Browse files
committed
Using packagemanager to handle package_index downloads
1 parent f43acbb commit e814a42

File tree

5 files changed

+37
-57
lines changed

5 files changed

+37
-57
lines changed

cli/common/common.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import (
2424
"path/filepath"
2525
"strings"
2626

27-
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
27+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2828
"github.com/arduino/arduino-cli/arduino/serialutils"
2929
"github.com/arduino/arduino-fwuploader/cli/feedback"
30+
"github.com/arduino/arduino-fwuploader/cli/globals"
3031
"github.com/arduino/arduino-fwuploader/indexes"
3132
"github.com/arduino/arduino-fwuploader/indexes/download"
3233
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
@@ -37,17 +38,18 @@ import (
3738
)
3839

3940
// InitIndexes downloads and parses the package_index.json and firmwares_index.json
40-
func InitIndexes() (*packageindex.Index, *firmwareindex.Index) {
41-
packageIndex, err := indexes.GetPackageIndex()
42-
if err != nil {
41+
func InitIndexes() (*packagemanager.PackageManager, *firmwareindex.Index) {
42+
// Load main package index and optional additional indexes
43+
pmbuilder := packagemanager.NewBuilder(nil, nil, nil, nil, "")
44+
if err := indexes.GetPackageIndex(pmbuilder, globals.PackageIndexGZURL); err != nil {
4345
feedback.Fatal(fmt.Sprintf("Can't load package index: %s", err), feedback.ErrGeneric)
4446
}
4547

4648
firmwareIndex, err := indexes.GetFirmwareIndex()
4749
if err != nil {
4850
feedback.Fatal(fmt.Sprintf("Can't load firmware index: %s", err), feedback.ErrGeneric)
4951
}
50-
return packageIndex, firmwareIndex
52+
return pmbuilder.Build(), firmwareIndex
5153
}
5254

5355
// CheckFlags runs a basic check, errors if the flags are not defined
@@ -75,25 +77,25 @@ func GetBoard(firmwareIndex *firmwareindex.Index, fqbn string) *firmwareindex.In
7577

7678
// DownloadRequiredToolsForBoard is an helper function that downloads the correct tool to flash a board,
7779
// it returns the path of the downloaded tool
78-
func DownloadRequiredToolsForBoard(packageIndex *packageindex.Index, board *firmwareindex.IndexBoard) *paths.Path {
80+
func DownloadRequiredToolsForBoard(pm *packagemanager.PackageManager, board *firmwareindex.IndexBoard) *paths.Path {
7981
if !board.IsPlugin() {
8082
// Just download the upload tool for integrated uploaders
81-
return downloadTool(packageIndex, board.Uploader)
83+
return downloadTool(pm, board.Uploader)
8284
}
8385

8486
// Download the plugin
85-
toolDir := downloadTool(packageIndex, board.UploaderPlugin)
87+
toolDir := downloadTool(pm, board.UploaderPlugin)
8688

8789
// Also download the other additional tools
8890
for _, tool := range board.AdditionalTools {
89-
_ = downloadTool(packageIndex, tool)
91+
_ = downloadTool(pm, tool)
9092
}
9193

9294
return toolDir
9395
}
9496

95-
func downloadTool(packageIndex *packageindex.Index, tool string) *paths.Path {
96-
toolRelease := indexes.GetToolRelease(packageIndex, tool)
97+
func downloadTool(pm *packagemanager.PackageManager, tool string) *paths.Path {
98+
toolRelease := indexes.GetToolRelease(pm, tool)
9799
if toolRelease == nil {
98100
feedback.Fatal(fmt.Sprintf("Error getting upload tool %s", tool), feedback.ErrGeneric)
99101
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
)
1919

2020
require (
21+
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b // indirect
2122
github.com/arduino/go-win32-utils v1.0.0 // indirect
2223
github.com/cmaglie/pb v1.0.27 // indirect
2324
github.com/codeclysm/extract/v3 v3.1.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ github.com/arduino/go-paths-helper v1.9.0 h1:IjWhDSF24n5bK/30NyApmzoVH9brWzc52KN
4747
github.com/arduino/go-paths-helper v1.9.0/go.mod h1:V82BWgAAp4IbmlybxQdk9Bpkz8M4Qyx+RAFKaG9NuvU=
4848
github.com/arduino/go-properties-orderedmap v1.7.1 h1:HQ9Pn/mk3+XyfrE39EEvaZwJkrvgiVSY5Oq3JSEfOR4=
4949
github.com/arduino/go-properties-orderedmap v1.7.1/go.mod h1:DKjD2VXY/NZmlingh4lSFMEYCVubfeArCsGPGDwb2yk=
50+
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b h1:9hDi4F2st6dbLC3y4i02zFT5quS4X6iioWifGlVwfy4=
51+
github.com/arduino/go-timeutils v0.0.0-20171220113728-d1dd9e313b1b/go.mod h1:uwGy5PpN4lqW97FiLnbcx+xx8jly5YuPMJWfVwwjJiQ=
5052
github.com/arduino/go-win32-utils v1.0.0 h1:/cXB86sOJxOsCHP7sQmXGLkdValwJt56mIwOHYxgQjQ=
5153
github.com/arduino/go-win32-utils v1.0.0/go.mod h1:0jqM7doGEAs6DaJCxxhLBUDS5OawrqF48HqXkcEie/Q=
5254
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=

indexes/download/download.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ func DownloadTool(toolRelease *cores.ToolRelease) (*paths.Path, error) {
5454
return nil, err
5555
}
5656
downloadsDir := globals.FwUploaderPath.Join("downloads")
57-
archivePath := downloadsDir.Join(resource.ArchiveFileName)
58-
if err := archivePath.Parent().MkdirAll(); err != nil {
57+
archivePath, err := resource.ArchivePath(downloadsDir)
58+
if err != nil {
5959
logrus.Error(err)
6060
return nil, err
6161
}

indexes/indexes.go

+19-44
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import (
2222
"strings"
2323

2424
"github.com/arduino/arduino-cli/arduino/cores"
25-
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
26-
"github.com/arduino/arduino-cli/arduino/resources"
25+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2726
"github.com/arduino/arduino-fwuploader/cli/globals"
2827
"github.com/arduino/arduino-fwuploader/indexes/download"
2928
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
@@ -34,57 +33,33 @@ import (
3433
// GetToolRelease returns a ToolRelease by searching the toolID in the index.
3534
// Returns nil if no matching tool release is found
3635
// Assumes toolID is formatted correctly as <packager>:<tool_name>@<version>
37-
func GetToolRelease(index *packageindex.Index, toolID string) *cores.ToolRelease {
38-
split := strings.Split(toolID, ":")
36+
func GetToolRelease(pm *packagemanager.PackageManager, toolID string) *cores.ToolRelease {
37+
split := strings.SplitN(toolID, ":", 2)
3938
packageName := split[0]
40-
split = strings.Split(split[1], "@")
39+
split = strings.SplitN(split[1], "@", 2)
4140
toolName := split[0]
4241
version := semver.ParseRelaxed(split[1])
43-
for _, pack := range index.Packages {
44-
if pack.Name != packageName {
45-
continue
46-
}
47-
for _, tool := range pack.Tools {
48-
if tool.Name == toolName && tool.Version.Equal(version) {
49-
flavors := []*cores.Flavor{}
50-
for _, system := range tool.Systems {
51-
size, _ := system.Size.Int64()
52-
flavors = append(flavors, &cores.Flavor{
53-
OS: system.OS,
54-
Resource: &resources.DownloadResource{
55-
URL: system.URL,
56-
ArchiveFileName: system.ArchiveFileName,
57-
Checksum: system.Checksum,
58-
Size: size,
59-
},
60-
})
61-
}
62-
return &cores.ToolRelease{
63-
Version: version,
64-
Flavors: flavors,
65-
Tool: &cores.Tool{
66-
Name: toolName,
67-
},
68-
}
69-
}
70-
}
71-
}
72-
return nil
42+
43+
pme, release := pm.NewExplorer()
44+
defer release()
45+
toolRelease := pme.FindToolDependency(&cores.ToolDependency{
46+
ToolName: toolName,
47+
ToolVersion: version,
48+
ToolPackager: packageName,
49+
})
50+
logrus.WithField("tool", toolRelease.String()).Debug("Tool release to download")
51+
return toolRelease
7352
}
7453

7554
// GetPackageIndex downloads and loads the Arduino package_index.json
76-
func GetPackageIndex() (*packageindex.Index, error) {
77-
indexPath, err := download.DownloadIndex(globals.PackageIndexGZURL)
78-
if err != nil {
79-
logrus.Error(err)
80-
return nil, err
81-
}
82-
in, err := packageindex.LoadIndex(indexPath)
55+
func GetPackageIndex(pmbuilder *packagemanager.Builder, indexURL string) error {
56+
indexPath, err := download.DownloadIndex(indexURL)
8357
if err != nil {
8458
logrus.Error(err)
85-
return nil, err
59+
return err
8660
}
87-
return in, err
61+
pmbuilder.LoadPackageIndexFromFile(indexPath)
62+
return nil
8863
}
8964

9065
// GetFirmwareIndex downloads and loads the arduino-fwuploader module_firmware_index.json

0 commit comments

Comments
 (0)