Skip to content

Commit 61c94d3

Browse files
author
Andrew
committed
class inheritance 1
1 parent 861bdf1 commit 61c94d3

File tree

3 files changed

+86
-60
lines changed

3 files changed

+86
-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: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,56 @@ 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+
}
86+
}
87+
}
88+
}
89+
if ($skipProperty)
90+
{
91+
continue;
92+
}
93+
94+
[DscResourcePropertyInfo]$prop = [DscResourcePropertyInfo]::new()
95+
$prop.Name = $property.Name
96+
$prop.PropertyType = $property.PropertyType.TypeName.Name
97+
$prop.IsMandatory = $isKeyProperty
98+
$Properties.Add($prop)
99+
}
100+
}
101+
52102
function FindAndParseResourceDefinitions
53103
{
54104
[CmdletBinding(HelpUri = '')]
@@ -68,7 +118,6 @@ function FindAndParseResourceDefinitions
68118
}
69119

70120
"Loading resources from file '$filePath'" | Write-DscTrace -Operation Trace
71-
#TODO: Handle class inheritance
72121
#TODO: Ensure embedded instances in properties are working correctly
73122
[System.Management.Automation.Language.Token[]] $tokens = $null
74123
[System.Management.Automation.Language.ParseError[]] $errors = $null
@@ -78,76 +127,38 @@ function FindAndParseResourceDefinitions
78127
$e | Out-String | Write-DscTrace -Operation Error
79128
}
80129

81-
$resourceDefinitions = $ast.FindAll(
130+
$typeDefinitions = $ast.FindAll(
82131
{
83132
$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;
133+
return $typeAst -ne $null;
96134
},
97135
$false);
98136

99137
$resourceList = [System.Collections.Generic.List[DscResourceInfo]]::new()
100138

101-
foreach($typeDefinitionAst in $resourceDefinitions)
139+
foreach($typeDefinitionAst in $typeDefinitions)
102140
{
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)
141+
foreach($a in $typeDefinitionAst.Attributes)
116142
{
117-
$property = $member -as [System.Management.Automation.Language.PropertyMemberAst]
118-
if (($property -eq $null) -or ($property.IsStatic))
119-
{
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)
143+
if ($a.TypeName.Name -eq 'DscResource')
139144
{
140-
continue;
145+
$DscResourceInfo = [DscResourceInfo]::new()
146+
$DscResourceInfo.Name = $typeDefinitionAst.Name
147+
$DscResourceInfo.ResourceType = $typeDefinitionAst.Name
148+
$DscResourceInfo.FriendlyName = $typeDefinitionAst.Name
149+
$DscResourceInfo.ImplementationDetail = 'ClassBased'
150+
$DscResourceInfo.Module = $filePath
151+
$DscResourceInfo.Path = $filePath
152+
#TODO: ModuleName, Version and ParentPath should be taken from psd1 contents
153+
$DscResourceInfo.ModuleName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
154+
$DscResourceInfo.ParentPath = [System.IO.Path]::GetDirectoryName($filePath)
155+
156+
$DscResourceInfo.Properties = [System.Collections.Generic.List[DscResourcePropertyInfo]]::new()
157+
Add-AstMembers $typeDefinitions $typeDefinitionAst $DscResourceInfo.Properties
158+
159+
$resourceList.Add($DscResourceInfo)
141160
}
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)
148161
}
149-
150-
$resourceList.Add($DscResourceInfo)
151162
}
152163

153164
return $resourceList

0 commit comments

Comments
 (0)