Skip to content

Commit 81fa91b

Browse files
committed
ProfileRemoveLibrary now accepts a full ProfileLibraryReference object
1 parent a8f527b commit 81fa91b

File tree

6 files changed

+705
-649
lines changed

6 files changed

+705
-649
lines changed

commands/service_profile_lib_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (s *arduinoCoreServerImpl) ProfileLibAdd(ctx context.Context, req *rpc.Prof
7272
return nil, err
7373
}
7474
// If the library has been already added to the profile, just update the version
75-
if lib, _ := profile.GetLibrary(reqIndexLib.GetName(), false); lib != nil {
75+
if lib, _ := profile.GetLibrary(reqIndexLib.GetName()); lib != nil {
7676
lib.Version = libRelease.GetVersion()
7777
addedLib = lib
7878
} else {

commands/service_profile_lib_remove.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ func (s *arduinoCoreServerImpl) ProfileLibRemove(ctx context.Context, req *rpc.P
4747
return nil, err
4848
}
4949

50-
lib, err := profile.GetLibrary(req.LibName, true)
50+
libToRemove, err := sketch.FromRpcProfileLibraryReference(req.GetLibrary())
5151
if err != nil {
52-
return nil, err
52+
return nil, &cmderrors.InvalidArgumentError{Message: "invalid library reference", Cause: err}
53+
}
54+
removedLib, err := profile.RemoveLibrary(libToRemove)
55+
if err != nil {
56+
return nil, &cmderrors.InvalidArgumentError{Message: "failed to remove library", Cause: err}
5357
}
5458

5559
err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml()))
5660
if err != nil {
5761
return nil, err
5862
}
5963

60-
return &rpc.ProfileLibRemoveResponse{LibName: lib.Library, LibVersion: lib.Version.String(), ProfileName: profileName}, nil
64+
return &rpc.ProfileLibRemoveResponse{Library: removedLib.ToRpc(), ProfileName: profileName}, nil
6165
}

internal/arduino/sketch/profiles.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,25 @@ func (p *Profile) RequireSystemInstalledPlatform() bool {
127127
return p.Platforms[0].RequireSystemInstalledPlatform()
128128
}
129129

