Skip to content

Allow platforms without fixed version in profiles. #2940

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
}

// Load Platforms
if profile == nil {
if profile == nil || profile.RequireSystemInstalledPlatform() {
for _, err := range pmb.LoadHardware() {
s := &cmderrors.PlatformLoadingError{Cause: err}
responseError(s.GRPCStatus())
Expand Down
142 changes: 87 additions & 55 deletions internal/arduino/cores/packageindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,8 @@ func (index Index) MergeIntoPackages(outPackages cores.Packages) {
// which in turn contains a single indexPlatformRelease converted from the one
// passed as argument
func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
boards := []indexBoard{}
for _, manifest := range pr.BoardsManifest {
board := indexBoard{
Name: manifest.Name,
}
for _, id := range manifest.ID {
if id.USB != "" {
board.ID = []indexBoardID{{USB: id.USB}}
}
}
boards = append(boards, board)
}
// While enumerating the dependencies we also build a set of required packages.
requiredPackages := map[string]bool{}

tools := []indexToolDependency{}
for _, t := range pr.ToolDependencies {
Expand All @@ -171,6 +161,7 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
Name: t.ToolName,
Version: t.ToolVersion,
})
requiredPackages[t.ToolPackager] = true
}

discoveries := []indexDiscoveryDependency{}
Expand All @@ -179,6 +170,7 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
Packager: d.Packager,
Name: d.Name,
})
requiredPackages[d.Packager] = true
}

monitors := []indexMonitorDependency{}
Expand All @@ -187,58 +179,98 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
Packager: m.Packager,
Name: m.Name,
})
requiredPackages[m.Packager] = true
}

