Files
install-sh/install-dua.ps1
T

252 lines
9.7 KiB
PowerShell
Raw Normal View History

2026-05-13 22:29:18 +09:00
#Requires -Version 5.1
<#
.SYNOPSIS
Installs dua (Disk Usage Analyzer) from GitHub releases.
2026-05-13 22:29:18 +09:00
.DESCRIPTION
Auto-detects the latest CLI release (v*) from
https://github.com/Byron/dua-cli/releases, downloads the zip archive
matching your Windows architecture, and installs dua.exe into
$env:USERPROFILE\.local\bin.
2026-05-13 22:29:18 +09:00
Supported platforms:
- Windows x64 (AMD64 / Intel 64bit)
- Windows ARM64
- Windows x86 (32bit)
2026-05-13 22:29:18 +09:00
.PARAMETER InstallDir
Directory to install dua.exe. Default: $env:USERPROFILE\.local\bin
.PARAMETER AddToPath
Whether to permanently add InstallDir to user PATH. Default: $true
2026-05-13 22:29:18 +09:00
.EXAMPLE
.\install-dua.ps1
# One-liner install from remote (works on Windows 10+):
powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-dua.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-dua.ps1' -OutFile $t; & $t"
.NOTES
If execution policy error occurs:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#>
[CmdletBinding()]
param(
[string] $InstallDir = "$env:USERPROFILE\.local\bin",
[bool] $AddToPath = $true
)
2026-05-13 22:29:18 +09:00
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Force TLS 1.2+ for HTTPS downloads (required on Windows 10 / PowerShell 5.1)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────
$REPO = "Byron/dua-cli"
$API_URL = "https://api.github.com/repos/$REPO/releases"
$FALLBACK_TAG = "v2.46.0"
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
# Utility functions
# ────────────────────────────────────────────
function Info { Write-Host "[INFO] $args" -ForegroundColor Cyan }
function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
function Success { Write-Host "[OK] $args" -ForegroundColor Green }
function ErrExit { Write-Host "[ERROR] $args" -ForegroundColor Red; exit 1 }
# ────────────────────────────────────────────
# Architecture detection
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
function Get-Platform {
$envArch = $env:PROCESSOR_ARCHITEW6432
if (-not $envArch) { $envArch = $env:PROCESSOR_ARCHITECTURE }
2026-05-13 22:29:18 +09:00
switch -Regex ($envArch) {
'^AMD64$' { return "x86_64-pc-windows-msvc" }
'^ARM64$' { return "aarch64-pc-windows-msvc" }
'^x86$' { return "i686-pc-windows-msvc" }
2026-05-13 22:29:18 +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 { ErrExit "Unsupported architecture: $arch" }
}
} catch {
ErrExit "Failed to detect architecture: $_"
2026-05-13 22:29:18 +09:00
}
}
# ────────────────────────────────────────────
# Get latest stable CLI release tag (starts with 'v')
# ────────────────────────────────────────────
function Get-LatestTag {
Info "Fetching latest dua release from GitHub API..."
try {
$releases = Invoke-RestMethod -Uri $API_URL -UseBasicParsing `
-Headers @{ 'User-Agent' = 'install-dua-ps1' } -TimeoutSec 15
# Byron/dua-cli also publishes 'dua-core-v*' library tags without binary assets.
# Filter only CLI releases whose tag_name starts with 'v' and have assets.
$latest = $releases | Where-Object {
$_.prerelease -eq $false -and
$_.tag_name -like 'v*' -and
$_.assets -and
$_.assets.Count -gt 0
} | Select-Object -First 1
if ($latest -and $latest.tag_name) {
return $latest.tag_name
}
} catch {
Warn "GitHub API lookup failed: $_"
}
Warn "Using fallback release tag: $FALLBACK_TAG"
return $FALLBACK_TAG
}
# ────────────────────────────────────────────
# Build download URL
# ────────────────────────────────────────────
function Get-DownloadUrl {
param([string]$Tag, [string]$Triple)
$filename = "dua-${Tag}-${Triple}.zip"
return "https://github.com/$REPO/releases/download/$Tag/$filename"
}
# ────────────────────────────────────────────
# Download, extract & install
# ────────────────────────────────────────────
function Install-Dua {
param([string]$Url)
$tmpDir = Join-Path $env:TEMP "dua_install_$(Get-Random)"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
try {
$zipPath = Join-Path $tmpDir "dua.zip"
Info "Downloading: $Url"
try {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $Url -OutFile $zipPath -UseBasicParsing
$ProgressPreference = 'Continue'
} catch {
ErrExit "Download failed from $Url : $_"
}
Info "Extracting archive..."
Expand-Archive -Path $zipPath -DestinationPath $tmpDir -Force
$duaExe = Get-ChildItem -Path $tmpDir -Recurse -Filter "dua.exe" |
Select-Object -First 1
if (-not $duaExe) {
ErrExit "Cannot find 'dua.exe' in the downloaded archive."
}
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$destPath = Join-Path $InstallDir "dua.exe"
Info "Installing to: $destPath"
Copy-Item -Path $duaExe.FullName -Destination $destPath -Force
Success "dua installed to $destPath"
return $destPath
} finally {
if (Test-Path $tmpDir) {
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# ────────────────────────────────────────────
# Add to PATH (user environment variable)
# ────────────────────────────────────────────
function Add-ToUserPath {
param([string]$Dir)
$normalizedDir = $Dir.TrimEnd('\')
if ($env:Path -notlike "*$normalizedDir*") {
$env:Path = "$normalizedDir;$env:Path"
Info "Added $normalizedDir to current session PATH."
}
$currentPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $currentPath) { $currentPath = '' }
$dirs = @($currentPath -split ';' | Where-Object { $_ -ne '' })
$alreadyInPath = $false
foreach ($d in $dirs) {
if ($d.Trim().TrimEnd('\') -eq $normalizedDir) {
$alreadyInPath = $true
break
2026-05-13 22:29:18 +09:00
}
}
if (-not $alreadyInPath) {
$newPath = if ($currentPath) { "$normalizedDir;$currentPath" } else { $normalizedDir }
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
Success "Added '$normalizedDir' to User PATH permanently."
Warn "Open a new terminal for PATH changes to take full effect."
2026-05-13 22:29:18 +09:00
} else {
Info "'$normalizedDir' is already in User PATH."
2026-05-13 22:29:18 +09:00
}
}
# ────────────────────────────────────────────
# Main
2026-05-13 22:29:18 +09:00
# ────────────────────────────────────────────
function Main {
Info "===== dua (Disk Usage Analyzer) install script (Windows PowerShell) ====="
$triple = Get-Platform
Info "Detected platform: $triple"
$tag = Get-LatestTag
Info "Release tag: $tag"
$url = Get-DownloadUrl -Tag $tag -Triple $triple
$duaBin = Install-Dua -Url $url
if ($AddToPath) {
Add-ToUserPath -Dir $InstallDir
} else {
Warn "Skipping automatic PATH registration."
Warn "Add to PATH manually: $InstallDir"
}
2026-05-13 22:29:18 +09:00
Info ""
Info "=================================================="
Success "dua installation complete!"
Info "=================================================="
if ($duaBin -and (Test-Path $duaBin)) {
$ver = & $duaBin --version 2>$null
Info "Installed version: $ver ($duaBin)"
}
Info ""
Info "Getting started:"
Info " dua - Show disk usage of current directory"
Info " dua /path/to/dir - Show disk usage of specific directory"
Info " dua i - Interactive mode (explore & delete)"
Info " dua --help - Show help"
Info ""
Info "Interactive mode shortcuts:"
Info " Arrow keys / h/j/k/l - Navigate"
Info " ? - Show help"
Info " d - Delete selected entry"
Info " q / Esc - Quit"
2026-05-13 22:29:18 +09:00
}
Main