@@ -127,19 +127,25 @@ func (p *Profile) RequireSystemInstalledPlatform() bool {
127
127
return p .Platforms [0 ].RequireSystemInstalledPlatform ()
128
128
}
129
129
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 {
133
132
if l .Library == libraryName {
134
- if toDelete {
135
- p .Libraries = slices .Delete (p .Libraries , i , i + 1 )
136
- }
137
133
return l , nil
138
134
}
139
135
}
140
136
return nil , & cmderrors.LibraryNotFoundError {Library : libraryName }
141
137
}
142
138
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
+
143
149
// ToRpc converts this Profile to an rpc.SketchProfile
144
150
func (p * Profile ) ToRpc () * rpc.SketchProfile {
145
151
var portConfig * rpc.MonitorPortConfiguration
@@ -392,6 +398,25 @@ func (l *ProfileLibraryReference) String() string {
392
398
return fmt .Sprintf ("%s@%s" , l .Library , l .Version )
393
399
}
394
400
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
395
420
func (l * ProfileLibraryReference ) ToRpc () * rpc.ProfileLibraryReference {
396
421
if l .InstallDir != nil {
397
422
return & rpc.ProfileLibraryReference {
@@ -412,6 +437,29 @@ func (l *ProfileLibraryReference) ToRpc() *rpc.ProfileLibraryReference {
412
437
}
413
438
}
414
439
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
+
415
463
// InternalUniqueIdentifier returns the unique identifier for this object
416
464
func (l * ProfileLibraryReference ) InternalUniqueIdentifier () string {
417
465
f .Assert (l .InstallDir == nil ,
0 commit comments