-
-
Notifications
You must be signed in to change notification settings - Fork 442
Added gRPC functions to manage libraries in profiles #3019
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
c7d0cb4
92cf64f
ed5d6a2
c74f073
34c378c
0c78b3f
3c5a3c9
4d78f7f
d4cbad2
140c069
5ac03d8
e17724b
4bb86e7
f520cb9
4e80e73
ce55e4b
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 |
|---|---|---|
|
|
@@ -31,6 +31,9 @@ func composeErrorMsg(msg string, cause error) string { | |
| if cause == nil { | ||
| return msg | ||
| } | ||
| if msg == "" { | ||
| return cause.Error() | ||
| } | ||
| return fmt.Sprintf("%v: %v", msg, cause) | ||
| } | ||
|
|
||
|
|
@@ -212,6 +215,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 | ||
|
|
@@ -456,7 +473,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 | ||
|
|
@@ -904,3 +921,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{} | ||
|
Contributor
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.
|
||
|
|
||
| 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()) | ||
|
Contributor
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 would be better to use |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // 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" | ||
|
|
||
| "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" | ||
| ) | ||
|
|
||
| // ProfileCreate 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) ProfileCreate(ctx context.Context, req *rpc.ProfileCreateRequest) (*rpc.ProfileCreateResponse, 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{} | ||
| } | ||
| fqbn, err := fqbn.Parse(req.GetFqbn()) | ||
| if err != nil { | ||
| return nil, &cmderrors.InvalidFQBNError{Cause: err} | ||
| } | ||
|
|
||
| // 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 | ||
| } | ||
| defer release() | ||
| if pme.Dirty() { | ||
| return nil, &cmderrors.InstanceNeedsReinitialization{} | ||
| } | ||
|
|
||
| // 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? | ||
|
Contributor
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. Great question, preferably, the CLI can tell if a resolved platform was detected by one of the additional board (aka. 3rd party) URLs. A missing feature (prop) on the platform object? |
||
| 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 | ||
|
Comment on lines
+93
to
+95
Contributor
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 comment does not reflect the code, but the code itself is strange; the client requests a profile creation, it explicitly sets |
||
| } | ||
|
|
||
| err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml())) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return &rpc.ProfileCreateResponse{ProjectFilePath: projectFilePath.String()}, 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.
Proposal: Why not call it
ProfileAlreadyExitsError, and clients know exactly what it means.