-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
executable file
·179 lines (153 loc) · 5.8 KB
/
build.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
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
#!/bin/env pwsh
param(
[switch]$watch = $false,
[switch]$drafts = $false,
[switch]$skip_build = $false
)
$ErrorActionPreference = 'Stop'
$env:LC_ALL = "C.UTF-8"
$env:MAGICK_THREAD_LIMIT = 1
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$root = $PSScriptRoot
$public = Join-Path $root "public"
$src = Join-Path $root "src"
$image_manifest = Join-Path $PSScriptRoot "images.json"
New-Item -ItemType Directory -Path $public -ErrorAction SilentlyContinue > $null
function Copy-StaticContent {
Copy-Item -Path (Join-Path $root "site_root/*") -Recurse -Destination $public -Force
$static_content = @("audio", "fonts", "small-images")
foreach ($dir in $static_content) {
Copy-Item -Path (Join-Path $root $dir) -Recurse -Destination $public -Force
}
}
function Resize-Images {
$target_dir = Join-Path $public "img"
New-Item -Path $target_dir -ItemType Directory -ErrorAction SilentlyContinue > $null
$target_sizes = @(300, 600, 800, 1200, 4000)
Push-Location $src
try {
$files = (git ls-tree HEAD . -r -z).Split("`0") | Select-String '\.(jpe?g|svg|png)$' | ForEach-Object {
$mode, $type, $hash, $path = $_ -split '\s+'
@{
mode = $mode
type = $type
hash = $hash
path = $path
}
}
Write-Host "Found $($files.Count) images to convert"
$file_lookup = @{}
$files | ForEach-Object {
$file_lookup[[uri]::EscapeUriString($_.path)] = @{
hash = $_.hash
}
}
# get sizes of raster formats, quickly
$sizes = exiftool -json -ImageHeight -ImageWidth -r -q -ext jpg -ext jpeg -ext png . 2>$null | ConvertFrom-Json
foreach ($size in $sizes) {
# Starts with "./"
$path = $size.SourceFile.Substring(2)
$target = $file_lookup[[uri]::EscapeUriString($path)]
$target.width = $size.ImageWidth
$target.height = $size.ImageHeight
}
# produce other sizes of JPEGs
$files | ForEach-Object -ThrottleLimit ([System.Environment]::ProcessorCount) -Parallel {
$hash = $_.hash
$path = $_.path
$absPath = Join-Path $using:src $path
$ext = Split-Path $path -Extension
$origPath = Join-Path $using:target_dir "$hash$ext"
if ((Split-Path $path -Extension) -match ".jpe?g") {
$rel_path = (Resolve-Path -Relative $path -RelativeBasePath $using:src).Substring(2).Replace('\', '/')
$meta = ($using:file_lookup)[[uri]::EscapeUriString($rel_path)]
$applicable_sizes = $using:target_sizes | Where-Object { $_ -lt $meta.width }
$meta_sizes = @{}
foreach ($size in $applicable_sizes) {
$meta_sizes["$size"] = "/img/$hash-$size.jpg"
}
$meta_sizes["$($meta.width)"] = "/img/$hash$ext"
$meta.sizes = $meta_sizes
if (Test-Path $origPath) {
Write-Debug "Skipping $path"
}
else {
Write-Information "Converting $path"
$magick_args = $applicable_sizes | ForEach-Object {
@(
'('
'mpr:x'
'-resize'
"$($_)x$($_)>"
'-colorspace'
'sRGB'
'-quality'
'80'
'-write'
(Join-Path $using:target_dir "$hash-$_.jpg")
')'
)
}
magick $path -colorspace RGB -write mpr:x +delete @magick_args null:
New-Item -ItemType SymbolicLink -Path $origPath -Value $absPath -ErrorAction SilentlyContinue > $null
}
}
else {
Write-Information "Linking $path"
New-Item -ItemType SymbolicLink -Path $origPath -Value $absPath -ErrorAction SilentlyContinue > $null
}
}
# get sizes of SVGs
$files | ForEach-Object {
if ((Split-Path -Extension $_.path) -eq ".svg") {
$size = yq '{"width": .svg.+@width | sub("px", "") | from_yaml, "height": .svg.+@height | sub("px", "") | from_yaml }' -px -oj $_.path | ConvertFrom-Json
$target = $file_lookup[[uri]::EscapeUriString($_.path)]
$target.width = $size.width
$target.height = $size.height
}
}
foreach ($x in $file_lookup.GetEnumerator()) {
$val = $x.Value
$val.url = "/img/$($val.hash)$(Split-Path $x.Key -Extension)"
}
Set-Content -Path $image_manifest -Value ($file_lookup | ConvertTo-Json -Depth 100)
}
finally {
Pop-Location
}
}
function Build-Builder {
Push-Location (Join-Path $root "build")
try {
cargo build --release
}
finally {
Pop-Location
}
}
$builder = Join-Path $root "build/target/release/wtp-build"
[array]$draft_arg = if ($drafts) { "--draft" } else { @() }
function Build-HTML {
$env:RUST_LOG = 'info'
& $builder --input $src --output $public --image-manifest $image_manifest @draft_arg
Remove-Item env:RUST_LOG
}
Copy-StaticContent
Resize-Images
if (-not $skip_build) {
Build-Builder
}
if ($watch) {
$bg = Start-Job { miniserve $using:public --index index.html }
try {
$env:RUST_LOG = 'info'
& $builder --input $src --output $public --image-manifest $image_manifest @draft_arg --watch
Remove-Item env:RUST_LOG
}
finally {
$bg.StopJob()
}
}
else {
Build-HTML
}