Skip to content

Commit ef23177

Browse files
committed
Allow platforms without fixed version in profiles.
1 parent 3dca438 commit ef23177

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
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/sketch/profiles.go

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

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

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

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

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

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

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

internal/arduino/sketch/profiles_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ func TestProjectFileLoading(t *testing.T) {
3939
require.NoError(t, err)
4040
require.Equal(t, proj.AsYaml(), string(golden))
4141
}
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+
}
4250
}
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)