|
| 1 | +param |
| 2 | +( |
| 3 | + [Parameter()] |
| 4 | + [System.IO.DirectoryInfo] |
| 5 | + $ProjectPath = (property ProjectPath $BuildRoot), |
| 6 | + |
| 7 | + [Parameter()] |
| 8 | + [System.String] |
| 9 | + $ProjectName = (property ProjectName ''), |
| 10 | + |
| 11 | + [Parameter()] |
| 12 | + [System.String] |
| 13 | + $SourcePath = (property SourcePath ''), |
| 14 | + |
| 15 | + [Parameter()] |
| 16 | + [System.String] |
| 17 | + $HelpSourceFolder = (property HelpSourceFolder 'docs'), |
| 18 | + |
| 19 | + [Parameter()] |
| 20 | + [System.String] |
| 21 | + $OutputDirectory = (property OutputDirectory 'output'), |
| 22 | + |
| 23 | + [Parameter()] |
| 24 | + [System.String] |
| 25 | + $BuiltModuleSubdirectory = (property BuiltModuleSubdirectory ''), |
| 26 | + |
| 27 | + [Parameter()] |
| 28 | + [System.Management.Automation.SwitchParameter] |
| 29 | + $VersionedOutputDirectory = (property VersionedOutputDirectory $true), |
| 30 | + |
| 31 | + [Parameter()] |
| 32 | + [System.String] |
| 33 | + $HelpOutputFolder = (property HelpOutputFolder 'help'), |
| 34 | + |
| 35 | + [Parameter()] |
| 36 | + [cultureInfo] |
| 37 | + $HelpCultureInfo = 'en-US', |
| 38 | + |
| 39 | + [Parameter()] |
| 40 | + [System.Management.Automation.SwitchParameter] |
| 41 | + $CopyHelpMamlToBuiltModuleBase = (property CopyHelpMamlToBuiltModuleBase $true), |
| 42 | + |
| 43 | + # Build Configuration object |
| 44 | + [Parameter()] |
| 45 | + [System.Collections.Hashtable] |
| 46 | + $BuildInfo = (property BuildInfo @{ }) |
| 47 | +) |
| 48 | + |
| 49 | +function Get-GenerateHelpPSVariables |
| 50 | +{ |
| 51 | + param () |
| 52 | + |
| 53 | + $script:PesterOutputFolder = Get-SamplerAbsolutePath -Path $PesterOutputFolder -RelativeTo $OutputDirectory |
| 54 | + |
| 55 | + "`tPester Output Folder = '$PesterOutputFolder" |
| 56 | + |
| 57 | + $script:HelpSourceFolder = Get-SamplerAbsolutePath -Path $HelpSourceFolder -RelativeTo $ProjectPath |
| 58 | + "`tHelp Source Folder = '$HelpSourceFolder'" |
| 59 | + |
| 60 | + $script:HelpOutputFolder = Get-SamplerAbsolutePath -Path $HelpOutputFolder -RelativeTo $OutputDirectory |
| 61 | + "`tHelp output Folder = '$HelpOutputFolder'" |
| 62 | + |
| 63 | + if ($ModuleVersion) |
| 64 | + { |
| 65 | + $script:HelpOutputVersionFolder = Get-SamplerAbsolutePath -Path $ModuleVersion -RelativeTo $HelpOutputFolder |
| 66 | + } |
| 67 | + |
| 68 | + "`tHelp output Version Folder = '$HelpOutputVersionFolder'" |
| 69 | + |
| 70 | + $script:HelpOutputCultureFolder = Get-SamplerAbsolutePath -Path $HelpCultureInfo -RelativeTo $HelpOutputVersionFolder |
| 71 | + "`tHelp output Culture path = '$HelpOutputCultureFolder'" |
| 72 | + |
| 73 | + $script:DocOutputFolder = Get-SamplerAbsolutePath -Path 'docs' -RelativeTo $OutputDirectory |
| 74 | + "`tDocs output folder path = '$DocOutputFolder'" |
| 75 | +} |
| 76 | + |
| 77 | +# Synopsis: Produces markdown help files from the built module. |
| 78 | +task Generate_help_from_built_module { |
| 79 | + . Set-SamplerTaskVariable |
| 80 | + |
| 81 | + Get-GenerateHelpPSVariables |
| 82 | + |
| 83 | + $generateHelpCommands = @" |
| 84 | + `$env:PSModulePath = '$Env:PSModulePath' |
| 85 | + `$targetModule = Import-Module -Name '$ProjectName' -ErrorAction Stop -Passthru |
| 86 | +
|
| 87 | + `$helpDestination = Join-Path '$HelpSourceFolder' -ChildPath '$HelpCultureInfo' |
| 88 | +
|
| 89 | + if (!(Test-Path -Path `$helpDestination)) { |
| 90 | + New-Item -Path `$helpDestination -ItemType Directory -Force -ErrorAction Ignore | Out-Null |
| 91 | + } |
| 92 | +
|
| 93 | + `$docOutputFolder = Join-Path '$DocOutputFolder' -ChildPath '$ProjectName' |
| 94 | +
|
| 95 | + `$commandsArray = @() |
| 96 | + foreach (`$publicFunction in `$targetModule.ExportedFunctions.Keys) { |
| 97 | + `$command = Get-Command -Name `$publicFunction -Module `$targetModule |
| 98 | +
|
| 99 | + `$newMarkdownCommandHelpParams = @{ |
| 100 | + CommandInfo = `$command |
| 101 | + OutputFolder = '$DocOutputFolder' |
| 102 | + HelpVersion = `$targetModule.Version |
| 103 | + Locale = '$HelpCultureInfo' |
| 104 | + Encoding = 'utf8' |
| 105 | + Force = `$true |
| 106 | + } |
| 107 | + `$Output = New-MarkdownCommandHelp @newMarkdownCommandHelpParams |
| 108 | +
|
| 109 | + `$helpCommand = Import-MarkdownCommandHelp -Path `$Output -ErrorAction Ignore |
| 110 | +
|
| 111 | + # Add the command to the array |
| 112 | + `$commandsArray += `$helpCommand |
| 113 | +
|
| 114 | + `$alias = Get-Alias -Definition `$command.Name -ErrorAction Ignore |
| 115 | +
|
| 116 | + if (`$alias) { |
| 117 | + `$helpCommand.Aliases = @(`$alias.Name) |
| 118 | + } else { |
| 119 | + `$helpCommand.Aliases = @() |
| 120 | + } |
| 121 | +
|
| 122 | + `$helpCommand | Export-MarkdownCommandHelp -OutputFolder '$DocOutputFolder' -Force |
| 123 | +
|
| 124 | + Copy-Item -Path `$Output -Destination `$helpDestination -Force |
| 125 | + } |
| 126 | +
|
| 127 | + `$newMarkdownModuleFileParams = @{ |
| 128 | + CommandHelp = `$commandsArray |
| 129 | + OutputFolder = '$DocOutputFolder' |
| 130 | + Force = `$true |
| 131 | + } |
| 132 | + `$markdownFile = New-MarkdownModuleFile @newMarkdownModuleFileParams |
| 133 | +
|
| 134 | + if (`$markdownFile) { |
| 135 | + Copy-Item -Path `$markdownFile -Destination `$helpDestination -Force |
| 136 | + } |
| 137 | +
|
| 138 | +"@ |
| 139 | + Write-Build -Color DarkGray -Text "$generateHelpCommands" |
| 140 | + $sb = [ScriptBlock]::create($generateHelpCommands) |
| 141 | + |
| 142 | + $pwshPath = (Get-Process -Id $PID).Path |
| 143 | + &$pwshPath -Command $sb -ExecutionPolicy 'ByPass' |
| 144 | +} |
0 commit comments