packageTools := []*indexToolRelease{}
for name, tool := range pr.Platform.Package.Tools {
for _, toolRelease := range tool.Releases {
flavours := []indexToolReleaseFlavour{}
for _, flavour := range toolRelease.Flavors {
flavours = append(flavours, indexToolReleaseFlavour{
OS: flavour.OS,
URL: flavour.Resource.URL,
ArchiveFileName: flavour.Resource.ArchiveFileName,
Size: json.Number(fmt.Sprintf("%d", flavour.Resource.Size)),
Checksum: flavour.Resource.Checksum,
// Helper functions: those are needed to build an extract of the package_index.json
// that is compatible with the one used by the CLI.
// The installed.json is a simplified version of the cores.Packages
// and therefore we need to extract the relevant information from the
// cores.PlatformRelease and cores.Package structures.
extractIndexPackage := func(pack *cores.Package) *indexPackage {
packageTools := []*indexToolRelease{}
for name, tool := range pack.Tools {
for _, toolRelease := range tool.Releases {
flavours := []indexToolReleaseFlavour{}
for _, flavour := range toolRelease.Flavors {
flavours = append(flavours, indexToolReleaseFlavour{
OS: flavour.OS,
URL: flavour.Resource.URL,
ArchiveFileName: flavour.Resource.ArchiveFileName,
Size: json.Number(fmt.Sprintf("%d", flavour.Resource.Size)),
Checksum: flavour.Resource.Checksum,
})
}
packageTools = append(packageTools, &indexToolRelease{
Name: name,
Version: toolRelease.Version,
Systems: flavours,
})
}
packageTools = append(packageTools, &indexToolRelease{
Name: name,
Version: toolRelease.Version,
Systems: flavours,
})
}
return &indexPackage{
Name: pack.Name,
Maintainer: pack.Maintainer,
WebsiteURL: pack.WebsiteURL,
URL: pack.URL,
Email: pack.Email,
Platforms: nil,
Tools: packageTools,
Help: indexHelp{Online: pack.Help.Online},
}
}
extractIndexPlatformRelease := func(pr *cores.PlatformRelease) *indexPlatformRelease {
boards := []indexBoard{}
for _, manifest := range pr.BoardsManifest {
board := indexBoard{
Name: manifest.Name,
}
for _, id := range manifest.ID {
if id.USB != "" {
board.ID = []indexBoardID{{USB: id.USB}}
}
}
boards = append(boards, board)
}

return &indexPlatformRelease{
Name: pr.Name,
Architecture: pr.Platform.Architecture,
Version: pr.Version,
Deprecated: pr.Deprecated,
Category: pr.Category,
URL: pr.Resource.URL,
ArchiveFileName: pr.Resource.ArchiveFileName,
Checksum: pr.Resource.Checksum,
Size: json.Number(fmt.Sprintf("%d", pr.Resource.Size)),
Help: indexHelp{Online: pr.Help.Online},
Boards: boards,
ToolDependencies: nil,
DiscoveryDependencies: nil,
MonitorDependencies: nil,
}
}

mainPlatform := extractIndexPlatformRelease(pr)
mainPlatform.ToolDependencies = tools
mainPlatform.DiscoveryDependencies = discoveries
mainPlatform.MonitorDependencies = monitors
delete(requiredPackages, pr.Platform.Package.Name)

mainPackage := extractIndexPackage(pr.Platform.Package)
mainPackage.Platforms = []*indexPlatformRelease{mainPlatform}

packages := []*indexPackage{mainPackage}
for requiredPackageName := range requiredPackages {
requiredPackage, ok := pr.Platform.Package.Packages.GetPackage(requiredPackageName)
if ok {
packages = append(packages, extractIndexPackage(requiredPackage))
}
}

return Index{
IsTrusted: pr.IsTrusted,
Packages: []*indexPackage{
{
Name: pr.Platform.Package.Name,
Maintainer: pr.Platform.Package.Maintainer,
WebsiteURL: pr.Platform.Package.WebsiteURL,
URL: pr.Platform.Package.URL,
Email: pr.Platform.Package.Email,
Platforms: []*indexPlatformRelease{{
Name: pr.Name,
Architecture: pr.Platform.Architecture,
Version: pr.Version,
Deprecated: pr.Deprecated,
Category: pr.Category,
URL: pr.Resource.URL,
ArchiveFileName: pr.Resource.ArchiveFileName,
Checksum: pr.Resource.Checksum,
Size: json.Number(fmt.Sprintf("%d", pr.Resource.Size)),
Boards: boards,
Help: indexHelp{Online: pr.Help.Online},
ToolDependencies: tools,
DiscoveryDependencies: discoveries,
MonitorDependencies: monitors,
}},
Tools: packageTools,
Help: indexHelp{Online: pr.Platform.Package.Help.Online},
},
},
Packages: packages,
}
}

Expand Down
7 changes: 7 additions & 0 deletions internal/arduino/cores/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ type Package struct {
Packages Packages `json:"-"`
}

// GetPackage returns the specified Package if it exists
// and a boolean indicating whether it was found or not.
func (packages Packages) GetPackage(packager string) (*Package, bool) {
targetPackage, ok := packages[packager]
return targetPackage, ok
}

// GetOrCreatePackage returns the specified Package or creates an empty one
// filling all the cross-references
func (packages Packages) GetOrCreatePackage(packager string) *Package {
Expand Down
67 changes: 54 additions & 13 deletions internal/arduino/sketch/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ type Profile struct {
Libraries ProfileRequiredLibraries `yaml:"libraries"`
}

// UsesSystemPlatform checks if this profile requires a system installed platform.
func (p *Profile) RequireSystemInstalledPlatform() bool {
return p.Platforms[0].RequireSystemInstalledPlatform()
}

// ToRpc converts this Profile to an rpc.SketchProfile
func (p *Profile) ToRpc() *rpc.SketchProfile {
var portConfig *rpc.MonitorPortConfiguration
Expand Down Expand Up @@ -187,6 +192,9 @@ type ProfileRequiredLibraries []*ProfileLibraryReference

// AsYaml outputs the required libraries as Yaml
func (p *ProfileRequiredLibraries) AsYaml() string {
if len(*p) == 0 {
return ""
}
res := " libraries:\n"
for _, lib := range *p {
res += lib.AsYaml()
Expand All @@ -202,6 +210,12 @@ type ProfilePlatformReference struct {
PlatformIndexURL *url.URL
}

// RequireSystemInstalledPlatform returns true if the platform reference
// does not specify a version, meaning it requires the system installed platform.
func (p *ProfilePlatformReference) RequireSystemInstalledPlatform() bool {
return p.Version == nil
}

// InternalUniqueIdentifier returns the unique identifier for this object
func (p *ProfilePlatformReference) InternalUniqueIdentifier() string {
id := p.String()
Expand All @@ -220,20 +234,38 @@ func (p *ProfilePlatformReference) String() string {

// AsYaml outputs the platform reference as Yaml
func (p *ProfilePlatformReference) AsYaml() string {
res := fmt.Sprintf(" - platform: %s:%s (%s)\n", p.Packager, p.Architecture, p.Version)
res := ""
if p.Version != nil {
res += fmt.Sprintf(" - platform: %s:%s (%s)\n", p.Packager, p.Architecture, p.Version)
} else {
res += fmt.Sprintf(" - platform: %s:%s\n", p.Packager, p.Architecture)
}
if p.PlatformIndexURL != nil {
res += fmt.Sprintf(" platform_index_url: %s\n", p.PlatformIndexURL)
}
return res
}

func parseNameAndVersion(in string) (string, string, bool) {
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+) \((.+)\)$`)
split := re.FindAllStringSubmatch(in, -1)
if len(split) != 1 || len(split[0]) != 3 {
return "", "", false
{
// Try to parse the input string in the format "VENDOR:ARCH (VERSION)"
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+) \((.+)\)$`)
split := re.FindAllStringSubmatch(in, -1)
if len(split) == 1 && len(split[0]) == 3 {
return split[0][1], split[0][2], true
}
}

{
// Try to parse the input string in the format "VENDOR:ARCH"
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+)$`)
split := re.FindAllStringSubmatch(in, -1)
if len(split) == 1 && len(split[0]) == 2 {
return split[0][1], "", true
}
}
return split[0][1], split[0][2], true

return "", "", false
}

// UnmarshalYAML decodes a ProfilePlatformReference from YAML source.
Expand All @@ -246,14 +278,23 @@ func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) err
return errors.New(i18n.Tr("missing '%s' directive", "platform"))
} else if platformID, platformVersion, ok := parseNameAndVersion(platformID); !ok {
return errors.New(i18n.Tr("invalid '%s' directive", "platform"))
} else if c, err := semver.Parse(platformVersion); err != nil {
return fmt.Errorf("%s: %w", i18n.Tr("error parsing version constraints"), err)
} else if split := strings.SplitN(platformID, ":", 2); len(split) != 2 {
return fmt.Errorf("%s: %s", i18n.Tr("invalid platform identifier"), platformID)
} else {
p.Packager = split[0]
p.Architecture = split[1]
p.Version = c
var version *semver.Version
if platformVersion != "" {
if v, err := semver.Parse(platformVersion); err != nil {
return fmt.Errorf("%s: %w", i18n.Tr("error parsing version constraints"), err)
} else {
version = v
}
}

if split := strings.SplitN(platformID, ":", 2); len(split) != 2 {
return fmt.Errorf("%s: %s", i18n.Tr("invalid platform identifier"), platformID)
} else {
p.Packager = split[0]
p.Architecture = split[1]
p.Version = version
}
}

if rawIndexURL, ok := data["platform_index_url"]; ok {
Expand Down
11 changes: 8 additions & 3 deletions internal/arduino/sketch/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package sketch

import (
"fmt"
"testing"

"github.com/arduino/go-paths-helper"
Expand All @@ -28,7 +27,6 @@ func TestProjectFileLoading(t *testing.T) {
sketchProj := paths.New("testdata", "SketchWithProfiles", "sketch.yml")
proj, err := LoadProjectFile(sketchProj)
require.NoError(t, err)
fmt.Println(proj)
golden, err := sketchProj.ReadFile()
require.NoError(t, err)
require.Equal(t, proj.AsYaml(), string(golden))
Expand All @@ -37,9 +35,16 @@ func TestProjectFileLoading(t *testing.T) {
sketchProj := paths.New("testdata", "SketchWithDefaultFQBNAndPort", "sketch.yml")
proj, err := LoadProjectFile(sketchProj)
require.NoError(t, err)
fmt.Println(proj)
golden, err := sketchProj.ReadFile()
require.NoError(t, err)
require.Equal(t, proj.AsYaml(), string(golden))
}
{
sketchProj := paths.New("testdata", "profiles", "profile_1.yml")
proj, err := LoadProjectFile(sketchProj)
require.NoError(t, err)
golden, err := sketchProj.ReadFile()
require.NoError(t, err)
require.Equal(t, string(golden), proj.AsYaml())
}
}
12 changes: 12 additions & 0 deletions internal/arduino/sketch/testdata/profiles/profile_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
profiles:
giga:
fqbn: arduino:mbed_giga:giga
platforms:
- platform: arduino:mbed_giga (4.3.1)

giga_any:
fqbn: arduino:mbed_giga:giga
platforms:
- platform: arduino:mbed_giga

default_profile: giga_any
Loading