1
1
# Copyright (c) Microsoft Corporation.
2
2
# Licensed under the MIT License.
3
3
4
- $script :CurrentCacheSchemaVersion = 1
4
+ $script :CurrentCacheSchemaVersion = 2
5
5
6
6
function Write-DscTrace {
7
7
param (
@@ -230,6 +230,7 @@ function Invoke-DscCacheRefresh {
230
230
if ($classBased -and ($classBased.CustomAttributes.AttributeType.Name -eq ' DscResourceAttribute' )) {
231
231
" Detected class-based resource: $ ( $dscResource.Name ) => Type: $ ( $classBased.BaseType.FullName ) " | Write-DscTrace
232
232
$dscResourceInfo.ImplementationDetail = ' ClassBased'
233
+ $dscResourceInfo.Methods = GetClassBasedCapabilities - filePath $dscResource.Path
233
234
}
234
235
235
236
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
@@ -239,10 +240,10 @@ function Invoke-DscCacheRefresh {
239
240
}
240
241
241
242
$dscResourceCacheEntries.Add ([dscResourceCacheEntry ]@ {
242
- Type = " $moduleName /$ ( $dscResource.Name ) "
243
- DscResourceInfo = $DscResourceInfo
244
- LastWriteTimes = $lastWriteTimes
245
- })
243
+ Type = " $moduleName /$ ( $dscResource.Name ) "
244
+ DscResourceInfo = $DscResourceInfo
245
+ LastWriteTimes = $lastWriteTimes
246
+ })
246
247
}
247
248
248
249
if ($namedModules.Count -gt 0 ) {
@@ -584,6 +585,61 @@ function ValidateMethod {
584
585
return $method
585
586
}
586
587
588
+ function GetClassBasedCapabilities {
589
+ param (
590
+ [Parameter (Mandatory = $true )]
591
+ [string ] $filePath
592
+ )
593
+
594
+ if (" .psd1" -notcontains ([System.IO.Path ]::GetExtension($filePath ))) {
595
+ return @ (' get' , ' set' , ' test' )
596
+ }
597
+
598
+ $module = $filePath.Replace (' .psd1' , ' .psm1' )
599
+
600
+ if (Test-Path $module - ErrorAction Ignore) {
601
+ [System.Management.Automation.Language.Token []] $tokens = $null
602
+ [System.Management.Automation.Language.ParseError []] $errors = $null
603
+ $ast = [System.Management.Automation.Language.Parser ]::ParseFile($module , [ref ]$tokens , [ref ]$errors )
604
+ foreach ($e in $errors ) {
605
+ $e | Out-String | Write-DscTrace - Operation Error
606
+ }
607
+
608
+ $typeDefinitions = $ast.FindAll (
609
+ {
610
+ $typeAst = $args [0 ] -as [System.Management.Automation.Language.TypeDefinitionAst ]
611
+ return $null -ne $typeAst ;
612
+ },
613
+ $false );
614
+
615
+
616
+ $capabilities = @ ()
617
+ $availableMethods = @ (' get' , ' set' , ' setHandlesExist' , ' whatIf' , ' test' , ' delete' , ' export' )
618
+ foreach ($typeDefinitionAst in $typeDefinitions ) {
619
+ foreach ($a in $typeDefinitionAst.Attributes ) {
620
+ if ($a.TypeName.Name -eq ' DscResource' ) {
621
+ $methods = $typeDefinitionAst.Members | Where-Object { $_ -is [System.Management.Automation.Language.FunctionMemberAst ] -and $_.Name -in $availableMethods }
622
+
623
+ foreach ($method in $methods.Name ) {
624
+ # We go through each method to properly case handle the method names.
625
+ switch ($method ) {
626
+ ' Get' { $capabilities += ' get' }
627
+ ' Set' { $capabilities += ' set' }
628
+ ' Test' { $capabilities += ' test' }
629
+ ' WhatIf' { $capabilities += ' whatIf' }
630
+ ' SetHandlesExist' { $capabilities += ' setHandlesExist' }
631
+ ' Delete' { $capabilities += ' delete' }
632
+ ' Export' { $capabilities += ' export' }
633
+ }
634
+ }
635
+ }
636
+ }
637
+ }
638
+
639
+ return $capabilities
640
+ }
641
+ }
642
+
587
643
# cached resource
588
644
class dscResourceCacheEntry {
589
645
[string ] $Type
@@ -626,4 +682,5 @@ class DscResourceInfo {
626
682
[string ] $ImplementedAs
627
683
[string ] $CompanyName
628
684
[psobject []] $Properties
685
+ [string []] $Methods
629
686
}
0 commit comments