|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Returns DSC class resource properties from the provided class or classes. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Returns DSC class resource properties from the provided class or classes. |
| 7 | +
|
| 8 | + .PARAMETER SourcePath |
| 9 | + The path to the source folder (in which the child folder 'Classes' exist). |
| 10 | +
|
| 11 | + .PARAMETER BuiltModuleScriptFilePath |
| 12 | + The path to the built module script file that contains the class. |
| 13 | +
|
| 14 | + .PARAMETER ClassName |
| 15 | + One or more class names to return properties for. |
| 16 | +
|
| 17 | + .EXAMPLE |
| 18 | + Get-ClassResourceProperty -ClassName @('myParentClass', 'myClass') -BuiltModuleScriptFilePath '.\output\MyModule\1.0.0\MyModule.psm1' -SourcePath '.\source' |
| 19 | +
|
| 20 | + Returns all DSC class resource properties. |
| 21 | +#> |
| 22 | +function Get-ClassResourcePropertyNew |
| 23 | +{ |
| 24 | + [CmdletBinding()] |
| 25 | + [OutputType([System.Collections.Hashtable[]])] |
| 26 | + param |
| 27 | + ( |
| 28 | + [Parameter(Mandatory = $true)] |
| 29 | + [System.String] |
| 30 | + $SourcePath, |
| 31 | + |
| 32 | + # [Parameter(Mandatory = $true)] |
| 33 | + # [System.String] |
| 34 | + # $BuiltModuleScriptFilePath, |
| 35 | + |
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [System.Reflection.PropertyInfo[]] |
| 38 | + $Properties |
| 39 | + |
| 40 | + |
| 41 | + ) |
| 42 | + |
| 43 | + $resourceProperty = [System.Collections.Hashtable[]] @() |
| 44 | + |
| 45 | + $className = ($dscProperties | Select-Object -Unique DeclaringType).DeclaringType.Name |
| 46 | + |
| 47 | + foreach ($currentClassName in $className) |
| 48 | + { |
| 49 | + #$dscResourceAst = Get-ClassAst -ClassName $currentClassName -ScriptFile $BuiltModuleScriptFilePath |
| 50 | + |
| 51 | + $classExists = $false |
| 52 | + $sourceFilePath = '' |
| 53 | + $childPaths = @( |
| 54 | + ('Classes/???.{0}.ps1' -f $currentClassName) |
| 55 | + ('Classes/{0}.ps1' -f $currentClassName) |
| 56 | + ) |
| 57 | + |
| 58 | + foreach ($childPath in $childPaths) |
| 59 | + { |
| 60 | + $sourceFilePath = Join-Path -Path $SourcePath -ChildPath $childPath |
| 61 | + |
| 62 | + if ((Test-Path -Path $sourceFilePath)) |
| 63 | + { |
| 64 | + $classExists = $true |
| 65 | + break |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + <# |
| 70 | + Skip if the class's source file does not exist. This can happen if the |
| 71 | + class uses a parent class from a different module. |
| 72 | + #> |
| 73 | + if (-not $classExists) |
| 74 | + { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + $dscResourceCommentBasedHelp = Get-CommentBasedHelp -Path $sourceFilePath |
| 79 | + |
| 80 | + # $astFilter = { |
| 81 | + # $args[0] -is [System.Management.Automation.Language.PropertyMemberAst] ` |
| 82 | + # -and $args[0].Attributes.TypeName.Name -eq 'DscProperty' |
| 83 | + # } |
| 84 | + |
| 85 | + # $propertyMemberAsts = $dscResourceAst.FindAll($astFilter, $true) |
| 86 | + $propertyMembers = $Properties | Where-Object { $_.DeclaringType.Name -eq $currentClassName } |
| 87 | + |
| 88 | + <# |
| 89 | + Looping through each resource property to build the resulting |
| 90 | + hashtable. Hashtable will be in the format: |
| 91 | +
|
| 92 | + @{ |
| 93 | + Name = <System.String> |
| 94 | + State = 'Key' | 'Required' |'Write' | 'Read' |
| 95 | + Description = <System.String> |
| 96 | + EmbeddedInstance = 'MSFT_Credential' | $null |
| 97 | + DataType = 'System.String' | 'System.String[] | etc. |
| 98 | + IsArray = $true | $false |
| 99 | + ValueMap = @(<System.String> | ...) |
| 100 | + } |
| 101 | + #> |
| 102 | + foreach ($propertyMember in $propertyMembers) |
| 103 | + { |
| 104 | + Write-Verbose -Message ($script:localizedData.FoundClassResourcePropertyMessage -f $propertyMember.Name, $currentClassName) |
| 105 | + |
| 106 | + $propertyAttribute = @{ |
| 107 | + Name = $propertyMember.Name |
| 108 | + DataType = Get-DscPropertyType -PropertyType $propertyMember.PropertyType |
| 109 | + |
| 110 | + # Always set to null, correct type name is set in DataType. |
| 111 | + EmbeddedInstance = $null |
| 112 | + |
| 113 | + # Always set to $false - correct type name is set in DataType. |
| 114 | + IsArray = $false |
| 115 | + } |
| 116 | + |
| 117 | + $propertyAttribute.State = Get-ClassResourcePropertyState2 -PropertyInfo $propertyMember |
| 118 | + |
| 119 | + $valueMapValues = $null |
| 120 | + if ($propertyMember.PropertyType.IsEnum) |
| 121 | + { |
| 122 | + $valueMapValues = $propertyMember.PropertyType.GetEnumNames() |
| 123 | + } |
| 124 | + |
| 125 | + $validateSet = Get-ClassPropertyCustomAttribute -Attributes $propertyMember.CustomAttributes -AttributeType 'ValidateSetAttribute' |
| 126 | + if ($validateSet) |
| 127 | + { |
| 128 | + $valueMapValues = $validateSet.ConstructorArguments.Value.Value |
| 129 | + } |
| 130 | + |
| 131 | + if ($valueMapValues) |
| 132 | + { |
| 133 | + $propertyAttribute.ValueMap = $valueMapValues |
| 134 | + } |
| 135 | + |
| 136 | + if ($dscResourceCommentBasedHelp -and $dscResourceCommentBasedHelp.Parameters.Count -gt 0) |
| 137 | + { |
| 138 | + # The key name must be upper-case for it to match the right item in the list of parameters. |
| 139 | + $propertyDescription = $dscResourceCommentBasedHelp.Parameters[$propertyMember.Name.ToUpper()] |
| 140 | + |
| 141 | + if ($propertyDescription) |
| 142 | + { |
| 143 | + $propertyDescription = Format-Text -Text $propertyDescription -Format @( |
| 144 | + 'Remove_Blank_Rows_At_End_Of_String', |
| 145 | + 'Remove_Indentation_From_Blank_Rows', |
| 146 | + 'Replace_NewLine_With_One_Whitespace', |
| 147 | + 'Replace_Vertical_Bar_With_One_Whitespace', |
| 148 | + 'Replace_Multiple_Whitespace_With_One', |
| 149 | + 'Remove_Whitespace_From_End_Of_String' |
| 150 | + ) |
| 151 | + } |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + $propertyDescription = '' |
| 156 | + } |
| 157 | + |
| 158 | + $propertyAttribute.Description = $propertyDescription |
| 159 | + |
| 160 | + $resourceProperty += $propertyAttribute |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return $resourceProperty |
| 165 | +} |
0 commit comments