Files

252 lines
10 KiB
PowerShell
Raw Permalink Normal View History

#Requires -Version 5.1
2026-05-12 15:12:52 +09:00
<#
.SYNOPSIS
Installs bottom(btm) process monitor from GitHub Nightly releases.
2026-05-12 15:12:52 +09:00
.DESCRIPTION
Auto-detects the latest Nightly release from
https://github.com/ClementTsang/bottom/releases, downloads the zip
file matching your Windows architecture, and installs it.
2026-05-12 15:12:52 +09:00
Supported architectures:
2026-05-12 15:12:52 +09:00
- x64 (AMD64 / Intel 64bit)
- ARM64 (Snapdragon X Elite, Qualcomm ARM, etc.)
2026-05-12 15:12:52 +09:00
.PARAMETER InstallDir
Directory to install btm.exe. Default: $env:USERPROFILE\.local\bin
2026-05-12 15:12:52 +09:00
.PARAMETER AddToPath
Whether to permanently add InstallDir to user PATH. Default: $true
2026-05-12 15:12:52 +09:00
.EXAMPLE
.\install-bottom.ps1
.\install-bottom.ps1 -InstallDir "C:\tools\bin"
.\install-bottom.ps1 -AddToPath $false
# One-liner install from remote (works on Windows 10+):
powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-bottom.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-bottom.ps1' -OutFile $t; & $t"
2026-05-13 07:09:56 +09:00
# With parameters:
powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-bottom.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-bottom.ps1' -OutFile $t; & $t -InstallDir 'C:\tools\bin'"
2026-05-13 07:09:56 +09:00
2026-05-12 15:12:52 +09:00
.NOTES
If execution policy error occurs:
2026-05-12 15:12:52 +09:00
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#>
[CmdletBinding()]
param(
[string] $InstallDir = "$env:USERPROFILE\.local\bin",
[bool] $AddToPath = $true
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# ────────────────────────────────────────────
# Constants
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
$REPO = "ClementTsang/bottom"
$API_URL = "https://api.github.com/repos/$REPO/releases"
$FALLBACK_TAG = "0.12.3"
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
# Utility functions
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Write-Info { param([string]$msg) Write-Host "[INFO] $msg" -ForegroundColor Cyan }
function Write-Warn { param([string]$msg) Write-Host "[WARN] $msg" -ForegroundColor Yellow }
function Write-Success { param([string]$msg) Write-Host "[OK] $msg" -ForegroundColor Green }
function Write-Err { param([string]$msg) Write-Host "[ERROR] $msg" -ForegroundColor Red; exit 1 }
# ────────────────────────────────────────────
# Enforce TLS 1.2 (for Windows 7/8.1 compatibility)
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ────────────────────────────────────────────
# Architecture detection
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Get-Platform {
# 1) Check environment variables first (even 32-bit PowerShell on
# Windows ARM can correctly detect OS architecture)
# - Native process : PROCESSOR_ARCHITECTURE = AMD64 / ARM64 / x86
2026-05-12 15:12:52 +09:00
# - WOW64(32bit on 64bit): PROCESSOR_ARCHITECTURE = x86,
# PROCESSOR_ARCHITEW6432 = AMD64 or ARM64 (actual OS arch)
2026-05-12 15:12:52 +09:00
$envArch = $env:PROCESSOR_ARCHITEW6432
if (-not $envArch) { $envArch = $env:PROCESSOR_ARCHITECTURE }
switch -Regex ($envArch) {
'^AMD64$' { return "x86_64-pc-windows-msvc" }
'^ARM64$' { return "aarch64-pc-windows-msvc" }
'^x86$' { return "i686-pc-windows-msvc" }
}
# 2) Fallback: .NET RuntimeInformation (requires PS 5.1 + .NET Framework 4.7.1+)
2026-05-12 15:12:52 +09:00
try {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
switch ($arch) {
'X64' { return "x86_64-pc-windows-msvc" }
'Arm64' { return "aarch64-pc-windows-msvc" }
'X86' { return "i686-pc-windows-msvc" }
default { Write-Err "Unsupported architecture: $arch" }
2026-05-12 15:12:52 +09:00
}
} catch {
Write-Err "Failed to detect architecture: $_"
2026-05-12 15:12:52 +09:00
}
}
# ────────────────────────────────────────────
# Get latest Nightly release tag
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Get-LatestNightlyTag {
Write-Info "Fetching latest release from GitHub API..."
2026-05-12 15:12:52 +09:00
try {
$releases = Invoke-RestMethod -Uri $API_URL -UseBasicParsing `
-Headers @{ 'User-Agent' = 'install-bottom-ps1' } -TimeoutSec 15
$nightly = $releases | Where-Object {
$_.assets -and $_.assets.Count -gt 0 -and
(($_.prerelease -eq $true -and $_.tag_name -like 'nightly-*') -or $_.prerelease -eq $false)
} | Select-Object -First 1
if ($nightly -and $nightly.tag_name) {
return $nightly.tag_name
}
2026-05-12 15:12:52 +09:00
} catch {
Write-Warn "GitHub API lookup failed: $_"
2026-05-12 15:12:52 +09:00
}
Write-Warn "Using fallback release tag: $FALLBACK_TAG"
return $FALLBACK_TAG
2026-05-12 15:12:52 +09:00
}
# ────────────────────────────────────────────
# Build download URL
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Get-DownloadUrl {
param([string]$Tag, [string]$Triple)
$filename = "bottom_${Triple}.zip"
return "https://github.com/$REPO/releases/download/$Tag/$filename"
}
# ────────────────────────────────────────────
# Download, extract & install
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Install-Bottom {
param([string]$Url, [string]$FallbackUrl)
2026-05-12 15:12:52 +09:00
# Create temp directory
2026-05-12 15:12:52 +09:00
$tmpDir = Join-Path $env:TEMP "bottom_install_$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir | Out-Null
try {
$zipPath = Join-Path $tmpDir "bottom.zip"
Write-Info "Downloading: $Url"
2026-05-12 15:12:52 +09:00
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $Url -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
if ($FallbackUrl -and ($FallbackUrl -ne $Url)) {
Write-Warn "Primary download failed. Retrying with fallback URL: $FallbackUrl"
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $FallbackUrl -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
Write-Err "Download failed: $_"
}
} else {
Write-Err "Download failed: $_"
}
2026-05-12 15:12:52 +09:00
}
Write-Info "Extracting..."
2026-05-12 15:12:52 +09:00
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
# Find btm.exe
2026-05-12 15:12:52 +09:00
$btmExe = Get-ChildItem -Path $tmpDir -Recurse -Filter "btm.exe" |
Select-Object -First 1
if (-not $btmExe) {
Write-Err "Cannot find 'btm.exe' in the archive."
2026-05-12 15:12:52 +09:00
}
# Create install directory
2026-05-12 15:12:52 +09:00
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$destPath = Join-Path $InstallDir "btm.exe"
Write-Info "Installing: $destPath"
2026-05-12 15:12:52 +09:00
Copy-Item -Path $btmExe.FullName -Destination $destPath -Force
Write-Success "Installation complete!"
2026-05-12 15:12:52 +09:00
# Check version
Write-Info "Version check:"
2026-05-12 15:12:52 +09:00
& $destPath --version
} finally {
# Cleanup temp files
2026-05-12 15:12:52 +09:00
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# ────────────────────────────────────────────
# Add to PATH (user environment variable)
2026-05-12 15:12:52 +09:00
# ────────────────────────────────────────────
function Add-ToUserPath {
param([string]$Dir)
$currentPath = [Environment]::GetEnvironmentVariable('PATH', 'User')
if (-not $currentPath) { $currentPath = '' }
# Force array even with single result (for safe -contains check)
2026-05-12 15:12:52 +09:00
$dirs = @($currentPath -split ';' | Where-Object { $_ -ne '' })
if ($dirs -contains $Dir) {
Write-Info "'$Dir' is already in your PATH."
2026-05-12 15:12:52 +09:00
return
}
$newPath = ($dirs + $Dir) -join ';'
[Environment]::SetEnvironmentVariable('PATH', $newPath, 'User')
# Also reflect in current session
2026-05-12 15:12:52 +09:00
$env:PATH = "$env:PATH;$Dir"
Write-Success "'$Dir' has been permanently added to user PATH."
Write-Warn "Open a new terminal for PATH changes to take effect."
2026-05-12 15:12:52 +09:00
}
# ────────────────────────────────────────────
# 메인
# ────────────────────────────────────────────
function Main {
Write-Info "===== bottom(btm) install script (Windows PowerShell) ====="
2026-05-12 15:12:52 +09:00
$triple = Get-Platform
Write-Info "Detected platform: $triple"
2026-05-12 15:12:52 +09:00
$tag = Get-LatestNightlyTag
Write-Info "Release tag: $tag"
2026-05-12 15:12:52 +09:00
$url = Get-DownloadUrl -Tag $tag -Triple $triple
$fallbackUrl = Get-DownloadUrl -Tag $FALLBACK_TAG -Triple $triple
Install-Bottom -Url $url -FallbackUrl $fallbackUrl
2026-05-12 15:12:52 +09:00
if ($AddToPath) {
Add-ToUserPath -Dir $InstallDir
} else {
Write-Warn "Skipping automatic PATH registration."
Write-Warn "Add to PATH manually: $InstallDir"
2026-05-12 15:12:52 +09:00
}
Write-Success "All tasks completed."
Write-Info "Usage: btm"
2026-05-12 15:12:52 +09:00
}
Main