-
-
Notifications
You must be signed in to change notification settings - Fork 410
Add first set of profile
commands
#2917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c282cc6
d3d7a59
f98bf7d
235005d
d47a687
4d167f3
a2c86ef
0d295ca
c8a3e4e
a99efdd
8f009bc
f5943ab
5381938
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 [email protected]. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/arduino/arduino-cli/commands/cmderrors" | ||
"github.com/arduino/arduino-cli/internal/arduino/sketch" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// ProfileDump dumps the content of the project file. | ||
func (s *arduinoCoreServerImpl) ProfileDump(ctx context.Context, req *rpc.ProfileDumpRequest) (*rpc.ProfileDumpResponse, error) { | ||
sk, err := sketch.New(paths.New(req.GetSketchPath())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch req.GetDumpFormat() { | ||
case "yaml": | ||
return &rpc.ProfileDumpResponse{EncodedProfile: sk.Project.AsYaml()}, nil | ||
case "", "json": | ||
data, err := json.MarshalIndent(sk.Project, "", " ") | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshalling settings: %v", err) | ||
} | ||
return &rpc.ProfileDumpResponse{EncodedProfile: string(data)}, nil | ||
default: | ||
return nil, &cmderrors.InvalidArgumentError{Message: fmt.Sprintf("unsupported format: %s", req.GetDumpFormat())} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 [email protected]. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/arduino/arduino-cli/commands/cmderrors" | ||
"github.com/arduino/arduino-cli/commands/internal/instances" | ||
"github.com/arduino/arduino-cli/internal/arduino/sketch" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
"github.com/arduino/arduino-cli/pkg/fqbn" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// InitProfile creates a new project file if it does not exist. If a profile name with the associated FQBN is specified, | ||
// it is added to the project. | ||
func (s *arduinoCoreServerImpl) InitProfile(ctx context.Context, req *rpc.InitProfileRequest) (*rpc.InitProfileResponse, error) { | ||
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists | ||
sk, err := sketch.New(paths.New(req.GetSketchPath())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
projectFilePath := sk.GetProjectPath() | ||
|
||
if !projectFilePath.Exist() { | ||
err := projectFilePath.WriteFile([]byte("profiles: {}\n")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if req.GetProfileName() != "" { | ||
if req.GetFqbn() == "" { | ||
return nil, &cmderrors.MissingFQBNError{} | ||
} | ||
|
||
// Check that the profile name is unique | ||
if profile, _ := sk.GetProfile(req.ProfileName); profile != nil { | ||
return nil, &cmderrors.DuplicateProfileError{Profile: req.ProfileName} | ||
} | ||
|
||
pme, release, err := instances.GetPackageManagerExplorer(req.GetInstance()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
release = sync.OnceFunc(release) | ||
defer release() | ||
|
||
if pme.Dirty() { | ||
return nil, &cmderrors.InstanceNeedsReinitialization{} | ||
} | ||
|
||
fqbn, err := fqbn.Parse(req.GetFqbn()) | ||
if err != nil { | ||
return nil, &cmderrors.InvalidFQBNError{Cause: err} | ||
} | ||
|
||
// Automatically detect the target platform if it is installed on the user's machine | ||
_, targetPlatform, _, _, _, err := pme.ResolveFQBN(fqbn) | ||
if err != nil { | ||
if targetPlatform == nil { | ||
return nil, &cmderrors.PlatformNotFoundError{ | ||
Platform: fmt.Sprintf("%s:%s", fqbn.Vendor, fqbn.Architecture), | ||
Cause: errors.New(i18n.Tr("platform not installed")), | ||
} | ||
} | ||
return nil, &cmderrors.InvalidFQBNError{Cause: err} | ||
} | ||
|
||
newProfile := &sketch.Profile{Name: req.GetProfileName(), FQBN: req.GetFqbn()} | ||
// TODO: what to do with the PlatformIndexURL? | ||
newProfile.Platforms = append(newProfile.Platforms, &sketch.ProfilePlatformReference{ | ||
Packager: targetPlatform.Platform.Package.Name, | ||
Architecture: targetPlatform.Platform.Architecture, | ||
Version: targetPlatform.Version, | ||
}) | ||
|
||
sk.Project.Profiles = append(sk.Project.Profiles, newProfile) | ||
// Set the profile as the default one if it's the only one | ||
if req.DefaultProfile || len(sk.Project.Profiles) == 1 { | ||
sk.Project.DefaultProfile = newProfile.Name | ||
} | ||
|
||
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &rpc.InitProfileResponse{ProjectFilePath: projectFilePath.String()}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 [email protected]. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/arduino/arduino-cli/commands/cmderrors" | ||
"github.com/arduino/arduino-cli/commands/internal/instances" | ||
"github.com/arduino/arduino-cli/internal/arduino/sketch" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
paths "github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// ProfileLibAdd adds a library to the specified profile or to the default profile. | ||
func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.ProfileLibAddRequest) (*rpc.ProfileLibAddResponse, error) { | ||
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists | ||
sk, err := sketch.New(paths.New(req.GetSketchPath())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
projectFilePath := sk.GetProjectPath() | ||
|
||
// If no profile is specified, try to use the default one | ||
profileName := sk.Project.DefaultProfile | ||
if req.GetProfileName() != "" { | ||
profileName = req.GetProfileName() | ||
} | ||
if profileName == "" { | ||
return nil, &cmderrors.MissingProfileError{} | ||
} | ||
|
||
profile, err := sk.GetProfile(profileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Obtain the library index from the manager | ||
li, err := instances.GetLibrariesIndex(req.GetInstance()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
version, err := parseVersion(req.LibVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
libRelease, err := li.FindRelease(req.GetLibName(), version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will improve usability if this operation also adds all the dependencies of that library; otherwise, the user will need to enumerate all of them one by one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The concern I have with this proposal is that the dependencies specified in the library metadata are not necessarily required to compile a specific sketch. Dependencies of a library may be required only for specific use cases that are not applicable to a given sketch project. The library author may declare dependencies because they are a dependency of one of the library's example sketches, even though they are not a dependency of the library itself. In this case, the unnecessary dependencies would pollute the environment generated by the build profile, which could result in problems caused by library discovery results other than what was intended by the user. So I would recommend adding a flag that allows the user to control this behavior, similar to what was done for https://arduino.github.io/arduino-cli/dev/commands/arduino-cli_lib_install/#options
|
||
|
||
// If the library has been already added to the profile, just update the version | ||
if lib, _ := profile.GetLibrary(req.LibName, false); lib != nil { | ||
lib.Version = libRelease.GetVersion() | ||
} else { | ||
profile.Libraries = append(profile.Libraries, &sketch.ProfileLibraryReference{ | ||
Library: req.GetLibName(), | ||
Version: libRelease.GetVersion(), | ||
}) | ||
} | ||
|
||
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &rpc.ProfileLibAddResponse{LibName: req.LibName, LibVersion: libRelease.GetVersion().String(), ProfileName: profileName}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 [email protected]. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/arduino/arduino-cli/commands/cmderrors" | ||
"github.com/arduino/arduino-cli/internal/arduino/sketch" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
paths "github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// ProfileLibRemove removes a library from the specified profile or from the default profile. | ||
func (s *arduinoCoreServerImpl) ProfileLibRemove(ctx context.Context, req *rpc.ProfileLibRemoveRequest) (*rpc.ProfileLibRemoveResponse, error) { | ||
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists | ||
sk, err := sketch.New(paths.New(req.GetSketchPath())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
projectFilePath := sk.GetProjectPath() | ||
|
||
// If no profile is specified, try to use the default one | ||
profileName := sk.Project.DefaultProfile | ||
if req.GetProfileName() != "" { | ||
profileName = req.GetProfileName() | ||
} | ||
if profileName == "" { | ||
return nil, &cmderrors.MissingProfileError{} | ||
} | ||
|
||
profile, err := sk.GetProfile(profileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lib, err := profile.GetLibrary(req.LibName, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &rpc.ProfileLibRemoveResponse{LibName: lib.Library, LibVersion: lib.Version.String(), ProfileName: profileName}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 [email protected]. | ||
|
||
package commands | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/arduino/arduino-cli/commands/cmderrors" | ||
"github.com/arduino/arduino-cli/internal/arduino/sketch" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
paths "github.com/arduino/go-paths-helper" | ||
) | ||
|
||
// ProfileSetDefault sets an existing profile as the default profile. | ||
func (s *arduinoCoreServerImpl) ProfileSetDefault(ctx context.Context, req *rpc.ProfileSetDefaultRequest) (*rpc.ProfileSetDefaultResponse, error) { | ||
if req.GetProfileName() == "" { | ||
return nil, &cmderrors.MissingProfileError{} | ||
} | ||
|
||
// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists | ||
sk, err := sketch.New(paths.New(req.GetSketchPath())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
projectFilePath := sk.GetProjectPath() | ||
|
||
if _, err := sk.GetProfile(req.GetProfileName()); err != nil { | ||
return nil, err | ||
} | ||
|
||
sk.Project.DefaultProfile = req.GetProfileName() | ||
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &rpc.ProfileSetDefaultResponse{}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 profile | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/internal/cli/arguments" | ||
"github.com/arduino/arduino-cli/internal/cli/feedback" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func initDumpCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
dumpCommand := &cobra.Command{ | ||
Use: "dump", | ||
Short: i18n.Tr("Print the sketch project file."), | ||
Long: i18n.Tr("Print the data from the sketch project file."), | ||
Example: "" + | ||
" " + os.Args[0] + " profile dump\n", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runDumpCommand(cmd.Context(), args, srv) | ||
}, | ||
} | ||
|
||
return dumpCommand | ||
} | ||
|
||
func runDumpCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer) { | ||
path := "" | ||
if len(args) > 0 { | ||
path = args[0] | ||
} | ||
|
||
sketchPath := arguments.InitSketchPath(path) | ||
res := &rawResult{} | ||
switch feedback.GetFormat() { | ||
case feedback.JSON, feedback.MinifiedJSON: | ||
resp, err := srv.ProfileDump(ctx, &rpc.ProfileDumpRequest{SketchPath: sketchPath.String(), DumpFormat: "json"}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Error dumping the profile: %v", err), feedback.ErrBadArgument) | ||
} | ||
res.rawJSON = []byte(resp.GetEncodedProfile()) | ||
case feedback.Text: | ||
resp, err := srv.ProfileDump(ctx, &rpc.ProfileDumpRequest{SketchPath: sketchPath.String(), DumpFormat: "yaml"}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Error dumping the profile: %v", err), feedback.ErrBadArgument) | ||
} | ||
res.rawYAML = []byte(resp.GetEncodedProfile()) | ||
default: | ||
feedback.Fatal(i18n.Tr("Unsupported format: %s", feedback.GetFormat()), feedback.ErrBadArgument) | ||
} | ||
feedback.PrintResult(dumpResult{Config: res}) | ||
} | ||
|
||
type rawResult struct { | ||
rawJSON []byte | ||
rawYAML []byte | ||
} | ||
|
||
func (r *rawResult) MarshalJSON() ([]byte, error) { | ||
// it is already encoded in rawJSON field | ||
return r.rawJSON, nil | ||
} | ||
|
||
type dumpResult struct { | ||
Config *rawResult `json:"project"` | ||
} | ||
|
||
func (dr dumpResult) Data() interface{} { | ||
return dr | ||
} | ||
|
||
func (dr dumpResult) String() string { | ||
// In case of text output do not wrap the output in outer JSON or YAML structure | ||
return string(dr.Config.rawYAML) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 profile | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/internal/cli/arguments" | ||
"github.com/arduino/arduino-cli/internal/cli/feedback" | ||
"github.com/arduino/arduino-cli/internal/cli/instance" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
var defaultProfile bool | ||
initCommand := &cobra.Command{ | ||
Use: "init", | ||
Short: i18n.Tr("Create or update the sketch project file."), | ||
Long: i18n.Tr("Create or update the sketch project file."), | ||
Example: "" + | ||
" # " + i18n.Tr("Creates or updates the sketch project file in the current directory.") + "\n" + | ||
" " + os.Args[0] + " profile init\n" + | ||
" " + os.Args[0] + " profile init --profile uno_profile -b arduino:avr:uno", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runInitCommand(cmd.Context(), args, srv, defaultProfile) | ||
}, | ||
} | ||
fqbnArg.AddToCommand(initCommand, srv) | ||
profileArg.AddToCommand(initCommand, srv) | ||
initCommand.Flags().BoolVar(&defaultProfile, "default", false, i18n.Tr("Set the profile as the default one.")) | ||
return initCommand | ||
} | ||
|
||
func runInitCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, defaultProfile bool) { | ||
path := "" | ||
if len(args) > 0 { | ||
path = args[0] | ||
} | ||
|
||
sketchPath := arguments.InitSketchPath(path) | ||
|
||
inst := instance.CreateAndInit(ctx, srv) | ||
|
||
resp, err := srv.InitProfile(ctx, &rpc.InitProfileRequest{Instance: inst, SketchPath: sketchPath.String(), ProfileName: profileArg.Get(), Fqbn: fqbnArg.String(), DefaultProfile: defaultProfile}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Error initializing the project file: %v", err), feedback.ErrGeneric) | ||
} | ||
feedback.PrintResult(profileResult{ProjectFilePath: resp.GetProjectFilePath()}) | ||
} | ||
|
||
type profileResult struct { | ||
ProjectFilePath string `json:"project_path"` | ||
} | ||
|
||
func (ir profileResult) Data() interface{} { | ||
return ir | ||
} | ||
|
||
func (ir profileResult) String() string { | ||
return i18n.Tr("Project file created in: %s", ir.ProjectFilePath) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 profile | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/internal/cli/arguments" | ||
"github.com/arduino/arduino-cli/internal/cli/feedback" | ||
"github.com/arduino/arduino-cli/internal/cli/instance" | ||
"github.com/arduino/arduino-cli/internal/cli/lib" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func initLibCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
libCommand := &cobra.Command{ | ||
Use: "lib", | ||
Short: i18n.Tr("Commands related to build profile libraries."), | ||
Long: i18n.Tr("Commands related to the library dependencies of build profiles."), | ||
Example: "" + | ||
" " + os.Args[0] + " profile lib add AudioZero -m my_profile\n" + | ||
" " + os.Args[0] + " profile lib remove Arduino_JSON --profile my_profile\n", | ||
} | ||
|
||
libCommand.AddCommand(initLibAddCommand(srv)) | ||
libCommand.AddCommand(initLibRemoveCommand(srv)) | ||
|
||
return libCommand | ||
} | ||
|
||
func initLibAddCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
var destDir string | ||
|
||
addCommand := &cobra.Command{ | ||
Use: fmt.Sprintf("add %s[@%s]...", i18n.Tr("LIBRARY"), i18n.Tr("VERSION_NUMBER")), | ||
Short: i18n.Tr("Adds a library to the profile."), | ||
Long: i18n.Tr("Adds a library to the profile."), | ||
Example: "" + | ||
" " + os.Args[0] + " profile lib add AudioZero -m my_profile\n" + | ||
" " + os.Args[0] + " profile lib add Arduino_JSON@0.2.0 --profile my_profile\n", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runLibAddCommand(cmd.Context(), args, srv, destDir) | ||
}, | ||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return arguments.GetInstallableLibs(cmd.Context(), srv), cobra.ShellCompDirectiveDefault | ||
}, | ||
} | ||
|
||
addCommand.Flags().StringVar(&destDir, "dest-dir", "", i18n.Tr("Location of the sketch project file.")) | ||
profileArg.AddToCommand(addCommand, srv) | ||
|
||
return addCommand | ||
} | ||
|
||
func runLibAddCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string) { | ||
sketchPath := arguments.InitSketchPath(destDir) | ||
|
||
instance := instance.CreateAndInit(ctx, srv) | ||
libRefs, err := lib.ParseLibraryReferenceArgsAndAdjustCase(ctx, srv, instance, args) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Arguments error: %v", err), feedback.ErrBadArgument) | ||
} | ||
for _, lib := range libRefs { | ||
resp, err := srv.ProfileLibAdd(ctx, &rpc.ProfileLibAddRequest{ | ||
Instance: instance, | ||
SketchPath: sketchPath.String(), | ||
ProfileName: profileArg.Get(), | ||
LibName: lib.Name, | ||
LibVersion: lib.Version, | ||
}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Error adding %s to the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric) | ||
} | ||
feedback.PrintResult(libAddResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: resp.ProfileName}) | ||
} | ||
} | ||
|
||
func initLibRemoveCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
var destDir string | ||
|
||
removeCommand := &cobra.Command{ | ||
Use: fmt.Sprintf("remove %s[@%s]...", i18n.Tr("LIBRARY"), i18n.Tr("VERSION_NUMBER")), | ||
Short: i18n.Tr("Removes a library from the profile."), | ||
Long: i18n.Tr("Removes a library from the profile."), | ||
Example: "" + | ||
" " + os.Args[0] + " profile lib remove AudioZero -m my_profile\n" + | ||
" " + os.Args[0] + " profile lib remove Arduino_JSON@0.2.0 --profile my_profile\n", | ||
Args: cobra.MinimumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runLibRemoveCommand(cmd.Context(), args, srv, destDir) | ||
}, | ||
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return arguments.GetInstallableLibs(cmd.Context(), srv), cobra.ShellCompDirectiveDefault | ||
}, | ||
} | ||
|
||
removeCommand.Flags().StringVar(&destDir, "dest-dir", "", i18n.Tr("Location of the sketch project file.")) | ||
profileArg.AddToCommand(removeCommand, srv) | ||
|
||
return removeCommand | ||
} | ||
|
||
func runLibRemoveCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string) { | ||
sketchPath := arguments.InitSketchPath(destDir) | ||
|
||
instance := instance.CreateAndInit(ctx, srv) | ||
libRefs, err := lib.ParseLibraryReferenceArgsAndAdjustCase(ctx, srv, instance, args) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Arguments error: %v", err), feedback.ErrBadArgument) | ||
} | ||
for _, lib := range libRefs { | ||
resp, err := srv.ProfileLibRemove(ctx, &rpc.ProfileLibRemoveRequest{ | ||
SketchPath: sketchPath.String(), | ||
ProfileName: profileArg.Get(), | ||
LibName: lib.Name, | ||
}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Error removing %s from the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric) | ||
} | ||
feedback.PrintResult(libRemoveResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: resp.ProfileName}) | ||
} | ||
} | ||
|
||
type libAddResult struct { | ||
LibName string `json:"library_name"` | ||
LibVersion string `json:"library_version"` | ||
ProfileName string `json:"profile_name"` | ||
} | ||
|
||
func (lr libAddResult) Data() interface{} { | ||
return lr | ||
} | ||
|
||
func (lr libAddResult) String() string { | ||
return i18n.Tr("Profile %s: %s@%s added successfully", lr.ProfileName, lr.LibName, lr.LibVersion) | ||
} | ||
|
||
type libRemoveResult struct { | ||
LibName string `json:"library_name"` | ||
LibVersion string `json:"library_version"` | ||
ProfileName string `json:"profile_name"` | ||
} | ||
|
||
func (lr libRemoveResult) Data() interface{} { | ||
return lr | ||
} | ||
|
||
func (lr libRemoveResult) String() string { | ||
return i18n.Tr("Profile %s: %s@%s removed successfully", lr.ProfileName, lr.LibName, lr.LibVersion) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 profile | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/internal/cli/arguments" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
fqbnArg arguments.Fqbn // Fully Qualified Board Name, e.g.: arduino:avr:uno. | ||
profileArg arguments.Profile // Name of the profile to add to the project | ||
) | ||
|
||
func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
profileCommand := &cobra.Command{ | ||
Use: "profile", | ||
Short: i18n.Tr("Build profile operations."), | ||
Long: i18n.Tr("Build profile operations."), | ||
Example: " " + os.Args[0] + " profile init", | ||
} | ||
|
||
profileCommand.AddCommand(initInitCommand(srv)) | ||
profileCommand.AddCommand(initLibCommand(srv)) | ||
profileCommand.AddCommand(initSetDefaultCommand(srv)) | ||
profileCommand.AddCommand(initDumpCommand(srv)) | ||
|
||
return profileCommand | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2025 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 profile | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/internal/cli/arguments" | ||
"github.com/arduino/arduino-cli/internal/cli/feedback" | ||
"github.com/arduino/arduino-cli/internal/i18n" | ||
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func initSetDefaultCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command { | ||
var destDir string | ||
setDefaultCommand := &cobra.Command{ | ||
Use: "set-default", | ||
Short: i18n.Tr("Set the default build profile."), | ||
Long: i18n.Tr("Set the default build profile."), | ||
Example: "" + | ||
" " + os.Args[0] + " profile set-default my_profile\n", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runSetDefaultCommand(cmd.Context(), args, srv, destDir) | ||
}, | ||
} | ||
|
||
setDefaultCommand.Flags().StringVar(&destDir, "dest-dir", "", i18n.Tr("Location of the sketch project file.")) | ||
|
||
return setDefaultCommand | ||
} | ||
|
||
func runSetDefaultCommand(ctx context.Context, args []string, srv rpc.ArduinoCoreServiceServer, destDir string) { | ||
profileName := args[0] | ||
sketchPath := arguments.InitSketchPath(destDir) | ||
|
||
_, err := srv.ProfileSetDefault(ctx, &rpc.ProfileSetDefaultRequest{SketchPath: sketchPath.String(), ProfileName: profileName}) | ||
if err != nil { | ||
feedback.Fatal(i18n.Tr("Cannot set %s as default profile: %v", profileName, err), feedback.ErrGeneric) | ||
} | ||
feedback.Print(i18n.Tr("Default profile set to: %s", profileName)) | ||
} | ||
MatteoPologruto marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have some kind of internal API that we can use to retrieve platform <-> index URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc: @cmaglie