Skip to content

Commit aca1d97

Browse files
PSresources
1 parent 6191b04 commit aca1d97

4 files changed

Lines changed: 406 additions & 23 deletions

File tree

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"csharp.semanticHighlighting.enabled": true,
33
"dotnet.automaticallyCreateSolutionInWorkspace": false,
44
"omnisharp.enableEditorConfigSupport": true,
5-
"omnisharp.enableRoslynAnalyzers": true
5+
"omnisharp.enableRoslynAnalyzers": true,
6+
"sarif-viewer.connectToGithubCodeScanning": "off"
67
}

src/dsc/psresourceget.ps1

Lines changed: 282 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1+
using namespace NuGet.Versioning
2+
13
[CmdletBinding()]
24
param(
35
[Parameter(Mandatory = $true)]
46
[ValidateSet('repository', 'psresource', 'repositories', 'psresources')]
57
[string]$ResourceType,
68
[Parameter(Mandatory = $true)]
7-
[ValidateSet('Get', 'Set', 'Export', 'Test')]
9+
[ValidateSet('get', 'set', 'export', 'test')]
810
[string]$Operation,
911
[Parameter(ValueFromPipeline)]
1012
$stdinput
1113
)
1214

13-
# catch any un-caught exception and write it to the error stream
14-
trap {
15-
Write-Trace -Level Error -message $_.Exception.Message
16-
exit 1
17-
}
18-
1915
function Write-Trace {
2016
param(
2117
[string]$message,
@@ -28,3 +24,282 @@ function Write-Trace {
2824

2925
$host.ui.WriteErrorLine($trace)
3026
}
27+
28+
# catch any un-caught exception and write it to the error stream
29+
trap {
30+
Write-Trace -Level Error -message $_.Exception.Message
31+
exit 1
32+
}
33+
34+
function GetAllPSResources {
35+
$resources = Get-PSResource
36+
37+
38+
}
39+
40+
function GetOperation {
41+
param(
42+
[string]$ResourceType
43+
)
44+
45+
$inputObj = $stdinput | ConvertFrom-Json -ErrorAction Stop
46+
47+
switch ($ResourceType) {
48+
'repository' {
49+
$rep = Get-PSResourceRepository -Name $inputObj.Name -ErrorVariable err -ErrorAction SilentlyContinue
50+
51+
if ($err.FullyQualifiedErrorId -eq 'ErrorGettingSpecifiedRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetPSResourceRepository') {
52+
return PopulateRepositoryObject -RepositoryInfo $null
53+
}
54+
55+
$ret = PopulateRepositoryObject -RepositoryInfo $rep
56+
return $ret
57+
}
58+
59+
'repositories' { return 'Get-PSRepository' }
60+
'psresource' { return 'Get-PSResource' }
61+
'psresources' {
62+
63+
$allPSResources = if ($inputObj.scope) {
64+
Get-PSResource -Scope $inputObj.Scope
65+
} else {
66+
Get-PSResource
67+
}
68+
69+
if ($inputObj.repositoryName) {
70+
$allPSResources = FilterPSResourcesByRepository -allPSResources $allPSResources -repositoryName $inputObj.repositoryName
71+
}
72+
73+
$resourcesExist = @()
74+
$resourcesMissing = @()
75+
76+
foreach ($resource in $allPSResources) {
77+
foreach ($inputResource in $inputObj.resources) {
78+
if ($resource.Name -eq $inputResource.Name) {
79+
if ($inputResource.Version) {
80+
# Use the NuGet.Versioning package if available, otherwise do a simple comparison
81+
try {
82+
$versionRange = [NuGet.Versioning.VersionRange]::Parse($inputResource.Version)
83+
$resourceVersion = [NuGet.Versioning.NuGetVersion]::Parse($resource.Version.ToString())
84+
if (-not $versionRange.Satisfies($resourceVersion)) {
85+
continue
86+
}
87+
else {
88+
$resourcesExist += $resource
89+
continue
90+
}
91+
} catch {
92+
# Fallback: simple string comparison (not full NuGet range support)
93+
if ($resource.Version.ToString() -ne $inputObj.resources.Version) {
94+
continue
95+
}
96+
}
97+
}
98+
}
99+
100+
$resourcesMissing += $resource
101+
}
102+
}
103+
104+
PopulatePSResourcesObjectByRepository -resourcesExist $resourcesExist -resourcesMissing $resourcesMissing -repositoryName $inputObj.repositoryName -scope $inputObj.Scope
105+
}
106+
default { throw "Unknown ResourceType: $ResourceType" }
107+
}
108+
}
109+
110+
function ExportOperation {
111+
switch ($ResourceType) {
112+
'repository' {
113+
$rep = Get-PSResourceRepository -ErrorAction Stop
114+
115+
$rep | ForEach-Object {
116+
PopulateRepositoryObject -RepositoryInfo $_
117+
}
118+
}
119+
120+
'repositories' { return 'Get-PSRepository' }
121+
'psresource' { return 'Get-PSResource' }
122+
'psresources' {
123+
$allPSResources = Get-PSResource
124+
PopulatePSResourcesObject -allPSResources $allPSResources
125+
}
126+
default { throw "Unknown ResourceType: $ResourceType" }
127+
}
128+
}
129+
130+
function SetOperation {
131+
param(
132+
[string]$ResourceType
133+
)
134+
135+
$inputObj = $stdinput | ConvertFrom-Json -ErrorAction Stop
136+
137+
switch ($ResourceType) {
138+
'repository' {
139+
$rep = Get-PSResourceRepository -Name $inputObj.Name -ErrorAction SilentlyContinue
140+
141+
$splatt = @{}
142+
143+
if ($inputObj.Name) {
144+
$splatt['Name'] = $inputObj.Name
145+
}
146+
147+
if ($inputObj.Uri) {
148+
$splatt['Uri'] = $inputObj.Uri
149+
}
150+
151+
if ($inputObj.Trusted) {
152+
$splatt['Trusted'] = $inputObj.Trusted
153+
}
154+
155+
if ($null -ne $inputObj.Priority ) {
156+
$splatt['Priority'] = $inputObj.Priority
157+
}
158+
159+
if ($inputObj.repositoryType) {
160+
$splatt['ApiVersion'] = $inputObj.repositoryType
161+
}
162+
163+
if ($null -eq $rep) {
164+
Register-PSResourceRepository @splatt
165+
}
166+
else {
167+
Set-PSResourceRepository @splatt
168+
}
169+
170+
return GetOperation -ResourceType $ResourceType
171+
}
172+
173+
'repositories' { return 'Set-PSRepository' }
174+
'psresource' { return 'Set-PSResource' }
175+
'psresources' { return 'Set-PSResource' }
176+
default { throw "Unknown ResourceType: $ResourceType" }
177+
}
178+
}
179+
180+
function FilterPSResourcesByRepository {
181+
param (
182+
$allPSResources,
183+
$repositoryName
184+
)
185+
186+
if (-not $repositoryName) {
187+
return $allPSResources
188+
}
189+
190+
$filteredResources = $allPSResources | Where-Object { $_.Repository -eq $repositoryName }
191+
192+
return $filteredResources
193+
}
194+
195+
function PopulatePSResourcesObjectByRepository {
196+
param (
197+
$resourcesExist,
198+
$resourcesMissing,
199+
$repositoryName,
200+
$scope
201+
)
202+
203+
$resources = @()
204+
205+
$resources += $resourcesExist | ForEach-Object {
206+
[pscustomobject]@{
207+
name = $_.Name
208+
version = $_.Version.ToString()
209+
_exists = $true
210+
}
211+
}
212+
213+
$resources += $resourcesMissing | ForEach-Object {
214+
[pscustomobject]@{
215+
name = $_.Name
216+
version = $_.Version.ToString()
217+
_exists = $false
218+
}
219+
}
220+
221+
$resourcesObj = if ($scope) {
222+
[pscustomobject]@{
223+
repositoryName = $repositoryName
224+
scope = $scope
225+
resources = $resources
226+
}
227+
}
228+
else {
229+
[pscustomobject]@{
230+
repositoryName = $repositoryName
231+
resources = $resources
232+
}
233+
}
234+
235+
return ($resourcesObj | ConvertTo-Json -Compress)
236+
}
237+
238+
function PopulatePSResourcesObject {
239+
param (
240+
$allPSResources
241+
)
242+
243+
$repoGrps = $allPSResources | Group-Object -Property Repository
244+
245+
$repoGrps | ForEach-Object {
246+
$repoName = $_.Name
247+
248+
249+
$resources = $_.Group | ForEach-Object {
250+
[pscustomobject]@{
251+
name = $_.Name
252+
version = $_.Version.ToString()
253+
_exists = $true
254+
}
255+
}
256+
257+
$resourcesObj = [pscustomobject]@{
258+
repositoryName = $repoName
259+
resources = $resources
260+
}
261+
262+
$resourcesObj | ConvertTo-Json -Compress
263+
}
264+
}
265+
266+
function PopulateRepositoryObject {
267+
param(
268+
$RepositoryInfo
269+
)
270+
271+
$repository = if (-not $RepositoryInfo) {
272+
Write-Trace -message "RepositoryInfo is null or empty. Returning _exists = false" -Level Information
273+
274+
$inputJson = $stdinput | ConvertFrom-Json -ErrorAction Stop
275+
276+
[pscustomobject]@{
277+
name = $inputJson.Name
278+
uri = $inputJson.Uri
279+
trusted = $inputJson.Trusted
280+
priority = $inputJson.Priority
281+
repositoryType = $inputJson.repositoryType
282+
_exists = $false
283+
}
284+
}
285+
else {
286+
Write-Trace -message "Populating repository object for: $($RepositoryInfo.Name)" -Level Verbose
287+
[pscustomobject]@{
288+
name = $RepositoryInfo.Name
289+
uri = $RepositoryInfo.Uri
290+
trusted = $RepositoryInfo.Trusted
291+
priority = $RepositoryInfo.Priority
292+
repositoryType = $RepositoryInfo.ApiVersion
293+
_exists = $true
294+
}
295+
}
296+
297+
return ($repository | ConvertTo-Json -Compress)
298+
}
299+
300+
switch ($Operation.ToLower()) {
301+
'get' { return (GetOperation -ResourceType $ResourceType) }
302+
'set' { return (SetOperation -ResourceType $ResourceType) }
303+
'export' { return (ExportOperation -ResourceType $ResourceType) }
304+
default { throw "Unknown Operation: $Operation" }
305+
}

0 commit comments

Comments
 (0)