-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
95 lines (82 loc) · 4.54 KB
/
Copy pathinstall.ps1
File metadata and controls
95 lines (82 loc) · 4.54 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
#Requires -Version 5.1
[CmdletBinding()]
param()
$ErrorActionPreference = "Stop"
$repo = "danielmrdev/dtasks-cli"
$binary = "dtasks"
$apiUrl = "https://api.github.com/repos/$repo/releases/latest"
# ── Detect arch ──────────────────────────────────────────────────────────────
$arch = switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { "amd64" }
"ARM64" { "arm64" }
default {
Write-Error "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE"
exit 1
}
}
$asset = "${binary}-windows-${arch}.exe"
# ── Resolve latest version ───────────────────────────────────────────────────
Write-Host "Fetching latest release..."
$release = Invoke-RestMethod -Uri $apiUrl -UseBasicParsing
$version = $release.tag_name
if (-not $version) {
Write-Error "Could not determine latest version"
exit 1
}
$downloadUrl = "https://github.com/$repo/releases/download/$version/$asset"
$checksumUrl = "https://github.com/$repo/releases/download/$version/checksums.txt"
# ── Install dir ──────────────────────────────────────────────────────────────
$installDir = Join-Path $env:LOCALAPPDATA "Programs\dtasks"
if (-not (Test-Path $installDir)) {
New-Item -ItemType Directory -Path $installDir | Out-Null
}
$dest = Join-Path $installDir "$binary.exe"
# ── Download ──────────────────────────────────────────────────────────────────
Write-Host "Downloading dtasks $version (windows/$arch)..."
$tmp = Join-Path $env:TEMP "$asset"
Invoke-WebRequest -Uri $downloadUrl -OutFile $tmp -UseBasicParsing
# ── Verify checksum ───────────────────────────────────────────────────────────
Write-Host "Verifying checksum..."
try {
$checksums = (Invoke-WebRequest -Uri $checksumUrl -UseBasicParsing).Content
$expected = ($checksums -split "`n" | Where-Object { $_ -match $asset }) -replace '\s+.*', '' | Select-Object -First 1
if ($expected) {
$actual = (Get-FileHash -Path $tmp -Algorithm SHA256).Hash.ToLower()
if ($actual -ne $expected.ToLower()) {
Write-Error "Checksum mismatch — aborting"
Remove-Item $tmp -Force
exit 1
}
Write-Host "Checksum OK"
}
} catch {
Write-Warning "Could not verify checksum: $_"
}
# ── Install ───────────────────────────────────────────────────────────────────
Move-Item -Path $tmp -Destination $dest -Force
Write-Host "Installed dtasks $version -> $dest"
# ── Add to user PATH if needed ───────────────────────────────────────────────
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($userPath -notlike "*$installDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$userPath;$installDir", "User")
Write-Host ""
Write-Host "$installDir added to your PATH."
Write-Host "Restart your terminal for the change to take effect."
}
# ── Shell completions ─────────────────────────────────────────────────────────
if ($Host.UI.RawUI -ne $null -and [System.Environment]::UserInteractive) {
$answer = Read-Host "Install PowerShell completions? [y/N]"
if ($answer -match "^[Yy]") {
if (-not (Test-Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force | Out-Null
}
$completionLine = "`n# dtasks shell completion`n& `"$dest`" completion powershell | Out-String | Invoke-Expression"
$profileContent = Get-Content $PROFILE -Raw -ErrorAction SilentlyContinue
if ($profileContent -notlike "*dtasks completion*") {
Add-Content -Path $PROFILE -Value $completionLine
Write-Host "PowerShell completions added to $PROFILE"
} else {
Write-Host "PowerShell completions already in $PROFILE"
}
}
}