@@ -44,6 +44,19 @@ class PSResource {
4444 [bool ] IsInDesiredState([PSResource ] $other ) {
4545 $retValue = $true
4646
47+ $psResourceSplat = @ {
48+ Name = $this.name
49+ Version = if ($this.version ) { $this.version } else { ' *' }
50+ }
51+
52+ Get-PSResource @psResourceSplat | Where-Object {
53+ ($null -eq $this.scope -or $_.Scope -eq $this.scope ) -and
54+ ($null -eq $this.repositoryName -or $_.Repository -eq $this.repositoryName )
55+ } | Select-Object - First 1 | ForEach-Object {
56+ Write-Trace - message " Matching resource found: Name=$ ( $_.Name ) , Version=$ ( $_.Version ) , Scope=$ ( $_.Scope ) , Repository=$ ( $_.Repository ) , PreRelease=$ ( $_.PreRelease ) " - level trace
57+ $this._exist = $true
58+ }
59+
4760 if ($this.name -ne $other.name ) {
4861 Write-Trace - message " Name mismatch: $ ( $this.name ) vs $ ( $other.name ) " - level trace
4962 $retValue = $false
@@ -60,28 +73,11 @@ class PSResource {
6073 Write-Trace - message " Repository mismatch: $ ( $this.repositoryName ) vs $ ( $other.repositoryName ) " - level trace
6174 $retValue = $false
6275 }
63- elseif ($null -ne $this.preRelease -and $this.preRelease -ne $other.preRelease ) {
64- Write-Trace - message " PreRelease mismatch: $ ( $this.preRelease ) vs $ ( $other.preRelease ) " - level trace
65- $retValue = $false
66- }
6776 elseif ($this._exist -ne $other._exist ) {
6877 Write-Trace - message " _exist mismatch: $ ( $this._exist ) vs $ ( $other._exist ) " - level trace
6978 $retValue = $false
7079 }
7180
72- $psResourceSplat = @ {
73- Name = $this.name
74- Version = $this.version
75- }
76-
77- Get-PSResource @psResourceSplat | Where-Object {
78- ($null -eq $this.scope -or $_.Scope -eq $this.scope ) -and
79- ($null -eq $this.repositoryName -or $_.Repository -eq $this.repositoryName )
80- } | Select-Object - First 1 | ForEach-Object {
81- Write-Trace - message " Matching resource found: Name=$ ( $_.Name ) , Version=$ ( $_.Version ) , Scope=$ ( $_.Scope ) , Repository=$ ( $_.Repository ) , PreRelease=$ ( $_.PreRelease ) " - level trace
82- $this._exist = $true
83- }
84-
8581 return $retValue
8682 }
8783
@@ -98,7 +94,6 @@ class PSResourceList {
9894 [string ]$repositoryName
9995 [PSResource []]$resources
10096 [bool ]$trustedRepository
101- [bool ]$_exist
10297 [bool ]$_inDesiredState
10398
10499 PSResourceList([string ]$repositoryName , [PSResource []]$resources , [bool ]$trustedRepository ) {
@@ -149,7 +144,10 @@ class PSResourceList {
149144 }
150145
151146 [string ] ToJsonForTest() {
152- return ($this | ConvertTo-Json - Compress - Depth 5 )
147+ Write-Trace - message " Serializing PSResourceList to JSON for test output. RepositoryName: $ ( $this.repositoryName ) , TrustedRepository: $ ( $this.trustedRepository ) , Resources count: $ ( $this.resources.Count ) " - level trace
148+ $jsonForTest = $this | ConvertTo-Json - Compress - Depth 5
149+ Write-Trace - message " Serialized JSON: $jsonForTest " - level trace
150+ return $jsonForTest
153151 }
154152}
155153
@@ -239,13 +237,19 @@ function ConvertInputToPSResource(
239237) {
240238 $scope = if ($inputObj.Scope ) { [Scope ]$inputObj.Scope } else { [Scope ]" CurrentUser" }
241239
242- return [PSResource ]::new(
240+ $psResource = [PSResource ]::new(
243241 $inputObj.Name ,
244242 $inputObj.Version ,
245243 $scope ,
246244 $inputObj.repositoryName ? $inputObj.repositoryName : $repositoryName ,
247245 $inputObj.PreRelease
248246 )
247+
248+ if ($null -ne $inputObj._exist ) {
249+ $psResource._exist = $inputObj._exist
250+ }
251+
252+ return $psResource
249253}
250254
251255# catch any un-caught exception and write it to the error stream
@@ -506,75 +510,35 @@ function SetPSResourceList {
506510 $currentState = GetPSResourceList - inputObj $inputObj
507511
508512 $inputObj.resources | ForEach-Object {
509- $resource = ConvertInputToPSResource - inputObj $_ - repositoryName $repositoryName
510- $name = $resource .name
511- $version = $resource .version
512- $scope = $resource .scope ?? " CurrentUser"
513+ $resourceDesiredState = ConvertInputToPSResource - inputObj $_ - repositoryName $repositoryName
514+ $name = $resourceDesiredState .name
515+ $version = $resourceDesiredState .version
516+ $scope = if ( $resourceDesiredState .scope ) { $resourceDesiredState .scope } else { " CurrentUser" }
513517
514518 # Resource should not exist - uninstall if it does
515519 $currentState.resources | Where - PSResource - name $name - version $version - scope $scope - repositoryName $repositoryName | ForEach-Object {
516- Write-Trace - message " Resource marked for uninstall: $ ( $_.Name ) version $ ( $_.Version ) " - level info
517520
518- if (-not $resource._exist -or -not $inputObj._exist ) {
519- Write-Trace - message " Resource $ ( $resource.name ) has _exist set to false and exists in current state. Adding to uninstall list." - level info
521+ $isInDesiredState = $_.IsInDesiredState ($resourceDesiredState )
522+
523+ # Uninstall if resource should not exist but does
524+ if (-not $resourceDesiredState._exist -and $_._exist ) {
525+ Write-Trace - message " Resource $ ( $resourceDesiredState.name ) exists but _exist is false. Adding to uninstall list." - level info
520526 $resourcesToUninstall += $_
521527 }
522-
523- if ($resource._exist -and $inputObj._exist ) {
524- Write-Trace - message " Resource $name has _exist set to true and exists in current state. Checking if it is in desired state." - level info
525- if ($resource.IsInDesiredState ($_ )) {
526- Write-Trace - message " Resource $name is in desired state. No action needed." - level info
527- }
528- else {
529- Write-Trace - message " Resource $name is NOT in desired state. Adding to install list." - level info
530- $key = $name.ToLowerInvariant () + ' -' + ($version ?? ' latest' ).ToLowerInvariant()
531- $resourcesToInstall [$key ] = $resource
528+ # Install if resource should exist but doesn't, or exists but not in desired state
529+ elseif ($resourceDesiredState._exist -and (-not $_._exist -or -not $isInDesiredState )) {
530+ Write-Trace - message " Resource $ ( $resourceDesiredState.name ) needs to be installed." - level info
531+ $versionStr = if ($version ) { $resourceDesiredState.version } else { ' latest' }
532+ $key = $name.ToLowerInvariant () + ' -' + $versionStr.ToLowerInvariant ()
533+ if (-not $resourcesToInstall.ContainsKey ($key )) {
534+ $resourcesToInstall [$key ] = $resourceDesiredState
532535 }
533536 }
537+ # Otherwise resource is in desired state, no action needed
538+ else {
539+ Write-Trace - message " Resource $ ( $resourceDesiredState.name ) is in desired state." - level info
540+ }
534541 }
535-
536- # if (-not $existingResources) {
537- # # No existing resources found, add to install list if _exist is true or not specified
538- # if ($resource._exist -ne $false) {
539- # $key = $name.ToLowerInvariant() + '-' + ($version ?? 'latest').ToLowerInvariant()
540- # if (-not $resourcesToInstall.ContainsKey($key)) {
541- # $resourcesToInstall[$key] = $resource
542- # }
543- # }
544- # # If _exist is false and resource doesn't exist, nothing to do (already in desired state)
545- # }
546- # else {
547- # # Existing resources found
548- # if ($resource._exist -eq $false) {
549- # # User wants resource removed - uninstall all existing versions
550- # $resourcesToUninstall += $existingResources
551- # }
552- # elseif ($version) {
553- # # Version specified - check if any existing version satisfies the range
554- # $satisfyingResource = $null
555- # foreach ($existing in $existingResources) {
556- # $versionRange = [NuGet.Versioning.VersionRange]::Parse($version)
557- # $resourceVersion = [NuGet.Versioning.NuGetVersion]::Parse($existing.Version.ToString())
558- # if ($versionRange.Satisfies($resourceVersion)) {
559- # $satisfyingResource = $existing
560- # break
561- # }
562- # }
563-
564- # if (-not $satisfyingResource) {
565- # # No existing version satisfies the range - install desired version
566- # $key = $name.ToLowerInvariant() + '-' + $version.ToLowerInvariant()
567- # if (-not $resourcesToInstall.ContainsKey($key)) {
568- # $resourcesToInstall[$key] = $resource
569- # }
570- # # Uninstall versions that don't satisfy the range
571- # $resourcesToUninstall += $existingResources
572- # }
573- # # If a satisfying version exists, resource is in desired state
574- # }
575- # # If no version specified and _exist is true/not specified, any existing version is acceptable
576- # }
577-
578542 }
579543
580544 if ($resourcesToUninstall.Count -gt 0 ) {
@@ -765,7 +729,7 @@ function PopulatePSResourceListObject {
765729 $repoGrps = $allPSResources | Group-Object - Property repositoryName
766730
767731 $repoGrps | ForEach-Object {
768- $repositoryTrust = Get-PSResourceRepository - Name $_.Name - ErrorAction SilentlyContinue | Select-Object - ExpandProperty Trusted ?? $false
732+ $repositoryTrust = if ( $_ .Name ) { ( Get-PSResourceRepository - Name $_.Name - ErrorAction SilentlyContinue).Trusted } else { $false }
769733 $repoName = $_.Name
770734 $resources = $_.Group
771735 [PSResourceList ]::new($repoName , $resources , $repositoryTrust ).ToJson()
0 commit comments