Skip to content

Commit 7e95692

Browse files
style: Site improvement ( Fixes #344 )
Improving build, buildPage, and config
1 parent 29d280c commit 7e95692

File tree

3 files changed

+109
-24
lines changed

3 files changed

+109
-24
lines changed

psturtle.com/build.ps1

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,59 @@ $Site.Files =
2828
else { Get-ChildItem -Recurse -File }
2929

3030
$Site.PSScriptRoot = "$PSScriptRoot"
31+
foreach ($underbarDirectory in Get-ChildItem -Path $site.PSScriptRoot -Filter _* -Directory) {
32+
$Site[$underbarDirectory.Name -replace '^_'] = $Site[$underbarDirectory.Name] = [Ordered]@{}
33+
foreach ($underbarFile in Get-ChildItem -Path $underbarDirectory -Recurse) {
34+
$relativePath = $underbarFile.FullName.Substring($underbarDirectory.FullName.Length + 1)
35+
$pointer = $site
36+
$hierarchy = @($relativePath -split '[\\/]')
37+
for ($index = 0; $index -lt ($hierarchy.Length - 1); $index++) {
38+
$subdirectory = $hierarchy[$index] -replace '_'
39+
if (-not $pointer[$subdirectory]) {
40+
$pointer[$subdirectory] = [Ordered]@{}
41+
}
42+
$pointer = $pointer[$subdirectory]
43+
}
44+
45+
$propertyName = $hierarchy[-1] -replace '_'
46+
$getFile = @{LiteralPath=$underbarFile.FullName}
47+
$fileData =
48+
switch -regex ($underbarFile.Extension) {
49+
'\.ps1$' { $ExecutionContext.SessionState.InvokeCommand.GetCommand($underbarFile.FullName, 'ExternalScript') }
50+
'\.(css|html|txt)$' { Get-Content @getFile }
51+
'\.json$' { Get-Content @getFile | ConvertFrom-Json }
52+
'\.jsonl$' { Get-Content @getFile | ConvertFrom-Json }
53+
'\.psd1$' { Get-Content @getFile -Raw | ConvertFrom-StringData }
54+
'\.(?>ps1xml|xml|svg)$' { (Get-Content @getFile -Raw) -as [xml] }
55+
'\.(?>yaml|toml)$' { Get-Content @getFile -Raw }
56+
'\.csv$' { Import-Csv @getFile }
57+
'\.tsv$' { Import-Csv @getFile -Delimiter "`t" }
58+
}
59+
if (-not $fileData) { continue }
60+
$pointer[$relativePath -replace '\.ps1$'] = $fileData
61+
}
62+
}
3163

3264
#region Common Functions and Filters
65+
# Any functions or filter file at the site root should be loaded.
3366
$functionFileNames = 'functions', 'function', 'filters', 'filter'
3467
$functionPattern = "(?>$($functionFileNames -join '|'))\.ps1$"
3568
$functionFiles = Get-ChildItem -Path $Site.PSScriptRoot |
3669
Where-Object Name -Match $functionPattern
3770

3871
foreach ($file in $functionFiles) {
39-
# If we have a file with the name function or functions, we'll use it to set the site configuration.
72+
# If we have a file with the name function or functions,
73+
# we'll dot source it now so we can use the functions in the config
4074
. $file.FullName
4175
}
4276
#endregion Common Functions and Filters
4377

4478
# Set an alias to buildPage.ps1
4579
Set-Alias BuildPage ./buildPage.ps1
4680

47-
# If we have an event path,
48-
$gitHubEvent =
81+
# If we have a github event,
82+
# save it to a variable and to the `$site`
83+
$site.GitHubEvent = $gitHubEvent =
4984
if ($env:GITHUB_EVENT_PATH) {
5085
# all we need to do to serve it is copy it.
5186
Copy-Item $env:GITHUB_EVENT_PATH .\gitHubEvent.json
@@ -59,12 +94,15 @@ if (Test-Path 'CNAME') {
5994
$Site.CNAME = $CNAME = (Get-Content -Path 'CNAME' -Raw).Trim()
6095
$Site.RootUrl = "https://$CNAME/"
6196
} elseif (
97+
# otherwise, if we are in a directory that could be a domain
6298
($site.PSScriptRoot | Split-Path -Leaf) -like '*.*'
6399
) {
100+
# assume it _is_ the domain.
64101
$site.CNAME = $CNAME = ($site.PSScriptRoot | Split-Path -Leaf)
65102
$site.RootUrl = "https://$CNAME/"
66103
}
67104

105+
#region config
68106
# If we have a config.json file, it can be used to set the site configuration.
69107
if (Test-Path 'config.json') {
70108
$siteConfig = Get-Content -Path 'config.json' -Raw | ConvertFrom-Json
@@ -98,17 +136,20 @@ if (Test-Path 'config.ps1') {
98136
# run it, and let it configure anything it chooses to.
99137
. $configScript
100138
}
139+
#endregion config
101140

102141
# Start the clock
103142
$site['LastBuildTime'] = $lastBuildTime = [DateTime]::Now
104143
#region Build Files
105144

106145
# Start the clock on the build process
107146
$buildStart = [DateTime]::Now
147+
Write-Host "Started Building Pages @ $buildStart"
108148
# pipe every file we find to buildFile
109149
$Site.Files | . buildPage
110150
# and stop the clock
111151
$buildEnd = [DateTime]::Now
152+
Write-Host "Finished Building Pages @ $buildEnd ($($buildEnd - $buildStart))"
112153

113154
#endregion Build Files
114155

psturtle.com/buildPage.ps1

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ if (-not $site.PagesByUrl) {
2828
}
2929
$pagesByUrl = $site.PagesByUrl
3030

31-
:nextFile foreach ($file in $allFiles) {
31+
$fileQueue = [Collections.Queue]::new()
32+
foreach ($file in $allFiles) { $fileQueue.Enqueue($file) }
33+
34+
:nextFile while ($fileQueue.Count) {
35+
$file = $fileQueue.Dequeue()
36+
if ($file.FullName -match '/_[^\.]') { continue }
3237
if ($Site -and $Site.Exclude) {
3338
$included = $false
3439
:exclude do {
@@ -50,6 +55,7 @@ $pagesByUrl = $site.PagesByUrl
5055
}
5156
$included = $true
5257
} until ($included)
58+
if (-not $included) { continue }
5359
}
5460
$fileRoot = $file.Directory.FullName
5561
Push-Location $fileRoot
@@ -83,15 +89,15 @@ $pagesByUrl = $site.PagesByUrl
8389
$gitDates =
8490
try {
8591
# we can use `git log --follow --format=%ci` to get the dates in order
86-
(& $gitCommand log --follow --format=%ci --date default $file.FullName *>&1) -as [datetime[]]
92+
(& $gitCommand log --follow --format=%ci --date default $file.FullName *>&1) -as [datetime]
8793
} catch {
8894
$null
8995
}
9096
# Because the file might not be in git, we want to always set the `$LASTEXITCODE` to 0
9197
$LASTEXITCODE = 0
92-
# Set the date to the last date we find.
98+
# Set the date to the first date we find.
9399
if ($gitDates) {
94-
$page.Date = $gitDates[-1]
100+
$page.Date = $gitDates[0]
95101
}
96102
}
97103
}
@@ -186,11 +192,37 @@ $pagesByUrl = $site.PagesByUrl
186192
}
187193

188194
}
195+
default {
196+
Pop-Location
197+
continue nextFile
198+
}
189199
}
190200
#endregion Get Page Content
201+
202+
# We want to filter out files from the rest of output
203+
$outputFiles = @()
204+
$otherOutput = @(foreach ($out in $output) {
205+
if ($out -is [IO.FileInfo]) {
206+
$outputFiles += $out
207+
} else {
208+
$out
209+
}
210+
})
211+
212+
# If there were any files output
213+
if ($outputFiles) {
214+
# queue them
215+
foreach ($outputFile in $outputFiles) {
216+
$fileQueue.Enqueue($outputFile)
217+
$totalFiles++
218+
}
219+
}
220+
221+
# Set out output to any non-file output.
222+
$output = $otherOutput
191223

