|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Builds the website. |
| 4 | +.DESCRIPTION |
| 5 | + Builds a static site using PowerShell. |
| 6 | +
|
| 7 | + * The site will be configured using any `config.*` files. |
| 8 | + * Functions and filters will be loaded from any `functions.*` or `filters.*` files. |
| 9 | + * All files will be processed using `buildFile.ps1` (any `*.*.ps1` file should be run). |
| 10 | +.EXAMPLE |
| 11 | + ./build.ps1 |
| 12 | +#> |
| 13 | +param( |
| 14 | +[string[]] |
| 15 | +$FilePath, |
| 16 | + |
| 17 | +[string] |
| 18 | +$Root = $PSScriptRoot |
| 19 | +) |
| 20 | + |
| 21 | +# Push into the script root directory |
| 22 | +if ($PSScriptRoot) { Push-Location $PSScriptRoot } |
| 23 | + |
| 24 | +# Creation of a sitewide object to hold configuration information. |
| 25 | +$Site = [Ordered]@{} |
| 26 | +$Site.Files = |
| 27 | + if ($filePath) { Get-ChildItem -Recurse -File -Path $FilePath } |
| 28 | + else { Get-ChildItem -Recurse -File } |
| 29 | + |
| 30 | +$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=$siteFile.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 | +} |
| 63 | + |
| 64 | +#region Common Functions and Filters |
| 65 | +# Any functions or filter file at the site root should be loaded. |
| 66 | +$functionFileNames = 'functions', 'function', 'filters', 'filter' |
| 67 | +$functionPattern = "(?>$($functionFileNames -join '|'))\.ps1$" |
| 68 | +$functionFiles = Get-ChildItem -Path $Site.PSScriptRoot | |
| 69 | + Where-Object Name -Match $functionPattern |
| 70 | + |
| 71 | +foreach ($file in $functionFiles) { |
| 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 |
| 74 | + . $file.FullName |
| 75 | +} |
| 76 | +#endregion Common Functions and Filters |
| 77 | + |
| 78 | +# Set an alias to buildPage.ps1 |
| 79 | +Set-Alias BuildPage ./buildPage.ps1 |
| 80 | + |
| 81 | +# If we have a github event, |
| 82 | +# save it to a variable and to the `$site` |
| 83 | +$site.GitHubEvent = $gitHubEvent = |
| 84 | + if ($env:GITHUB_EVENT_PATH) { |
| 85 | + # all we need to do to serve it is copy it. |
| 86 | + Copy-Item $env:GITHUB_EVENT_PATH .\gitHubEvent.json |
| 87 | + |
| 88 | + # and we can assign it to a variable, so you we can use it in any files we build. |
| 89 | + Get-Content -Path .\gitHubEvent.json -Raw | ConvertFrom-Json |
| 90 | + } |
| 91 | + |
| 92 | +# If we have a CNAME, read it, trim it, and update the site object. |
| 93 | +if (Test-Path 'CNAME') { |
| 94 | + $Site.CNAME = $CNAME = (Get-Content -Path 'CNAME' -Raw).Trim() |
| 95 | + $Site.RootUrl = "https://$CNAME/" |
| 96 | +} elseif ( |
| 97 | + # otherwise, if we are in a directory that could be a domain |
| 98 | + ($site.PSScriptRoot | Split-Path -Leaf) -like '*.*' |
| 99 | +) { |
| 100 | + # assume it _is_ the domain. |
| 101 | + $site.CNAME = $CNAME = ($site.PSScriptRoot | Split-Path -Leaf) |
| 102 | + $site.RootUrl = "https://$CNAME/" |
| 103 | +} |
| 104 | + |
| 105 | +#region config |
| 106 | +# If we have a config.json file, it can be used to set the site configuration. |
| 107 | +if (Test-Path 'config.json') { |
| 108 | + $siteConfig = Get-Content -Path 'config.json' -Raw | ConvertFrom-Json |
| 109 | + foreach ($property in $siteConfig.psobject.properties) { |
| 110 | + $Site[$property.Name] = $property.Value |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +# If we have a config.psd1 file, we'll use it to set the site configuration. |
| 115 | +if (Test-Path 'config.psd1') { |
| 116 | + $siteConfig = Import-LocalizedData -FileName 'config.psd1' -BaseDirectory $PSScriptRoot |
| 117 | + foreach ($property in $siteConfig.GetEnumerator()) { |
| 118 | + $Site[$property.Key] = $property.Value |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +# If we have a config yaml, things |
| 123 | +if (Test-Path 'config.yaml') { |
| 124 | + $siteConfig = Get-Item 'config.yaml' | from_yaml |
| 125 | + foreach ($property in $siteConfig.GetEnumerator()) { |
| 126 | + $Site[$property.Name] = $property.Value |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +# If we have a config.ps1 file, |
| 131 | +if (Test-Path 'config.ps1') { |
| 132 | + # Get the script command |
| 133 | + $configScript = Get-Command -Name './config.ps1' |
| 134 | + # and install any requirements it has. |
| 135 | + $configScript | RequireModule |
| 136 | + # run it, and let it configure anything it chooses to. |
| 137 | + . $configScript |
| 138 | +} |
| 139 | +#endregion config |
| 140 | + |
| 141 | +# Start the clock |
| 142 | +$site['LastBuildTime'] = $lastBuildTime = [DateTime]::Now |
| 143 | +#region Build Files |
| 144 | + |
| 145 | +# Start the clock on the build process |
| 146 | +$buildStart = [DateTime]::Now |
| 147 | +Write-Host "Started Building Pages @ $buildStart" |
| 148 | +# pipe every file we find to buildFile |
| 149 | +$Site.Files | . buildPage |
| 150 | +# and stop the clock |
| 151 | +$buildEnd = [DateTime]::Now |
| 152 | +Write-Host "Finished Building Pages @ $buildEnd ($($buildEnd - $buildStart))" |
| 153 | + |
| 154 | +#endregion Build Files |
| 155 | + |
| 156 | +# If we have changed directories, we need to push back into the script root directory. |
| 157 | +if ($PSScriptRoot -and "$PSScriptRoot" -ne "$pwd") { |
| 158 | + Push-Location $psScriptRoot |
| 159 | +} |
| 160 | + |
| 161 | +if ($site.includes.'LastBuild.json' -is [Management.Automation.ExternalScriptInfo]) { |
| 162 | + . $site.includes.'LastBuild.json' > ./lastBuild.json |
| 163 | +} |
| 164 | + |
| 165 | +if ($site.includes.'Sitemap.xml' -is [Management.Automation.ExternalScriptInfo]) { |
| 166 | + . $site.includes.'Sitemap.xml' > sitemap.xml |
| 167 | +} |
| 168 | + |
| 169 | +if ($site.includes.'Robots.txt' -is [Management.Automation.ExternalScriptInfo]) { |
| 170 | + . $site.includes.'Robots.txt' > robots.txt |
| 171 | +} |
| 172 | + |
| 173 | +if ($site.includes.'Index.rss' -is [Management.Automation.ExternalScriptInfo]) { |
| 174 | + New-Item -ItemType File -Path ./RSS/index.rss -Force -Value ( |
| 175 | + . $site.includes.'Index.rss' |
| 176 | + ) |
| 177 | +} |
| 178 | + |
| 179 | +if ($site.includes.'Index.json' -is [Management.Automation.ExternalScriptInfo]) { |
| 180 | + . $site.includes.'Index.json' > index.json |
| 181 | +} |
| 182 | + |
| 183 | +#region archive.zip |
| 184 | +if ($site.Archive) { |
| 185 | + # Create an archive of the current deployment. |
| 186 | + Compress-Archive -Path $pwd -DestinationPath "archive.zip" -CompressionLevel Optimal -Force |
| 187 | +} |
| 188 | +#endregion archive.zip |
| 189 | +if ($PSScriptRoot) { Pop-Location } |
0 commit comments