Skip to content

Allow multiple instances of the same 'required_tool' in cores #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package cores

import (
"encoding/json"
"sort"
"strings"

paths "github.com/arduino/go-paths-helper"

"github.com/arduino/arduino-cli/arduino/resources"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
semver "go.bug.st/relaxed-semver"
)
Expand Down Expand Up @@ -79,6 +79,20 @@ func (bm *BoardManifest) HasUsbID(vid, pid string) bool {
// ToolDependencies is a set of tool dependency
type ToolDependencies []*ToolDependency

// Sort sorts the ToolDependencies by name and (if multiple instance of the same
// tool is required) by version.
func (deps ToolDependencies) Sort() {
sort.Slice(deps, func(i, j int) bool {
if deps[i].ToolPackager != deps[j].ToolPackager {
return deps[i].ToolPackager < deps[j].ToolPackager
}
if deps[i].ToolName != deps[j].ToolName {
return deps[i].ToolName < deps[j].ToolName
}
return deps[i].ToolVersion.LessThan(deps[j].ToolVersion)
})
}

// ToolDependency is a tuple that uniquely identifies a specific version of a Tool
type ToolDependency struct {
ToolName string
Expand Down
9 changes: 6 additions & 3 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,23 +403,26 @@ func (pm *PackageManager) FindToolsRequiredForBoard(board *cores.Board) ([]*core
}

// replace the default tools above with the specific required by the current platform
requiredTools := []*cores.ToolRelease{}
platform.Dependencies.Sort()
for _, toolDep := range platform.Dependencies {
pm.Log.WithField("tool", toolDep).Infof("Required tool")
tool := pm.FindToolDependency(toolDep)
if tool == nil {
return nil, fmt.Errorf("tool release not found: %s", toolDep)
}
foundTools[tool.Tool.Name] = tool
requiredTools = append(requiredTools, tool)
delete(foundTools, tool.Tool.Name)
}

requiredTools := []*cores.ToolRelease{}
for _, toolRel := range foundTools {
requiredTools = append(requiredTools, toolRel)
}
return requiredTools, nil
}

// FindToolDependency FIXMEDOC
// FindToolDependency returns the ToolRelease referenced by the ToolDependency or nil if
// the referenced tool doesn't exists.
func (pm *PackageManager) FindToolDependency(dep *cores.ToolDependency) *cores.ToolRelease {
toolRelease, err := pm.Package(dep.ToolPackager).Tool(dep.ToolName).Release(dep.ToolVersion).Get()
if err != nil {
Expand Down
35 changes: 33 additions & 2 deletions arduino/cores/packagemanager/package_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import (
"net/url"
"testing"

"go.bug.st/relaxed-semver"

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/configs"
"github.com/arduino/go-paths-helper"
"github.com/arduino/go-properties-orderedmap"
"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
)

var customHardware = paths.New("testdata", "custom_hardware")
Expand Down Expand Up @@ -102,6 +101,7 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
}
loadIndex("https://dl.espressif.com/dl/package_esp32_index.json")
loadIndex("http://arduino.esp8266.com/stable/package_esp8266com_index.json")
loadIndex("https://adafruit.github.io/arduino-board-index/package_adafruit_index.json")
require.NoError(t, pm.LoadHardware(conf))
esp32, err := pm.FindBoardWithFQBN("esp32:esp32:esp32")
require.NoError(t, err)
Expand Down Expand Up @@ -138,4 +138,35 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()
testConflictingToolsInDifferentPackages()

feather, err := pm.FindBoardWithFQBN("adafruit:samd:adafruit_feather_m0_express")
require.NoError(t, err)
require.NotNil(t, feather)
featherTools, err := pm.FindToolsRequiredForBoard(feather)
require.NoError(t, err)
require.NotNil(t, featherTools)

// Test when a package index requires two different version of the same tool
// See: https://github.com/arduino/arduino-cli/issues/166#issuecomment-528295989
bossac17 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "arduino",
ToolName: "bossac",
ToolVersion: semver.ParseRelaxed("1.7.0"),
})
require.NotNil(t, bossac17)
bossac18 := pm.FindToolDependency(&cores.ToolDependency{
ToolPackager: "arduino",
ToolName: "bossac",
ToolVersion: semver.ParseRelaxed("1.8.0-48-gb176eee"),
})
require.NotNil(t, bossac18)
require.Contains(t, featherTools, bossac17)
require.Contains(t, featherTools, bossac18)

// Check if the runtime variable is set correctly to the latest version
uploadProperties := properties.NewMap()
for _, requiredTool := range featherTools {
uploadProperties.Merge(requiredTool.RuntimeProperties())
}
require.Equal(t, bossac18.InstallDir.String(), uploadProperties.Get("runtime.tools.bossac.path"))
}
Loading