forked from ButterscotchV/SlimeVR-Integration-Testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.ps1
129 lines (105 loc) · 4.99 KB
/
compile.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
$sourceDir = "src"
$targetDir = "dist"
$htmlPath = "/"
$targetHtmlPath = "/"
Write-Host "Copying to target folder..."
Remove-Item –Path "./$targetDir" -Recurse -ErrorAction Ignore
Copy-Item –Path "./$sourceDir" -Destination "./$targetDir" -Recurse
Write-Host "Compiling markdown checklist(s)..."
$markdownAssets = Get-ChildItem –Path "./$targetDir" -File -Include @("*.checkmd") –Recurse
foreach ($asset in $markdownAssets) {
$markdownContent = Get-Content -Path $asset
$htmlContent = ""
$testId = 0
$headerLevel = 0
foreach ($line in $markdownContent.Split("`n")) {
$line = $line.Trim()
if ($line.Length -le 0) {
continue
}
# Start of a new header
if ($line.StartsWith("#")) {
$headerEnd = $line.IndexOf(" ")
if ($headerEnd -le 0) {
continue
}
$header = $line.Remove($headerEnd)
# +1 to remove space
$headerLabel = $line.Substring($headerEnd + 1)
if ($header.Length -gt $headerLevel) {
# Increment the actual header level
$headerLevel++
}
else {
# If there's 0 difference, that means it's a new section on the same level, so we need to add 1 to handle it
$headerLevelDiff = ($headerLevel - $header.Length) + 1
# Close each header over the difference
foreach ($i in 1..$headerLevelDiff) {
$htmlContent += "</fieldset>"
}
# Now the header level is what was defined
$headerLevel = $header.Length
}
# Write the header
$htmlContent += "<fieldset><legend>$headerLabel</legend>"
}
# Return to header level (a little hacky but required for formatting)
elseif ($line.StartsWith("/#")) {
# Remove "/"
$header = $line.Substring(1)
$headerLevelDiff = $headerLevel - $header.Length
if ($headerLevelDiff -le 0) {
# Targeting higher headers than the current will break and going to the same one is useless
continue
}
# Close each header over the difference
foreach ($i in 1..$headerLevelDiff) {
$htmlContent += "</fieldset>"
}
# Now the header level is what was defined
$headerLevel = $header.Length
}
# New checkbox in current header
else {
$htmlContent += "<div><input type=""checkbox"" id=""test$testId"" name=""test$testId"" /><label for=""test$testId"">$line</label></div>"
$testId++
}
}
# Close any remaining headers
foreach ($i in 1..$headerLevel) {
$htmlContent += "</fieldset>"
}
# Replace requested sections with the compiled markdown
$replaceToken = "{checklist}$((Resolve-Path -Relative $asset).TrimStart('.').Replace('\', '/').TrimStart('/').Substring($targetDir.Length)){/checklist}"
Write-Host "Replacing ""$replaceToken"" with compiled markdown:"
$sourceFiles = Get-ChildItem –Path "./$targetDir" -File -Include @("*.html", "*.htm", "*.css", "*.js", "*.ts") –Recurse
foreach ($sourceFile in $sourceFiles) {
Write-Host " > Updating ""$sourceFile""..."
(Get-Content -Path $sourceFile).Replace($replaceToken, $htmlContent) | Set-Content -Path $sourceFile
}
# The markdown source is no longer needed, delete it
$asset.Delete()
}
Write-Host "Hashing static assets..."
$assets = Get-ChildItem –Path "./$targetDir/assets" -File –Recurse
foreach ($asset in $assets) {
# Get an 8 character lowercase SHA256 hash, this is what most use
$assetHash = (Get-FileHash -Algorithm "SHA256" $asset).Hash.ToLower().Remove(8)
# Get an HTML friendly relative path
$relativeAssetPath = (Resolve-Path -Relative $asset).TrimStart('.').Replace('\', '/').TrimStart('/').Substring($targetDir.Length)
# Add the hash to the HTML friendly relative path
$relativeAssetPathHashed = $relativeAssetPath.Remove($relativeAssetPath.Length - $asset.Extension.Length) + "-$assetHash" + $asset.Extension
# Update the start of the paths
$relativeAssetPath = $htmlPath + $relativeAssetPath.TrimStart('/')
$relativeAssetPathHashed = $targetHtmlPath + $relativeAssetPathHashed.TrimStart('/')
# Rename the file to be hashed
$assetPathHashed = $asset.FullName.Remove($asset.FullName.Length - $asset.Extension.Length) + "-$assetHash" + $asset.Extension
$asset.MoveTo($assetPathHashed)
Write-Host "Replacing ""$relativeAssetPath"" with ""$relativeAssetPathHashed"":"
$sourceFiles = Get-ChildItem –Path "./$targetDir" -File -Include @("*.html", "*.htm", "*.css", "*.js", "*.ts") –Recurse
foreach ($sourceFile in $sourceFiles) {
Write-Host " > Updating ""$sourceFile""..."
(Get-Content -Path $sourceFile).Replace($relativeAssetPath, $relativeAssetPathHashed) | Set-Content -Path $sourceFile
}
}
Write-Host "Done!"