Skip to content

Commit 10e82cc

Browse files
committed
Fix discovery of capabilities WinPSAdapter
1 parent 05f00db commit 10e82cc

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ class PSClassResource {
373373
$LASTEXITCODE | Should -Be 0
374374
$out.type | Should -Contain 'PSClassResource/PSClassResource'
375375
$out | Where-Object -Property type -EQ PSClassResource/PSClassResource | Select-Object -ExpandProperty implementedAs | Should -Be 1 # Class-based
376+
($out | Where-Object -Property type -EQ 'PSClassResource/PSClassResource').capabilities | Should -BeIn @('get', 'test', 'set', 'export')
376377
}
377378

378379
It 'Get works with class-based PS DSC resources' -Skip:(!$IsWindows) {

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
$script:CurrentCacheSchemaVersion = 1
4+
$script:CurrentCacheSchemaVersion = 2
55

66
function 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
588644
class 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

Comments
 (0)