Files
install-sh/install-bottom.ps1
T

240 lines
9.6 KiB
PowerShell

#Requires -Version 5.1
<#
.SYNOPSIS
Installs bottom(btm) process monitor from GitHub Nightly releases.
.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.
Supported architectures:
- x64 (AMD64 / Intel 64bit)
- ARM64 (Snapdragon X Elite, Qualcomm ARM, etc.)
.PARAMETER InstallDir
Directory to install btm.exe. Default: $env:USERPROFILE\.local\bin
.PARAMETER AddToPath
Whether to permanently add InstallDir to user PATH. Default: $true
.EXAMPLE
.\install-bottom.ps1
.\install-bottom.ps1 -InstallDir "C:\tools\bin"
.\install-bottom.ps1 -AddToPath $false
# One-liner install from remote (download then run; -ExecutionPolicy Bypass for current process only):
powershell -ExecutionPolicy Bypass -c "$t=\"$env:TEMP\install-bottom.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-bottom.ps1' -OutFile $t; & $t"
# With parameters:
powershell -ExecutionPolicy Bypass -c "$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'"
.NOTES
If execution policy error occurs:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#>
[CmdletBinding()]
param(
[string] $InstallDir = "$env:USERPROFILE\.local\bin",
[bool] $AddToPath = $true
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# ────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────
$REPO = "ClementTsang/bottom"
$API_URL = "https://api.github.com/repos/$REPO/releases"
# ────────────────────────────────────────────
# Utility functions
# ────────────────────────────────────────────
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)
# ────────────────────────────────────────────
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ────────────────────────────────────────────
# Architecture detection
# ────────────────────────────────────────────
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
# - WOW64(32bit on 64bit): PROCESSOR_ARCHITECTURE = x86,
# PROCESSOR_ARCHITEW6432 = AMD64 or ARM64 (actual OS arch)
$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+)
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" }
}
} catch {
Write-Err "Failed to detect architecture: $_"
}
}
# ────────────────────────────────────────────
# Get latest Nightly release tag
# ────────────────────────────────────────────
function Get-LatestNightlyTag {
Write-Info "Fetching latest Nightly release from GitHub API..."
try {
$releases = Invoke-RestMethod -Uri $API_URL -UseBasicParsing `
-Headers @{ 'User-Agent' = 'install-bottom-ps1' }
} catch {
Write-Err "Failed to call GitHub API: $_"
}
# Get first item where prerelease=true and tag_name starts with 'nightly-'
$nightly = $releases | Where-Object {
$_.prerelease -eq $true -and $_.tag_name -like 'nightly-*'
} | Select-Object -First 1
if (-not $nightly) {
Write-Err "Could not find Nightly release."
}
return $nightly.tag_name
}
# ────────────────────────────────────────────
# Build download URL
# ────────────────────────────────────────────
function Get-DownloadUrl {
param([string]$Tag, [string]$Triple)
$filename = "bottom_${Triple}.zip"
return "https://github.com/$REPO/releases/download/$Tag/$filename"
}
# ────────────────────────────────────────────
# Download, extract & install
# ────────────────────────────────────────────
function Install-Bottom {
param([string]$Url)
# Create temp directory
$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"
try {
# Suppress progress bar in PowerShell 5+
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $Url -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
Write-Err "Download failed: $_"
}
Write-Info "Extracting..."
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
# Find btm.exe
$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."
}
# Create install directory
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$destPath = Join-Path $InstallDir "btm.exe"
Write-Info "Installing: $destPath"
Copy-Item -Path $btmExe.FullName -Destination $destPath -Force
Write-Success "Installation complete!"
# Check version
Write-Info "Version check:"
& $destPath --version
} finally {
# Cleanup temp files
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# ────────────────────────────────────────────
# Add to PATH (user environment variable)
# ────────────────────────────────────────────
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)
$dirs = @($currentPath -split ';' | Where-Object { $_ -ne '' })
if ($dirs -contains $Dir) {
Write-Info "'$Dir' is already in your PATH."
return
}
$newPath = ($dirs + $Dir) -join ';'
[Environment]::SetEnvironmentVariable('PATH', $newPath, 'User')
# Also reflect in current session
$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."
}
# ────────────────────────────────────────────
# 메인
# ────────────────────────────────────────────
function Main {
Write-Info "===== bottom(btm) install script (Windows PowerShell) ====="
$triple = Get-Platform
Write-Info "Detected platform: $triple"
$tag = Get-LatestNightlyTag
Write-Info "Latest Nightly tag: $tag"
$url = Get-DownloadUrl -Tag $tag -Triple $triple
Install-Bottom -Url $url
if ($AddToPath) {
Add-ToUserPath -Dir $InstallDir
} else {
Write-Warn "Skipping automatic PATH registration."
Write-Warn "Add to PATH manually: $InstallDir"
}
Write-Success "All tasks completed."
Write-Info "Usage: btm"
}
Main