@@ -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
144150func (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
395420func (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
416464func (l * ProfileLibraryReference ) InternalUniqueIdentifier () string {
417465 f .Assert (l .InstallDir == nil ,
0 commit comments