@@ -66,9 +66,10 @@ function FindAndParseResourceDefinitions
66
66
{
67
67
return
68
68
}
69
-
70
- $filePath
71
-
69
+
70
+ " Loading resources from '$filePath '" | Write-DscTrace - Operation Trace
71
+ # TODO: Handle class inheritance
72
+ # TODO: Ensure embedded instances in properties are working correctly
72
73
[System.Management.Automation.Language.Token []] $tokens = $null
73
74
[System.Management.Automation.Language.ParseError []] $errors = $null
74
75
$ast = [System.Management.Automation.Language.Parser ]::ParseFile($filePath , [ref ]$tokens , [ref ]$errors )
@@ -90,7 +91,61 @@ function FindAndParseResourceDefinitions
90
91
},
91
92
$false );
92
93
93
- $resourceDefinitions.Name
94
+ $resourceList = [System.Collections.Generic.List [DscResourceInfo ]]::new()
95
+
96
+ foreach ($typeDefinitionAst in $resourceDefinitions )
97
+ {
98
+ $DscResourceInfo = [DscResourceInfo ]::new()
99
+ $DscResourceInfo.Name = $typeDefinitionAst.Name
100
+ $DscResourceInfo.ResourceType = $typeDefinitionAst.Name
101
+ $DscResourceInfo.FriendlyName = $typeDefinitionAst.Name
102
+ $DscResourceInfo.ImplementationDetail = ' ClassBased'
103
+ $DscResourceInfo.Module = $filePath
104
+ $DscResourceInfo.Path = $filePath
105
+ # TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
106
+ $DscResourceInfo.ModuleName = [System.IO.Path ]::GetFileNameWithoutExtension($filePath )
107
+ $DscResourceInfo.ParentPath = [System.IO.Path ]::GetDirectoryName($filePath )
108
+
109
+ $DscResourceInfo.Properties = [System.Collections.Generic.List [DscResourcePropertyInfo ]]::new()
110
+ foreach ($member in $typeDefinitionAst.Members )
111
+ {
112
+ $property = $member -as [System.Management.Automation.Language.PropertyMemberAst ]
113
+ if (($property -eq $null ) -or ($property.IsStatic ))
114
+ {
115
+ continue ;
116
+ }
117
+ $skipProperty = $true
118
+ $isKeyProperty = $false
119
+ foreach ($attr in $property.Attributes )
120
+ {
121
+ if ($attr.TypeName.Name -eq ' DscProperty' )
122
+ {
123
+ $skipProperty = $false
124
+ foreach ($attrArg in $attr.NamedArguments )
125
+ {
126
+ if ($attrArg.ArgumentName -eq ' Key' )
127
+ {
128
+ $isKeyProperty = $true
129
+ }
130
+ }
131
+ }
132
+ }
133
+ if ($skipProperty )
134
+ {
135
+ continue ;
136
+ }
137
+
138
+ [DscResourcePropertyInfo ]$prop = [DscResourcePropertyInfo ]::new()
139
+ $prop.Name = $property.Name
140
+ $prop.PropertyType = $property.PropertyType.TypeName.Name
141
+ $prop.IsMandatory = $isKeyProperty
142
+ $DscResourceInfo.Properties.Add ($prop )
143
+ }
144
+
145
+ $resourceList.Add ($DscResourceInfo )
146
+ }
147
+
148
+ return $resourceList
94
149
}
95
150
96
151
function LoadPowerShellClassResourcesFromModule
@@ -110,28 +165,20 @@ function LoadPowerShellClassResourcesFromModule
110
165
$scriptPath = $moduleInfo.Path ;
111
166
}
112
167
113
- FindAndParseResourceDefinitions $scriptPath
168
+ $Resources = FindAndParseResourceDefinitions $scriptPath
114
169
115
170
if ($moduleInfo.NestedModules )
116
171
{
117
172
foreach ($nestedModule in $moduleInfo.NestedModules )
118
173
{
119
- LoadPowerShellClassResourcesFromModule $nestedModule
174
+ $resourcesOfNestedModules = LoadPowerShellClassResourcesFromModule $nestedModule
175
+ $Resources.AddRange ($resourcesOfNestedModules )
120
176
}
121
177
}
122
- }
123
178
124
- function Invoke-DscCacheRefresh {
125
-
126
- $dscResourceModulePsd1s = Get-DSCResourceModules
127
- if ($null -ne $dscResourceModulePsd1s ) {
128
- $modules = Get-Module - ListAvailable - Name ($dscResourceModulePsd1s )
129
- foreach ($mod in $modules )
130
- {
131
- LoadPowerShellClassResourcesFromModule - moduleInfo $mod
132
- }
133
- }
179
+ return $Resources
134
180
}
181
+
135
182
<# public function Invoke-DscCacheRefresh
136
183
. SYNOPSIS
137
184
This function caches the results of the Get-DscResource call to optimize performance.
@@ -144,7 +191,7 @@ function Invoke-DscCacheRefresh {
144
191
. EXAMPLE
145
192
Invoke-DscCacheRefresh -Module "PSDesiredStateConfiguration"
146
193
#>
147
- function Invoke-DscCacheRefresh_ORIGINAL {
194
+ function Invoke-DscCacheRefresh {
148
195
[CmdletBinding (HelpUri = ' ' )]
149
196
param (
150
197
[Parameter (ValueFromPipeline = $true , ValueFromPipelineByPropertyName = $true )]
@@ -158,12 +205,8 @@ function Invoke-DscCacheRefresh_ORIGINAL {
158
205
# PS 6+ on Windows
159
206
Join-Path $env: LocalAppData " dsc\PSAdapterCache.json"
160
207
} else {
161
- # either WinPS or PS 6+ on Linux/Mac
162
- if ($PSVersionTable.PSVersion.Major -le 5 ) {
163
- Join-Path $env: LocalAppData " dsc\WindowsPSAdapterCache.json"
164
- } else {
165
- Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
166
- }
208
+ # PS 6+ on Linux/Mac
209
+ Join-Path $env: HOME " .dsc" " PSAdapterCache.json"
167
210
}
168
211
169
212
if (Test-Path $cacheFilePath ) {
@@ -225,33 +268,18 @@ function Invoke-DscCacheRefresh_ORIGINAL {
225
268
# create a list object to store cache of Get-DscResource
226
269
[dscResourceCacheEntry []]$dscResourceCacheEntries = [System.Collections.Generic.List [Object ]]::new()
227
270
228
- Import-PSDSCModule
229
- $DscResources = Get-DscResource
230
-
231
- foreach ($dscResource in $DscResources ) {
232
- # resources that shipped in Windows should only be used with Windows PowerShell
233
- if ($dscResource.ParentPath -like " $env: windir \System32\*" -and $PSVersionTable.PSVersion.Major -gt 5 ) {
234
- continue
235
- }
236
-
237
- if ( $dscResource.ImplementationDetail ) {
238
- # only support known dscResourceType
239
- if ([dscResourceType ].GetEnumNames() -notcontains $dscResource.ImplementationDetail ) {
240
- ' WARNING: implementation detail not found: ' + $dscResource.ImplementationDetail | Write-DscTrace
241
- continue
242
- }
243
- }
244
-
245
- $DscResourceInfo = [DscResourceInfo ]::new()
246
- $dscResource.PSObject.Properties | ForEach-Object - Process {
247
- if ($null -ne $_.Value ) {
248
- $DscResourceInfo .$ ($_.Name ) = $_.Value
249
- }
250
- else {
251
- $DscResourceInfo .$ ($_.Name ) = ' '
252
- }
271
+ $DscResources = [System.Collections.Generic.List [DscResourceInfo ]]::new()
272
+ $dscResourceModulePsd1s = Get-DSCResourceModules
273
+ if ($null -ne $dscResourceModulePsd1s ) {
274
+ $modules = Get-Module - ListAvailable - Name ($dscResourceModulePsd1s )
275
+ foreach ($mod in $modules )
276
+ {
277
+ [System.Collections.Generic.List [DscResourceInfo ]]$r = LoadPowerShellClassResourcesFromModule - moduleInfo $mod
278
+ $DscResources.AddRange ($r )
253
279
}
280
+ }
254
281
282
+ foreach ($dscResource in $DscResources ) {
255
283
$moduleName = $dscResource.ModuleName
256
284
257
285
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
@@ -262,7 +290,7 @@ function Invoke-DscCacheRefresh_ORIGINAL {
262
290
263
291
$dscResourceCacheEntries += [dscResourceCacheEntry ]@ {
264
292
Type = " $moduleName /$ ( $dscResource.Name ) "
265
- DscResourceInfo = $DscResourceInfo
293
+ DscResourceInfo = $dscResource
266
294
LastWriteTimes = $lastWriteTimes
267
295
}
268
296
}
@@ -454,6 +482,14 @@ enum dscResourceType {
454
482
Composite
455
483
}
456
484
485
+ class DscResourcePropertyInfo
486
+ {
487
+ [string ] $Name
488
+ [string ] $PropertyType
489
+ [bool ] $IsMandatory
490
+ [System.Collections.Generic.List [string ]] $Values
491
+ }
492
+
457
493
# dsc resource type (settable clone)
458
494
class DscResourceInfo {
459
495
[dscResourceType ] $ImplementationDetail
@@ -467,5 +503,5 @@ class DscResourceInfo {
467
503
[string ] $ParentPath
468
504
[string ] $ImplementedAs
469
505
[string ] $CompanyName
470
- [psobject [ ]] $Properties
506
+ [System.Collections.Generic.List [ DscResourcePropertyInfo ]] $Properties
471
507
}
0 commit comments