-
-
Notifications
You must be signed in to change notification settings - Fork 406
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
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 license@arduino.cc. | ||||||
|
||||||
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": | ||||||
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.
Suggested change
Let's allow a default value if the DumpFormat is not specified to be |
||||||
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,114 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
// 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 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) { | ||||||||||||||||||||||||||||||||||||||||||||||
sketchPath := paths.New(req.GetSketchPath()) | ||||||||||||||||||||||||||||||||||||||||||||||
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs() | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
// 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(sketchPath) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+36
to
+46
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.
Suggested change
Can you please move this snippet of code under a private function? Something like |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
if !projectFilePath.Exist() { | ||||||||||||||||||||||||||||||||||||||||||||||
err := projectFilePath.WriteFile([]byte("profiles:\n")) | ||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||
return nil, err | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+48
to
+53
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. In the GRPC api, is possible to endup with a project file containing an invalid yaml: profiles:
This can happen if: the path doesn't exists and the ProfileName is empty
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
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? | ||||||||||||||||||||||||||||||||||||||||||||||
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. 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 commentThe 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 | ||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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 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) { | ||
sketchPath := paths.New(req.GetSketchPath()) | ||
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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(sketchPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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 | ||
} | ||
|
||
// 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,66 @@ | ||
// 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 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) { | ||
sketchPath := paths.New(req.GetSketchPath()) | ||
projectFilePath, err := sketchPath.Join("sketch.yaml").Abs() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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(sketchPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// 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 | ||
} |
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.