Skip to content

Commit 69761fb

Browse files
committed
Allow platforms without fixed version in profiles.
1 parent 0fd6e54 commit 69761fb

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

commands/instances.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
243243
}
244244

245245
// Load Platforms
246-
if profile == nil {
246+
if profile == nil || profile.RequireSystemInstalledPlatform() {
247247
for _, err := range pmb.LoadHardware() {
248248
s := &cmderrors.PlatformLoadingError{Cause: err}
249249
responseError(s.GRPCStatus())

internal/arduino/cores/status.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ type Package struct {
5050
Packages Packages `json:"-"`
5151
}
5252

53+
// GetPackage returns the specified Package if it exists
54+
// and a boolean indicating whether it was found or not.
55+
func (packages Packages) GetPackage(packager string) (*Package, bool) {
56+
targetPackage, ok := packages[packager]
57+
return targetPackage, ok
58+
}
59+
5360
// GetOrCreatePackage returns the specified Package or creates an empty one
5461
// filling all the cross-references
5562
func (packages Packages) GetOrCreatePackage(packager string) *Package {

internal/arduino/sketch/profiles.go

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ type Profile struct {
119119
Libraries ProfileRequiredLibraries `yaml:"libraries"`
120120
}
121121

122+
// UsesSystemPlatform checks if this profile requires a system installed platform.
123+
func (p *Profile) RequireSystemInstalledPlatform() bool {
124+
return p.Platforms[0].RequireSystemInstalledPlatform()
125+
}
126+
122127
// ToRpc converts this Profile to an rpc.SketchProfile
123128
func (p *Profile) ToRpc() *rpc.SketchProfile {
124129
var portConfig *rpc.MonitorPortConfiguration
@@ -205,6 +210,12 @@ type ProfilePlatformReference struct {
205210
PlatformIndexURL *url.URL
206211
}
207212

213+
// RequireSystemInstalledPlatform returns true if the platform reference
214+
// does not specify a version, meaning it requires the system installed platform.
215+
func (p *ProfilePlatformReference) RequireSystemInstalledPlatform() bool {
216+
return p.Version == nil
217+
}
218+
208219
// InternalUniqueIdentifier returns the unique identifier for this object
209220
func (p *ProfilePlatformReference) InternalUniqueIdentifier() string {
210221
id := p.String()
@@ -223,20 +234,38 @@ func (p *ProfilePlatformReference) String() string {
223234

224235
// AsYaml outputs the platform reference as Yaml
225236
func (p *ProfilePlatformReference) AsYaml() string {
226-
res := fmt.Sprintf(" - platform: %s:%s (%s)\n", p.Packager, p.Architecture, p.Version)
237+
res := ""
238+
if p.Version != nil {
239+
res += fmt.Sprintf(" - platform: %s:%s (%s)\n", p.Packager, p.Architecture, p.Version)
240+
} else {
241+
res += fmt.Sprintf(" - platform: %s:%s\n", p.Packager, p.Architecture)
242+
}
227243
if p.PlatformIndexURL != nil {
228244
res += fmt.Sprintf(" platform_index_url: %s\n", p.PlatformIndexURL)
229245
}
230246
return res
231247
}
232248

233249
func parseNameAndVersion(in string) (string, string, bool) {
234-
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+) \((.+)\)$`)
235-
split := re.FindAllStringSubmatch(in, -1)
236-
if len(split) != 1 || len(split[0]) != 3 {
237-
return "", "", false
250+
{
251+
// Try to parse the input string in the format "VENDOR:ARCH (VERSION)"
252+
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+) \((.+)\)$`)
253+
split := re.FindAllStringSubmatch(in, -1)
254+
if len(split) == 1 && len(split[0]) == 3 {
255+
return split[0][1], split[0][2], true
256+
}
238257
}
239-
return split[0][1], split[0][2], true
258+
259+
{
260+
// Try to parse the input string in the format "VENDOR:ARCH"
261+
re := regexp.MustCompile(`^([a-zA-Z0-9.\-_ :]+)$`)
262+
split := re.FindAllStringSubmatch(in, -1)
263+
if len(split) == 1 && len(split[0]) == 2 {
264+
return split[0][1], "", true
265+
}
266+
}
267+
268+
return "", "", false
240269
}
241270

242271
// UnmarshalYAML decodes a ProfilePlatformReference from YAML source.
@@ -249,14 +278,23 @@ func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) err
249278
return errors.New(i18n.Tr("missing '%s' directive", "platform"))
250279
} else if platformID, platformVersion, ok := parseNameAndVersion(platformID); !ok {
251280
return errors.New(i18n.Tr("invalid '%s' directive", "platform"))
252-
} else if c, err := semver.Parse(platformVersion); err != nil {
253-
return fmt.Errorf("%s: %w", i18n.Tr("error parsing version constraints"), err)
254-
} else if split := strings.SplitN(platformID, ":", 2); len(split) != 2 {
255-
return fmt.Errorf("%s: %s", i18n.Tr("invalid platform identifier"), platformID)
256281
} else {
257-
p.Packager = split[0]
258-
p.Architecture = split[1]
259-
p.Version = c
282+
var version *semver.Version
283+
if platformVersion != "" {
284+
if v, err := semver.Parse(platformVersion); err != nil {
285+
return fmt.Errorf("%s: %w", i18n.Tr("error parsing version constraints"), err)
286+
} else {
287+
version = v
288+
}
289+
}
290+
291+
if split := strings.SplitN(platformID, ":", 2); len(split) != 2 {
292+
return fmt.Errorf("%s: %s", i18n.Tr("invalid platform identifier"), platformID)
293+
} else {
294+
p.Packager = split[0]
295+
p.Architecture = split[1]
296+
p.Version = version
297+
}
260298
}
261299

262300
if rawIndexURL, ok := data["platform_index_url"]; ok {

internal/arduino/sketch/profiles_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package sketch
1717

1818
import (
19-
"fmt"
2019
"testing"
2120

2221
"github.com/arduino/go-paths-helper"
@@ -28,7 +27,6 @@ func TestProjectFileLoading(t *testing.T) {
2827
sketchProj := paths.New("testdata", "SketchWithProfiles", "sketch.yml")
2928
proj, err := LoadProjectFile(sketchProj)
3029
require.NoError(t, err)
31-
fmt.Println(proj)
3230
golden, err := sketchProj.ReadFile()
3331
require.NoError(t, err)
3432
require.Equal(t, proj.AsYaml(), string(golden))
@@ -37,9 +35,16 @@ func TestProjectFileLoading(t *testing.T) {
3735
sketchProj := paths.New("testdata", "SketchWithDefaultFQBNAndPort", "sketch.yml")
3836
proj, err := LoadProjectFile(sketchProj)
3937
require.NoError(t, err)
40-
fmt.Println(proj)
4138
golden, err := sketchProj.ReadFile()
4239
require.NoError(t, err)
4340
require.Equal(t, proj.AsYaml(), string(golden))
4441
}
42+
{
43+
sketchProj := paths.New("testdata", "profiles", "profile_1.yml")
44+
proj, err := LoadProjectFile(sketchProj)
45+
require.NoError(t, err)
46+
golden, err := sketchProj.ReadFile()
47+
require.NoError(t, err)
48+
require.Equal(t, string(golden), proj.AsYaml())
49+
}
4550
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
profiles:
2+
giga:
3+
fqbn: arduino:mbed_giga:giga
4+
platforms:
5+
- platform: arduino:mbed_giga (4.3.1)
6+
7+
giga_any:
8+
fqbn: arduino:mbed_giga:giga
9+
platforms:
10+
- platform: arduino:mbed_giga
11+
12+
default_profile: giga_any

0 commit comments

Comments
 (0)