Skip to content

Commit 7efc412

Browse files
Before removing psresourcelist _exist
1 parent 7bd4ab8 commit 7efc412

File tree

5 files changed

+228
-149
lines changed

5 files changed

+228
-149
lines changed

src/dsc/psresourceget.ps1

Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class PSResource {
3939
PSResource([string]$name) {
4040
$this.name = $name
4141
$this._exist = $false
42-
$this._inDesiredState = $true
4342
}
4443

4544
[bool] IsInDesiredState([PSResource] $other) {
@@ -70,6 +69,19 @@ class PSResource {
7069
$retValue = $false
7170
}
7271

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+
7385
return $retValue
7486
}
7587

@@ -451,6 +463,35 @@ function ExportOperation {
451463
}
452464
}
453465

466+
filter Where-PSResource {
467+
param(
468+
[string]$name,
469+
[string]$version,
470+
[Scope]$scope,
471+
[string]$repositoryName
472+
)
473+
474+
process {
475+
$nameMatch = if ($name) { $_.Name -eq $name } else { $true }
476+
$versionMatch = if ($_.Version) {
477+
try {
478+
SatisfiesVersion -version $_.Version -versionRange $version
479+
}
480+
catch {
481+
$_.Version.ToString() -eq $version
482+
}
483+
}
484+
else { $true }
485+
$scopeMatch = if ($_.Scope) { $_.Scope -eq $scope } else { $true }
486+
$repositoryMatch = if ($_.Repository) { $_.Repository -eq $repositoryName } else { $true }
487+
488+
if ($nameMatch -and $versionMatch -and $scopeMatch -and $repositoryMatch) {
489+
Write-Trace -message "Resource matches filter criteria: Name=$($_.Name), Version=$($_.Version), Scope=$($_.Scope), Repository=$($_.Repository)" -level trace
490+
$_
491+
}
492+
}
493+
}
494+
454495
function SetPSResourceList {
455496
param(
456497
$inputObj
@@ -462,66 +503,78 @@ function SetPSResourceList {
462503

463504
$resourcesChanged = $false
464505

506+
$currentState = GetPSResourceList -inputObj $inputObj
507+
465508
$inputObj.resources | ForEach-Object {
466-
$resource = $_
509+
$resource = ConvertInputToPSResource -inputObj $_ -repositoryName $repositoryName
467510
$name = $resource.name
468511
$version = $resource.version
469512
$scope = $resource.scope ?? "CurrentUser"
470513

471-
$getSplat = @{
472-
Name = $name
473-
Scope = $scope
474-
ErrorAction = 'SilentlyContinue'
475-
}
514+
# Resource should not exist - uninstall if it does
515+
$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
476517

477-
$existingResources = if ($repositoryName) {
478-
Get-PSResource @getSplat | Where-Object { $_.Repository -eq $repositoryName }
479-
}
480-
else {
481-
Get-PSResource @getSplat
482-
}
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
520+
$resourcesToUninstall += $_
521+
}
483522

484-
if (-not $existingResources) {
485-
# No existing resources found, add to install list if _exist is true or not specified
486-
if ($resource._exist -ne $false) {
487-
$key = $name.ToLowerInvariant() + '-' + ($version ?? 'latest').ToLowerInvariant()
488-
if (-not $resourcesToInstall.ContainsKey($key)) {
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()
489531
$resourcesToInstall[$key] = $resource
490532
}
491533
}
492-
# If _exist is false and resource doesn't exist, nothing to do (already in desired state)
493534
}
494-
else {
495-
# Existing resources found
496-
if ($resource._exist -eq $false) {
497-
# User wants resource removed - uninstall all existing versions
498-
$resourcesToUninstall += $existingResources
499-
}
500-
elseif ($version) {
501-
# Version specified - check if any existing version satisfies the range
502-
$satisfyingResource = $null
503-
foreach ($existing in $existingResources) {
504-
$versionRange = [NuGet.Versioning.VersionRange]::Parse($version)
505-
$resourceVersion = [NuGet.Versioning.NuGetVersion]::Parse($existing.Version.ToString())
506-
if ($versionRange.Satisfies($resourceVersion)) {
507-
$satisfyingResource = $existing
508-
break
509-
}
510-
}
511535

512-
if (-not $satisfyingResource) {
513-
# No existing version satisfies the range - install desired version
514-
$key = $name.ToLowerInvariant() + '-' + $version.ToLowerInvariant()
515-
if (-not $resourcesToInstall.ContainsKey($key)) {
516-
$resourcesToInstall[$key] = $resource
517-
}
518-
# Uninstall versions that don't satisfy the range
519-
$resourcesToUninstall += $existingResources
520-
}
521-
# If a satisfying version exists, resource is in desired state
522-
}
523-
# If no version specified and _exist is true/not specified, any existing version is acceptable
524-
}
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+
525578
}
526579

527580
if ($resourcesToUninstall.Count -gt 0) {

0 commit comments

Comments
 (0)