DscResource.Tests/DscResource.DocumentationHelper/PowerShellHelp.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
<#
Define enumeration for use by help example generation to determine the type of block that a text line is within. #> if (-not ([System.Management.Automation.PSTypeName]'HelpExampleBlockType').Type) { $typeDefinition = @' public enum HelpExampleBlockType { None, PSScriptInfo, Configuration, ExampleCommentHeader } '@ Add-Type -TypeDefinition $typeDefinition } $projectRootPath = Split-Path -Path $PSScriptRoot -Parent $testHelperPath = Join-Path -Path $projectRootPath -ChildPath 'TestHelper.psm1' Import-Module -Name $testHelperPath -Force $moduleName = $ExecutionContext.SessionState.Module $script:localizedData = Get-LocalizedData -ModuleName $moduleName -ModuleRoot $PSScriptRoot <# .SYNOPSIS New-DscResourcePowerShellHelp generates PowerShell compatible help files for a DSC resource module .DESCRIPTION The New-DscResourcePowerShellHelp cmdlet will review all of the MOF based resources in a specified module directory and will inject PowerShell help files for each resource. These help files include details on the property types for each resource, as well as a text description and examples where they exist. The help files are output to the OutputPath directory if specified, or if not, they are output to the releveant resource's 'en-US' directory. A README.md with a text description must exist in the resource's subdirectory for the help file to be generated. These help files can then be read by passing the name of the resource as a parameter to Get-Help. .PARAMETER ModulePath The path to the root of the DSC resource module (where the PSD1 file is found, not the folder for each individual DSC resource) .EXAMPLE This example shows how to generate help for a specific module New-DscResourcePowerShellHelp -ModulePath C:\repos\SharePointdsc #> function New-DscResourcePowerShellHelp { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] $ModulePath, [Parameter()] [System.String] $OutputPath ) Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'MofHelper.psm1') -Verbose:$false $mofSearchPath = (Join-Path -Path $ModulePath -ChildPath '\**\*.schema.mof') $mofSchemas = Get-ChildItem -Path $mofSearchPath -Recurse Write-Verbose -Message ($script:localizedData.FoundMofFilesMessage -f $mofSchemas.Count, $ModulePath) $mofSchemas | ForEach-Object { $mofFileObject = $_ $result = (Get-MofSchemaObject -FileName $_.FullName) | Where-Object -FilterScript { ($_.ClassName -eq $mofFileObject.Name.Replace('.schema.mof', '')) ` -and ($null -ne $_.FriendlyName) } $descriptionPath = Join-Path -Path $mofFileObject.DirectoryName -ChildPath 'readme.md' if (Test-Path -Path $descriptionPath) { Write-Verbose -Message ($script:localizedData.GenerateHelpDocumentMessage -f $result.FriendlyName) $output = '.NAME' + [Environment]::NewLine $output += " $($result.FriendlyName)" $output += [Environment]::NewLine + [Environment]::NewLine $descriptionContent = Get-Content -Path $descriptionPath -Raw $descriptionContent = $descriptionContent -replace '\n', "`n " $descriptionContent = $descriptionContent -replace '# Description\r\n ', '.DESCRIPTION' $descriptionContent = $descriptionContent -replace '\r\n\s{4}\r\n', "`n`n" $descriptionContent = $descriptionContent -replace '\s{4}$', '' $output += $descriptionContent $output += [Environment]::NewLine foreach ($property in $result.Attributes) { $output += ".PARAMETER $($property.Name)" + [Environment]::NewLine $output += " $($property.State) - $($property.DataType)" $output += [Environment]::NewLine if ([string]::IsNullOrEmpty($property.ValueMap) -ne $true) { $output += " Allowed values: " $property.ValueMap | ForEach-Object { $output += $_ + ", " } $output = $output.TrimEnd(" ") $output = $output.TrimEnd(",") $output += [Environment]::NewLine } $output += " " + $property.Description $output += [Environment]::NewLine + [Environment]::NewLine } $exampleSearchPath = "\Examples\Resources\$($result.FriendlyName)\*.ps1" $examplesPath = (Join-Path -Path $ModulePath -ChildPath $exampleSearchPath) $exampleFiles = Get-ChildItem -Path $examplesPath -ErrorAction SilentlyContinue if ($null -ne $exampleFiles) { $exampleCount = 1 Write-Verbose -Message "Found $($exampleFiles.count) Examples for resource $($result.FriendlyName)" foreach ($exampleFile in $exampleFiles) { $exampleContent = Get-DscResourceHelpExampleContent ` -ExamplePath $exampleFile.FullName ` -ExampleNumber ($exampleCount++) $output += $exampleContent $output += [Environment]::NewLine } } else { Write-Warning -Message ($script:localizedData.NoExampleFileFoundWarning -f $result.FriendlyName) } # Output to $OutputPath if specified or the resource 'en-US' directory if not. $outputFileName = "about_$($result.FriendlyName).help.txt" if ($OutputPath) { $savePath = Join-Path -Path $OutputPath -ChildPath $outputFileName } else { $savePath = Join-Path -Path $mofFileObject.DirectoryName -ChildPath 'en-US' | Join-Path -ChildPath $outputFileName } Write-Verbose -Message ($script:localizedData.OutputHelpDocumentMessage -f $savePath) $output | Out-File -FilePath $savePath -Encoding ascii -Force } else { Write-Warning -Message ($script:localizedData.NoDescriptionFileFoundWarning -f $result.FriendlyName) } } } <# .SYNOPSIS This function reads an example file from a resource and converts it to help text for inclusion in a PowerShell help file. .DESCRIPTION The function will read the example PS1 file and convert the help header into the description text for the example. .PARAMETER ExamplePath The path to the example file. .PARAMETER ModulePath The number of the example. .EXAMPLE Get-DscResourceHelpExampleContent -ExamplePath 'C:\repos\NetworkingDsc\Examples\Resources\DhcpClient\1-DhcpClient_EnableDHCP.ps1' -ExampleNumber 1 Reads the content of 'C:\repos\NetworkingDsc\Examples\Resources\DhcpClient\1-DhcpClient_EnableDHCP.ps1' and converts it to help text in preparation for being added to a PowerShell help file. #> function Get-DscResourceHelpExampleContent { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter(Mandatory = $true)] [System.String] $ExamplePath, [Parameter(Mandatory = $true)] [System.Int32] $ExampleNumber ) $exampleContent = Get-Content -Path $ExamplePath # Use a string builder to assemble the example description and code $exampleDescriptionStringBuilder = New-Object -TypeName System.Text.StringBuilder $exampleCodeStringBuilder = New-Object -TypeName System.Text.StringBuilder <# Step through each line in the source example and determine the content and act accordingly: \<#PSScriptInfo...#\> - Drop block \#Requires - Drop Line \<#...#\> - Drop .EXAMPLE, .SYNOPSIS and .DESCRIPTION but include all other lines Configuration ... - Include entire block until EOF #> $blockType = [HelpExampleBlockType]::None foreach ($exampleLine in $exampleContent) { Write-Debug -Message ('Processing Line: {0}' -f $exampleLine) # Determine the behavior based on the current block type switch ($blockType.ToString()) { 'PSScriptInfo' { Write-Debug -Message 'PSScriptInfo Block Processing' # Exclude PSScriptInfo block from any output if ($exampleLine -eq '#>') { Write-Debug -Message 'PSScriptInfo Block Ended' # End of the PSScriptInfo block $blockType = [HelpExampleBlockType]::None } } 'Configuration' { Write-Debug -Message 'Configuration Block Processing' # Include all lines in the configuration block in the code output $null = $exampleCodeStringBuilder.AppendLine($exampleLine) } 'ExampleCommentHeader' { Write-Debug -Message 'ExampleCommentHeader Block Processing' # Include all lines in Example Comment Header block except for headers $exampleLine = $exampleLine.TrimStart() if ($exampleLine -notin ('.SYNOPSIS', '.DESCRIPTION', '.EXAMPLE', '#>')) { # Not a header so add this to the output $null = $exampleDescriptionStringBuilder.AppendLine($exampleLine) } if ($exampleLine -eq '#>') { Write-Debug -Message 'ExampleCommentHeader Block Ended' # End of the Example Comment Header block $blockType = [HelpExampleBlockType]::None } } default { Write-Debug -Message 'Not Currently Processing Block' # Check the current line if ($exampleLine.TrimStart() -eq '<#PSScriptInfo') { Write-Debug -Message 'PSScriptInfo Block Started' $blockType = [HelpExampleBlockType]::PSScriptInfo } elseif ($exampleLine -match 'Configuration') { Write-Debug -Message 'Configuration Block Started' $null = $exampleCodeStringBuilder.AppendLine($exampleLine) $blockType = [HelpExampleBlockType]::Configuration } elseif ($exampleLine.TrimStart() -eq '<#') { Write-Debug -Message 'ExampleCommentHeader Block Started' $blockType = [HelpExampleBlockType]::ExampleCommentHeader } } } } # Assemble the final output $null = $exampleStringBuilder = New-Object -TypeName System.Text.StringBuilder $null = $exampleStringBuilder.AppendLine(".EXAMPLE $ExampleNumber") $null = $exampleStringBuilder.AppendLine() $null = $exampleStringBuilder.AppendLine($exampleDescriptionStringBuilder) $null = $exampleStringBuilder.Append($exampleCodeStringBuilder) return $exampleStringBuilder.ToString() } Export-ModuleMember -Function New-DscResourcePowerShellHelp |