Skip to content

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
28 changes: 27 additions & 1 deletion commands/cmderrors/cmderrors.go
Original file line number Diff line number Diff line change
@@ -211,6 +211,20 @@ func (e *UnknownProfileError) GRPCStatus() *status.Status {
return status.New(codes.NotFound, e.Error())
}

// DuplicateProfileError is returned when the profile is a duplicate of an already existing one
type DuplicateProfileError struct {
Profile string
}

func (e *DuplicateProfileError) Error() string {
return i18n.Tr("Profile '%s' already exists", e.Profile)
}

// GRPCStatus converts the error into a *status.Status
func (e *DuplicateProfileError) GRPCStatus() *status.Status {
return status.New(codes.AlreadyExists, e.Error())
}

// InvalidProfileError is returned when the profile has errors
type InvalidProfileError struct {
Cause error
@@ -435,7 +449,7 @@ func (e *PlatformLoadingError) Unwrap() error {
return e.Cause
}

// LibraryNotFoundError is returned when a platform is not found
// LibraryNotFoundError is returned when a library is not found
type LibraryNotFoundError struct {
Library string
Cause error
@@ -883,3 +897,15 @@ func (e *InstanceNeedsReinitialization) GRPCStatus() *status.Status {
WithDetails(&rpc.InstanceNeedsReinitializationError{})
return st
}

// MissingProfileError is returned when the Profile is mandatory and not specified
type MissingProfileError struct{}

func (e *MissingProfileError) Error() string {
return i18n.Tr("Missing Profile name")
}

// GRPCStatus converts the error into a *status.Status
func (e *MissingProfileError) GRPCStatus() *status.Status {
return status.New(codes.InvalidArgument, e.Error())
}
47 changes: 47 additions & 0 deletions commands/service_profile_dump.go
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())}
}
}
109 changes: 109 additions & 0 deletions commands/service_profile_init.go
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?
Copy link
Contributor

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @cmaglie

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
}
81 changes: 81 additions & 0 deletions commands/service_profile_lib_add.go
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
}

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 lib install:

https://arduino.github.io/arduino-cli/dev/commands/arduino-cli_lib_install/#options

--no-deps                  Do not install dependencies.


