11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
33
4- $script :CurrentCacheSchemaVersion = 1
4+ $script :CurrentCacheSchemaVersion = 2
55
66function Write-DscTrace {
77 param (
@@ -230,6 +230,7 @@ function Invoke-DscCacheRefresh {
230230 if ($classBased -and ($classBased.CustomAttributes.AttributeType.Name -eq ' DscResourceAttribute' )) {
231231 " Detected class-based resource: $ ( $dscResource.Name ) => Type: $ ( $classBased.BaseType.FullName ) " | Write-DscTrace
232232 $dscResourceInfo.ImplementationDetail = ' ClassBased'
233+ $dscResourceInfo.Methods = GetClassBasedCapabilities - filePath $dscResource.Path
233234 }
234235
235236 # 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 {
239240 }
240241
241242 $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+ })
246247 }
247248
248249 if ($namedModules.Count -gt 0 ) {
@@ -584,6 +585,61 @@ function ValidateMethod {
584585 return $method
585586}
586587
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+
587643# cached resource
588644class dscResourceCacheEntry {
589645 [string ] $Type
@@ -626,4 +682,5 @@ class DscResourceInfo {
626682 [string ] $ImplementedAs
627683 [string ] $CompanyName
628684 [psobject []] $Properties
685+ [string []] $Methods
629686}
0 commit comments