Repository navigation
Expand file tree
/
Copy pathBuild-PSBuildMAMLHelp.ps1
More file actions
75 lines (67 loc) · 3.24 KB
/
Copy pathBuild-PSBuildMAMLHelp.ps1
File metadata and controls
75 lines (67 loc) · 3.24 KB
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
function Build-PSBuildMAMLHelp {
<#
.SYNOPSIS
Builds PowerShell MAML XML help file from PlatyPS markdown files
.DESCRIPTION
Builds PowerShell MAML XML help file from PlatyPS markdown files
.PARAMETER Path
The path to the PlatyPS markdown documents.
.PARAMETER DestinationPath
The path to the output module directory.
.EXAMPLE
PS> Build-PSBuildMAMLHelp -Path ./docs -Destination ./output/MyModule
Uses PlatyPS to generate MAML XML help from markdown files in ./docs
and saves the XML file to a directory under ./output/MyModule
#>
[CmdletBinding()]
param(
[parameter(Mandatory)]
[string]$Path,
[parameter(Mandatory)]
[string]$DestinationPath
)
$helpLocales = (Get-ChildItem -Path $Path -Directory).Name
# Generate the module's primary MAML help file
foreach ($locale in $helpLocales) {
$localePath = [IO.Path]::Combine($Path, $locale)
# Only command documents can be exported. A module landing page imports without
# complaint but fails on export, and that failure aborts the whole batch and writes
# nothing, so it has to be filtered out rather than caught.
$commandMarkdownPath = @(
Measure-PlatyPSMarkdown -Path ([IO.Path]::Combine($localePath, '*.md')) |
Where-Object { $_.Filetype -match 'CommandHelp' } |
Select-Object -ExpandProperty 'FilePath'
)
if ($commandMarkdownPath.Count -eq 0) {
continue
}
# Export-MamlCommandHelp writes to <OutputFolder>/<ModuleName>/<external help file>,
# a level deeper than PowerShell looks and with the file name taken from the
# document's front matter. Export to a staging directory and move the results so the
# published layout stays <DestinationPath>/<locale>/<ModuleName>-help.xml, which is
# where every consumer's existing .ExternalHelp directive already points.
$stagingPath = [IO.Path]::Combine(
[IO.Path]::GetTempPath(),
[IO.Path]::GetRandomFileName()
)
try {
$mamlFile = @(
Import-MarkdownCommandHelp -Path $commandMarkdownPath |
Export-MamlCommandHelp -OutputFolder $stagingPath -Force -Verbose:($VerbosePreference -eq 'Continue')
)
$localeDestinationPath = [IO.Path]::Combine($DestinationPath, $locale)
if (-not (Test-Path -LiteralPath $localeDestinationPath)) {
New-Item -Path $localeDestinationPath -ItemType Directory -Force > $null
}
foreach ($file in $mamlFile) {
# The file name is whatever the document's "external help file" front matter
# says, which Build-PSBuildMarkdown pins to the 0.14.x casing. Renaming here
# instead would leave the markdown and the MAML disagreeing about the name.
$destinationFilePath = Join-Path -Path $localeDestinationPath -ChildPath $file.Name
Move-Item -LiteralPath $file.FullName -Destination $destinationFilePath -Force
}
} finally {
Remove-Item -LiteralPath $stagingPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
}