Skip to content

Commit e20117f

Browse files
committed
Preserve profiles ordering in profiles.yaml
1 parent 8203266 commit e20117f

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Diff for: arduino/sketch/profiles.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import (
3131

3232
// Project represents all the profiles defined for the sketch
3333
type Project struct {
34-
Profiles map[string]*Profile `yaml:"profiles"`
35-
DefaultProfile string `yaml:"default_profile"`
34+
Profiles Profiles `yaml:"profiles"`
35+
DefaultProfile string `yaml:"default_profile"`
3636
}
3737

3838
// AsYaml outputs the project file as Yaml
3939
func (p *Project) AsYaml() string {
4040
res := "profiles:\n"
41-
for name, profile := range p.Profiles {
42-
res += fmt.Sprintf(" %s:\n", name)
41+
for _, profile := range p.Profiles {
42+
res += fmt.Sprintf(" %s:\n", profile.Name)
4343
res += profile.AsYaml()
4444
res += "\n"
4545
}
@@ -49,9 +49,38 @@ func (p *Project) AsYaml() string {
4949
return res
5050
}
5151

52+
// Profiles are a list of Profile
53+
type Profiles []*Profile
54+
55+
// UnmarshalYAML decodes a Profiles section from YAML source.
56+
func (p *Profiles) UnmarshalYAML(unmarshal func(interface{}) error) error {
57+
unmarshaledProfiles := map[string]*Profile{}
58+
if err := unmarshal(&unmarshaledProfiles); err != nil {
59+
return err
60+
}
61+
62+
var profilesData yaml.MapSlice
63+
if err := unmarshal(&profilesData); err != nil {
64+
return err
65+
}
66+
67+
for _, profileData := range profilesData {
68+
profileName, ok := profileData.Key.(string)
69+
if !ok {
70+
return fmt.Errorf("invalid profile name: %v", profileData.Key)
71+
}
72+
profile := unmarshaledProfiles[profileName]
73+
profile.Name = profileName
74+
*p = append(*p, profile)
75+
}
76+
77+
return nil
78+
}
79+
5280
// Profile is a sketch profile, it contains a reference to all the resources
5381
// needed to build and upload a sketch
5482
type Profile struct {
83+
Name string
5584
Notes string `yaml:"notes"`
5685
FQBN string `yaml:"fqbn"`
5786
Platforms ProfileRequiredPlatforms `yaml:"platforms"`

Diff for: arduino/sketch/sketch.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,12 @@ func (s *Sketch) ExportMetadata() error {
234234
// GetProfile returns the requested profile or nil if the profile
235235
// is not found.
236236
func (s *Sketch) GetProfile(profileName string) *Profile {
237-
return s.Project.Profiles[profileName]
237+
for _, p := range s.Project.Profiles {
238+
if p.Name == profileName {
239+
return p
240+
}
241+
}
242+
return nil
238243
}
239244

240245
// checkSketchCasing returns an error if the casing of the sketch folder and the main file are different.

0 commit comments

Comments
 (0)