Skip to content

Commit 46714a2

Browse files
authored
Merge pull request #469 from anmenaga/class_inheritance
PSAdapter support for class inheritance
2 parents c8d7f75 + e744673 commit 46714a2

File tree

3 files changed

+87
-60
lines changed

3 files changed

+87
-60
lines changed

powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ enum EnumPropEnumeration {
88
Expected
99
}
1010

11+
class BaseTestClass
12+
{
13+
[DscProperty()]
14+
[string] $BaseProperty
15+
}
16+
1117
[DscResource()]
12-
class TestClassResource
18+
class TestClassResource : BaseTestClass
1319
{
1420
[DscProperty(Key)]
1521
[string] $Name

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ Describe 'PowerShell adapter resource tests' {
8181
$res.actualState.result.count | Should -Be 5
8282
$res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty}
8383
}
84+
85+
It 'Verify inheritance works in class-based resources' {
86+
87+
$r = dsc resource list '*' -a Microsoft.DSC/PowerShell
88+
$LASTEXITCODE | Should -Be 0
89+
$resources = $r | ConvertFrom-Json
90+
$t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}
91+
$t.properties | Should -Contain "BaseProperty"
92+
}
8493
}

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 71 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,57 @@ function Get-DSCResourceModules
4949
return $dscModulePsd1List
5050
}
5151

52+
function Add-AstMembers {
53+
param(
54+
$AllTypeDefinitions,
55+
$TypeAst,
56+
$Properties
57+
)
58+
59+
foreach($TypeConstraint in $TypeAst.BaseTypes) {
60+
$t = $AllTypeDefinitions | Where-Object {$_.Name -eq $TypeConstraint.TypeName.Name}
61+
if ($t) {
62+
Add-AstMembers $AllTypeDefinitions $t $Properties
63+
}
64+
}
65+
66+
foreach ($member in $TypeAst.Members)
67+
{
68+
$property = $member -as [System.Management.Automation.Language.PropertyMemberAst]
69+
if (($property -eq $null) -or ($property.IsStatic))
70+
{
71+
continue;
72+
}
73+
$skipProperty = $true
74+
$isKeyProperty = $false
75+
foreach($attr in $property.Attributes)
76+
{
77+
if ($attr.TypeName.Name -eq 'DscProperty')
78+
{
79+
$skipProperty = $false
80+
foreach($attrArg in $attr.NamedArguments)
81+
{
82+
if ($attrArg.ArgumentName -eq 'Key')
83+
{
84+
$isKeyProperty = $true
85+
break
86+
}
87+
}
88+
}
89+
}
90+
if ($skipProperty)
91+
{
92+
continue;
93+
}
94+
95+
[DscResourcePropertyInfo]$prop = [DscResourcePropertyInfo]::new()
96+
$prop.Name = $property.Name
97+
$prop.PropertyType = $property.PropertyType.TypeName.Name
98+
$prop.IsMandatory = $isKeyProperty
99+
$Properties.Add($prop)
100+
}
101+
}
102+
52103
function FindAndParseResourceDefinitions
53104
{
54105
[CmdletBinding(HelpUri = '')]
@@ -68,7 +119,6 @@ function FindAndParseResourceDefinitions
68119
}
69120

70121
"Loading resources from file '$filePath'" | Write-DscTrace -Operation Trace
71-
#TODO: Handle class inheritance
72122
#TODO: Ensure embedded instances in properties are working correctly
73123
[System.Management.Automation.Language.Token[]] $tokens = $null
74124
[System.Management.Automation.Language.ParseError[]] $errors = $null
@@ -78,76 +128,38 @@ function FindAndParseResourceDefinitions
78128
$e | Out-String | Write-DscTrace -Operation Error
79129
}
80130

81-
$resourceDefinitions = $ast.FindAll(
131+
$typeDefinitions = $ast.FindAll(
82132
{
83133
$typeAst = $args[0] -as [System.Management.Automation.Language.TypeDefinitionAst]
84-
if ($typeAst)
85-
{
86-
foreach($a in $typeAst.Attributes)
87-
{
88-
if ($a.TypeName.Name -eq 'DscResource')
89-
{
90-
return $true;
91-
}
92-
}
93-
}
94-
95-
return $false;
134+
return $typeAst -ne $null;
96135
},
97136
$false);
98137

99138
$resourceList = [System.Collections.Generic.List[DscResourceInfo]]::new()
100139

101-
foreach($typeDefinitionAst in $resourceDefinitions)
140+
foreach($typeDefinitionAst in $typeDefinitions)
102141
{
103-
$DscResourceInfo = [DscResourceInfo]::new()
104-
$DscResourceInfo.Name = $typeDefinitionAst.Name
105-
$DscResourceInfo.ResourceType = $typeDefinitionAst.Name
106-
$DscResourceInfo.FriendlyName = $typeDefinitionAst.Name
107-
$DscResourceInfo.ImplementationDetail = 'ClassBased'
108-
$DscResourceInfo.Module = $filePath
109-
$DscResourceInfo.Path = $filePath
110-
#TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
111-
$DscResourceInfo.ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
112-
$DscResourceInfo.ParentPath = [System.IO.Path]::GetDirectoryName($filePath)
113-
114-
$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
115-
foreach ($member in $typeDefinitionAst.Members)
142+
foreach($a in $typeDefinitionAst.Attributes)
116143
{
117-
$property = $member -as [System.Management.Automation.Language.PropertyMemberAst]
118-
if (($property -eq $null) -or ($property.IsStatic))
144+
if ($a.TypeName.Name -eq 'DscResource')
119145
{
120-
continue;
121-
}
122-
$skipProperty = $true
123-
$isKeyProperty = $false
124-
foreach($attr in $property.Attributes)
125-
{
126-
if ($attr.TypeName.Name -eq 'DscProperty')
127-
{
128-
$skipProperty = $false
129-
foreach($attrArg in $attr.NamedArguments)
130-
{
131-
if ($attrArg.ArgumentName -eq 'Key')
132-
{
133-
$isKeyProperty = $true
134-
}
135-
}
136-
}
137-
}
138-
if ($skipProperty)
139-
{
140-
continue;
146+
$DscResourceInfo = [DscResourceInfo]::new()
147+
$DscResourceInfo.Name = $typeDefinitionAst.Name
148+
$DscResourceInfo.ResourceType = $typeDefinitionAst.Name
149+
$DscResourceInfo.FriendlyName = $typeDefinitionAst.Name
150+
$DscResourceInfo.ImplementationDetail = 'ClassBased'
151+
$DscResourceInfo.Module = $filePath
152+
$DscResourceInfo.Path = $filePath
153+
#TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
154+
$DscResourceInfo.ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
155+
$DscResourceInfo.ParentPath = [System.IO.Path]::GetDirectoryName($filePath)
156+
157+
$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
158+
Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties
159+
160+
$resourceList.Add($DscResourceInfo)
141161
}
142-
143-
[DscResourcePropertyInfo]$prop = [DscResourcePropertyInfo]::new()
144-
$prop.Name = $property.Name
145-
$prop.PropertyType = $property.PropertyType.TypeName.Name
146-
$prop.IsMandatory = $isKeyProperty
147-
$DscResourceInfo.Properties.Add($prop)
148162
}
149-
150-
$resourceList.Add($DscResourceInfo)
151163
}
152164

153165
return $resourceList

0 commit comments

Comments
 (0)