-
-
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
7 changed files
with
233 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
name: gopkg.in/yaml.v3 | ||
version: v3.0.1 | ||
type: go | ||
summary: Package yaml implements YAML support for the Go language. | ||
homepage: https://pkg.go.dev/gopkg.in/yaml.v3 | ||
license: other | ||
licenses: | ||
- sources: LICENSE | ||
text: |2 | ||
This project is covered by two different licenses: MIT and Apache. | ||
#### MIT License #### | ||
The following files were ported to Go from C files of libyaml, and thus | ||
are still covered by their original MIT license, with the additional | ||
copyright staring in 2011 when the project was ported over: | ||
apic.go emitterc.go parserc.go readerc.go scannerc.go | ||
writerc.go yamlh.go yamlprivateh.go | ||
Copyright (c) 2006-2010 Kirill Simonov | ||
Copyright (c) 2006-2011 Kirill Simonov | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
### Apache License ### | ||
All the remaining project files are covered by the Apache license: | ||
Copyright (c) 2011-2019 Canonical Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
- sources: README.md | ||
text: |- | ||
The yaml package is licensed under the MIT and Apache License 2.0 licenses. | ||
Please see the LICENSE file for details. | ||
notices: | ||
- sources: NOTICE | ||
text: |- | ||
Copyright 2011-2016 Canonical Ltd. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
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
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