Skip to content

Commit b880b09

Browse files
authored
Merge pull request #877 from Gijsreyn/fix-capabilities-discover-winpsadapter
Fix discovery of capabilities WinPSAdapter
2 parents cc97a6a + d66588c commit b880b09

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ function Invoke-DscCacheRefresh {
226226
if ($null -ne $properties) {
227227
$DscResourceInfo.Properties = $properties
228228
}
229+
230+
$dscResourceInfo.Capabilities = GetClassBasedCapabilities -filePath $dscResource.Path -className $dscResource.Name
229231
}
230232

231233
# fill in resource files (and their last-write-times) that will be used for up-do-date checks
@@ -632,6 +634,64 @@ function GetClassBasedProperties {
632634
}
633635
}
634636

637+
function GetClassBasedCapabilities {
638+
param (
639+
[Parameter(Mandatory = $true)]
640+
[string] $filePath,
641+
642+
[Parameter(Mandatory = $true)]
643+
[string] $className
644+
)
645+
646+
if (".psd1" -notcontains ([System.IO.Path]::GetExtension($filePath))) {
647+
return @('get', 'set', 'test')
648+
}
649+
650+
$module = $filePath.Replace('.psd1', '.psm1')
651+
652+
if (Test-Path $module -ErrorAction Ignore) {
653+
[System.Management.Automation.Language.Token[]] $tokens = $null
654+
[System.Management.Automation.Language.ParseError[]] $errors = $null
655+
$ast = [System.Management.Automation.Language.Parser]::ParseFile($module, [ref]$tokens, [ref]$errors)
656+
foreach ($e in $errors) {
657+
$e | Out-String | Write-DscTrace -Operation Error
658+
}
659+
660+
$typeDefinitions = $ast.FindAll(
661+
{
662+
$typeAst = $args[0] -as [System.Management.Automation.Language.TypeDefinitionAst]
663+
return $null -ne $typeAst;
664+
},
665+
$false);
666+
667+
668+
$capabilities = [System.Collections.Generic.List[string[]]]::new()
669+
$availableMethods = @('get', 'set', 'setHandlesExist', 'whatIf', 'test', 'delete', 'export')
670+
foreach ($typeDefinitionAst in $typeDefinitions) {
671+
foreach ($a in $typeDefinitionAst.Attributes) {
672+
if ($a.TypeName.Name -eq 'DscResource' -and $a.Parent.Name -eq $className) {
673+
$methods = $typeDefinitionAst.Members | Where-Object { $_ -is [System.Management.Automation.Language.FunctionMemberAst] -and $_.Name -in $availableMethods }
674+
675+
foreach ($method in $methods.Name) {
676+
# We go through each method to properly case handle the method names.
677+
switch ($method) {
678+
'Get' { $capabilities.Add('get') }
679+
'Set' { $capabilities.Add('set') }
680+
'Test' { $capabilities.Add('test') }
681+
'WhatIf' { $capabilities.Add('whatIf') }
682+
'SetHandlesExist' { $capabilities.Add('setHandlesExist') }
683+
'Delete' { $capabilities.Add('delete') }
684+
'Export' { $capabilities.Add('export') }
685+
}
686+
}
687+
}
688+
}
689+
}
690+
691+
return $capabilities
692+
}
693+
}
694+
635695
# cached resource
636696
class dscResourceCacheEntry {
637697
[string] $Type
@@ -681,4 +741,5 @@ class DscResourceInfo {
681741
[string] $ImplementedAs
682742
[string] $CompanyName
683743
[psobject[]] $Properties
684-
}
744+
[string[]] $Capabilities
745+
}

0 commit comments

Comments
 (0)