-
Notifications
You must be signed in to change notification settings - Fork 0
285 lines (240 loc) · 10.8 KB
/
Copy pathBuild-Site.yml
File metadata and controls
285 lines (240 loc) · 10.8 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
name: Build-Site
on:
workflow_call:
inputs:
Settings:
type: string
description: The complete settings object.
required: true
permissions:
contents: read # to checkout the repo
jobs:
Build-Site:
name: Build-Site
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
persist-credentials: false
fetch-depth: 0
- name: Checkout Process-PSModule
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
path: _wf
persist-credentials: false
- name: Install-PSModule
uses: ./_wf/.github/actions/Install-PSModule
- name: Download docs artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: docs
path: ${{ fromJson(inputs.Settings).WorkingDirectory }}/outputs/docs
- name: Install Zensical
shell: pwsh
run: |
pip install zensical
- name: Structure site
uses: PSModule/GitHub-Script@8083ec1f733f00357ee4d0db0c6056686e483bc0 # v1.9.0
with:
Debug: ${{ fromJson(inputs.Settings).Debug }}
Prerelease: ${{ fromJson(inputs.Settings).Prerelease }}
Verbose: ${{ fromJson(inputs.Settings).Verbose }}
Version: ${{ fromJson(inputs.Settings).Version }}
WorkingDirectory: ${{ fromJson(inputs.Settings).WorkingDirectory }}
Script: |
LogGroup "Get folder structure" {
$functionDocsFolder = New-Item -Path "outputs/site/docs/Functions" -ItemType Directory -Force
Copy-Item -Path "outputs/docs/*" -Destination "outputs/site/docs/Functions" -Recurse -Force
$moduleName = [string]::IsNullOrEmpty('${{ fromJson(inputs.Settings).Name }}') ? $env:GITHUB_REPOSITORY_NAME : '${{ fromJson(inputs.Settings).Name }}'
$ModuleSourcePath = Resolve-Path 'src' | Select-Object -ExpandProperty Path
$SiteOutputPath = Resolve-Path 'outputs/site' | Select-Object -ExpandProperty Path
Write-Host "Function Docs Folder: $functionDocsFolder"
Write-Host "Module Name: $moduleName"
Write-Host "Module Source Path: $ModuleSourcePath"
Write-Host "Site Output Path: $SiteOutputPath"
}
LogGroup "Get folder structure" {
Get-ChildItem -Recurse | Select-Object -ExpandProperty FullName | Sort-Object | Format-List
}
Get-ChildItem -Path $functionDocsFolder -Recurse -Force -Include '*.md' | ForEach-Object {
$fileName = $_.Name
LogGroup " - $fileName" {
Show-FileContent -Path $_
}
}
LogGroup 'Build docs - Process about topics' {
$aboutDocsFolderPath = Join-Path -Path $SiteOutputPath -ChildPath 'docs/About'
$aboutDocsFolder = New-Item -Path $aboutDocsFolderPath -ItemType Directory -Force
$aboutSourceFolder = Get-ChildItem -Path $ModuleSourcePath -Directory | Where-Object { $_.Name -eq 'en-US' }
Get-ChildItem -Path $aboutSourceFolder -Filter *.txt | Copy-Item -Destination $aboutDocsFolder -Force -Verbose -PassThru |
Rename-Item -NewName { $_.Name -replace '.txt', '.md' }
Write-Host "About Docs Folder: $aboutDocsFolder"
Write-Host "About Source Folder: $aboutSourceFolder"
}
LogGroup 'Build docs - Copy icon to assets' {
$assetsFolderPath = Join-Path -Path $SiteOutputPath -ChildPath 'docs/Assets'
$assetsFolder = New-Item -Path $assetsFolderPath -ItemType Directory -Force
$rootPath = Split-Path -Path $ModuleSourcePath -Parent
$iconPath = Resolve-Path 'icon\icon.png' | Select-Object -ExpandProperty Path
Copy-Item -Path $iconPath -Destination $assetsFolder -Force -Verbose
Write-Host "Assets Folder: $assetsFolder"
Write-Host "Icon Path: $iconPath"
}
LogGroup 'Build docs - Copy readme.md' {
$readmePath = Resolve-Path 'README.md' | Select-Object -ExpandProperty Path
$readmeTargetPath = Join-Path -Path $SiteOutputPath -ChildPath 'docs/README.md'
Copy-Item -Path $readmePath -Destination $readmeTargetPath -Force -Verbose
Write-Host "Readme Path: $readmePath"
Write-Host "Readme Target Path: $readmeTargetPath"
}
LogGroup 'Build docs - Create docs config' {
$rootPath = Split-Path -Path $ModuleSourcePath -Parent
$possiblePaths = @(
'.github/zensical.toml',
'docs/zensical.toml',
'zensical.toml'
)
$docsConfigSourcePath = $null
foreach ($path in $possiblePaths) {
$candidatePath = Join-Path -Path $rootPath -ChildPath $path
if (Test-Path -Path $candidatePath) {
$docsConfigSourcePath = $candidatePath
break
}
}
if (-not $docsConfigSourcePath) {
throw "Documentation config file not found. Expected one of: $($possiblePaths -join ', ')"
}
$docsConfigFileName = [System.IO.Path]::GetFileName($docsConfigSourcePath)
$docsConfigTargetPath = Join-Path -Path $SiteOutputPath -ChildPath $docsConfigFileName
Write-Host "Docs Config Source: $docsConfigSourcePath"
Write-Host "Docs Config Target: $docsConfigTargetPath"
$docsConfigContent = Get-Content -Path $docsConfigSourcePath -Raw
$docsConfigContent = $docsConfigContent.Replace('-{{ REPO_NAME }}-', $ModuleName)
$docsConfigContent = $docsConfigContent.Replace('-{{ REPO_OWNER }}-', $env:GITHUB_REPOSITORY_OWNER)
if ($docsConfigFileName -eq 'zensical.toml') {
if ($docsConfigContent -match '(?m)^\s*site_dir\s*=') {
$docsConfigContent = $docsConfigContent -replace '(?m)^\s*site_dir\s*=.*$', 'site_dir = "_site"'
} else {
$docsConfigContent = $docsConfigContent -replace '(?m)^\[project\]\s*$', "[project]`nsite_dir = ""_site"""
}
}
$docsConfigContent | Set-Content -Path $docsConfigTargetPath -Force
Write-Host "Build Config Type: $docsConfigFileName"
Show-FileContent -Path $docsConfigTargetPath
}
- name: Build documentation site with Zensical
working-directory: ${{ fromJson(inputs.Settings).WorkingDirectory }}/outputs/site
shell: pwsh
run: |
if (-not (Test-Path -Path 'zensical.toml')) {
throw "No documentation config file found in outputs/site. Expected zensical.toml."
}
$configFile = 'zensical.toml'
LogGroup 'Build docs - Zensical config content' {
Write-Host "Using config: $configFile"
Show-FileContent -Path $configFile
}
LogGroup 'Build docs - Zensical build' {
zensical build --config-file $configFile
}
LogGroup 'Build docs - verify output' {
if (Test-Path -Path '_site') {
if (Test-Path -Path '../../_site') {
Remove-Item -Path '../../_site' -Recurse -Force
}
Move-Item -Path '_site' -Destination '../../_site' -Force
}
if (-not (Test-Path -Path '../../_site')) {
throw "Expected Zensical output at ../../_site but it was not created."
}
$navStateScript = @'
(() => {
const storageKey = "zensical-nav-state-v1";
let lastSignature = "";
const getToggles = () =>
Array.from(document.querySelectorAll("input.md-nav__toggle.md-toggle[id]"));
const getPrimaryList = () =>
document.querySelector("nav.md-nav--primary > ul.md-nav__list");
const applyDefaultTopLevelState = (toggles) => {
const primaryList = getPrimaryList();
if (!primaryList) {
return;
}
const topLevelToggles = Array.from(
primaryList.querySelectorAll(
":scope > li > input.md-nav__toggle.md-toggle[id]"
)
);
const topLevelIds = new Set(topLevelToggles.map((toggle) => toggle.id));
for (const toggle of toggles) {
toggle.checked = topLevelIds.has(toggle.id);
}
};
const restoreState = (toggles) => {
const raw = localStorage.getItem(storageKey);
if (!raw) {
applyDefaultTopLevelState(toggles);
return;
}
try {
const state = JSON.parse(raw);
for (const toggle of toggles) {
if (Object.prototype.hasOwnProperty.call(state, toggle.id)) {
toggle.checked = !!state[toggle.id];
}
}
} catch {
applyDefaultTopLevelState(toggles);
}
};
const persistState = (toggles) => {
const state = {};
for (const toggle of toggles) {
state[toggle.id] = !!toggle.checked;
}
localStorage.setItem(storageKey, JSON.stringify(state));
};
const initialize = () => {
const toggles = getToggles();
if (toggles.length === 0) {
return;
}
const signature = toggles.map((toggle) => toggle.id).join("|");
if (signature === lastSignature) {
return;
}
lastSignature = signature;
restoreState(toggles);
for (const toggle of toggles) {
if (toggle.dataset.navStateBound === "true") {
continue;
}
toggle.dataset.navStateBound = "true";
toggle.addEventListener("change", () => persistState(getToggles()));
}
};
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initialize, { once: true });
} else {
initialize();
}
setInterval(initialize, 500);
})();
'@
Get-ChildItem -Path '../../_site' -Filter '*.html' -Recurse | ForEach-Object {
$html = Get-Content -Path $_.FullName -Raw
if ($html -notmatch 'zensical-nav-state-v1') {
$html = $html -replace '</body>', "<script>$navStateScript</script>`n</body>"
Set-Content -Path $_.FullName -Value $html -NoNewline
}
}
}
- uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
name: github-pages
path: ${{ fromJson(inputs.Settings).WorkingDirectory }}/_site
retention-days: 1