1212 [Parameter (ValueFromPipeline )]
1313 $stdinput
1414)
15-
1615function Write-Trace {
1716 param (
1817 [string ]$message ,
@@ -23,7 +22,21 @@ function Write-Trace {
2322 $level.ToLower () = $message
2423 } | ConvertTo-Json - Compress
2524
26- $host.ui.WriteErrorLine ($trace )
25+ if ($level -eq ' Error' ) {
26+ $host.ui.WriteErrorLine ($trace )
27+ }
28+ elseif ($level -eq ' Warning' ) {
29+ $host.ui.WriteWarningLine ($trace )
30+ }
31+ elseif ($level -eq ' Verbose' ) {
32+ $host.ui.WriteVerboseLine ($trace )
33+ }
34+ elseif ($level -eq ' Debug' ) {
35+ $host.ui.WriteDebugLine ($trace )
36+ }
37+ else {
38+ $host.ui.WriteInformation ($trace )
39+ }
2740}
2841
2942# catch any un-caught exception and write it to the error stream
@@ -32,10 +45,6 @@ trap {
3245 exit 1
3346}
3447
35- function GetAllPSResources {
36- $resources = Get-PSResource
37- }
38-
3948function GetOperation {
4049 param (
4150 [string ]$ResourceType
@@ -55,13 +64,13 @@ function GetOperation {
5564 return $ret
5665 }
5766
58- ' repositories' { return ' Get-PSRepository ' }
59- ' psresource' { return ' Get- PSResource' }
67+ ' repositories' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for Repositories resource. " ) }
68+ ' psresource' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for PSResource resource. " ) }
6069 ' psresources' {
61-
62- $allPSResources = if ($inputObj.scope ) {
70+ $allPSResources = if ($inputObj.scope ) {
6371 Get-PSResource - Scope $inputObj.Scope
64- } else {
72+ }
73+ else {
6574 Get-PSResource
6675 }
6776
@@ -97,7 +106,8 @@ function GetOperation {
97106 }
98107
99108 PopulatePSResourcesObjectByRepository - resourcesExist $resourcesExist - inputResources $inputObj.resources - repositoryName $inputObj.repositoryName - scope $inputObj.Scope
100- }
109+ }
110+
101111 default { throw " Unknown ResourceType: $ResourceType " }
102112 }
103113}
@@ -112,16 +122,89 @@ function ExportOperation {
112122 }
113123 }
114124
115- ' repositories' { return ' Get-PSRepository ' }
116- ' psresource' { return ' Get- PSResource' }
125+ ' repositories' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for Repositories resource. " ) }
126+ ' psresource' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for PSResource resource. " ) }
117127 ' psresources' {
118128 $allPSResources = Get-PSResource
119129 PopulatePSResourcesObject - allPSResources $allPSResources
120- }
130+ }
121131 default { throw " Unknown ResourceType: $ResourceType " }
122132 }
123133}
124134
135+ function SetPSResources {
136+ param (
137+ $inputObj
138+ )
139+
140+ $repositoryName = $inputObj.repositoryName
141+ $scope = $inputObj.scope
142+
143+ if (-not $scope ) {
144+ $scope = ' CurrentUser'
145+ }
146+
147+ $resourcesToUninstall = @ ()
148+ $resourcesToInstall = [System.Collections.Generic.Dictionary [string , psobject ]]::new()
149+
150+ Add-Type - AssemblyName " $PSScriptRoot /dependencies/NuGet.Versioning.dll"
151+
152+ $inputObj.resources | ForEach-Object {
153+ $resource = $_
154+ $name = $resource.name
155+ $version = $resource.version
156+
157+ $getSplat = @ {
158+ Name = $name
159+ Scope = $scope
160+ ErrorAction = ' SilentlyContinue'
161+ }
162+
163+ $existingResources = if ($repositoryName ) {
164+ Get-PSResource @getSplat | Where-Object { $_.Repository -eq $repositoryName }
165+ }
166+ else {
167+ Get-PSResource @getSplat
168+ }
169+
170+ # uninstall all resources that do not satisfy the version range and install the ones that do
171+ $existingResources | ForEach-Object {
172+ $versionRange = [NuGet.Versioning.VersionRange ]::Parse($version )
173+ $resourceVersion = [NuGet.Versioning.NuGetVersion ]::Parse($_.Version.ToString ())
174+ if (-not $versionRange.Satisfies ($resourceVersion )) {
175+ if ($resource._exists ) {
176+ # $resourcesToInstall += $resource
177+ $key = $resource.Name.ToLowerInvariant () + ' -' + $resource.Version.ToLowerInvariant ()
178+ if (-not $resourcesToInstall.ContainsKey ($key )) {
179+ $resourcesToInstall [$key ] = $resource
180+ }
181+ }
182+
183+ $resourcesToUninstall += $_
184+ }
185+ else {
186+ if (-not $resource._exists ) {
187+ $resourcesToUninstall += $_
188+ }
189+ }
190+ }
191+ }
192+
193+ if ($resourcesToUninstall.Count -gt 0 ) {
194+ Write-Trace - message " Uninstalling resources: $ ( $resourcesToUninstall | ForEach-Object { " $ ( $_.Name ) - $ ( $_.Version ) " }) " - Level Verbose
195+ $resourcesToUninstall | ForEach-Object {
196+ Uninstall-PSResource - Name $_.Name - Scope $scope - ErrorAction Stop
197+ }
198+ }
199+
200+ if ($resourcesToInstall.Count -gt 0 ) {
201+ Write-Trace - message " Installing resources: $ ( $resourcesToInstall.Values | ForEach-Object { " $ ( $_.Name ) -- $ ( $_.Version ) " }) " - Level Verbose
202+ $resourcesToInstall.Values | ForEach-Object {
203+ Install-PSResource - Name $_.Name - Version $_.Version - Scope $scope - Repository $repositoryName - ErrorAction Stop
204+ }
205+ }
206+ }
207+
125208function SetOperation {
126209 param (
127210 [string ]$ResourceType
@@ -165,13 +248,12 @@ function SetOperation {
165248 return GetOperation - ResourceType $ResourceType
166249 }
167250
168- ' repositories' { return ' Set-PSRepository ' }
169- ' psresource' { return ' Set- PSResource' }
170- ' psresources' { return ' Set-PSResource ' }
251+ ' repositories' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for Repositories resource. " ) }
252+ ' psresource' { throw [ System.NotImplementedException ]::new( " Get operation is not implemented for PSResource resource. " ) }
253+ ' psresources' { return SetPSResources - inputObj $inputObj }
171254 default { throw " Unknown ResourceType: $ResourceType " }
172255 }
173256}
174-
175257function FilterPSResourcesByRepository {
176258 param (
177259 $allPSResources ,
@@ -247,15 +329,15 @@ function PopulatePSResourcesObject {
247329
248330 $resources = $_.Group | ForEach-Object {
249331 [pscustomobject ]@ {
250- name = $_.Name
251- version = $_.Version.ToString ()
252- _exists = $true
332+ name = $_.Name
333+ version = $_.Version.ToString ()
334+ _exists = $true
253335 }
254336 }
255337
256338 $resourcesObj = [pscustomobject ]@ {
257339 repositoryName = $repoName
258- resources = $resources
340+ resources = $resources
259341 }
260342
261343 $resourcesObj | ConvertTo-Json - Compress
@@ -297,8 +379,8 @@ function PopulateRepositoryObject {
297379}
298380
299381switch ($Operation.ToLower ()) {
300- ' get' { return (GetOperation - ResourceType $ResourceType ) }
301- ' set' { return (SetOperation - ResourceType $ResourceType ) }
382+ ' get' { return (GetOperation - ResourceType $ResourceType ) }
383+ ' set' { return (SetOperation - ResourceType $ResourceType ) }
302384 ' export' { return (ExportOperation - ResourceType $ResourceType ) }
303385 default { throw " Unknown Operation: $Operation " }
304386}
0 commit comments