forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofiles.go
334 lines (304 loc) · 10.1 KB
/
profiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// This file is part of arduino-cli.
//
// Copyright 2020-2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package sketch
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
semver "go.bug.st/relaxed-semver"
"gopkg.in/yaml.v3"
)
// projectRaw is a support struct used only to unmarshal the yaml
type projectRaw struct {
ProfilesRaw yaml.Node `yaml:"profiles"`
DefaultProfile string `yaml:"default_profile"`
DefaultFqbn string `yaml:"default_fqbn"`
DefaultPort string `yaml:"default_port,omitempty"`
DefaultPortConfig map[string]string `yaml:"default_port_config,omitempty"`
DefaultProtocol string `yaml:"default_protocol,omitempty"`
DefaultProgrammer string `yaml:"default_programmer,omitempty"`
}
// Project represents the sketch project file
type Project struct {
Profiles []*Profile
DefaultProfile string
DefaultFqbn string
DefaultPort string
DefaultPortConfig map[string]string
DefaultProtocol string
DefaultProgrammer string
}
// AsYaml outputs the sketch project file as YAML
func (p *Project) AsYaml() string {
res := "profiles:\n"
for _, profile := range p.Profiles {
res += fmt.Sprintf(" %s:\n", profile.Name)
res += profile.AsYaml()
res += "\n"
}
if p.DefaultProfile != "" {
res += fmt.Sprintf("default_profile: %s\n", p.DefaultProfile)
}
if p.DefaultFqbn != "" {
res += fmt.Sprintf("default_fqbn: %s\n", p.DefaultFqbn)
}
if p.DefaultPort != "" {
res += fmt.Sprintf("default_port: %s\n", p.DefaultPort)
}
if len(p.DefaultPortConfig) > 0 {
res += "default_port_config:\n"
for k, v := range p.DefaultPortConfig {
res += fmt.Sprintf(" %s: %s\n", k, v)
}
}
if p.DefaultProtocol != "" {
res += fmt.Sprintf("default_protocol: %s\n", p.DefaultProtocol)
}
if p.DefaultProgrammer != "" {
res += fmt.Sprintf("default_programmer: %s\n", p.DefaultProgrammer)
}
return res
}
func (p *projectRaw) getProfiles() ([]*Profile, error) {
profiles := []*Profile{}
for i, node := range p.ProfilesRaw.Content {
if node.Tag != "!!str" {
continue // Node is a map, so it is read out at key.
}
var profile Profile
profile.Name = node.Value
if err := p.ProfilesRaw.Content[i+1].Decode(&profile); err != nil {
return nil, err
}
profiles = append(profiles, &profile)
}
return profiles, nil
}
// UnmarshalYAML decodes a Profiles section from YAML source.
// Profile is a sketch profile, it contains a reference to all the resources
// needed to build and upload a sketch
type Profile struct {
Name string
Notes string `yaml:"notes"`
FQBN string `yaml:"fqbn"`
Port string `yaml:"port"`
PortConfig map[string]string `yaml:"port_config"`
Protocol string `yaml:"protocol"`
Programmer string `yaml:"programmer"`
Platforms ProfileRequiredPlatforms `yaml:"platforms"`
Libraries ProfileRequiredLibraries `yaml:"libraries"`
}
// ToRpc converts this Profile to an rpc.SketchProfile
func (p *Profile) ToRpc() *rpc.SketchProfile {
var portConfig *rpc.MonitorPortConfiguration
if len(p.PortConfig) > 0 {
portConfig = &rpc.MonitorPortConfiguration{}
for k, v := range p.PortConfig {
portConfig.Settings = append(portConfig.Settings, &rpc.MonitorPortSetting{
SettingId: k,
Value: v,
})
}
}
return &rpc.SketchProfile{
Name: p.Name,
Fqbn: p.FQBN,
Programmer: p.Programmer,
Port: p.Port,
PortConfig: portConfig,
Protocol: p.Protocol,
}
}
// AsYaml outputs the profile as Yaml
func (p *Profile) AsYaml() string {
res := ""
if p.Notes != "" {
res += fmt.Sprintf(" notes: %s\n", p.Notes)
}
res += fmt.Sprintf(" fqbn: %s\n", p.FQBN)
if p.Programmer != "" {
res += fmt.Sprintf(" programmer: %s\n", p.Programmer)
}
if p.Port != "" {
res += fmt.Sprintf(" port: %s\n", p.Port)
}
if p.Protocol != "" {
res += fmt.Sprintf(" protocol: %s\n", p.Protocol)
}
if len(p.PortConfig) > 0 {
res += " port_config:\n"
for k, v := range p.PortConfig {
res += fmt.Sprintf(" %s: %s\n", k, v)
}
}
res += p.Platforms.AsYaml()
res += p.Libraries.AsYaml()
return res
}
// ProfileRequiredPlatforms is a list of ProfilePlatformReference (platforms
// required to build the sketch using this profile)
type ProfileRequiredPlatforms []*ProfilePlatformReference
// AsYaml outputs the required platforms as Yaml
func (p *ProfileRequiredPlatforms) AsYaml() string {
res := " platforms:\n"
for _, platform := range *p {
res += platform.AsYaml()
}
return res
}
// ProfileRequiredLibraries is a list of ProfileLibraryReference (libraries
// required to build the sketch using this profile)
type ProfileRequiredLibraries []*ProfileLibraryReference
// AsYaml outputs the required libraries as Yaml
func (p *ProfileRequiredLibraries) AsYaml() string {
res := " libraries:\n"
for _, lib := range *p {
res += lib.AsYaml()
}
return res
}
// ProfilePlatformReference is a reference to a platform
type ProfilePlatformReference struct {
Packager string
Architecture string
Version *semver.Version
PlatformIndexURL *url.URL
}
// InternalUniqueIdentifier returns the unique identifier for this object
func (p *ProfilePlatformReference) InternalUniqueIdentifier() string {
id := p.String()
h := sha256.Sum256([]byte(id))
res := fmt.Sprintf("%s:%s@%s_%s", p.Packager, p.Architecture, p.Version, hex.EncodeToString(h[:])[:16])
return utils.SanitizeName(res)
}
func (p *ProfilePlatformReference) String() string {
res := fmt.Sprintf("%s:%s@%s", p.Packager, p.Architecture, p.Version)
if p.PlatformIndexURL != nil {
res += fmt.Sprintf(" (%s)", p.PlatformIndexURL)
}
return res
}
// 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)
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
}
return split[0][1], split[0][2], true
}
// UnmarshalYAML decodes a ProfilePlatformReference from YAML source.
func (p *ProfilePlatformReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data map[string]string
if err := unmarshal(&data); err != nil {
return err
}
if platformID, ok := data["platform"]; !ok {
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
}
if rawIndexURL, ok := data["platform_index_url"]; ok {
indexURL, err := url.Parse(rawIndexURL)
if err != nil {
return fmt.Errorf("%s: %w", i18n.Tr("invalid platform index URL:"), err)
}
p.PlatformIndexURL = indexURL
}
return nil
}
// ProfileLibraryReference is a reference to a library
type ProfileLibraryReference struct {
Library string
Version *semver.Version
}
// UnmarshalYAML decodes a ProfileLibraryReference from YAML source.
func (l *ProfileLibraryReference) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data string
if err := unmarshal(&data); err != nil {
return err
}
if libName, libVersion, ok := parseNameAndVersion(data); !ok {
return fmt.Errorf("%s %s", i18n.Tr("invalid library directive:"), data)
} else if v, err := semver.Parse(libVersion); err != nil {
return fmt.Errorf("%s %w", i18n.Tr("invalid version:"), err)
} else {
l.Library = libName
l.Version = v
}
return nil
}
// AsYaml outputs the required library as Yaml
func (l *ProfileLibraryReference) AsYaml() string {
res := fmt.Sprintf(" - %s (%s)\n", l.Library, l.Version)
return res
}
func (l *ProfileLibraryReference) String() string {
return fmt.Sprintf("%s@%s", l.Library, l.Version)
}
// InternalUniqueIdentifier returns the unique identifier for this object
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
id := l.String()
h := sha256.Sum256([]byte(id))
res := fmt.Sprintf("%s_%s", id, hex.EncodeToString(h[:])[:16])
return utils.SanitizeName(res)
}
// LoadProjectFile reads a sketch project file
func LoadProjectFile(file *paths.Path) (*Project, error) {
data, err := file.ReadFile()
if err != nil {
return nil, err
}
raw := &projectRaw{}
if err := yaml.Unmarshal(data, &raw); err != nil {
return nil, err
}
profiles, err := raw.getProfiles()
if err != nil {
return nil, err
}
return &Project{
Profiles: profiles,
DefaultProfile: raw.DefaultProfile,
DefaultFqbn: raw.DefaultFqbn,
DefaultPort: raw.DefaultPort,
DefaultPortConfig: raw.DefaultPortConfig,
DefaultProtocol: raw.DefaultProtocol,
DefaultProgrammer: raw.DefaultProgrammer,
}, nil
}