192224
# If we don't have output,
193-
if ($null -eq $Output) {
225+
if ($null -eq $OtherOutput) {
194226
Pop-Location
195227
continue nextFile # continue to the next file.
196228
}
@@ -330,6 +362,7 @@ $pagesByUrl = $site.PagesByUrl
330362
if ($?) {
331363
$page.OutputFile = Get-Item -Path $outFile
332364
$page.OutputFile
365+
Pop-Location
333366
continue nextFile
334367
}
335368
}
@@ -338,6 +371,7 @@ $pagesByUrl = $site.PagesByUrl
338371
if ($?) {
339372
$page.OutputFile = Get-Item -Path $outFile
340373
$page.OutputFile
374+
Pop-Location
341375
continue nextFile
342376
}
343377
}
@@ -346,6 +380,7 @@ $pagesByUrl = $site.PagesByUrl
346380
if ($?) {
347381
$page.OutputFile = Get-Item -Path $outFile
348382
$page.OutputFile
383+
Pop-Location
349384
continue nextFile
350385
}
351386
}
@@ -379,21 +414,26 @@ $pagesByUrl = $site.PagesByUrl
379414
# just output them directly.
380415
$outputFiles
381416
} else {
382-
# otherwise, we'll save output to a file.
383-
384-
# If the file does not exists
385-
if (-not (Test-Path -Path $outFile)) {
386-
# create an empty file.
387-
$null = New-Item -Path $outFile -ItemType File -Force
388-
}
389-
390-
$output > $outFile
391-
# and if that worked,
392-
if ($?) {
393-
# output the file.
394-
$page.OutputFile = Get-Item -Path $outFile
395-
$page.OutputFile
396-
}
417+
# otherwise, we'll save output to a file (assuming we have output).
418+
if ("$output") {
419+
# If the file does not exists
420+
if (-not (Test-Path -Path $outFile)) {
421+
# create an empty file.
422+
$null = New-Item -Path $outFile -ItemType File -Force
423+
}
424+
if ($outFile -like '*.svg') {
425+
$null = $null
426+
}
427+
$output > $outFile
428+
# and if that worked,
429+
if ($?) {
430+
# output the file.
431+
$page.OutputFile = Get-Item -Path $outFile
432+
$page.OutputFile
433+
}
434+
} else {
435+
$null = $null
436+
}
397437
}
398438
#endregion Output
399439

psturtle.com/config.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ $site.Taskbar = [Ordered]@{
145145
'BlueSky' = 'https://bsky.app/profile/psturtle.com'
146146
'GitHub' = 'https://github.com/PowerShellWeb/Turtle'
147147
'RSS' = 'https://psturtle.com/RSS/index.rss'
148-
'Help' = 'https://psturtle.com/Commands/Get-Turtle'
148+
'Help' = '/Commands/Get-Turtle'
149149
}
150150

151+
$env:TURTLE_BOT = $true
152+
153+
$Site.Palette = "Cyberdyne"
154+
151155
$site.Footer = @(
152156
. $site.includes.SelectPalette
153157
. $site.includes.GetRandomPalette

0 commit comments

Comments
 (0)