130-
// GetLibrary returns the requested library or an error if not found
131-
func (p *Profile) GetLibrary(libraryName string, toDelete bool) (*ProfileLibraryReference, error) {
132-
for i, l := range p.Libraries {
130+
func (p *Profile) GetLibrary(libraryName string) (*ProfileLibraryReference, error) {
131+
for _, l := range p.Libraries {
133132
if l.Library == libraryName {
134-
if toDelete {
135-
p.Libraries = slices.Delete(p.Libraries, i, i+1)
136-
}
137133
return l, nil
138134
}
139135
}
140136
return nil, &cmderrors.LibraryNotFoundError{Library: libraryName}
141137
}
142138

139+
func (p *Profile) RemoveLibrary(library *ProfileLibraryReference) (*ProfileLibraryReference, error) {
140+
for i, l := range p.Libraries {
141+
if l.Match(library) {
142+
p.Libraries = slices.Delete(p.Libraries, i, i+1)
143+
return l, nil
144+
}
145+
}
146+
return nil, &cmderrors.LibraryNotFoundError{Library: library.String()}
147+
}
148+
143149
// ToRpc converts this Profile to an rpc.SketchProfile
144150
func (p *Profile) ToRpc() *rpc.SketchProfile {
145151
var portConfig *rpc.MonitorPortConfiguration
@@ -392,6 +398,25 @@ func (l *ProfileLibraryReference) String() string {
392398
return fmt.Sprintf("%s@%s", l.Library, l.Version)
393399
}
394400

401+
// Match checks if this library reference matches another one.
402+
// If one reference has the version not set, it matches any version of the other reference.
403+
func (l *ProfileLibraryReference) Match(other *ProfileLibraryReference) bool {
404+
if l.InstallDir != nil {
405+
return other.InstallDir != nil && l.InstallDir.EqualsTo(other.InstallDir)
406+
}
407+
if other.InstallDir != nil {
408+
return false
409+
}
410+
if l.Library != other.Library {
411+
return false
412+
}
413+
if l.Version == nil || other.Version == nil {
414+
return true
415+
}
416+
return l.Version.Equal(other.Version)
417+
}
418+
419+
// ToRpc converts this ProfileLibraryReference to an rpc.ProfileLibraryReference
395420
func (l *ProfileLibraryReference) ToRpc() *rpc.ProfileLibraryReference {
396421
if l.InstallDir != nil {
397422
return &rpc.ProfileLibraryReference{
@@ -412,6 +437,29 @@ func (l *ProfileLibraryReference) ToRpc() *rpc.ProfileLibraryReference {
412437
}
413438
}
414439

440+
// FromRpcProfileLibraryReference converts an rpc.ProfileLibraryReference to a ProfileLibraryReference
441+
func FromRpcProfileLibraryReference(l *rpc.ProfileLibraryReference) (*ProfileLibraryReference, error) {
442+
if localLib := l.GetLocalLibrary(); localLib != nil {
443+
path := paths.New(localLib.GetPath())
444+
if path == nil {
445+
return nil, &cmderrors.InvalidArgumentError{Message: "invalid library path"}
446+
}
447+
return &ProfileLibraryReference{InstallDir: path}, nil
448+
}
449+
if indexLib := l.GetIndexLibrary(); indexLib != nil {
450+
var version *semver.Version
451+
if indexLib.GetVersion() != "" {
452+
v, err := semver.Parse(indexLib.GetVersion())
453+
if err != nil {
454+
return nil, &cmderrors.InvalidVersionError{Cause: err}
455+
}
456+
version = v
457+
}
458+
return &ProfileLibraryReference{Library: indexLib.GetName(), Version: version}, nil
459+
}
460+
return nil, &cmderrors.InvalidArgumentError{Message: "library not specified"}
461+
}
462+
415463
// InternalUniqueIdentifier returns the unique identifier for this object
416464
func (l *ProfileLibraryReference) InternalUniqueIdentifier() string {
417465
f.Assert(l.InstallDir == nil,

internal/cli/profile/lib.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,21 @@ func runLibRemoveCommand(ctx context.Context, args []string, srv rpc.ArduinoCore
139139
resp, err := srv.ProfileLibRemove(ctx, &rpc.ProfileLibRemoveRequest{
140140
SketchPath: sketchPath.String(),
141141
ProfileName: profileArg.Get(),
142-
LibName: lib.Name,
142+
Library: &rpc.ProfileLibraryReference{
143+
Library: &rpc.ProfileLibraryReference_IndexLibrary_{
144+
IndexLibrary: &rpc.ProfileLibraryReference_IndexLibrary{
145+
Name: lib.Name,
146+
Version: lib.Version,
147+
},
148+
},
149+
},
143150
})
144151
if err != nil {
145152
feedback.Fatal(i18n.Tr("Error removing %s from the profile %s: %v", lib.Name, profileArg.Get(), err), feedback.ErrGeneric)
146153
}
147-
feedback.PrintResult(libRemoveResult{LibName: resp.GetLibName(), LibVersion: resp.GetLibVersion(), ProfileName: resp.ProfileName})
154+
feedback.PrintResult(libRemoveResult{
155+
Library: result.NewProfileLibraryReference_IndexLibraryResult(resp.GetLibrary().GetIndexLibrary()),
156+
ProfileName: resp.ProfileName})
148157
}
149158
}
150159

@@ -162,15 +171,14 @@ func (lr libAddResult) String() string {
162171
}
163172

164173
type libRemoveResult struct {
165-
LibName string `json:"library_name"`
166-
LibVersion string `json:"library_version"`
167-
ProfileName string `json:"profile_name"`
174+
Library *result.ProfileLibraryReference_IndexLibraryResult `json:"library"`
175+
ProfileName string `json:"profile_name"`
168176
}
169177

170178
func (lr libRemoveResult) Data() interface{} {
171179
return lr
172180
}
173181

174182
func (lr libRemoveResult) String() string {
175-
return i18n.Tr("Profile %s: %s@%s removed successfully", lr.ProfileName, lr.LibName, lr.LibVersion)
183+
return i18n.Tr("Profile %s: %s@%s removed successfully", lr.ProfileName, lr.Library.Name, lr.Library.Version)
176184
}

0 commit comments

Comments
 (0)