Modules/xNetworking/DSCResource.Tests/DscResource.DocumentationHelper/WikiPages.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
<#
.SYNOPSIS
 
New-DscResourceWikiSite generates wiki pages that can be uploaded to GitHub to use as
public documentation for a module.
 
.DESCRIPTION
 
The New-DscResourceWikiSite cmdlet will review all of the MOF based resources
in a specified module directory and will output the Markdown files to the specified directory.
These help files include details on the property types for each resource, as well as a text
description and examples where they exist.
 
.PARAMETER OutputPath
 
Where should the files be saved to
 
.PARAMETER ModulePath
 
The path to the root of the DSC resource module (where the PSD1 file is found, not the folder for
and individual DSC resource)
 
.EXAMPLE
 
This example shows how to generate help for a specific module
 
    New-DscResourceWikiSite -ModulePath C:\repos\SharePointdsc -OutputPath C:\repos\SharePointDsc\en-US
 
#>

function New-DscResourceWikiSite
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $OutputPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $ModulePath
    )

    Import-Module (Join-Path -Path $PSScriptRoot -ChildPath "MofHelper.psm1")

    $mofSearchPath = (Join-Path -Path $ModulePath -ChildPath "\**\*.schema.mof")
    $mofSchemas = Get-ChildItem -Path $mofSearchPath -Recurse
    $mofSchemas | ForEach-Object {
        $mofFileObject = $_
        $result = (Get-MofSchemaObject $_.FullName) | Where-Object {
            ($_.ClassName -eq $mofFileObject.Name.Replace(".schema.mof", "")) `
                -and ($null -ne $_.FriendlyName)
        }

        $descriptionPath = Join-Path -Path $_.DirectoryName -ChildPath "readme.md"
        if (Test-Path -Path $descriptionPath)
        {
            Write-Verbose -Message "Generating wiki page for $($result.FriendlyName)"

            $output = New-Object System.Text.StringBuilder
            $null = $output.AppendLine("# $($result.FriendlyName)")
            $null = $output.AppendLine('')
            $null = $output.AppendLine('## Parameters')
            $null = $output.AppendLine('')
            $null = $output.AppendLine('| Parameter | Attribute | DataType | Description | Allowed Values |')
            $null = $output.AppendLine('| --- | --- | --- | --- | --- |')
            foreach ($property in $result.Attributes)
            {
                # If the attribute is an array, add [] to the DataType string
                $dataType = $property.DataType
                if ($property.IsArray)
                {
                    $dataType += '[]'
                }
                if ($property.EmbeddedInstance -eq 'MSFT_Credential')
                {
                    $dataType = 'PSCredential'
                }
                $null = $output.Append("| **$($property.Name)** " + `
                    "| $($property.State) " + `
                    "| $dataType " + `
                    "| $($property.Description) |")
                if ([string]::IsNullOrEmpty($property.ValueMap) -ne $true)
                {
                    $null = $output.Append(($property.ValueMap -Join ', '))
                }
                $null = $output.AppendLine('|')
            }

            $descriptionContent = Get-Content -Path $descriptionPath -Raw
            # Change the description H1 header to an H2
            $descriptionContent = $descriptionContent -replace '# Description','## Description'
            $null = $output.AppendLine()
            $null = $output.AppendLine($descriptionContent)

            $exampleSearchPath = "\Examples\Resources\$($result.FriendlyName)\*.ps1"
            $examplesPath = (Join-Path -Path $ModulePath -ChildPath $exampleSearchPath)
            $exampleFiles = Get-ChildItem -Path $examplesPath -ErrorAction SilentlyContinue

            if ($null -ne $exampleFiles)
            {
                $null = $output.AppendLine('## Examples')
                $exampleCount = 1
                foreach ($exampleFile in $exampleFiles)
                {
                    $exampleContent = Get-Content -Path $exampleFile.FullName -Raw
                    $helpStart = $exampleContent.IndexOf('<#')
                    $helpEnd = $exampleContent.IndexOf('#>') + 2
                    $help = $exampleContent.Substring($helpStart, $helpEnd - $helpStart)
                    $helpOriginal = $help
                    $help += [Environment]::NewLine + '````powershell'
                    $help = $help.Replace(' ', '')
                    $exampleContent = $exampleContent.Replace($helpOriginal, $help)
                    $exampleContent = $exampleContent -replace '<#'
                    $exampleContent = $exampleContent -replace '#>'
                    $exampleContent = $exampleContent.Replace(".EXAMPLE", `
                                                            "### Example $exampleCount`n")
                    $exampleContent += '````'

                    $null = $output.AppendLine($exampleContent)

                    $exampleCount ++
                }
            }
            $output.ToString() | Out-File -FilePath (Join-Path $OutputPath "$($result.FriendlyName).md") `
                               -Encoding utf8 -Force
        }
    }
}

Export-ModuleMember -Function *