Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
143 lines (128 loc) · 7.65 KB
/
Copy pathinstall.ps1
File metadata and controls
143 lines (128 loc) · 7.65 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
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
# FastShell installer for Windows: current user only, no administrator rights.
#
# Install or update: irm https://raw.githubusercontent.com/cpu-alt/FastShell/main/install.ps1 | iex
# Uninstall: & "$env:LOCALAPPDATA\Programs\FastShell\install.ps1" -Uninstall
#
# It downloads the latest release from GitHub, checks its SHA-256 against
# the published one, installs it in %LOCALAPPDATA%\Programs\FastShell and
# adds Start menu and desktop shortcuts. Sessions and settings are kept.
param(
[switch]$Uninstall,
[string]$Version = $env:FASTSHELL_VERSION, # e.g. 1.2.0; empty = latest release
[string]$InstallDir = $(if ($env:FASTSHELL_DIR) { $env:FASTSHELL_DIR } else { Join-Path $env:LOCALAPPDATA 'Programs\FastShell' }),
[string]$Package, # a local .zip instead of downloading (its .sha256 next to it)
[switch]$NoDesktopShortcut,
[switch]$FilesOnly, # portable copy: no shortcuts, no entry in Windows settings
[switch]$NoStart
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue' # much faster downloads on Windows PowerShell 5.1
$Repo = 'cpu-alt/FastShell'
$UninstallKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\FastShell'
$StartMenuLink = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs\FastShell.lnk'
$DesktopLink = Join-Path ([Environment]::GetFolderPath('Desktop')) 'FastShell.lnk'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
function Write-Step($text) { Write-Host " $text" -ForegroundColor Cyan }
function Stop-FastShell([string]$dir) {
$full = [IO.Path]::GetFullPath($dir).TrimEnd('\') + '\'
Get-Process FastShell -ErrorAction SilentlyContinue |
Where-Object { $_.Path -and $_.Path.StartsWith($full, [StringComparison]::OrdinalIgnoreCase) } |
ForEach-Object {
Write-Step "Closing FastShell (process $($_.Id))"
$null = $_.CloseMainWindow()
if (-not $_.WaitForExit(5000)) { Stop-Process -Id $_.Id -Force; Start-Sleep -Milliseconds 500 }
}
}
function New-Shortcut([string]$path, [string]$target) {
$shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut($path)
$shortcut.TargetPath = $target
$shortcut.WorkingDirectory = Split-Path $target
$shortcut.Description = 'FastShell terminal'
$shortcut.Save()
}
function Get-ExpectedHash([string]$file) {
$hash = ((Get-Content -LiteralPath $file -Raw).Trim() -split '\s+')[0].ToLowerInvariant()
if ($hash -notmatch '^[0-9a-f]{64}$') { throw "The checksum file is not a SHA-256: $file" }
return $hash
}
function Assert-ReleaseUrl([string]$url) {
if (-not $url.StartsWith("https://github.com/$Repo/releases/download/", [StringComparison]::OrdinalIgnoreCase) -or $url.Contains('..')) {
throw "Refusing a download that is not a FastShell release: $url"
}
}
if ($Uninstall) {
Write-Host 'Uninstalling FastShell...' -ForegroundColor White
Stop-FastShell $InstallDir
foreach ($link in $StartMenuLink, $DesktopLink) { if (Test-Path -LiteralPath $link) { Remove-Item -LiteralPath $link -Force } }
if (Test-Path -LiteralPath (Join-Path $InstallDir 'FastShell.exe')) { Remove-Item -LiteralPath $InstallDir -Recurse -Force }
if (Test-Path $UninstallKey) { Remove-Item $UninstallKey -Recurse -Force }
Write-Host 'FastShell is uninstalled. Your sessions are kept in %APPDATA%\EMF NET\FastShell.' -ForegroundColor Green
return
}
Write-Host 'Installing FastShell...' -ForegroundColor White
$work = Join-Path ([IO.Path]::GetTempPath()) ('FastShell-' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $work | Out-Null
try {
if ($Package) {
$zip = (Resolve-Path -LiteralPath $Package).Path
$sha = "$zip.sha256"
$label = Split-Path $zip -Leaf
} else {
$api = if ($Version) { "https://api.github.com/repos/$Repo/releases/tags/v$($Version.TrimStart('v'))" }
else { "https://api.github.com/repos/$Repo/releases/latest" }
Write-Step 'Looking for the latest release'
try {
$release = Invoke-RestMethod -Uri $api -Headers @{ 'User-Agent' = 'FastShell-installer'; 'Accept' = 'application/vnd.github+json' }
} catch {
if ($_.Exception.Response -and [int]$_.Exception.Response.StatusCode -eq 404) {
throw "No FastShell release$(if ($Version) { " $Version" }) is published on https://github.com/$Repo/releases yet."
}
throw "Cannot reach GitHub: $($_.Exception.Message)"
}
$zipAsset = $release.assets | Where-Object { $_.name -like 'FastShell-*-windows-x64.zip' } | Select-Object -First 1
if (-not $zipAsset) { throw "Release $($release.tag_name) has no Windows package." }
$shaAsset = $release.assets | Where-Object { $_.name -eq "$($zipAsset.name).sha256" } | Select-Object -First 1
if (-not $shaAsset) { throw "Release $($release.tag_name) has no checksum for $($zipAsset.name)." }
Assert-ReleaseUrl $zipAsset.browser_download_url
Assert-ReleaseUrl $shaAsset.browser_download_url
$zip = Join-Path $work $zipAsset.name
$sha = "$zip.sha256"
$label = "$($release.tag_name) ($([math]::Round($zipAsset.size / 1MB)) MB)"
Write-Step "Downloading FastShell $label"
Invoke-WebRequest -Uri $shaAsset.browser_download_url -OutFile $sha -UseBasicParsing -Headers @{ 'User-Agent' = 'FastShell-installer' }
Invoke-WebRequest -Uri $zipAsset.browser_download_url -OutFile $zip -UseBasicParsing -Headers @{ 'User-Agent' = 'FastShell-installer' }
}
Write-Step 'Checking the SHA-256 checksum'
$expected = Get-ExpectedHash $sha
$actual = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actual -ne $expected) { throw "Checksum mismatch: the download is damaged or was modified. Nothing was installed." }
Expand-Archive -LiteralPath $zip -DestinationPath (Join-Path $work 'unpacked') -Force
$source = Join-Path $work 'unpacked\FastShell'
if (-not (Test-Path -LiteralPath (Join-Path $source 'FastShell.exe'))) { throw 'The package has no FastShell.exe.' }
Stop-FastShell $InstallDir
Write-Step "Installing in $InstallDir"
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
Copy-Item -Path (Join-Path $source '*') -Destination $InstallDir -Recurse -Force
$exe = Join-Path $InstallDir 'FastShell.exe'
$installedVersion = (Get-Item -LiteralPath $exe).VersionInfo.ProductVersion
if ($FilesOnly) {
Write-Host "FastShell $installedVersion is copied to $InstallDir." -ForegroundColor Green
return
}
New-Shortcut $StartMenuLink $exe
if (-not $NoDesktopShortcut) { New-Shortcut $DesktopLink $exe }
New-Item -Path $UninstallKey -Force | Out-Null
$values = @{
DisplayName = 'FastShell'; DisplayVersion = $installedVersion; Publisher = 'Nathan Witz & Patrizio Ventura'
InstallLocation = $InstallDir; DisplayIcon = $exe
UninstallString = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$(Join-Path $InstallDir 'install.ps1')`" -Uninstall"
URLInfoAbout = "https://github.com/$Repo"
}
foreach ($entry in $values.GetEnumerator()) { Set-ItemProperty -Path $UninstallKey -Name $entry.Key -Value $entry.Value }
Set-ItemProperty -Path $UninstallKey -Name NoModify -Value 1 -Type DWord
Set-ItemProperty -Path $UninstallKey -Name NoRepair -Value 1 -Type DWord
Write-Host "FastShell $installedVersion is installed. Start it from the Start menu or the desktop." -ForegroundColor Green
if (-not $NoStart) { Start-Process -FilePath $exe }
} finally {
Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue
}