Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
419 lines (376 loc) · 18 KB
/
Copy pathinstall.ps1
File metadata and controls
419 lines (376 loc) · 18 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#Requires -Version 5.1
[CmdletBinding()]
param(
[switch]$Force,
[switch]$RestoreHandler,
[switch]$Uninstall,
[switch]$Purge,
[string]$Target = (Join-Path $env:LOCALAPPDATA 'Slick')
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {}
$Root = $PSScriptRoot
$Repo = '3kh0/slick'
function Step($m) { Write-Host "==> " -ForegroundColor Magenta -NoNewline; Write-Host $m -ForegroundColor White }
function Die($m) { Write-Host "error: " -ForegroundColor Red -NoNewline; Write-Host $m; exit 1 }
function Assert-ReleaseAttestation([string]$Path) {
if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
Write-Host " (gh CLI not found; skipping provenance check - https://cli.github.com)" -ForegroundColor DarkGray
return
}
Step "Verifying build provenance"
$prevEap = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
$out = & gh attestation verify $Path -R $Repo 2>&1 | Out-String
$code = $LASTEXITCODE
$ErrorActionPreference = $prevEap
if ($code -eq 0) {
Write-Host " attestation OK (signed by $Repo)"
return
}
Write-Host ""
Write-Host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" -ForegroundColor Red
Write-Host " BUILD PROVENANCE VERIFICATION FAILED" -ForegroundColor Red
Write-Host " This download may have been tampered with." -ForegroundColor Red
Write-Host " Refusing to install." -ForegroundColor Red
Write-Host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" -ForegroundColor Red
if ($out) { Write-Host $out }
Die "refusing to install an unattested or mismatched build"
}
# PowerShell 5.1 turns redirected native stderr into ErrorRecords, fatal under
# $ErrorActionPreference = 'Stop' (a Node warning aborted the build). Run with
# errors non-terminating, judge by exit code, show the log only on failure.
function Invoke-Logged([string]$Log, [scriptblock]$Command) {
$prevEap = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
& $Command *> $Log
return $LASTEXITCODE
} finally {
$ErrorActionPreference = $prevEap
}
}
function Get-File($url, $dest, $label) {
$ProgressPreference = 'Continue'
$resp = $null; $stream = $null; $out = $null
try {
$req = [System.Net.HttpWebRequest]::Create($url)
$req.UserAgent = 'slick-install'
$req.AllowAutoRedirect = $true
$resp = $req.GetResponse()
$total = $resp.ContentLength
$stream = $resp.GetResponseStream()
$out = [System.IO.File]::Create($dest)
$buffer = New-Object byte[] 1048576
$read = 0L
while (($n = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) {
$out.Write($buffer, 0, $n)
$read += $n
if ($total -gt 0) {
Write-Progress -Activity $label `
-Status ('{0:N1} / {1:N1} MB' -f ($read / 1MB), ($total / 1MB)) `
-PercentComplete ([int](($read / $total) * 100))
} else {
Write-Progress -Activity $label -Status ('{0:N1} MB' -f ($read / 1MB))
}
}
} finally {
Write-Progress -Activity $label -Completed
if ($out) { $out.Close() }
if ($stream) { $stream.Close() }
if ($resp) { $resp.Close() }
}
}
function Reg($key, $vals) {
New-Item $key -Force | Out-Null
foreach ($n in $vals.Keys) { Set-ItemProperty $key $n $vals[$n] }
}
$Protocol = 'slack'
$ProgId = 'Slick.slack'
function Get-ShellFolder($name, $fallback) {
try {
$p = [Environment]::GetFolderPath($name)
if ($p) { return $p }
} catch {}
$fallback
}
$DesktopDir = Get-ShellFolder 'DesktopDirectory' (Join-Path $env:USERPROFILE 'Desktop')
$ProgramsDir = Get-ShellFolder 'Programs' (Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs')
$Shortcuts = @((Join-Path $DesktopDir 'Slick.lnk'), (Join-Path $ProgramsDir 'Slick.lnk'))
$LegacyShortcuts = @("$env:USERPROFILE\Desktop\Slick.lnk", "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Slick.lnk")
function Register-SlackHandler($exe, $iconFile) {
$cmd = "`"$exe`" `"%1`""
$iconRes = if ($iconFile -and (Test-Path $iconFile)) { $iconFile } else { "$exe,0" }
Reg "HKCU:\Software\Classes\$ProgId" @{ '(default)' = 'Slick'; 'FriendlyTypeName' = 'Slick'; 'URL Protocol' = '' }
Reg "HKCU:\Software\Classes\$ProgId\DefaultIcon" @{ '(default)' = $iconRes }
Reg "HKCU:\Software\Classes\$ProgId\shell\open\command" @{ '(default)' = $cmd }
Reg "HKCU:\Software\Slick\Capabilities" @{ ApplicationName = 'Slick'; ApplicationDescription = 'Slack client mod (BYOE)' }
Reg "HKCU:\Software\Slick\Capabilities\URLAssociations" @{ $Protocol = $ProgId }
Reg "HKCU:\Software\RegisteredApplications" @{ Slick = 'Software\Slick\Capabilities' }
New-Item "HKCU:\Software\Classes\$Protocol\OpenWithProgids" -Force | Out-Null
New-ItemProperty "HKCU:\Software\Classes\$Protocol\OpenWithProgids" -Name $ProgId -PropertyType None -Value ([byte[]]@()) -Force | Out-Null
Reg "HKCU:\Software\Classes\$Protocol" @{ '(default)' = 'URL:Slack Protocol'; 'URL Protocol' = '' }
Reg "HKCU:\Software\Classes\$Protocol\shell\open\command" @{ '(default)' = $cmd }
& ie4uinit.exe -show 2>$null
}
function Unregister-SlackHandler {
Remove-Item "HKCU:\Software\Classes\$ProgId", 'HKCU:\Software\Slick' -Recurse -Force -EA SilentlyContinue
Remove-ItemProperty 'HKCU:\Software\RegisteredApplications' -Name Slick -EA SilentlyContinue
Remove-ItemProperty "HKCU:\Software\Classes\$Protocol\OpenWithProgids" -Name $ProgId -EA SilentlyContinue
}
function Get-PEArch($exe) {
try {
$fs = [IO.File]::OpenRead($exe); $br = New-Object IO.BinaryReader($fs)
$fs.Position = 0x3C; $fs.Position = $br.ReadInt32() + 4
$m = $br.ReadUInt16(); $br.Close(); $fs.Close()
switch ($m) { 0x8664 { 'x64' } 0xAA64 { 'arm64' } default { '' } }
} catch { '' }
}
function Find-SlackStandalone {
$base = Join-Path $env:LOCALAPPDATA 'slack'
if (-not (Test-Path $base)) { return $null }
Get-ChildItem $base -Directory -Filter 'app-*' -EA SilentlyContinue |
Sort-Object { [version]($_.Name -replace '^app-', '') } -Descending |
ForEach-Object { Join-Path $_.FullName 'resources' } |
Where-Object { Test-Path (Join-Path $_ 'app.asar') } |
Select-Object -First 1
}
function Find-SlackMsix {
$base = 'HKLM:\SOFTWARE\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\PackageRepository\Packages'
try {
$pkgs = Get-ChildItem $base -EA Stop | Where-Object { $_.PSChildName -match '^com\.tinyspeck\.slackdesktop_' }
$pkgs = $pkgs | Sort-Object { try { [version](($_.PSChildName -split '_')[1]) } catch { [version]'0.0.0' } } -Descending
foreach ($pkg in $pkgs) {
try {
$installPath = (Get-ItemProperty $pkg.PSPath -Name Path -EA Stop).Path
$res = Join-Path $installPath 'app\resources'
if (Test-Path (Join-Path $res 'app.asar')) { return $res }
} catch {}
}
} catch {}
return $null
}
function Find-SlackResources {
$standalone = Find-SlackStandalone
$msix = Find-SlackMsix
$cands = @($standalone, $msix) | Where-Object { $_ }
if (-not $cands) { return $null }
$want = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'x64' }
foreach ($res in $cands) {
$exe = Get-ChildItem (Split-Path $res) -Filter 'slack.exe' -EA SilentlyContinue | Select-Object -First 1
if ($exe -and (Get-PEArch $exe.FullName) -eq $want) { return $res }
}
return $cands | Select-Object -First 1
}
function Slack-Exe($res) { if ($res) { Get-ChildItem (Split-Path $res) -Filter 'slack.exe' -EA SilentlyContinue | Select-Object -First 1 } }
# Slack sets this AppUserModelID on its window outside MSIX. Shortcuts need it too,
# or a pinned Slick gets a separate, iconless taskbar button.
$AppUserModelId = 'com.squirrel.slack.slack'
function Set-ShortcutAppId([string]$lnk, [string]$id) {
if (-not ('SlickShortcut' -as [type])) {
Add-Type -TypeDefinition @'
using System; using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack = 4)] public struct SlickPropKey { public Guid fmtid; public uint pid; }
[StructLayout(LayoutKind.Explicit)] public struct SlickPropVariant { [FieldOffset(0)] public ushort vt; [FieldOffset(8)] public IntPtr p; }
[ComImport, Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISlickPropertyStore {
int GetCount(out uint c); int GetAt(uint i, out SlickPropKey k); int GetValue(ref SlickPropKey k, out SlickPropVariant v);
int SetValue(ref SlickPropKey k, ref SlickPropVariant v); int Commit();
}
public static class SlickShortcut {
[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
static extern void SHGetPropertyStoreFromParsingName(string path, IntPtr pbc, int flags, ref Guid riid, out ISlickPropertyStore ps);
public static void SetAppId(string path, string id) {
Guid iid = typeof(ISlickPropertyStore).GUID; ISlickPropertyStore ps;
SHGetPropertyStoreFromParsingName(path, IntPtr.Zero, 2, ref iid, out ps);
var key = new SlickPropKey { fmtid = new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), pid = 5 };
var pv = new SlickPropVariant { vt = 31, p = Marshal.StringToCoTaskMemUni(id) };
try { Marshal.ThrowExceptionForHR(ps.SetValue(ref key, ref pv)); Marshal.ThrowExceptionForHR(ps.Commit()); }
finally { Marshal.FreeCoTaskMem(pv.p); Marshal.ReleaseComObject(ps); }
}
}
'@
}
[SlickShortcut]::SetAppId($lnk, $id)
}
function New-Shortcuts($exe, $iconFile) {
$ws = New-Object -ComObject WScript.Shell
foreach ($lnk in $Shortcuts) {
try {
New-Item -ItemType Directory -Force (Split-Path $lnk) | Out-Null
$sc = $ws.CreateShortcut($lnk)
$sc.TargetPath = $exe; $sc.WorkingDirectory = (Split-Path $exe); $sc.Description = 'Slick (Slack mod)'
if ($iconFile -and (Test-Path $iconFile)) { $sc.IconLocation = "$iconFile,0" }
$sc.Save()
try { Set-ShortcutAppId $lnk $AppUserModelId }
catch { Write-Host " warning: could not set taskbar ID on $lnk ($($_.Exception.Message))" -ForegroundColor Yellow }
Write-Host " $lnk"
} catch {
Write-Host " warning: could not create $lnk ($($_.Exception.Message))" -ForegroundColor Yellow
}
}
}
function Stop-Slick([string]$InstalledAt) {
$expected = [IO.Path]::GetFullPath((Join-Path $InstalledAt 'Slick.exe'))
Get-Process Slick -EA SilentlyContinue | Where-Object {
try { $_.Path -and ([IO.Path]::GetFullPath($_.Path) -eq $expected) } catch { $false }
} | Stop-Process -Force -EA SilentlyContinue
Start-Sleep -Milliseconds 400
}
function Restore-OfficialHandler {
$res = Find-SlackResources
# The Store build registers slack:// via its package, at a path that changes
# every update; delete our override so the package's registration answers.
if ($res -match '\\WindowsApps\\') {
Remove-Item "HKCU:\Software\Classes\$Protocol" -Recurse -Force -EA SilentlyContinue
& ie4uinit.exe -show 2>$null
return $true
}
$exe = Slack-Exe $res
if ($exe) {
Reg "HKCU:\Software\Classes\$Protocol\shell\open\command" @{ '(default)' = "`"$($exe.FullName)`" `"%1`"" }
& ie4uinit.exe -show 2>$null
return $true
}
return $false
}
if ($RestoreHandler) {
Unregister-SlackHandler
if (Restore-OfficialHandler) { Write-Host "Slick unregistered; slack:// now points at the official Slack." }
else { Write-Host "Slick unregistered. Launch the official Slack once to reclaim slack://." }
exit 0
}
if ($Uninstall) {
Step "Uninstalling Slick"
Stop-Slick $Target
Unregister-SlackHandler
Restore-OfficialHandler | Out-Null
$Shortcuts + $LegacyShortcuts | Select-Object -Unique | ForEach-Object { Remove-Item $_ -Force -EA SilentlyContinue }
Remove-Item $Target -Recurse -Force -EA SilentlyContinue
Write-Host " removed $Target, shortcuts, and the slack:// handler"
if ($Purge) {
$profileDir = Join-Path $env:APPDATA 'Slick'
Remove-Item $profileDir, (Join-Path $env:LOCALAPPDATA 'slick-byoe') -Recurse -Force -EA SilentlyContinue
Write-Host " purged profile ($profileDir) and the Electron/rcedit cache"
}
Write-Host ""
Write-Host "Slick uninstalled." -ForegroundColor Green
if (-not $Purge) { Write-Host "Your sign-in/settings are kept at $env:APPDATA\Slick (rerun with -Purge to remove them too)." }
exit 0
}
Step "Checking prerequisites"
$slackRes = Find-SlackResources
if (-not $slackRes) { Die "Slack not found. Install Slack Desktop from https://slack.com/download or the Microsoft Store, then rerun." }
Write-Host " Slack resources: $slackRes"
$slackExe = Slack-Exe $slackRes
$slackArch = if ($slackExe) { Get-PEArch $slackExe.FullName } else {
if ($slackRes -match '\\WindowsApps\\[^\\]+_arm64_') { 'arm64' }
elseif ($slackRes -match '\\WindowsApps\\[^\\]+_x64_') { 'x64' }
else { 'x64' }
}
if ($slackArch -eq 'arm64') {
Write-Host " Microsoft Store (MSIX) ARM64 Slack detected." -ForegroundColor Cyan
} elseif ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') {
Write-Host " note: x64 Slack on an ARM64 PC runs emulated (a drop in performance is expected)." -ForegroundColor Yellow
}
$InstallTarget = $Target
# From a checkout, build; piped through `irm | iex`, download the latest release.
$FromSource = [bool]$Root -and (Test-Path (Join-Path $Root 'src\desktop\main.ts'))
if ($FromSource) {
if (-not (Get-Command node -EA SilentlyContinue)) { Die "Node.js 22+ is required to build Slick v2 (get it from nodejs.org)" }
$unpacked = if ($slackArch -eq 'arm64') { 'win-arm64-unpacked' } else { 'win-unpacked' }
if (-not (Test-Path (Join-Path $Root 'node_modules\electron-builder'))) {
Step "Installing build dependencies"
Push-Location $Root
try {
if (Get-Command bun -EA SilentlyContinue) { & bun install --frozen-lockfile }
else { & npm install --no-audit --no-fund }
if ($LASTEXITCODE -ne 0) { Die "dependency install failed" }
} finally { Pop-Location }
}
Step "Building Slick v2 (this bundles Electron; give it a minute)"
Push-Location $Root
try {
$log = Join-Path $env:TEMP 'slick-build.log'
$code = Invoke-Logged $log { node (Join-Path $Root 'scripts\build.ts') package --arch $slackArch }
if ($code -ne 0) {
Get-Content $log -Tail 30 -EA SilentlyContinue | Write-Host
Die "build failed (full log: $log)"
}
} finally { Pop-Location }
$built = Join-Path $Root "dist\release\$unpacked"
if (-not (Test-Path (Join-Path $built 'Slick.exe'))) { Die "electron-builder produced no Slick.exe at $built" }
# Copied rather than moved, so a failed install does not destroy the build.
$Target = Join-Path (Split-Path $InstallTarget) ('slick-stage-' + [Guid]::NewGuid().ToString('N'))
Copy-Item $built $Target -Recurse -Force
} else {
Step "Finding the latest Slick release"
$asset = $null; $tag = $null
$releaseArch = $slackArch
try {
$rel = Invoke-RestMethod "https://api.github.com/repos/$Repo/releases/latest" -Headers @{ 'User-Agent' = 'slick-install' }
$tag = $rel.tag_name
$asset = $rel.assets |
Where-Object { $_.name -match "win32-$releaseArch\.zip$" } |
Select-Object -First 1
} catch {
Die "could not reach GitHub to find a release ($($_.Exception.Message))"
}
if (-not $asset) {
Die "the latest release ($tag) has no Windows (win32-$releaseArch) build yet. Clone the repo and run install.ps1 to build from source: git clone https://github.com/$Repo"
}
Step "Downloading Slick $tag (win32-$releaseArch)"
$zip = Join-Path $env:TEMP "slick-$tag-win32-$releaseArch.zip"
Get-File $asset.browser_download_url $zip "Downloading Slick $tag (win32-$releaseArch)"
Assert-ReleaseAttestation $zip
$stage = Join-Path (Split-Path $InstallTarget) ("slick-stage-" + [Guid]::NewGuid().ToString('N'))
Expand-Archive $zip -DestinationPath $stage -Force
Remove-Item $zip -EA SilentlyContinue
$exeItem = Get-ChildItem $stage -Recurse -Filter 'Slick.exe' -EA SilentlyContinue | Select-Object -First 1
if (-not $exeItem) { Die "release zip did not contain Slick.exe" }
$Target = $exeItem.Directory.FullName
}
$exe = Join-Path $Target 'Slick.exe'
if (-not (Test-Path $exe)) { Die "install incomplete: $exe is missing" }
Stop-Slick $InstallTarget
$backup = $InstallTarget + '.previous-' + [Guid]::NewGuid().ToString('N')
$hadInstall = Test-Path $InstallTarget
if ($hadInstall) { Move-Item $InstallTarget $backup }
try {
New-Item -ItemType Directory -Force (Split-Path $InstallTarget) | Out-Null
Move-Item $Target $InstallTarget
} catch {
if ($hadInstall) { Move-Item $backup $InstallTarget }
throw
}
if ($hadInstall) { Remove-Item $backup -Recurse -Force }
if (-not $FromSource) { Remove-Item $stage -Recurse -Force -EA SilentlyContinue }
$Target = $InstallTarget
$exe = Join-Path $Target 'Slick.exe'
$iconFile = if ($FromSource) { Join-Path $Root 'assets\icon.ico' } else { $null }
$muiCache = 'HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache'
Remove-ItemProperty $muiCache -Name "$exe.FriendlyAppName" -EA SilentlyContinue
Remove-ItemProperty $muiCache -Name "$exe.ApplicationCompany" -EA SilentlyContinue
Step "Registering Slick as the slack:// handler"
Register-SlackHandler $exe $iconFile
Step "Creating shortcuts"
New-Shortcuts $exe $iconFile
Step "Launching Slick"
$prevNoAttach = $env:ELECTRON_NO_ATTACH_CONSOLE
$env:ELECTRON_NO_ATTACH_CONSOLE = '1'
# Lets the boot timeline price launch -> electron start. Shortcuts cannot carry
# env vars, so normal launches fall back to the wrapper-entry mark instead.
$prevLaunchT0 = $env:SLICK_LAUNCH_T0
$env:SLICK_LAUNCH_T0 = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds().ToString()
Start-Process $exe
$env:SLICK_LAUNCH_T0 = $prevLaunchT0
$env:ELECTRON_NO_ATTACH_CONSOLE = $prevNoAttach
Write-Host ""
Write-Host "Yippee! " -ForegroundColor Green -NoNewline
Write-Host "Slick is installed at $Target"
Write-Host "Things to know:"
Write-Host "- A new install starts at Slack's sign-in screen (Slick keeps its own session, separate from the official app). Sign in once; it persists."
Write-Host "- Configure at Preferences -> Slick."
Write-Host "- Uninstall: powershell -File install.ps1 -Uninstall (add -Purge to also wipe your profile)"
Write-Host "- Restore slack:// to official Slack: powershell -File install.ps1 -RestoreHandler"