// 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
}
61 changes: 61 additions & 0 deletions commands/service_profile_lib_remove.go
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
}
51 changes: 51 additions & 0 deletions commands/service_profile_set_default.go
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
}
21 changes: 21 additions & 0 deletions internal/arduino/sketch/profiles.go
Original file line number Diff line number Diff line change
@@ -22,8 +22,10 @@ import (
"fmt"
"net/url"
"regexp"
"slices"
"strings"

"github.com/arduino/arduino-cli/commands/cmderrors"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -119,6 +121,19 @@ type Profile struct {
Libraries ProfileRequiredLibraries `yaml:"libraries"`
}

// GetLibrary returns the requested library or an error if not found
func (p *Profile) GetLibrary(libraryName string, toDelete bool) (*ProfileLibraryReference, error) {
for i, l := range p.Libraries {
if l.Library == libraryName {
if toDelete {
p.Libraries = slices.Delete(p.Libraries, i, i+1)
}
return l, nil
}
}
return nil, &cmderrors.LibraryNotFoundError{Library: libraryName}
}

// ToRpc converts this Profile to an rpc.SketchProfile
func (p *Profile) ToRpc() *rpc.SketchProfile {
var portConfig *rpc.MonitorPortConfiguration
@@ -174,6 +189,9 @@ type ProfileRequiredPlatforms []*ProfilePlatformReference

// AsYaml outputs the required platforms as Yaml
func (p *ProfileRequiredPlatforms) AsYaml() string {
if len(*p) == 0 {
return " platforms: []\n"
}
res := " platforms:\n"
for _, platform := range *p {
res += platform.AsYaml()
@@ -187,6 +205,9 @@ type ProfileRequiredLibraries []*ProfileLibraryReference

// AsYaml outputs the required libraries as Yaml
func (p *ProfileRequiredLibraries) AsYaml() string {
if len(*p) == 0 {
return " libraries: []\n"
}
res := " libraries:\n"
for _, lib := range *p {
res += lib.AsYaml()
2 changes: 2 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ import (
"github.com/arduino/arduino-cli/internal/cli/lib"
"github.com/arduino/arduino-cli/internal/cli/monitor"
"github.com/arduino/arduino-cli/internal/cli/outdated"
"github.com/arduino/arduino-cli/internal/cli/profile"
"github.com/arduino/arduino-cli/internal/cli/sketch"
"github.com/arduino/arduino-cli/internal/cli/update"
"github.com/arduino/arduino-cli/internal/cli/updater"
@@ -162,6 +163,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
cmd.AddCommand(burnbootloader.NewCommand(srv))
cmd.AddCommand(version.NewCommand(srv))
cmd.AddCommand(feedback.NewCommand())
cmd.AddCommand(profile.NewCommand(srv))

cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, i18n.Tr("Print the logs on the standard output."))
cmd.Flag("verbose").Hidden = true
93 changes: 93 additions & 0 deletions internal/cli/profile/dump.go
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)
}
78 changes: 78 additions & 0 deletions internal/cli/profile/init.go
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)
}
168 changes: 168 additions & 0 deletions internal/cli/profile/lib.go
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)
}
46 changes: 46 additions & 0 deletions internal/cli/profile/profile.go
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
}
57 changes: 57 additions & 0 deletions internal/cli/profile/set_default.go
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))
}
254 changes: 254 additions & 0 deletions internal/integrationtest/profiles/profiles_test.go
Original file line number Diff line number Diff line change
@@ -150,3 +150,257 @@ func TestCompileWithDefaultProfile(t *testing.T) {
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:nano" ]`)
}
}

func TestInitProfile(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("sketch", "new", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

integrationtest.CLISubtests{
{"NoProfile", initNoProfile},
{"ProfileCorrectFQBN", initWithCorrectFqbn},
{"ProfileWrongFQBN", initWithWrongFqbn},
{"ProfileMissingFQBN", initMissingFqbn},
{"ExistingProfile", initExistingProfile},
{"SetDefaultProfile", initSetDefaultProfile},
}.Run(t, env, cli)
}

func initNoProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
projectFile := cli.SketchbookDir().Join("Simple", "sketch.yaml")
// Create an empty project file
stdout, _, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
require.Contains(t, string(stdout), "Project file created in: "+projectFile.String())
require.FileExists(t, projectFile.String())
fileContent, err := projectFile.ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles: {}\n", string(fileContent))
}

func initWithCorrectFqbn(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
projectFile := cli.SketchbookDir().Join("Simple", "sketch.yaml")
// Add a profile with a correct FQBN
_, _, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.NoError(t, err)
require.FileExists(t, projectFile.String())
fileContent, err := projectFile.ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries: []\n\ndefault_profile: Uno\n", string(fileContent))
}

func initWithWrongFqbn(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Adding a profile with an incorrect FQBN should return an error
_, stderr, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "wrong_fqbn", "-b", "foo:bar")
require.Error(t, err)
require.Contains(t, string(stderr), "Invalid FQBN")
}

func initMissingFqbn(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Add a profile with no FQBN should return an error
_, stderr, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno")
require.Error(t, err)
require.Contains(t, string(stderr), "Missing FQBN (Fully Qualified Board Name)")
}

func initExistingProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Adding a profile with a name that already exists should return an error
_, stderr, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.Error(t, err)
require.Contains(t, string(stderr), "Profile 'Uno' already exists")
}

func initSetDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
// Adding a profile with a name that already exists should return an error
_, _, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "new_profile", "-b", "arduino:avr:uno", "--default")
require.NoError(t, err)
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFileAsLines()
require.NoError(t, err)
require.Contains(t, fileContent, "default_profile: new_profile")
}

