public/Write-ModuleHelpDocs.ps1

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
function Write-ModuleHelpDocs() {
    <#
    .SYNOPSIS
        Writes help docs for a given powershell module.
    .DESCRIPTION
        Writes help docs for a given powershell module in
        dfx flavored markdown.
 
    .PARAMETER Module
        Required. The target module for generating help docs.
     
    .PARAMETER Path
        Optional. The output directory. Aliases: -Dest,-Destination,-O
         
    .PARAMETER Do
        Optional. Do is an action that overrides the default behavior for
        `Write-CmdletDoc`. The default Do action is a pipeline of functions
        that are passed a model. The Do action can be used to override
        item templates for each part generating help docs.
 
    .PARAMETER PostProcess
        Optional. A script block with the parameters of for the
        file that is generated and the associated PowerShell help
        meta data so that a generate file can be processed after
        it is generated.
 
    .PARAMETER PublishedAt
        Optional. Generates and inserts the published at date for
        all the generated help documents.
 
    .PARAMETER BaseUri
        Optional. Sets the base uri for the documents when the
        help doc module is bundled under a larger site.
 
    .EXAMPLE
        PS C:\> Write-Help -Module "PsReadline" -O "$Home/Desktop/PsReadline"
 
    #>

    [CmdletBinding()]
    Param(
        [Alias("Destination")]
        [Alias("Dest")]
        [Alias("O")]
        [Parameter(Position = 1)]
        [String] $Path,

        [ScriptBlock] $Do,
        
        [ScriptBlock] $PostProcess,

        [String] $PublishedAt,

        [String] $BaseUri 
    )

    DynamicParam {
        
        $parameterAttr = New-Object  System.Management.Automation.ParameterAttribute -Property @{
            Mandatory = $true 
            Position = 0
            ParameterSetName =  '__AllParameterSets'
        }

        $list = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
        $list.Add($parameterAttr)

        $modules =  Get-Module | Select-Object -ExpandProperty Name
        $validateSetAttr = New-Object System.Management.Automation.ValidateSetAttribute($modules)
        $list.Add($validateSetAttr)

        $moduleParam = New-Object  System.Management.Automation.RuntimeDefinedParameter('Module', [string],  $list)

        $parameters = New-Object  System.Management.Automation.RuntimeDefinedParameterDictionary
        $parameters.Add('Module', $moduleParam)

        return $parameters
    }

    Process {
        $module = $PsBoundParameters.module 
        
        if($module) {
            $mod = Get-Module $module 
            $author = $mod.Author
            $help = Read-ModuleHelp -Module $module
           
            if($help) {
                foreach ($cmdlet in $help) {
                   $name = $cmdlet.Name
                  
                  
                   $sb = New-Object System.Text.StringBuilder
                   $isUpper = $false
                   $last = $null; 
              
                   for($i = 0; $i -lt $name.Length; $i++) {
                       
                      $c = $name[$i]


                       if([Char]::IsUpper($c)) {
                           if($i -eq 0) {
                               $sb.Append($c.ToString().ToLower())  | Out-Null ;
                               continue;
                           }

                           $next = $name[$i + 1]
                           if([Char]::IsUpper($next)) {
                               $sb.Append($c.ToString().ToLower())  | Out-Null ;
                               $isUpper = $true;
                              
                               continue;
                           }

                           
                            if([Char]::IsLower($next)) {
                                $last = $name[$i-1];
                                if($last -ne "-") {
                                    $sb.Append("-") | Out-Null ;
                                }
                                
                                $sb.Append($c.ToString().ToLower())  | Out-Null ;
                                continue;
                            }
                           
                           
                           $sb.Append($c.ToString().ToLower())  | Out-Null ;
                           continue;
                       }
                       

                       $isUpper = $false;
                       $sb.Append($c) | Out-Null ;
                   }

                   $name = $sb.ToString()
                   $now = [DateTime]::UtcNow.ToShortDateString();
                   $cmdlet | Add-Member -MemberType NoteProperty -Name "UpdatedAt" -Value $now
                   $cmdlet | Add-Member -MemberType NoteProperty -Name "HyphenatedName" -Value $Name
                   $cmdlet | Add-Member -MemberType NoteProperty -Name Author -Value $author
                   #$cmdlet | Add-Member -MemberType NoteProperty -Name ModuleName -Value $module
                   $cmdlet | Add-Member -MemberType NoteProperty -Name PublishedAt -Value $PublishedAt
                   $cmdlet | Add-Member -MemberType NoteProperty -Name BaseUri -Value $BaseUri
                   $cmdlet | Add-Member -MemberType NoteProperty -Name Version -Value $mod.Version
                   $cmdlet | Add-Member -MemberType NoteProperty -Name Company -Value $mod.Company 
                   $cmdlet | Add-Member -MemberType NoteProperty -Name Tags -Value $mod.Tags 
                   $out = Write-CmdletDoc -Model $cmdlet -Do $Do
                   $out = $out.Trim()

                   if(!(Test-Path $Path)) {
                        New-Item $Path -ItemType Directory | Write-Debug
                   }

                   $out | Out-File "$Path/$name.md" -Encoding "UTF8"
                }

                if($PostProcess) {
                    & $PostProcess -Path $path -Help $help 
                }
            }
        } 
    }
}
Set-Alias -Name "Write-Help" -Value "Write-ModuleHelpDocs"