|
16 | 16 | package sketch
|
17 | 17 |
|
18 | 18 | import (
|
19 |
| - "errors" |
20 |
| - "fmt" |
21 |
| - "strings" |
22 |
| - |
23 |
| - "github.com/arduino/arduino-cli/internal/i18n" |
24 | 19 | "github.com/arduino/go-paths-helper"
|
25 |
| - "gopkg.in/yaml.v3" |
| 20 | + "github.com/goccy/go-yaml" |
| 21 | + "github.com/goccy/go-yaml/parser" |
| 22 | + "go.bug.st/f" |
26 | 23 | )
|
27 | 24 |
|
28 | 25 | // updateOrAddYamlRootEntry updates or adds a new entry to the root of the yaml file.
|
29 | 26 | // If the value is empty the entry is removed.
|
30 |
| -func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error { |
31 |
| - var srcYaml []string |
32 |
| - if path.Exist() { |
33 |
| - src, err := path.ReadFileAsLines() |
34 |
| - if err != nil { |
35 |
| - return err |
36 |
| - } |
37 |
| - lastLine := len(src) - 1 |
38 |
| - if lastLine > 0 && src[lastLine] == "" { |
39 |
| - srcYaml = src[:lastLine] |
40 |
| - } else { |
41 |
| - srcYaml = src |
42 |
| - } |
43 |
| - } |
44 |
| - |
45 |
| - // Generate the new yaml key/value pair |
46 |
| - v, err := yaml.Marshal(newValue) |
| 27 | +func updateOrAddYamlRootEntry(srcPath *paths.Path, key, newValue string) error { |
| 28 | + // First encode the new value as YAML and parse it to an AST |
| 29 | + newValueYaml, err := yaml.Marshal(map[string]string{key: newValue}) |
47 | 30 | if err != nil {
|
48 | 31 | return err
|
49 | 32 | }
|
50 |
| - updatedLine := key + ": " + strings.TrimSpace(string(v)) |
| 33 | + newValueAst := f.Must(parser.ParseBytes(newValueYaml, parser.ParseComments)) |
51 | 34 |
|
52 |
| - // Update or add the key/value pair into the original yaml |
53 |
| - addMissing := (newValue != "") |
54 |
| - for i, line := range srcYaml { |
55 |
| - if strings.HasPrefix(line, key+": ") { |
56 |
| - if newValue == "" { |
57 |
| - // Remove the key/value pair |
58 |
| - srcYaml = append(srcYaml[:i], srcYaml[i+1:]...) |
59 |
| - } else { |
60 |
| - // Update the key/value pair |
61 |
| - srcYaml[i] = updatedLine |
62 |
| - } |
63 |
| - addMissing = false |
64 |
| - break |
65 |
| - } |
| 35 | + // If the src file does not exist, we can just write the new value |
| 36 | + if !srcPath.Exist() { |
| 37 | + return srcPath.WriteFile(newValueYaml) |
| 38 | + } |
| 39 | + |
| 40 | + // Read the source YAML file and parse it to an AST |
| 41 | + srcYaml, err := srcPath.ReadFile() |
| 42 | + if err != nil { |
| 43 | + return err |
66 | 44 | }
|
67 |
| - if addMissing { |
68 |
| - lastLine := len(srcYaml) - 1 |
69 |
| - if lastLine >= 0 && srcYaml[lastLine] == "" { |
70 |
| - srcYaml[lastLine] = updatedLine |
71 |
| - } else { |
72 |
| - srcYaml = append(srcYaml, updatedLine) |
73 |
| - } |
| 45 | + srcAst, err := parser.ParseBytes(srcYaml, parser.ParseComments) |
| 46 | + if err != nil { |
| 47 | + return err |
74 | 48 | }
|
75 | 49 |
|
76 |
| - // Validate the new yaml |
77 |
| - dstYaml := []byte(strings.Join(srcYaml, fmt.Sprintln()) + fmt.Sprintln()) |
78 |
| - var dst interface{} |
79 |
| - if err := yaml.Unmarshal(dstYaml, &dst); err != nil { |
80 |
| - return fmt.Errorf("%s: %w", i18n.Tr("could not update sketch project file"), err) |
| 50 | + // Perform the merge operation |
| 51 | + keyYmlPath, err := yaml.PathString("$") |
| 52 | + if err != nil { |
| 53 | + return err |
81 | 54 | }
|
82 |
| - dstMap, ok := dst.(map[string]interface{}) |
83 |
| - if !ok { |
84 |
| - return errors.New(i18n.Tr("could not update sketch project file")) |
| 55 | + if n, _ := keyYmlPath.FilterFile(srcAst); n == nil { |
| 56 | + // In this case the file is empty, we can just write the new value at the bottom |
| 57 | + srcYaml = append(srcYaml, '\n') |
| 58 | + srcYaml = append(srcYaml, newValueYaml...) |
| 59 | + return srcPath.WriteFile(srcYaml) |
85 | 60 | }
|
86 |
| - writtenValue, notRemoved := dstMap[key] |
87 |
| - if (newValue == "" && notRemoved) || (newValue != "" && newValue != writtenValue) { |
88 |
| - return errors.New(i18n.Tr("could not update sketch project file")) |
| 61 | + if err := keyYmlPath.MergeFromFile(srcAst, newValueAst); err != nil { |
| 62 | + return err |
89 | 63 | }
|
90 | 64 |
|
91 | 65 | // Write back the updated YAML
|
92 |
| - return path.WriteFile(dstYaml) |
| 66 | + return srcPath.WriteFile([]byte(srcAst.String())) |
93 | 67 | }
|
0 commit comments