-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ps1
More file actions
474 lines (402 loc) · 13.9 KB
/
Copy pathdeploy.ps1
File metadata and controls
474 lines (402 loc) · 13.9 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<#
.SYNOPSIS
`deploy.ps1`
.DESCRIPTION
`deploy.ps1` deploys a website.
This is a simple and helpful scripting convention for site deployment.
Just run `./deploy.ps1`
`./deploy.ps1` can deploy any way we want.
This `./deploy.ps1` dynamically generates a static site.
Feel free to copy and paste this code.
Please document your parameters, and add NOTES.
.NOTES
This deploys a static site by running any `*.*.ps1`.
The output from each file will go into a corresponding file.
For example: `a.css.ps1` will output `a.css`
* `*.html.ps1` files will be piped into `layout`
* `*.json.ps1` files will be converted to `json`
`html`, `json`, and `xml` files will be made into `index` files
(unless `-Ugly` links are preferred)
#>
[CmdletBinding(SupportsShouldProcess)]
param(
# An anlytics ID
[string]$AnalyticsID,
# The list of acceptable extensions
[string[]]
$AcceptableExtensions = @(
'css',
'md',
'html',
'js',
'json',
'svg',
'xml'
),
# If set, will make "ugly" page links
# This will make `a.html.ps1` generate `a.html`, instead of `/a/index.html`
[switch]
$Ugly,
[string[]]
$IndexExtension = @('html','xml','json'),
# The root page url
[uri]
$PageUrl,
# The site root
[string]
$SiteRoot
)
# Know thyself
$mySelf = $MyInvocation.MyCommand
if (-not $SiteRoot) {
$SiteRoot = $PSScriptRoot
}
# If there is no script root,
if (-not $SiteRoot) {
# error out.
Write-Error "`$psScriptRoot is empty, Will not deploy"
return
}
# Push into $psScriptRoot so everything is root relative.
Push-Location $PSScriptRoot
#region Map Parameters from Environment
# Look at our environment
foreach ($env in Get-ChildItem env:) {
# See if any environment variables map to parameter
# (after we remove any punctuation from the name)
$envName = $env.Name -replace '\p{P}'
if (
# If they map
$mySelf.Parameters[$envName] -and
# and are not already bound
(-not $PSBoundParameters.ContainsKey($envName))
) {
# set them
$ExecutionContext.SessionState.PSVariable.Set(
$envName,
$(
# If the value looks like json
if ($env.Value -match '^\s{0,}[\[\{]') {
# convert it before we set the value.
ConvertFrom-Json -InputObject $env.Value
}
# If the value is a boolean string
elseif ($env.Value -match '^true|false$') {
# convert it to a boolean
$evn.Value -match 'true'
}
# Otherwise, directly map it.
else {
$env.Value
}
)
)
# After we set the variable, map it into `$psBoundParameters`
$PSBoundParameters[$envName] =
$ExecutionContext.SessionState.PSVariable.Get(
$envName
).Value
}
}
#endregion Map Parameters from Environment
# Configs, Layouts, and Pages all may require modules
# Make a little filter to import and install any module a script `#requires`.
filter requireModule {
# Our input should be a file.
$in = $_
# Get the script at this location
$command = Get-Command $in.FullName -CommandType ExternalScript
# If that somehow failed, return
if (-not $command) { return }
# If the script has requirements
foreach ($requirement in
$command.ScriptBlock.Ast.ScriptRequirements.RequiredModules
) {
# Check if they are loaded
$requiredModule = Get-Module -ErrorAction Ignore -Name $requirement.Name
# If they are not,
if (-not $requiredModule) {
# try to load the requirement
"Importing Requirement $($requirement.Name) for $($in.FullName)" |
Out-Host
$requiredModule = Import-Module $requiredModule -Force -PassThru -ErrorAction Ignore
# If that did not work,
if (-not $requiredModule) {
"Installing Requirement $($requirement.Name)" | Out-Host
Install-Module $requirement.Name -Scope CurrentUser -Force
Import-Module $requirement.Name -Global -Force -PassThru |
Out-Host
}
}
}
}
# Get whatever local module exists
$psd1Path =
Get-ChildItem -Filter *.psd1 |
Select-String 'ModuleVersion' |
Select-Object -ExpandProperty Path -First 1
# If one was found
if ($psd1Path) {
# import it
"Importing $psd1Path" | Out-Host
Import-Module $psd1Path -Force -PassThru |
Out-Host
}
# Check for a layout script.
$layout = Get-Command ./layout.ps1 -ErrorAction Ignore
if ($layout) {
# Import any requirements it might have
if ($layout.ScriptBlock.Ast.ScriptRequirements) {
[IO.FileInfo]$layout.Source | . requireModule
}
# And alias it to `layout`.
Set-Alias layout $layout.Source
}
# Any `$site` metadata can be stored in a dictionary
# Any of our parameters should be site wide metadata
$site = [Ordered]@{} + $PSBoundParameters
$Pages = [Ordered]@{}
$PagesByUrl = [Ordered]@{}
# A `config.ps1` file may do anything it wants to configure the site.
# If there is a `.config.ps1`
$configPs1 = Get-Command ./config.ps1 -ErrorAction Ignore
if ($configPs1) {
# Import any requirements it might have
if ($configPs1.ScriptBlock.Ast.ScriptRequirements) {
[IO.FileInfo]$configPs1.Source | . requireModule
}
. ./config.ps1 # and run it.
}
# We will be run any `*.*.ps1`(with an acceptable extension).
# The output from each script will go to the corresponding file.
# Declare some patterns we will use:
# * `$matchExtension` will tell us which extension
$matchExtension = '\.(?<x>[^\.]+)\.ps1$'
# * `$acceptablePattern` will ensure we only build file types we accept
$acceptablePattern = "\.(?>$($acceptableExtensions -join '|'))\.ps1$"
# * `$indexPattern` indicates which file types will become indeces.
$indexPattern = "\.(?>$($IndexExtension -join '|'))\.ps1$"
# Get all files that match our pattern
$files = @(
Get-ChildItem -Path *.ps1 -File -Recurse |
Where-Object Name -match $acceptablePattern
) |
Sort-Object @{
# We want to generate Markdown files first
# because html files may include their result inline.
Expression = {
# So sort on any md files first
$_.Name -match '\.md'
}
Descending = $true # in descending order
# (so they are at the top of the pile)
}, Fullname
# Prepare our progress bars
$progress = @{ID=Get-Random;Activity="Building Pages"}
$total = @($files).Length
$counter = 0
# Walk over each of our files
foreach ($in in $files) {
$progress.PercentComplete = ++$counter * 100 / $total
$progress.Status = $in.Name
Write-Progress @progress
# import any requirements
$in | . requireModule
$inScript =
$ExecutionContext.SessionState.InvokeCommand.GetCommand($in.FullName, 'ExternalScript')
# Make sure we can map the output extension
$outputExtension =
if ($in -match $matchExtension) {
$matches.x
} else {
# otherwise, warn and continue.
Write-Warning "Will not output $in without an extension"
continue
}
# Default the title to the file name
$FileName = $in.Name -replace $matchExtension
$title = $FileName -replace '-', ' '
$help = Get-Help $in.FullName -ErrorAction Ignore
$meta = [Ordered]@{}
foreach ($attr in $inScript.ScriptBlock.Attributes) {
if ($attr.Key -and $attr.Value) {
$meta[$attr.Key] = $attr.Value
}
}
# and initialize any page metadata
$page = [Ordered]@{
Title = $title
Command = $inScript
File = $in
FileName = $FileName
Source = $inScript.ScriptBlock
Help = $help
Meta = $meta
}
$description = $help.description.text -join [Environment]::NewLine
# Generate a file date by:
$fileDate = $fileName -replace
# * Remove any non-digit (except colon, dash, and underscore, and Z)
'[^\d:-_Z]' -replace
# * Trim leading punctuation, and trailing punctuation (and Z),
'^\p{P}+' -replace '[-Z]+$' -replace
# * replace underscores with colons, and try to cast to `[DateTime]`
'_',':' -as [DateTime]
# If we have a file date,
if ($fileDate) {
$page.Date = $fileDate # set the `$Page.Date`
} else {
# otherwise, we'll try to get the date from git.
$gitCommand = $ExecutionContext.SessionState.InvokeCommand.GetCommand('git', 'Application')
if ($gitCommand) {
$gitDates =
try {
# we can use `git log --follow --format=%ci` to get the dates in order
(& $gitCommand log --follow --format=%ci --date default $in.FullName *>&1) -as [datetime[]]
} catch {
$null
}
# Because the file might not be in git, we want to always set the `$LASTEXITCODE` to 0
$LASTEXITCODE = 0
# Set the date to the last date we find.
if ($gitDates) {
$page.Date = $gitDates[-1]
}
}
}
# If we map the output path _before_ we run our script
# we can know what URL we will be publishing to.
# Our output file path starts by replacing the .ps1
$outputPath = $in.FullName -replace '\.ps1$'
# If we do not care about "pretty" url format
# or are not an extension that we can make into an index,
# we do not need to change the file path
if ((-not $Ugly) -and ($in.Name -match $indexPattern)) {
# If we do, things get a little more complicated
# We will want to special case some extensions we want to use as an index.
# If the name of the file matches the name of the directory
if ($in.Name -match
"^$([Regex]::Escape($in.Directory.Name))$matchExtension"
) {
# it will become an index of that directory.
$outputPath = Join-Path $in.Directory "index.$outputExtension"
} else {
# otherwise, make it an index of it's own directory
$outputPath =
($in.FullName -replace $matchExtension) +
"/index.$outputExtension" -replace
'index/index', 'index'
}
}
# Now that we know our output path,
# we can predict our url
$page.Url = $outputPath -replace "^$(
[Regex]::Escape($siteRoot) # just remove the site root
)" -replace # replace any index with a slash
'[\\/]index\.[^\.]+?$','/' -replace
'[\\/]', '/' # and fix any slashes.
$page.OutputPath = $outputPath
$page.Extension = $outputExtension
# Get our page output
$output = @(. $in.FullName)
if ($title) {
$page.Title = $title
}
if ($description) {
$page.Description = $description
}
# Store our output in the page
$page.Output = $output
# And put our page in two collections
# One by output path (we will use this output)
$Pages[$outputPath] = $page
# and one by url
$PagesByUrl[$page.Url] = $page
}
$progress.Activity = "Deploying Pages"
$total = $pages.Count
$counter = 0
foreach ($outputPath in $pages.Keys) {
$progress.PercentComplete = ++$counter * 100 / $total
$progress.Status = "$($page.Title) "
Write-Progress @progress
$page = $pages[$outputPath]
$title = $page.Title
$description = $page.Description
$outputExtension = $page.Extension
$output = $page.Output
$outputFiles = @()
$output = @(foreach ($out in $output) {
if ($out -is [IO.FileInfo]) {
$outputFiles += $out
} else {
$out
}
})
# If the output was a list of files
if ($outputFiles -and -not $output) {
# then do not write anything to disk
$outputFiles
continue
}
# If we have a layout file and are outputting html
if ($layout -and (
$page.File.FullName -replace '\.ps1$' -match '\.html$'
)) {
# Pipe our output to the layout script.
# By doing this in a second pass,
# our layout script can have much more context.
$output = $output | layout
}
# Otherwise, if our output extension is xml
# and we have only that one xml
elseif (($outputExtension -eq 'xml') -and
($output.Length -eq 1) -and
($output[0] -is [xml])
) {
# set our output to the outerXML.
$output = $output.OuterXml
}
# Otherwise, if our output extension is json
# and the output is not already a string
elseif (($outputExtension -eq 'json') -and (
$output[0] -isnot [string]
)) {
# convert it into json.
# If there is one more than one item,
if ($output.Length -gt 1) {
# make it a list
$output = $output | ConvertTo-Json -Depth (
$FormatEnumerationLimit
)
} else {
# If there is only one item, make it an object.
$output = ConvertTo-Json -InputObject $output -Depth (
$FormatEnumerationLimit
)
}
}
$outputFile = [Ordered]@{
ItemType = 'File';Force = $true
Value = $output -join [Environment]::NewLine
Path = $outputPath
}
if ($WhatIfPreference) {
$outputFile
} elseif ($PSCmdlet.ShouldProcess("Output $($outputPath)")) {
New-Item @outputFile
}
}
if ($PageUrl) {
$sitemap = /_includes/sitemap -Url $pageUrl -Pages $PagesByUrl
if ($sitemap) {
$sitemap.Save((
Join-Path $SiteRoot "sitemap.xml"
))
Get-Item (Join-Path $SiteRoot "sitemap.xml")
}
}
$progress.Remove('PercentComplete')
$progress.Completed = $true
Write-Progress @progress
Pop-Location