-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall_or_update.ps1
97 lines (80 loc) · 3.06 KB
/
install_or_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
### VARIABLES ###
# Define URL and the target directories
$repoUrlBasePath = "https://github.com/pirafrank/zed_unofficial_win_builds/releases/latest/download/"
$tempDir = "$env:TEMP\zed_temp"
$targetDir = "$env:LocalAppData\Programs\Zed"
$targetShortcutDir = "$env:AppData\Microsoft\Windows\Start Menu\Programs\Zed"
$targetShortcutFile = "$targetShortcutDir\Zed.lnk"
### FUNCTIONS ###
# Define a function to download an artifact
function Get-Artifact {
param (
[string]$fileName,
[string]$repoUrl
)
# Define the path for the downloaded file
$tempFile = Join-Path -Path $tempDir -ChildPath $fileName
# Download the latest artifact
Invoke-WebRequest -Uri $repoUrl -OutFile $tempFile
}
function Create-Shortcut {
# Test target dir
if (-not (Test-Path -Path $targetShortcutDir) -or -not (Test-Path -Path $targetShortcutFile)) {
New-Item -ItemType Directory -Path $targetShortcutDir -Force
}
# Creeate a shortcut to Zed
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$targetShortcutDir\Zed.lnk")
$Shortcut.TargetPath = "$env:LocalAppData\Programs\Zed\zed.exe"
$Shortcut.Save()
Write-Output "Shortcut created."
}
### SCRIPT ###
# Check if zed.exe process is running, if yes, prompt the user about it and exit
if (Get-Process -Name zed -ErrorAction SilentlyContinue) {
Write-Error "Zed is running. Please close it before updating."
Exit
}
# Create dirs if they don't exist
if (-not (Test-Path -Path $tempDir)) {
New-Item -ItemType Directory -Path $tempDir
}
if (-not (Test-Path -Path $targetDir)) {
New-Item -ItemType Directory -Path $targetDir
}
# Download the latest artifact and its checksum
$repoUrl = $repoUrlBasePath + "zed.zip"
Get-Artifact -fileName "zed.zip" -repoUrl $repoUrl
$repoUrl = $repoUrlBasePath + "zed.zip.sha256"
Get-Artifact -fileName "zed.zip.sha256" -repoUrl $repoUrl
# Verify the downloaded file against the SHA256 checksum
$downloadedFile = Join-Path -Path $tempDir -ChildPath "zed.zip"
$checksumFile = Join-Path -Path $tempDir -ChildPath "zed.zip.sha256"
# Read the expected checksum
$expectedChecksum = (Get-Content -Path $checksumFile).Split(" ")[0]
# Calculate the actual checksum of the downloaded file
$actualChecksum = Get-FileHash -Path $downloadedFile -Algorithm SHA256 | Select-Object -ExpandProperty Hash
# Case-insensitive checksums compare
if ($expectedChecksum.ToUpper() -eq $actualChecksum.ToUpper()) {
Write-Output "Checksum verification successful."
} else {
Write-Error "Checksum verification failed."
Exit
}
# Check if the download was successful
$tempFile = Join-Path -Path $tempDir -ChildPath "zed.zip"
$targetFile = Join-Path -Path $targetDir -ChildPath "zed.exe"
if (Test-Path -Path $tempFile) {
# Extract the downloaded zip file
Expand-Archive -Path $tempFile -DestinationPath $targetDir -Force
if (Test-Path -Path $targetFile) {
Write-Output "Extracted to install directory successfully."
Create-Shortcut
Write-Output "Installation complete."
} else {
Write-Error "Extraction failed."
}
}
else {
Write-Error "Download failed. Installation aborted."
}