install.ps1
|
#Requires -Version 7.0 <# .SYNOPSIS WinMole Installer — sets up the 'mo' command on your PATH. .DESCRIPTION Installs WinMole and adds a 'mo' function to your PowerShell profile so you can run 'mo' from any terminal session. Supports in-place installation (when you've already cloned the repo) or fresh clone-and-install via the -Clone switch. .PARAMETER InstallDir Directory to install WinMole into. Default: $env:LOCALAPPDATA\WinMole .PARAMETER AddToPath Add the install directory to the current user's PATH. .PARAMETER CreateShortcut Create a Start Menu shortcut. .PARAMETER Clone Clone the repository into InstallDir before installing. .PARAMETER Uninstall Remove WinMole from the system. .EXAMPLE # Install from a cloned repo (run from the repo root) pwsh -ExecutionPolicy Bypass -File .\install.ps1 # Fresh install pwsh -ExecutionPolicy Bypass -File .\install.ps1 -InstallDir C:\Tools\WinMole -AddToPath # Uninstall pwsh -ExecutionPolicy Bypass -File .\install.ps1 -Uninstall #> param( [string]$InstallDir = "$env:LOCALAPPDATA\WinMole", [switch]$AddToPath, [switch]$CreateShortcut, [switch]$Clone, [switch]$Uninstall ) $ErrorActionPreference = 'Stop' function Write-Step { param($m) Write-Host " ✓ $m" -ForegroundColor Green } function Write-Info2 { param($m) Write-Host " → $m" -ForegroundColor Cyan } function Write-Warn2 { param($m) Write-Host " ⚠ $m" -ForegroundColor Yellow } function Write-Err2 { param($m) Write-Host " ✗ $m" -ForegroundColor Red } Write-Host "" Write-Host " WinMole Installer" -ForegroundColor Cyan Write-Host "" # ── Uninstall mode ──────────────────────────────────────────────────────────── if ($Uninstall) { Write-Info2 "Removing WinMole..." # Locate install dir $target = $InstallDir if (-not (Test-Path $target)) { # Try to find from PATH $moScript = Get-Command mo -ErrorAction SilentlyContinue if ($moScript) { $target = Split-Path -Parent $moScript.Source } } # Remove PATH entry $currentPath = [System.Environment]::GetEnvironmentVariable('PATH', 'User') if ($currentPath) { $newPath = ($currentPath -split ';' | Where-Object { $_ -notlike "*WinMole*" }) -join ';' [System.Environment]::SetEnvironmentVariable('PATH', $newPath, 'User') Write-Step "Removed from PATH" } # Remove profile entry foreach ($prof in @($PROFILE.CurrentUserAllHosts, $PROFILE.CurrentUserCurrentHost)) { if (Test-Path $prof) { $content = Get-Content $prof -Raw -ErrorAction SilentlyContinue if ($content -match 'WinMole') { $newContent = $content -replace '(?ms)# WinMole BEGIN.*?# WinMole END\r?\n?', '' Set-Content -Path $prof -Value $newContent.TrimEnd() -ErrorAction SilentlyContinue Write-Step "Removed from profile: $prof" } } } # Remove files if (Test-Path $target) { Remove-Item -LiteralPath $target -Recurse -Force Write-Step "Removed: $target" } # Remove Start Menu shortcut $shortcutPath = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\WinMole.lnk" if (Test-Path $shortcutPath) { Remove-Item -LiteralPath $shortcutPath -Force Write-Step "Removed Start Menu shortcut" } Write-Host "" Write-Host " WinMole uninstalled." -ForegroundColor Green -NoNewline Write-Host " Restart your terminal to complete removal." Write-Host "" exit 0 } # ── Clone if requested ──────────────────────────────────────────────────────── if ($Clone) { if (-not (Get-Command git -ErrorAction SilentlyContinue)) { Write-Err2 "git is required. Install from https://git-scm.com and retry." exit 1 } Write-Info2 "Cloning WinMole into $InstallDir..." if (Test-Path $InstallDir) { Write-Warn2 "$InstallDir already exists. Use -Uninstall first or choose a different -InstallDir." exit 1 } & git clone https://github.com/jorgeasaurus/WinMole.git $InstallDir if ($LASTEXITCODE -ne 0) { Write-Err2 "Clone failed." exit 1 } Write-Step "Cloned successfully" } else { # In-place install: copy files from current directory if needed $scriptDir = $PSScriptRoot if ($scriptDir -ne $InstallDir -and (Test-Path $scriptDir)) { Write-Info2 "Installing from $scriptDir to $InstallDir..." if (-not (Test-Path $InstallDir)) { [void][System.IO.Directory]::CreateDirectory($InstallDir) } Copy-Item -Path "$scriptDir\*" -Destination $InstallDir -Recurse -Force Write-Step "Copied files" } } $winmoleScript = Join-Path $InstallDir 'WinMole.ps1' if (-not (Test-Path $winmoleScript)) { Write-Err2 "WinMole.ps1 not found in $InstallDir. Installation failed." exit 1 } Write-Step "WinMole found at: $winmoleScript" # ── Install PwshSpectreConsole dependency ───────────────────────────────────── Write-Info2 "Checking PwshSpectreConsole module..." if (-not (Get-Module PwshSpectreConsole -ListAvailable)) { Write-Info2 "Installing PwshSpectreConsole..." Install-Module -Name PwshSpectreConsole -Scope CurrentUser -Force -AllowClobber Write-Step "PwshSpectreConsole installed" } else { Write-Info2 "PwshSpectreConsole already installed (skipped)" } # ── Add 'mo' function to PowerShell profile ─────────────────────────────────── $profilePath = $PROFILE.CurrentUserAllHosts $profileDir = Split-Path -Parent $profilePath if (-not (Test-Path $profileDir)) { [void][System.IO.Directory]::CreateDirectory($profileDir) } if (-not (Test-Path $profilePath)) { [void][System.IO.File]::Create($profilePath).Dispose() } $profileContent = Get-Content $profilePath -Raw -ErrorAction SilentlyContinue $moFunc = @" # WinMole BEGIN function mo { & '$winmoleScript' @args } # WinMole END "@ if ($profileContent -notmatch 'WinMole BEGIN') { Add-Content -Path $profilePath -Value $moFunc Write-Step "Added 'mo' function to: $profilePath" } else { Write-Info2 "'mo' function already in profile (skipped)" } # ── Add to PATH (optional) ──────────────────────────────────────────────────── if ($AddToPath) { $currentPath = [System.Environment]::GetEnvironmentVariable('PATH', 'User') if ($currentPath -notlike "*$InstallDir*") { $newPath = "$currentPath;$InstallDir" [System.Environment]::SetEnvironmentVariable('PATH', $newPath, 'User') Write-Step "Added to PATH" } else { Write-Info2 "Already in PATH (skipped)" } } # ── Create Start Menu shortcut (optional) ──────────────────────────────────── if ($CreateShortcut) { try { $shortcutDir = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs" $shortcutPath = Join-Path $shortcutDir 'WinMole.lnk' $wsh = New-Object -ComObject WScript.Shell $shortcut = $wsh.CreateShortcut($shortcutPath) $shortcut.TargetPath = 'pwsh.exe' $shortcut.Arguments = "-NoExit -ExecutionPolicy Bypass -File `"$winmoleScript`"" $shortcut.Description = 'WinMole - Windows system cleaner' $shortcut.Save() Write-Step "Created Start Menu shortcut" } catch { Write-Warn2 "Could not create shortcut: $_" } } # ── Done ────────────────────────────────────────────────────────────────────── Write-Host "" Write-Host " WinMole installed successfully!" -ForegroundColor Green Write-Host "" Write-Host " Restart your terminal, then run:" -ForegroundColor DarkGray Write-Host "" Write-Host " mo" -ForegroundColor Cyan -NoNewline Write-Host " " -NoNewline Write-Host "# Interactive menu" -ForegroundColor DarkGray Write-Host " mo clean" -ForegroundColor Cyan -NoNewline Write-Host " " -NoNewline Write-Host "# Deep cleanup" -ForegroundColor DarkGray Write-Host " mo clean --dry-run" -ForegroundColor Cyan -NoNewline Write-Host " " -NoNewline Write-Host "# Preview cleanup" -ForegroundColor DarkGray Write-Host " mo --help" -ForegroundColor Cyan -NoNewline Write-Host " " -NoNewline Write-Host "# All commands" -ForegroundColor DarkGray Write-Host "" Write-Host " Config: $env:USERPROFILE\.config\winmole" -ForegroundColor DarkGray Write-Host " Logs: $env:USERPROFILE\.cache\winmole\logs" -ForegroundColor DarkGray Write-Host "" |