forked from AutoHotkey/AutoHotkey
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ahkh_update.ps1
83 lines (81 loc) · 2.46 KB
/
ahkh_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
# useage: script.ps1 [updatefiles...]
$7zexe = 'C:\Program Files\7-Zip\7z.exe'
if (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
try {
(New-Object System.IO.FileStream($PSCommandPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite)).Dispose()
}
catch {
Start-Process (Get-WmiObject Win32_Process -Filter "ProcessId = '$PID'").ExecutablePath "-f `"$PSCommandPath`" $args" -Verb RunAs | Wait-Process
exit
}
}
$ahkrelease = Invoke-RestMethod 'https://api.github.com/repos/thqby/AutoHotkey_H/releases/latest'
if ($ahkrelease) {
foreach ($asset in $ahkrelease.assets) {
if ($asset.name.ToLower().EndsWith('.7z')) {
if ($asset.updated_at.GetType().Name -eq 'String') {
$asset.updated_at = [System.DateTime]::Parse($asset.updated_at)
}
$asset.updated_at = $asset.updated_at.ToLocalTime()
Break
}
}
$ahkneedupdate = 0
$allahkfiles = 'AutoHotkey64.exe', 'AutoHotkey32.exe', 'AutoHotkey64.dll', 'AutoHotkey32.dll'
if ($args.Length) {
$needahkfiles = $args
}
else {
$needahkfiles = $allahkfiles
}
foreach ($ahkfile in $needahkfiles) {
$ahkfile = Get-Item ($PSScriptRoot + '\' + $ahkfile) -ErrorAction SilentlyContinue
if (!$ahkfile -or ($ahkfile.CreationTime -lt $asset.updated_at)) {
$ahkneedupdate = 1
Break
}
}
if ($ahkneedupdate) {
$ahkfile = $PSScriptRoot + '\ahk.7z'
try {
Invoke-WebRequest $asset.browser_download_url -OutFile $ahkfile
Write-Output ('The current version: ' + $ahkrelease.tag_name)
}
catch {
Write-Error 'Download fail'
}
if (!(Get-Item $7zexe)) {
$7zexe = where.exe 7z.exe
if (!$7zexe) {
$7zexe = where.exe /R 'C:\Program Files' 7z.exe
if ($7zexe -and ($7zexe.GetType().Name -ne 'String')) {
$7zexe = $7zexe[0]
}
}
}
if ($7zexe) {
& $7zexe x -aoa ('-o' + $PSScriptRoot) $ahkfile $needahkfiles
Remove-Item ($PSScriptRoot + '\ahk.7z')
foreach ($ahkfile in $needahkfiles) {
$ahkfile = Get-Item ($PSScriptRoot + '\' + $ahkfile)
if ($ahkfile) {
$ahkfile.CreationTime = $asset.updated_at
$ahkfile.LastWriteTime = $asset.updated_at
}
}
}
else {
Write-Error 'Cannot find 7z.exe'
}
}
else {
Write-Output "It's the latest version"
}
}
Remove-Variable 7zexe
Remove-Variable ahkrelease
Remove-Variable asset
Remove-Variable ahkneedupdate
Remove-Variable allahkfiles
Remove-Variable needahkfiles
Remove-Variable ahkfile