func TestInitProfileMissingSketchFile(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, stderr, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String())
require.Error(t, err)
require.Contains(t, string(stderr), "no such file or directory")

err = cli.SketchbookDir().Join("Simple").MkdirAll()
require.NoError(t, err)
_, stderr, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String())
require.Error(t, err)
require.Contains(t, string(stderr), "main file missing from sketch")
}

func TestInitProfilePlatformNotInstalled(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("sketch", "new", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)

// Adding a profile with a name that already exists should return an error
_, stderr, err := cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.Error(t, err)
require.Contains(t, string(stderr), "platform not installed")
}

func TestProfileLib(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("sketch", "new", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

_, _, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.NoError(t, err)

integrationtest.CLISubtests{
{"AddLibToDefaultProfile", addLibToDefaultProfile},
{"ChangeLibVersionDefaultProfile", changeLibVersionDefaultProfile},
{"RemoveLibFromDefaultProfile", removeLibFromDefaultProfile},
{"AddInexistentLibToDefaultProfile", addInexistentLibToDefaultProfile},
{"RemoveLibNotInDefaultProfile", removeLibNotInDefaultProfile},
}.Run(t, env, cli)
}

func addLibToDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
_, _, err := cli.Run("profile", "lib", "add", "Modulino@0.5.0", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Modulino (0.5.0)\n\ndefault_profile: Uno\n", string(fileContent))
}

func changeLibVersionDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Modulino (0.5.0)\n\ndefault_profile: Uno\n", string(fileContent))

_, _, err = cli.Run("profile", "lib", "add", "Modulino@0.4.0", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err = cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Modulino (0.4.0)\n\ndefault_profile: Uno\n", string(fileContent))
}

func removeLibFromDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
_, _, err := cli.Run("profile", "lib", "remove", "Modulino", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.Equal(t, "profiles:\n Uno:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries: []\n\ndefault_profile: Uno\n", string(fileContent))
}

func addInexistentLibToDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
_, stderr, err := cli.Run("profile", "lib", "add", "foobar", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.Error(t, err)
require.Equal(t, "Error adding foobar to the profile : Library 'foobar@latest' not found\n", string(stderr))
}

func removeLibNotInDefaultProfile(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
_, stderr, err := cli.Run("profile", "lib", "remove", "Arduino_JSON", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.Error(t, err)
require.Equal(t, "Error removing Arduino_JSON from the profile : Library 'Arduino_JSON' not found\n", string(stderr))
}

func TestProfileLibSpecificProfile(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("sketch", "new", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

_, _, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.NoError(t, err)

// Add a second profile
_, _, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "my_profile", "-b", "arduino:avr:uno")
require.NoError(t, err)

// Add library to a specific profile
_, _, err = cli.Run("profile", "lib", "add", "Modulino@0.5.0", "-m", "my_profile", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.Contains(t, string(fileContent), " my_profile:\n fqbn: arduino:avr:uno\n platforms:\n - platform: arduino:avr (1.8.6)\n libraries:\n - Modulino (0.5.0)\n")

// Remove library from a specific profile
_, _, err = cli.Run("profile", "lib", "remove", "Modulino", "-m", "my_profile", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err = cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFile()
require.NoError(t, err)
require.NotContains(t, string(fileContent), "- Modulino (0.5.0)")
}

func TestProfileSetDefault(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

// Init the environment explicitly
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("sketch", "new", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

_, _, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "Uno", "-b", "arduino:avr:uno")
require.NoError(t, err)

// Add a second profile
_, _, err = cli.Run("profile", "init", cli.SketchbookDir().Join("Simple").String(), "-m", "my_profile", "-b", "arduino:avr:uno")
require.NoError(t, err)
fileContent, err := cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFileAsLines()
require.NoError(t, err)
require.Contains(t, fileContent, "default_profile: Uno")
require.NotContains(t, fileContent, "default_profile: my_profile")

// Change default profile
_, _, err = cli.Run("profile", "set-default", "my_profile", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.NoError(t, err)
fileContent, err = cli.SketchbookDir().Join("Simple", "sketch.yaml").ReadFileAsLines()
require.NoError(t, err)
require.NotContains(t, fileContent, "default_profile: Uno")
require.Contains(t, fileContent, "default_profile: my_profile")

// Changing to an inexistent profile returns an error
_, stderr, err := cli.Run("profile", "set-default", "inexistent_profile", "--dest-dir", cli.SketchbookDir().Join("Simple").String())
require.Error(t, err)
require.Equal(t, "Cannot set inexistent_profile as default profile: Profile 'inexistent_profile' not found\n", string(stderr))
}
2,041 changes: 1,453 additions & 588 deletions rpc/cc/arduino/cli/commands/v1/commands.pb.go

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/commands.proto
Original file line number Diff line number Diff line change
@@ -190,6 +190,21 @@ service ArduinoCoreService {

// Set a single configuration value.
rpc SettingsSetValue(SettingsSetValueRequest) returns (SettingsSetValueResponse);

// Create the sketch project file and add a build profile to it.
rpc InitProfile(InitProfileRequest) returns (InitProfileResponse) {}

// Add a library to the build profile.
rpc ProfileLibAdd(ProfileLibAddRequest) returns (ProfileLibAddResponse) {}

// Remove a library from the build profile.
rpc ProfileLibRemove(ProfileLibRemoveRequest) returns (ProfileLibRemoveResponse) {}

// Set the default build profile.
rpc ProfileSetDefault(ProfileSetDefaultRequest) returns (ProfileSetDefaultResponse) {}

// Dump the sketch project file.
rpc ProfileDump(ProfileDumpRequest) returns (ProfileDumpResponse) {}
}

message CreateRequest {}
@@ -419,3 +434,82 @@ message CleanDownloadCacheDirectoryRequest {
}

message CleanDownloadCacheDirectoryResponse {}

message InitProfileRequest {
// An Arduino Core instance.
Instance instance = 1;
// Absolute path to Sketch folder.
string sketch_path = 2;
// Name of the profile.
string profile_name = 3;
// FQBN of the provided profile.
string fqbn = 4;
// Set the profile as the default one.
bool default_profile = 5;
}

message InitProfileResponse {
// Absolute path to the project file.
string project_file_path = 1;
}

message ProfileLibAddRequest {
// An Arduino Core instance.
Instance instance = 1;
// Absolute path to Sketch folder.
string sketch_path = 2;
// Name of the profile.
string profile_name = 3;
// Name of the library.
string lib_name = 4;
// Version of the library
string lib_version = 5;
}

message ProfileLibAddResponse {
// Name of the library.
string lib_name = 1;
// Version of the library
string lib_version = 2;
// Name of the profile.
string profile_name = 3;
}

message ProfileLibRemoveRequest {
// Absolute path to Sketch folder.
string sketch_path = 1;
// Name of the profile.
string profile_name = 2;
// Name of the library.
string lib_name = 3;
}

message ProfileLibRemoveResponse {
// Name of the library.
string lib_name = 1;
// Version of the library
string lib_version = 2;
// Name of the profile.
string profile_name = 3;
}

message ProfileSetDefaultRequest {
// Absolute path to Sketch folder.
string sketch_path = 1;
// Name of the profile.
string profile_name = 2;
}

message ProfileSetDefaultResponse {}

message ProfileDumpRequest {
// Absolute path to Sketch folder.
string sketch_path = 1;
// The format of the dump (default is "json", allowed values are "json", and "yaml").
string dump_format = 2;
}

message ProfileDumpResponse {
// The encoded profile dump.
string encoded_profile = 1;
}
200 changes: 200 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/commands_grpc.pb.go