forked from pnp/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild-Site.ps1
196 lines (156 loc) · 6.74 KB
/
Build-Site.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
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
# copy documentation to output folder
#New-Item -Path "./dev/pages/cmdlets/released" -ItemType Directory
#New-Item -Path "./dev/pages/cmdlets/nightly" -ItemType Directory
$nightlycmdlets = Get-ChildItem "./dev/documentation/*.md" | ForEach-Object { $_ | Select-Object -ExpandProperty BaseName }
class FrontMatters {
[hashtable] GetHeader($path) {
$c = get-content $path
$header = @{}
if ($c[0].equals("---")) {
for ($q = 1; $q -lt $c.Length; $q++) {
if ($c[$q] -eq "---") {
# front-matter ended
$q = $c.Length;
}
else {
$colonIndex = $c[$q].IndexOf(":");
$key = $c[$q].Substring(0, $colonIndex).Trim()
$value = $c[$q].Substring($colonIndex + 1).Trim()
$header[$key] = $value;
}
}
}
return $header
}
[string] WriteHeader($path, $header) {
$c = get-content $path
if ($c[0].equals("---")) {
$newFile = [System.Collections.ArrayList]@()
$frontMatterEnded = $false
for ($q = 1; $q -lt $c.Length; $q++) {
if ($c[$q] -eq "---") {
$frontMatterEnded = $true
$q++;
}
if ($frontMatterEnded -ne $false) {
$newFile.Add($c[$q])
}
}
$contents = ""
foreach ($line in $newFile) {
$contents += "$line`n"
}
$newHeader = "---`n";
$header.Keys.ForEach({ $newHeader += "$($_): $($header.Item($_))`n" });
$newHeader += "---`n"
Set-Content -Path $path -Value "$newHeader $contents" -Force
}
return $null
}
}
$fm = New-Object -TypeName FrontMatters
$aliasCmdletsCount = 0
$aliasCmdlets = @()
Try {
Write-Host "Generating documentation files for alias cmdlets" -ForegroundColor Yellow
# Load the Module in a new PowerShell session
$scriptBlockNightlyRelease = {
Write-Host "Installing latest nightly release of PnP PowerShell"
Install-Module PnP.PowerShell -AllowPrerelease -Force
Write-Host "Retrieving PnP PowerShell alias cmdlets"
$cmdlets = Get-Command -Module PnP.PowerShell | Where-Object CommandType -eq "Alias" | Select-Object -Property @{N="Alias";E={$_.Name}}, @{N="ReferencedCommand";E={$_.ReferencedCommand.Name}}
$cmdlets
Write-Host "$($cmdlets.Length) alias cmdlets retrieved"
}
$aliasCmdlets = Start-ThreadJob -ScriptBlock $scriptBlockNightlyRelease | Receive-Job -Wait
$aliasCmdletsCount = $aliasCmdlets.Length
$scriptBlockStableRelease = {
Write-Host "Retrieving PnP PowerShell cmdlets from latest stable release"
$cmdlets = (Find-Module -Name PnP.PowerShell).AdditionalMetadata.Cmdlets.Split(" ")
$cmdlets
Write-Host "$($cmdlets.Length) cmdlets retrieved"
}
$stableReleaseCmdlets = Start-ThreadJob -ScriptBlock $scriptBlockStableRelease | Receive-Job -Wait
Write-Host "- Retrieving alias template page"
$aliasTemplatePageContent = Get-Content -Path "./dev/pages/cmdlets/alias.template" -Raw
ForEach($aliasCmdlet in $aliasCmdlets)
{
$destinationFileName = "./dev/documentation/$($aliasCmdlet.Alias).md"
Write-Host "- Creating page for $($aliasCmdlet.Alias) being an alias for $($aliasCmdlet.ReferencedCommand) as $destinationFileName" -ForegroundColor Yellow
$aliasTemplatePageContent.Replace("%%cmdletname%%", $aliasCmdlet.Alias).Replace("%%referencedcmdletname%%", $aliasCmdlet.ReferencedCommand) | Out-File $destinationFileName -Force
}
}
Catch {
Write-Host "Error: Cannot generate alias documentation files"
Write-Host $_
}
Write-Host "Copying documentation files to page cmdlets"
Copy-Item -Path "./dev/documentation/*.md" -Destination "./dev/pages/cmdlets" -Force
foreach ($nightlycmdlet in $nightlycmdlets) {
if (!($stableReleaseCmdlets -like $nightlycmdlet)) {
Copy-Item "./dev/documentation/$nightlycmdlet.md" -Destination "./dev/pages/cmdlets" -Force | Out-Null
# update the document to state it's only available in the nightly build
$header = $fm.GetHeader("./dev/pages/cmdlets/$nightlycmdlet.md")
$header["tags"] = "Available in the current Nightly Release only."
#Write-Host "Writing $nightlycmdlet.md"
$fm.WriteHeader("./dev/pages/cmdlets/$nightlycmdlet.md",$header)
}
}
# Generate cmdlet toc
Write-Host "Retrieving all cmdlet pages"
$cmdletPages = Get-ChildItem -Path "./dev/pages/cmdlets/*.md" -Exclude "index.md","alias.template"
$toc = ""
foreach ($cmdletPage in $cmdletPages) {
$toc = $toc + "- name: $($cmdletPage.BaseName)`n href: $($cmdletPage.Name)`n"
}
$toc | Out-File "./dev/pages/cmdlets/toc.yml" -Force
# Generate cmdlet index page
Write-Host "Creating cmdlets index page"
$cmdletIndexPageContent = Get-Content -Path "./dev/pages/cmdlets/index.md" -Raw
$cmdletIndexPageContent = $cmdletIndexPageContent.Replace("%%cmdletcount%%", $cmdletPages.Length - $aliasCmdletsCount)
$cmdletIndexPageList = ""
$previousCmdletVerb = ""
foreach ($cmdletPage in $cmdletPages)
{
Write-Host "- $($cmdletPage.Name)"
# Define the verb of the cmdlet
if($cmdletPage.BaseName.Contains("-"))
{
$cmdletVerb = $cmdletPage.BaseName.Remove($cmdletPage.BaseName.IndexOf("-"))
if($cmdletVerb -ne $previousCmdletVerb)
{
# Add a new heading for the new verb
$cmdletIndexPageList += "## $($cmdletVerb)`n"
}
}
else
{
$cmdletVerb = ""
}
# Add a new entry for the verb
$cmdletIndexPageList += "- [$($cmdletPage.BaseName)]($($cmdletPage.Name))"
# Check if the cmdlet only exists in the nightly build
if (!($stableReleaseCmdlets -like $cmdletPage.BaseName))
{
# Add a 1 to the cmdlet name if it's only available in the nightly build
$cmdletIndexPageList = $cmdletIndexPageList + " <sup>1</sup>"
Write-Host " - Nightly only"
}
# Check if the cmdlet is an alias
if ($aliasCmdlets.Alias -contains $cmdletPage.BaseName)
{
# Add a 2 to the cmdlet name if it's an alias
$cmdletIndexPageList = $cmdletIndexPageList + " <sup>2</sup>"
Write-Host " - Alias"
}
$cmdletIndexPageList = $cmdletIndexPageList + "`n"
if($cmdletVerb -ne "")
{
# Track the last verb so we know if we need to add a new heading for the next cmdlet
$previousCmdletVerb = $cmdletVerb
}
}
$cmdletIndexPageContent = $cmdletIndexPageContent.Replace("%%cmdletlisting%%", $cmdletIndexPageList)
$cmdletIndexPageContent | Out-File "./dev/pages/cmdlets/index.md" -Force
docfx build ./dev/pages/docfx.json
Copy-Item -Path "./dev/pages/_site/*" -Destination "./gh-pages" -Force -Recurse