-
-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not fully rewrite sketch.yaml when a key is automatically updated
- Loading branch information
Showing
6 changed files
with
156 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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 license@arduino.cc. | ||
|
||
package sketch | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func updateOrAddYamlRootEntry(path *paths.Path, key, newValue string) error { | ||
var srcYaml []string | ||
if path.Exist() { | ||
src, err := path.ReadFileAsLines() | ||
if err != nil { | ||
return err | ||
} | ||
srcYaml = src | ||
} | ||
|
||
// Generate the new yaml key/value pair | ||
v, err := yaml.Marshal(newValue) | ||
if err != nil { | ||
return err | ||
} | ||
updatedLine := key + ": " + strings.TrimSpace(string(v)) | ||
|
||
// Update or add the key/value pair into the original yaml | ||
addMissing := true | ||
for i, line := range srcYaml { | ||
if strings.HasPrefix(line, key+": ") { | ||
srcYaml[i] = updatedLine | ||
addMissing = false | ||
break | ||
} | ||
} | ||
if addMissing { | ||
srcYaml = append(srcYaml, updatedLine) | ||
} | ||
|
||
// Validate the new yaml | ||
dstYaml := []byte(strings.Join(srcYaml, fmt.Sprintln())) | ||
var dst interface{} | ||
if err := yaml.Unmarshal(dstYaml, &dst); err != nil { | ||
return fmt.Errorf("%s: %w", tr("could not update sketch project file"), err) | ||
} | ||
if dstMap, ok := dst.(map[string]interface{}); !ok || dstMap[key] != newValue { | ||
return fmt.Errorf(tr("could not update sketch project file")) | ||
} | ||
|
||
// Write back the updated YAML | ||
return path.WriteFile(dstYaml) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 license@arduino.cc. | ||
|
||
package sketch | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/arduino/go-paths-helper" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestYamlUpdate(t *testing.T) { | ||
{ | ||
sample, err := paths.New("testdata", "SketchWithProfiles", "sketch.yml").ReadFile() | ||
require.NoError(t, err) | ||
tmp, err := paths.WriteToTempFile(sample, nil, "") | ||
require.NoError(t, err) | ||
defer tmp.Remove() | ||
|
||
err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "arduino:avr:uno") | ||
require.NoError(t, err) | ||
err = updateOrAddYamlRootEntry(tmp, "default_port", "/dev/ttyACM0") | ||
require.NoError(t, err) | ||
|
||
updated, err := tmp.ReadFile() | ||
require.NoError(t, err) | ||
expected := string(sample) + fmt.Sprintln() | ||
expected += fmt.Sprintln("default_fqbn: arduino:avr:uno") | ||
expected += "default_port: /dev/ttyACM0" | ||
require.Equal(t, expected, string(updated)) | ||
} | ||
{ | ||
sample, err := paths.New("testdata", "SketchWithDefaultFQBNAndPort", "sketch.yml").ReadFile() | ||
require.NoError(t, err) | ||
tmp, err := paths.WriteToTempFile(sample, nil, "") | ||
require.NoError(t, err) | ||
defer tmp.Remove() | ||
|
||
err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "TEST1") | ||
require.NoError(t, err) | ||
err = updateOrAddYamlRootEntry(tmp, "default_port", "TEST2") | ||
require.NoError(t, err) | ||
|
||
updated, err := tmp.ReadFile() | ||
fmt.Print(string(updated)) | ||
require.NoError(t, err) | ||
expected := strings.Replace(string(sample), "arduino:avr:uno", "TEST1", 1) | ||
expected = strings.Replace(expected, "/dev/ttyACM0", "TEST2", 1) | ||
require.Equal(t, expected, string(updated)) | ||
} | ||
{ | ||
tmp, err := paths.WriteToTempFile([]byte{}, nil, "") | ||
require.NoError(t, err) | ||
require.NoError(t, tmp.Remove()) | ||
err = updateOrAddYamlRootEntry(tmp, "default_fqbn", "TEST1") | ||
require.NoError(t, err) | ||
|
||
updated, err := tmp.ReadFile() | ||
require.NoError(t, err) | ||
expected := "default_fqbn: TEST1" | ||
require.Equal(t, expected, string(updated)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters