From 302ff5f936e88e3819d3bf7fe6dbedf9d1f18e86 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 11:32:40 +0200 Subject: [PATCH 1/4] Removed unused class --- legacy/builder/types/accessories.go | 34 ----------------------------- 1 file changed, 34 deletions(-) diff --git a/legacy/builder/types/accessories.go b/legacy/builder/types/accessories.go index b0c85d37e20..626380e89a0 100644 --- a/legacy/builder/types/accessories.go +++ b/legacy/builder/types/accessories.go @@ -15,11 +15,6 @@ package types -import ( - "bytes" - "sync" -) - type UniqueStringQueue []string func (queue UniqueStringQueue) Len() int { return len(queue) } @@ -65,32 +60,3 @@ func (queue *UniqueSourceFileQueue) Pop() SourceFile { func (queue *UniqueSourceFileQueue) Empty() bool { return queue.Len() == 0 } - -type BufferedUntilNewLineWriter struct { - PrintFunc PrintFunc - Buffer bytes.Buffer - lock sync.Mutex -} - -type PrintFunc func([]byte) - -func (w *BufferedUntilNewLineWriter) Write(p []byte) (n int, err error) { - w.lock.Lock() - defer w.lock.Unlock() - - writtenToBuffer, err := w.Buffer.Write(p) - return writtenToBuffer, err -} - -func (w *BufferedUntilNewLineWriter) Flush() { - w.lock.Lock() - defer w.lock.Unlock() - - remainingBytes := w.Buffer.Bytes() - if len(remainingBytes) > 0 { - if remainingBytes[len(remainingBytes)-1] != '\n' { - remainingBytes = append(remainingBytes, '\n') - } - w.PrintFunc(remainingBytes) - } -} From 68e08c37fec3b084042f3ba227bb4fe99f226c7e Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 12:01:27 +0200 Subject: [PATCH 2/4] Replaced two accessory functions with stdlib functions --- .../go/golang.org/x/exp/constraints.dep.yml | 63 +++++++++++++++++++ .licenses/go/golang.org/x/exp/slices.dep.yml | 62 ++++++++++++++++++ legacy/builder/types/accessories.go | 9 ++- legacy/builder/types/utils.go | 35 ----------- 4 files changed, 132 insertions(+), 37 deletions(-) create mode 100644 .licenses/go/golang.org/x/exp/constraints.dep.yml create mode 100644 .licenses/go/golang.org/x/exp/slices.dep.yml delete mode 100644 legacy/builder/types/utils.go diff --git a/.licenses/go/golang.org/x/exp/constraints.dep.yml b/.licenses/go/golang.org/x/exp/constraints.dep.yml new file mode 100644 index 00000000000..14cd0ef4868 --- /dev/null +++ b/.licenses/go/golang.org/x/exp/constraints.dep.yml @@ -0,0 +1,63 @@ +--- +name: golang.org/x/exp/constraints +version: v0.0.0-20230321023759-10a507213a29 +type: go +summary: Package constraints defines a set of useful constraints to be used with type + parameters. +homepage: https://pkg.go.dev/golang.org/x/exp/constraints +license: bsd-3-clause +licenses: +- sources: exp@v0.0.0-20230321023759-10a507213a29/LICENSE + text: | + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +- sources: exp@v0.0.0-20230321023759-10a507213a29/PATENTS + text: | + Additional IP Rights Grant (Patents) + + "This implementation" means the copyrightable works distributed by + Google as part of the Go project. + + Google hereby grants to You a perpetual, worldwide, non-exclusive, + no-charge, royalty-free, irrevocable (except as stated in this section) + patent license to make, have made, use, offer to sell, sell, import, + transfer and otherwise run, modify and propagate the contents of this + implementation of Go, where such license applies only to those patent + claims, both currently owned or controlled by Google and acquired in + the future, licensable by Google that are necessarily infringed by this + implementation of Go. This grant does not include claims that would be + infringed only as a consequence of further modification of this + implementation. If you or your agent or exclusive licensee institute or + order or agree to the institution of patent litigation against any + entity (including a cross-claim or counterclaim in a lawsuit) alleging + that this implementation of Go or any code incorporated within this + implementation of Go constitutes direct or contributory patent + infringement, or inducement of patent infringement, then any patent + rights granted to you under this License for this implementation of Go + shall terminate as of the date such litigation is filed. +notices: [] diff --git a/.licenses/go/golang.org/x/exp/slices.dep.yml b/.licenses/go/golang.org/x/exp/slices.dep.yml new file mode 100644 index 00000000000..8fca8863335 --- /dev/null +++ b/.licenses/go/golang.org/x/exp/slices.dep.yml @@ -0,0 +1,62 @@ +--- +name: golang.org/x/exp/slices +version: v0.0.0-20230321023759-10a507213a29 +type: go +summary: Package slices defines various functions useful with slices of any type. +homepage: https://pkg.go.dev/golang.org/x/exp/slices +license: bsd-3-clause +licenses: +- sources: exp@v0.0.0-20230321023759-10a507213a29/LICENSE + text: | + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +- sources: exp@v0.0.0-20230321023759-10a507213a29/PATENTS + text: | + Additional IP Rights Grant (Patents) + + "This implementation" means the copyrightable works distributed by + Google as part of the Go project. + + Google hereby grants to You a perpetual, worldwide, non-exclusive, + no-charge, royalty-free, irrevocable (except as stated in this section) + patent license to make, have made, use, offer to sell, sell, import, + transfer and otherwise run, modify and propagate the contents of this + implementation of Go, where such license applies only to those patent + claims, both currently owned or controlled by Google and acquired in + the future, licensable by Google that are necessarily infringed by this + implementation of Go. This grant does not include claims that would be + infringed only as a consequence of further modification of this + implementation. If you or your agent or exclusive licensee institute or + order or agree to the institution of patent litigation against any + entity (including a cross-claim or counterclaim in a lawsuit) alleging + that this implementation of Go or any code incorporated within this + implementation of Go constitutes direct or contributory patent + infringement, or inducement of patent infringement, then any patent + rights granted to you under this License for this implementation of Go + shall terminate as of the date such litigation is filed. +notices: [] diff --git a/legacy/builder/types/accessories.go b/legacy/builder/types/accessories.go index 626380e89a0..a110977dde5 100644 --- a/legacy/builder/types/accessories.go +++ b/legacy/builder/types/accessories.go @@ -15,6 +15,8 @@ package types +import "golang.org/x/exp/slices" + type UniqueStringQueue []string func (queue UniqueStringQueue) Len() int { return len(queue) } @@ -22,7 +24,7 @@ func (queue UniqueStringQueue) Less(i, j int) bool { return false } func (queue UniqueStringQueue) Swap(i, j int) { panic("Who called me?!?") } func (queue *UniqueStringQueue) Push(value string) { - if !sliceContains(*queue, value) { + if !slices.Contains(*queue, value) { *queue = append(*queue, value) } } @@ -45,7 +47,10 @@ func (queue UniqueSourceFileQueue) Less(i, j int) bool { return false } func (queue UniqueSourceFileQueue) Swap(i, j int) { panic("Who called me?!?") } func (queue *UniqueSourceFileQueue) Push(value SourceFile) { - if !sliceContainsSourceFile(*queue, value) { + equals := func(elem SourceFile) bool { + return elem.Origin == value.Origin && elem.RelativePath.EqualsTo(value.RelativePath) + } + if !slices.ContainsFunc(*queue, equals) { *queue = append(*queue, value) } } diff --git a/legacy/builder/types/utils.go b/legacy/builder/types/utils.go deleted file mode 100644 index 7102cd555ca..00000000000 --- a/legacy/builder/types/utils.go +++ /dev/null @@ -1,35 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 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 types - -// duplication of utils.SliceContains! Thanks golang! Why? Because with golang you can't have import cycles -func sliceContains(slice []string, target string) bool { - for _, value := range slice { - if value == target { - return true - } - } - return false -} - -func sliceContainsSourceFile(slice []SourceFile, target SourceFile) bool { - for _, elem := range slice { - if elem.Origin == target.Origin && elem.RelativePath.EqualsTo(target.RelativePath) { - return true - } - } - return false -} From 5d5cd18b6459361659ee717610ac0385c4c6beb4 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 12:08:10 +0200 Subject: [PATCH 3/4] Removed unused structs --- .../builder/test/unique_string_queue_test.go | 34 ------------------- legacy/builder/types/accessories.go | 23 ------------- legacy/builder/types/types.go | 32 ----------------- 3 files changed, 89 deletions(-) delete mode 100644 legacy/builder/test/unique_string_queue_test.go diff --git a/legacy/builder/test/unique_string_queue_test.go b/legacy/builder/test/unique_string_queue_test.go deleted file mode 100644 index 20158792a53..00000000000 --- a/legacy/builder/test/unique_string_queue_test.go +++ /dev/null @@ -1,34 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 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 test - -import ( - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/stretchr/testify/require" - "testing" -) - -func TestUniqueStringQueue(t *testing.T) { - queue := types.UniqueStringQueue{} - queue.Push("hello") - queue.Push("world") - queue.Push("hello") - queue.Push("world") - - require.Equal(t, "hello", queue.Pop()) - require.Equal(t, "world", queue.Pop()) - require.True(t, queue.Empty()) -} diff --git a/legacy/builder/types/accessories.go b/legacy/builder/types/accessories.go index a110977dde5..8decf206162 100644 --- a/legacy/builder/types/accessories.go +++ b/legacy/builder/types/accessories.go @@ -17,29 +17,6 @@ package types import "golang.org/x/exp/slices" -type UniqueStringQueue []string - -func (queue UniqueStringQueue) Len() int { return len(queue) } -func (queue UniqueStringQueue) Less(i, j int) bool { return false } -func (queue UniqueStringQueue) Swap(i, j int) { panic("Who called me?!?") } - -func (queue *UniqueStringQueue) Push(value string) { - if !slices.Contains(*queue, value) { - *queue = append(*queue, value) - } -} - -func (queue *UniqueStringQueue) Pop() interface{} { - old := *queue - x := old[0] - *queue = old[1:] - return x -} - -func (queue *UniqueStringQueue) Empty() bool { - return queue.Len() == 0 -} - type UniqueSourceFileQueue []SourceFile func (queue UniqueSourceFileQueue) Len() int { return len(queue) } diff --git a/legacy/builder/types/types.go b/legacy/builder/types/types.go index 855c0295ffb..9640d8c0c7a 100644 --- a/legacy/builder/types/types.go +++ b/legacy/builder/types/types.go @@ -85,38 +85,6 @@ func (f *SourceFile) DepfilePath(ctx *Context) *paths.Path { return buildRoot(ctx, f.Origin).Join(f.RelativePath.String() + ".d") } -type SketchFile struct { - Name *paths.Path -} - -type SketchFileSortByName []SketchFile - -func (s SketchFileSortByName) Len() int { - return len(s) -} - -func (s SketchFileSortByName) Swap(i, j int) { - s[i], s[j] = s[j], s[i] -} - -func (s SketchFileSortByName) Less(i, j int) bool { - return s[i].Name.String() < s[j].Name.String() -} - -type PlatforKeysRewrite struct { - Rewrites []PlatforKeyRewrite -} - -func (p *PlatforKeysRewrite) Empty() bool { - return len(p.Rewrites) == 0 -} - -type PlatforKeyRewrite struct { - Key string - OldValue string - NewValue string -} - type Prototype struct { FunctionName string File string From 75ac9d12dff21c578b986d9c20427d1dca6704ba Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 16:44:48 +0200 Subject: [PATCH 4/4] Replaced utils.SliceContains with stdlib function --- legacy/builder/create_cmake_rule.go | 5 +++-- legacy/builder/test/helper_tools_downloader.go | 6 +++--- .../builder/unused_compiled_libraries_remover.go | 4 ++-- legacy/builder/utils/utils.go | 14 +++----------- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index b7640ca58be..16a9aa7781a 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -22,6 +22,7 @@ import ( "strings" properties "github.com/arduino/go-properties-orderedmap" + "golang.org/x/exp/slices" "github.com/arduino/arduino-cli/arduino/globals" "github.com/arduino/arduino-cli/legacy/builder/builder_utils" @@ -213,7 +214,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { lib := staticLib.Base() lib = strings.TrimPrefix(lib, "lib") lib = strings.TrimSuffix(lib, ".a") - if !utils.SliceContains(dynamicLibsFromGccMinusL, lib) { + if !slices.Contains(dynamicLibsFromGccMinusL, lib) { linkGroup += " " + lib cmakelist += "add_library (" + lib + " STATIC IMPORTED)\n" location := strings.TrimPrefix(staticLib.String(), cmakeFolder.String()) @@ -268,7 +269,7 @@ func findUniqueFoldersRelative(slice []string, base string) string { for _, element := range slice { path := filepath.Dir(element) path = strings.TrimPrefix(path, base+"/") - if !utils.SliceContains(out, path) { + if !slices.Contains(out, path) { out = append(out, path) } } diff --git a/legacy/builder/test/helper_tools_downloader.go b/legacy/builder/test/helper_tools_downloader.go index 6e79e4d3aac..133122c4ab6 100644 --- a/legacy/builder/test/helper_tools_downloader.go +++ b/legacy/builder/test/helper_tools_downloader.go @@ -29,10 +29,10 @@ import ( "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/arduino-cli/legacy/builder/gohasissues" - "github.com/arduino/arduino-cli/legacy/builder/utils" "github.com/arduino/go-paths-helper" "github.com/arduino/go-properties-orderedmap" "github.com/pkg/errors" + "golang.org/x/exp/slices" ) var hardwareFolder = paths.New("downloaded_hardware") @@ -692,7 +692,7 @@ func translateGOOSGOARCHToPackageIndexValue() []string { func findToolUrl(index map[string]interface{}, tool Tool, host []string) (string, error) { if len(tool.OsUrls) > 0 { for _, osUrl := range tool.OsUrls { - if utils.SliceContains(host, osUrl.Os) { + if slices.Contains(host, osUrl.Os) { return osUrl.Url, nil } } @@ -709,7 +709,7 @@ func findToolUrl(index map[string]interface{}, tool Tool, host []string) (string systems := packageTool["systems"].([]interface{}) for _, s := range systems { system := s.(map[string]interface{}) - if utils.SliceContains(host, system["host"].(string)) { + if slices.Contains(host, system["host"].(string)) { return system[constants.TOOL_URL].(string), nil } } diff --git a/legacy/builder/unused_compiled_libraries_remover.go b/legacy/builder/unused_compiled_libraries_remover.go index 595ce91302e..d1b36b147ac 100644 --- a/legacy/builder/unused_compiled_libraries_remover.go +++ b/legacy/builder/unused_compiled_libraries_remover.go @@ -18,8 +18,8 @@ package builder import ( "github.com/arduino/arduino-cli/arduino/libraries" "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/arduino/arduino-cli/legacy/builder/utils" "github.com/pkg/errors" + "golang.org/x/exp/slices" ) type UnusedCompiledLibrariesRemover struct{} @@ -39,7 +39,7 @@ func (s *UnusedCompiledLibrariesRemover) Run(ctx *types.Context) error { } for _, file := range files { if file.IsDir() { - if !utils.SliceContains(libraryNames, file.Base()) { + if !slices.Contains(libraryNames, file.Base()) { if err := file.RemoveAll(); err != nil { return errors.WithStack(err) } diff --git a/legacy/builder/utils/utils.go b/legacy/builder/utils/utils.go index 87113cce633..036fab0dc72 100644 --- a/legacy/builder/utils/utils.go +++ b/legacy/builder/utils/utils.go @@ -33,6 +33,7 @@ import ( "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" "github.com/pkg/errors" + "golang.org/x/exp/slices" "golang.org/x/text/runes" "golang.org/x/text/transform" "golang.org/x/text/unicode/norm" @@ -64,7 +65,7 @@ func FilterFilesWithExtensions(extensions ...string) filterFiles { return func(files []os.FileInfo) []os.FileInfo { var filtered []os.FileInfo for _, file := range files { - if !file.IsDir() && SliceContains(extensions, filepath.Ext(file.Name())) { + if !file.IsDir() && slices.Contains(extensions, filepath.Ext(file.Name())) { filtered = append(filtered, file) } } @@ -119,15 +120,6 @@ func IsSCCSFile(file os.FileInfo) bool { return SOURCE_CONTROL_FOLDERS[name] } -func SliceContains(slice []string, target string) bool { - for _, value := range slice { - if value == target { - return true - } - } - return false -} - type mapFunc func(string) string func Map(slice []string, fn mapFunc) []string { @@ -263,7 +255,7 @@ func FindFilesInFolder(dir *paths.Path, recurse bool, extensions []string) (path func AppendIfNotPresent(target []string, elements ...string) []string { for _, element := range elements { - if !SliceContains(target, element) { + if !slices.Contains(target, element) { target = append(target, element) } }