Skip to content

Commit 890f734

Browse files
committed
Auto-resolve includes before pushing updated stats
1 parent 7b3b519 commit 890f734

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

.netconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@
9393
sha = 032439dbf180fca0539a5bd3a019f18ab3484b76
9494
etag = da7c0104131bd474b52fc9bc9f9bda6470e24ae38d4fb9f5c4f719bc01370ab5
9595
weak
96+
[file "resolve-file-includes.ps1"]
97+
url = https://github.com/devlooped/actions-includes/blob/main/resolve-file-includes.ps1
98+
sha = f8dba1e4ce78910971ac028cc8c572015deadba0
99+
100+
etag = 0a3acaa0f014bc8f5c31762e44e45fccf25f9ed9c690b9a1f92924522135b370
101+
weak

resolve-file-includes.ps1

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
$include = $env:RESOLVE_INCLUDE ?? '*.md'
2+
$exclude = $env:RESOLVE_EXCLUDE ?? $null
3+
$recurse = [bool]::Parse($env:RESOLVE_RECURSE ?? "true")
4+
$validate = [bool]::Parse($env:RESOLVE_VALIDATE ?? "true")
5+
6+
Write-Output "Include: $include"
7+
Write-Output "Exclude: $exclude"
8+
9+
$files = if ($recurse) {
10+
Get-ChildItem -Include $include -Exclude $exclude -Recurse
11+
} else {
12+
Get-ChildItem -Include $include -Exclude $exclude
13+
}
14+
15+
foreach ($file in $files) {
16+
Write-Output "Processing $file"
17+
$content = ((Get-Content $file -Raw -Encoding UTF8) ?? '').Trim()
18+
if ($content.StartsWith('<!-- exclude -->') -or $content.EndsWith('<!-- exclude -->')) {
19+
Write-Output "Excluding $file"
20+
continue
21+
}
22+
23+
$replacements = @{}
24+
foreach ($match in (Select-String -Pattern '<!--\s?include (.*?)\s?-->' -Path $file)) {
25+
$includedPath = ($match.Matches[0].Value -replace '<!--\s?include ','' -replace '\s?-->', '').Trim()
26+
$fragment = $null
27+
if ($includedPath.Contains('#')) {
28+
$fragment = '#' + $includedPath.Split('#')[1]
29+
$includedPath = $includedPath.Substring(0, $includedPath.IndexOf('#'))
30+
}
31+
32+
$isuri = [uri]::IsWellFormedUriString($includedPath, 'Absolute')
33+
$includedFullPath = Join-Path (Get-ChildItem -Path $file).DirectoryName -ChildPath $includedPath
34+
35+
if ((Test-Path $includedFullPath) -Or $isuri) {
36+
$include = if ($isuri) {
37+
(Invoke-RestMethod $includedPath) ?? ''
38+
} else {
39+
(Get-Content $includedFullPath -Raw -Encoding UTF8) ?? ''
40+
}
41+
42+
# Resolve fragment specifier if present
43+
if ($fragment) {
44+
$anchor = "<!-- $fragment -->"
45+
$start = $include.IndexOf($anchor)
46+
if ($start -eq -1) {
47+
if ($validate) {
48+
Write-Error "Referenced anchor $fragment not found in $includedPath"
49+
} else {
50+
Write-Warning "Referenced anchor $fragment not found in $includedPath"
51+
}
52+
continue
53+
}
54+
$include = $include.Substring($start)
55+
$end = $include.IndexOf($anchor, $anchor.Length)
56+
if ($end -ne -1) {
57+
$include = $include.Substring(0, $end + $anchor.Length)
58+
}
59+
}
60+
# TODO: disable nested includes until we can properly support that scenario, and nested excludes
61+
$include = $include -replace '<!--\s?include ','<!-- ' -replace '<!-- exclude -->', ''
62+
# see if we already have a section we previously replaced
63+
$existingRegex = "<!--\s?include $includedPath$fragment\s?-->[\s\S]*<!-- $includedPath$fragment -->"
64+
$replacement = "<!-- include $includedPath$fragment -->`n$include`n<!-- $includedPath$fragment -->"
65+
if ($content -match $existingRegex) {
66+
$replacements[$existingRegex] = $replacement
67+
} else {
68+
$replacements["<!--\s?include $includedPath$fragment\s?-->"] = $replacement
69+
}
70+
} else {
71+
if ($validate) {
72+
Write-Error "Included file $includedPath in $($file.Name) not found"
73+
} else {
74+
Write-Warning "Included file $includedPath in $($file.Name) not found"
75+
}
76+
}
77+
}
78+
79+
if ($replacements.Count -gt 0) {
80+
foreach ($replacement in $replacements.GetEnumerator()) {
81+
#Write-Host "Replacing $($replacement.Key) with $($replacement.Value)"
82+
$content = $content -replace $replacement.Key, $replacement.Value
83+
}
84+
85+
$content = $content.TrimEnd()
86+
$actual = (Get-Content $file -Raw -Encoding UTF8).TrimEnd()
87+
88+
if ($content -ne $actual) {
89+
Set-Content $file -Value $content.TrimEnd() -Encoding UTF8
90+
Write-Output "Updated $($file.Name)"
91+
}
92+
}
93+
}

update.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jq -r '.HostEnvironmentInfo.ChronometerFrequency.Hertz as $ticks
2222

2323
if (-not $?) { throw 'Failed to create summary' }
2424

25+
.\resolve-file-includes.ps1
2526
add *.json
2627
add *.md
2728
commit -m "🖉 Update AI benchmarks"

0 commit comments

Comments
 (0)