From 84f38d42782b2ae554371e1479a7081190d08d61 Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Wed, 30 Aug 2023 15:49:33 +0200 Subject: [PATCH 1/3] Move WarnDeprecatedFiles to commands/sketch --- commands/sketch/warn_deprecated.go | 36 ++++++++++++++++++++++++++++++ internal/cli/arguments/sketch.go | 18 +++------------ internal/cli/sketch/archive.go | 5 +++-- internal/cli/upload/upload.go | 4 ++-- 4 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 commands/sketch/warn_deprecated.go diff --git a/commands/sketch/warn_deprecated.go b/commands/sketch/warn_deprecated.go new file mode 100644 index 00000000000..a78f7156973 --- /dev/null +++ b/commands/sketch/warn_deprecated.go @@ -0,0 +1,36 @@ +// 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 sketch + +import ( + "fmt" + + "github.com/arduino/arduino-cli/arduino/sketch" + paths "github.com/arduino/go-paths-helper" +) + +// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated +func WarnDeprecatedFiles(sketchPath *paths.Path) string { + // .pde files are still supported but deprecated, this warning urges the user to rename them + if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 { + msg := tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:") + for _, f := range files { + msg += fmt.Sprintf("\n - %s", f) + } + return msg + } + return "" +} diff --git a/internal/cli/arguments/sketch.go b/internal/cli/arguments/sketch.go index 6c167ad23dd..4e670bab6ac 100644 --- a/internal/cli/arguments/sketch.go +++ b/internal/cli/arguments/sketch.go @@ -16,9 +16,7 @@ package arguments import ( - "fmt" - - "github.com/arduino/arduino-cli/arduino/sketch" + sk "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/internal/cli/feedback" "github.com/arduino/go-paths-helper" "github.com/sirupsen/logrus" @@ -38,18 +36,8 @@ func InitSketchPath(path string) (sketchPath *paths.Path) { logrus.Infof("Reading sketch from dir: %s", wd) sketchPath = wd } - WarnDeprecatedFiles(sketchPath) - return sketchPath -} - -// WarnDeprecatedFiles warns the user that a type of sketch files are deprecated -func WarnDeprecatedFiles(sketchPath *paths.Path) { - // .pde files are still supported but deprecated, this warning urges the user to rename them - if files := sketch.CheckForPdeFiles(sketchPath); len(files) > 0 { - msg := tr("Sketches with .pde extension are deprecated, please rename the following files to .ino:") - for _, f := range files { - msg += fmt.Sprintf("\n - %s", f) - } + if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" { feedback.Warning(msg) } + return sketchPath } diff --git a/internal/cli/sketch/archive.go b/internal/cli/sketch/archive.go index 9410bb0d0c6..e21221aa3e7 100644 --- a/internal/cli/sketch/archive.go +++ b/internal/cli/sketch/archive.go @@ -21,7 +21,6 @@ import ( "os" sk "github.com/arduino/arduino-cli/commands/sketch" - "github.com/arduino/arduino-cli/internal/cli/arguments" "github.com/arduino/arduino-cli/internal/cli/feedback" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" "github.com/arduino/go-paths-helper" @@ -61,7 +60,9 @@ func runArchiveCommand(args []string, includeBuildDir bool, overwrite bool) { sketchPath = paths.New(args[0]) } - arguments.WarnDeprecatedFiles(sketchPath) + if msg := sk.WarnDeprecatedFiles(sketchPath); msg != "" { + feedback.Warning(msg) + } archivePath := "" if len(args) == 2 { diff --git a/internal/cli/upload/upload.go b/internal/cli/upload/upload.go index 3c33f2675b0..c7a5eb8866a 100644 --- a/internal/cli/upload/upload.go +++ b/internal/cli/upload/upload.go @@ -86,8 +86,8 @@ func runUploadCommand(command *cobra.Command, args []string) { } sketchPath := arguments.InitSketchPath(path) - if importDir == "" && importFile == "" { - arguments.WarnDeprecatedFiles(sketchPath) + if msg := sk.WarnDeprecatedFiles(sketchPath); importDir == "" && importFile == "" && msg != "" { + feedback.Warning(msg) } sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()}) From f99145e752f1b7f9dae9d43476c19ddbcff0a1a6 Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 5 Sep 2023 15:32:55 +0200 Subject: [PATCH 2/3] Change FindPlatform's signature to avoid references to the Package Manager --- arduino/cores/packagemanager/download.go | 8 ++++---- arduino/cores/packagemanager/install_uninstall.go | 2 +- arduino/cores/packagemanager/package_manager_test.go | 5 +---- commands/core/uninstall.go | 2 +- commands/upload/upload.go | 5 +---- internal/cli/compile/compile.go | 7 +------ internal/cli/upload/upload.go | 7 +------ 7 files changed, 10 insertions(+), 26 deletions(-) diff --git a/arduino/cores/packagemanager/download.go b/arduino/cores/packagemanager/download.go index f3a5877c8a6..5baf34d2b9c 100644 --- a/arduino/cores/packagemanager/download.go +++ b/arduino/cores/packagemanager/download.go @@ -43,12 +43,12 @@ func (platform *PlatformReference) String() string { // FindPlatform returns the Platform matching the PlatformReference or nil if not found. // The PlatformVersion field of the reference is ignored. -func (pme *Explorer) FindPlatform(ref *PlatformReference) *cores.Platform { - targetPackage, ok := pme.packages[ref.Package] +func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) *cores.Platform { + targetPackage, ok := pme.packages[platformPackage] if !ok { return nil } - platform, ok := targetPackage.Platforms[ref.PlatformArchitecture] + platform, ok := targetPackage.Platforms[platformArchitecture] if !ok { return nil } @@ -57,7 +57,7 @@ func (pme *Explorer) FindPlatform(ref *PlatformReference) *cores.Platform { // FindPlatformRelease returns the PlatformRelease matching the PlatformReference or nil if not found func (pme *Explorer) FindPlatformRelease(ref *PlatformReference) *cores.PlatformRelease { - platform := pme.FindPlatform(ref) + platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) if platform == nil { return nil } diff --git a/arduino/cores/packagemanager/install_uninstall.go b/arduino/cores/packagemanager/install_uninstall.go index fb34d3971c9..4187db87aea 100644 --- a/arduino/cores/packagemanager/install_uninstall.go +++ b/arduino/cores/packagemanager/install_uninstall.go @@ -45,7 +45,7 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades( } // Search the latest version for all specified platforms - platform := pme.FindPlatform(platformRef) + platform := pme.FindPlatform(platformRef.Package, platformRef.PlatformArchitecture) if platform == nil { return nil, &arduino.PlatformNotFoundError{Platform: platformRef.String()} } diff --git a/arduino/cores/packagemanager/package_manager_test.go b/arduino/cores/packagemanager/package_manager_test.go index c02fa34eaa6..c234b0a4d6f 100644 --- a/arduino/cores/packagemanager/package_manager_test.go +++ b/arduino/cores/packagemanager/package_manager_test.go @@ -387,10 +387,7 @@ func TestBoardOrdering(t *testing.T) { pme, release := pm.NewExplorer() defer release() - pl := pme.FindPlatform(&PlatformReference{ - Package: "arduino", - PlatformArchitecture: "avr", - }) + pl := pme.FindPlatform("arduino", "avr") require.NotNil(t, pl) plReleases := pl.GetAllInstalled() require.NotEmpty(t, plReleases) diff --git a/commands/core/uninstall.go b/commands/core/uninstall.go index 3bdbf85fbcc..cb1b4491408 100644 --- a/commands/core/uninstall.go +++ b/commands/core/uninstall.go @@ -48,7 +48,7 @@ func platformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t PlatformArchitecture: req.Architecture, } if ref.PlatformVersion == nil { - platform := pme.FindPlatform(ref) + platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) if platform == nil { return &arduino.PlatformNotFoundError{Platform: ref.String()} } diff --git a/commands/upload/upload.go b/commands/upload/upload.go index aa74023b0e3..40cbb0bb018 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -285,10 +285,7 @@ func runProgramAction(pme *packagemanager.Explorer, Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...) Value: uploadToolID} } else if len(split) == 2 { - p := pme.FindPlatform(&packagemanager.PlatformReference{ - Package: split[0], - PlatformArchitecture: boardPlatform.Platform.Architecture, - }) + p := pme.FindPlatform(split[0], boardPlatform.Platform.Architecture) if p == nil { return nil, &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture} } diff --git a/internal/cli/compile/compile.go b/internal/cli/compile/compile.go index dfd5d4c70ba..fbb6ef6cb1a 100644 --- a/internal/cli/compile/compile.go +++ b/internal/cli/compile/compile.go @@ -25,7 +25,6 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands/compile" "github.com/arduino/arduino-cli/commands/sketch" @@ -363,12 +362,8 @@ func runCompileCommand(cmd *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - // FIXME: Here we should not access PackageManager... pme, release := commands.GetPackageManagerExplorer(compileRequest) - platform := pme.FindPlatform(&packagemanager.PlatformReference{ - Package: split[0], - PlatformArchitecture: split[1], - }) + platform := pme.FindPlatform(split[0], split[1]) release() if profileArg.String() == "" { diff --git a/internal/cli/upload/upload.go b/internal/cli/upload/upload.go index c7a5eb8866a..2217b0ccc1c 100644 --- a/internal/cli/upload/upload.go +++ b/internal/cli/upload/upload.go @@ -23,7 +23,6 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/arduino/cores/packagemanager" "github.com/arduino/arduino-cli/commands" sk "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/commands/upload" @@ -130,12 +129,8 @@ func runUploadCommand(command *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - // FIXME: Here we must not access package manager... pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst}) - platform := pme.FindPlatform(&packagemanager.PlatformReference{ - Package: split[0], - PlatformArchitecture: split[1], - }) + platform := pme.FindPlatform(split[0], split[1]) release() msg += "\n" From 11326b5352c759e5b975d3d1fffb397404e01988 Mon Sep 17 00:00:00 2001 From: MatteoPologruto Date: Tue, 12 Sep 2023 15:27:10 +0200 Subject: [PATCH 3/3] Replace direct access to the package manager with a call to PlatformSearch function --- arduino/cores/packagemanager/download.go | 8 ++++---- arduino/cores/packagemanager/install_uninstall.go | 2 +- .../cores/packagemanager/package_manager_test.go | 5 ++++- commands/core/uninstall.go | 2 +- commands/upload/upload.go | 5 ++++- internal/cli/compile/compile.go | 15 +++++++++------ internal/cli/upload/upload.go | 14 ++++++++------ 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/arduino/cores/packagemanager/download.go b/arduino/cores/packagemanager/download.go index 5baf34d2b9c..f3a5877c8a6 100644 --- a/arduino/cores/packagemanager/download.go +++ b/arduino/cores/packagemanager/download.go @@ -43,12 +43,12 @@ func (platform *PlatformReference) String() string { // FindPlatform returns the Platform matching the PlatformReference or nil if not found. // The PlatformVersion field of the reference is ignored. -func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) *cores.Platform { - targetPackage, ok := pme.packages[platformPackage] +func (pme *Explorer) FindPlatform(ref *PlatformReference) *cores.Platform { + targetPackage, ok := pme.packages[ref.Package] if !ok { return nil } - platform, ok := targetPackage.Platforms[platformArchitecture] + platform, ok := targetPackage.Platforms[ref.PlatformArchitecture] if !ok { return nil } @@ -57,7 +57,7 @@ func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) // FindPlatformRelease returns the PlatformRelease matching the PlatformReference or nil if not found func (pme *Explorer) FindPlatformRelease(ref *PlatformReference) *cores.PlatformRelease { - platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) + platform := pme.FindPlatform(ref) if platform == nil { return nil } diff --git a/arduino/cores/packagemanager/install_uninstall.go b/arduino/cores/packagemanager/install_uninstall.go index 4187db87aea..fb34d3971c9 100644 --- a/arduino/cores/packagemanager/install_uninstall.go +++ b/arduino/cores/packagemanager/install_uninstall.go @@ -45,7 +45,7 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades( } // Search the latest version for all specified platforms - platform := pme.FindPlatform(platformRef.Package, platformRef.PlatformArchitecture) + platform := pme.FindPlatform(platformRef) if platform == nil { return nil, &arduino.PlatformNotFoundError{Platform: platformRef.String()} } diff --git a/arduino/cores/packagemanager/package_manager_test.go b/arduino/cores/packagemanager/package_manager_test.go index c234b0a4d6f..c02fa34eaa6 100644 --- a/arduino/cores/packagemanager/package_manager_test.go +++ b/arduino/cores/packagemanager/package_manager_test.go @@ -387,7 +387,10 @@ func TestBoardOrdering(t *testing.T) { pme, release := pm.NewExplorer() defer release() - pl := pme.FindPlatform("arduino", "avr") + pl := pme.FindPlatform(&PlatformReference{ + Package: "arduino", + PlatformArchitecture: "avr", + }) require.NotNil(t, pl) plReleases := pl.GetAllInstalled() require.NotEmpty(t, plReleases) diff --git a/commands/core/uninstall.go b/commands/core/uninstall.go index cb1b4491408..3bdbf85fbcc 100644 --- a/commands/core/uninstall.go +++ b/commands/core/uninstall.go @@ -48,7 +48,7 @@ func platformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t PlatformArchitecture: req.Architecture, } if ref.PlatformVersion == nil { - platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture) + platform := pme.FindPlatform(ref) if platform == nil { return &arduino.PlatformNotFoundError{Platform: ref.String()} } diff --git a/commands/upload/upload.go b/commands/upload/upload.go index 40cbb0bb018..aa74023b0e3 100644 --- a/commands/upload/upload.go +++ b/commands/upload/upload.go @@ -285,7 +285,10 @@ func runProgramAction(pme *packagemanager.Explorer, Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...) Value: uploadToolID} } else if len(split) == 2 { - p := pme.FindPlatform(split[0], boardPlatform.Platform.Architecture) + p := pme.FindPlatform(&packagemanager.PlatformReference{ + Package: split[0], + PlatformArchitecture: boardPlatform.Platform.Architecture, + }) if p == nil { return nil, &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture} } diff --git a/internal/cli/compile/compile.go b/internal/cli/compile/compile.go index fbb6ef6cb1a..4ed8ab6638e 100644 --- a/internal/cli/compile/compile.go +++ b/internal/cli/compile/compile.go @@ -25,8 +25,8 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands/compile" + "github.com/arduino/arduino-cli/commands/core" "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/configuration" @@ -362,13 +362,16 @@ func runCompileCommand(cmd *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - pme, release := commands.GetPackageManagerExplorer(compileRequest) - platform := pme.FindPlatform(split[0], split[1]) - release() - if profileArg.String() == "" { res.Error += fmt.Sprintln() - if platform != nil { + + if platform, err := core.PlatformSearch(&rpc.PlatformSearchRequest{ + Instance: inst, + SearchArgs: platformErr.Platform, + AllVersions: false, + }); err != nil { + res.Error += err.Error() + } else if len(platform.GetSearchOutput()) > 0 { suggestion := fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform) res.Error += tr("Try running %s", suggestion) } else { diff --git a/internal/cli/upload/upload.go b/internal/cli/upload/upload.go index 2217b0ccc1c..aa079f98653 100644 --- a/internal/cli/upload/upload.go +++ b/internal/cli/upload/upload.go @@ -23,7 +23,7 @@ import ( "strings" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/commands" + "github.com/arduino/arduino-cli/commands/core" sk "github.com/arduino/arduino-cli/commands/sketch" "github.com/arduino/arduino-cli/commands/upload" "github.com/arduino/arduino-cli/i18n" @@ -129,12 +129,14 @@ func runUploadCommand(command *cobra.Command, args []string) { panic(tr("Platform ID is not correct")) } - pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst}) - platform := pme.FindPlatform(split[0], split[1]) - release() - msg += "\n" - if platform != nil { + if platform, err := core.PlatformSearch(&rpc.PlatformSearchRequest{ + Instance: inst, + SearchArgs: platformErr.Platform, + AllVersions: false, + }); err != nil { + msg += err.Error() + } else if len(platform.GetSearchOutput()) > 0 { msg += tr("Try running %s", fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform)) } else { msg += tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)