-
Notifications
You must be signed in to change notification settings - Fork 13
/
update.ps1
83 lines (61 loc) · 2.36 KB
/
update.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
# First figure out if we need to update the files
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent -Resolve
Write-Output "Running from $scriptDir"
$target = Split-Path -Path "$scriptDir" -Parent
Start-Transcript -Path "$target\update.log"
Write-Output "Aperture-control target path $target"
$repoPath = "$target\repo.txt"
$shaPath = "$target\sha.txt"
if (-Not (Test-Path $repoPath)) {
Write-Output "Couldn't find $repoPath"
exit
}
if (-Not (Test-Path $shaPath)) {
Write-Output "Couldn't find $shaPath"
exit
}
$repo = (Get-Content -Path "$repoPath").Trim()
$oldSha = (Get-Content -Path "$shaPath").Trim()
$apiUrl = "https://api.github.com/repos/$repo/commits/master"
Write-Output "Deployed from $repo (commit $oldSha)"
Write-Output "Checking for new commits from $apiUrl"
try {
$response = Invoke-WebRequest $apiUrl -UseBasicParsing
$sha = ($response.Content | ConvertFrom-Json).sha
Write-Output "Latest commit is $sha"
if ($sha -eq $oldSha) {
Write-Output "No new commits."
exit
}
} catch {
Write-Output "Failed to check for latest updates, retrying later."
exit
}
# Then if there are updates, download them
Write-Output "New commits found - updating"
$url = "https://github.com/$repo/archive/master.zip"
$zip = "$env:USERPROFILE\Downloads\aperture-control.zip"
# https://stackoverflow.com/a/27768628
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip {
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
Write-Output "Downloading $url to $zip"
if (Test-Path $zip) {
Remove-Item -Force $zip
}
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $zip)
$repoName = $repo.Split("/")[1]
if (Test-Path "$target\$repoName-master") {
Write-Output "Cleaning $target\$repoName-master"
Remove-Item -Recurse -Force "$target\$repoName-master"
}
Write-Output "Extracting $zip to $target"
Unzip $zip $target
Write-Output "Running run-ac-recipes.ps1"
Start-Process -Wait -Verb runAs -ArgumentList "-ExecutionPolicy", "Bypass", "-NoLogo", "-NonInteractive", "-Command", "cd $acpath; .\run-ac-recipes.ps1" powershell.exe
Write-Output "Updating stored repostitory SHA hash to $sha"
"$sha" | Out-File "$shaPath"
Stop-Transcript