-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
102 lines (81 loc) · 2.83 KB
/
Copy pathbuild.ps1
File metadata and controls
102 lines (81 loc) · 2.83 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
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $repoRoot
function Invoke-CheckedCommand {
param(
[Parameter(Mandatory = $true)]
[string]$FilePath,
[string[]]$Arguments = @()
)
& $FilePath @Arguments
if ($LASTEXITCODE -ne 0) {
throw "Command failed with exit code $LASTEXITCODE: $FilePath $($Arguments -join ' ')"
}
}
function Get-Python2Runtime {
$localRuntime = Get-ChildItem -Path (Join-Path $repoRoot '.python2') -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.Name -in @('pypy.exe', 'python.exe', 'pypy', 'python2') } |
Select-Object -First 1
if ($localRuntime) {
return $localRuntime.FullName
}
$python2 = Get-Command python2 -ErrorAction SilentlyContinue
if ($python2) {
return $python2.Source
}
throw 'Python 2 runtime not found. Install a repo-local runtime under .python2/ or add python2 to PATH.'
}
$python2 = Get-Python2Runtime
$dependencies = @(
(Join-Path $repoRoot 'MENU'),
(Join-Path $repoRoot 'jemdoc.py'),
(Join-Path $repoRoot 'jemdoc.css')
)
$compiled = @()
Get-ChildItem -Path $repoRoot -Filter '*.jemdoc' | Sort-Object Name | ForEach-Object {
$source = $_.FullName
$target = [System.IO.Path]::ChangeExtension($source, '.html')
$needsBuild = -not (Test-Path $target)
if (-not $needsBuild) {
foreach ($dependency in @($source) + $dependencies) {
if ((Get-Item $dependency).LastWriteTime -gt (Get-Item $target).LastWriteTime) {
$needsBuild = $true
break
}
}
}
if ($needsBuild) {
Invoke-CheckedCommand -FilePath $python2 -Arguments @((Join-Path $repoRoot 'jemdoc.py'), $source)
Write-Host "Compiled $($_.Name)"
$compiled += $_.Name
}
}
if ($compiled.Count -eq 0) {
Write-Host 'No pages needed recompilation.'
} else {
Write-Host ('Updated files: ' + ($compiled -join ', '))
}
$status = (& git status --porcelain)
if ($LASTEXITCODE -ne 0) {
throw 'Failed to inspect git status.'
}
if ([string]::IsNullOrWhiteSpace(($status | Out-String))) {
Write-Host 'No git changes to commit.'
return
}
$commitMessage = 'Update site'
if ($compiled.Count -gt 0) {
$commitMessage = "Rebuild site ($($compiled.Count) pages)"
}
Invoke-CheckedCommand -FilePath 'git' -Arguments @('add', '-A')
$stagedChanges = (& git diff --cached --name-only)
if ($LASTEXITCODE -ne 0) {
throw 'Failed to inspect staged changes.'
}
if ([string]::IsNullOrWhiteSpace(($stagedChanges | Out-String))) {
Write-Host 'No staged changes to commit.'
return
}
Invoke-CheckedCommand -FilePath 'git' -Arguments @('commit', '-m', $commitMessage)
Invoke-CheckedCommand -FilePath 'git' -Arguments @('push')
Write-Host 'Committed and pushed successfully.'