-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ps1
More file actions
63 lines (50 loc) · 1.91 KB
/
Copy pathmain.ps1
File metadata and controls
63 lines (50 loc) · 1.91 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
#Requires -Version 7.0
<#
.SYNOPSIS
Injects shared JavaScript snippets into generated site HTML files.
.DESCRIPTION
Reads scripts from .github/scripts/site-injectors in the checked out workflow
repository and injects each script into every HTML file under SitePath, once per file.
.EXAMPLE
./main.ps1 -SitePath './_site' -WorkflowPath '_wf'
.INPUTS
None.
.OUTPUTS
None.
#>
[CmdletBinding()]
param(
# Path to generated site output directory containing HTML files.
[Parameter(Mandatory)]
[string]$SitePath,
# Relative path to the checked out workflow repository root.
[Parameter(Mandatory)]
[string]$WorkflowPath
)
$resolvedSitePath = Resolve-Path -Path $SitePath -ErrorAction Stop | Select-Object -ExpandProperty Path
$injectorsPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath "$WorkflowPath/.github/scripts/site-injectors"
if (-not (Test-Path -Path $injectorsPath)) {
throw "Expected site injector folder at $injectorsPath but it was not found."
}
$injectorScripts = Get-ChildItem -Path $injectorsPath -File -Filter '*.js' | Sort-Object -Property Name
if (-not $injectorScripts) {
Write-Host "No site injector scripts found under $injectorsPath."
exit 0
}
Get-ChildItem -Path $resolvedSitePath -Filter '*.html' -Recurse | ForEach-Object {
$html = Get-Content -Path $_.FullName -Raw
$modified = $false
foreach ($injectorScript in $injectorScripts) {
$marker = "data-psmodule-site-injector=""$($injectorScript.Name)"""
if ($html -match [Regex]::Escape($marker)) {
continue
}
$scriptContent = Get-Content -Path $injectorScript.FullName -Raw
$injectedScript = "<script $marker>$scriptContent</script>"
$html = $html -replace '</body>', "$injectedScript`n</body>"
$modified = $true
}
if ($modified) {
Set-Content -Path $_.FullName -Value $html -NoNewline
}
}