|
| 1 | +#Requires -Version 5.1 |
| 2 | +$ErrorActionPreference = 'Stop' |
| 3 | + |
| 4 | +# Interactive builder for scoop-based tool-pack installers. Prompts for |
| 5 | +# output path, install scope, buckets, and tool list - then writes a script |
| 6 | +# that you can run inside a Windows Sandbox (global / admin install) OR |
| 7 | +# from any local PowerShell session (per-user install). |
| 8 | +# |
| 9 | +# Output path is freeform - relative or absolute, anywhere on disk. |
| 10 | + |
| 11 | + |
| 12 | +# -------- prompt helpers -------- |
| 13 | + |
| 14 | +function Read-Option { |
| 15 | + param([string]$Prompt, [string[]]$Options, [int]$DefaultIdx = 0) |
| 16 | + Write-Host "" |
| 17 | + Write-Host $Prompt -ForegroundColor Cyan |
| 18 | + for ($i = 0; $i -lt $Options.Count; $i++) { |
| 19 | + $marker = if ($i -eq $DefaultIdx) { '*' } else { ' ' } |
| 20 | + Write-Host (" {0} {1}) {2}" -f $marker, ($i + 1), $Options[$i]) |
| 21 | + } |
| 22 | + while ($true) { |
| 23 | + $resp = Read-Host "Choice [$($DefaultIdx + 1)]" |
| 24 | + if ([string]::IsNullOrWhiteSpace($resp)) { return $Options[$DefaultIdx] } |
| 25 | + $parsed = 0 |
| 26 | + if ([int]::TryParse($resp, [ref]$parsed)) { |
| 27 | + $idx = $parsed - 1 |
| 28 | + if ($idx -ge 0 -and $idx -lt $Options.Count) { return $Options[$idx] } |
| 29 | + } |
| 30 | + Write-Host " Invalid choice; try again." -ForegroundColor Yellow |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function Read-WithDefault { |
| 35 | + param([string]$Prompt, [string]$Default) |
| 36 | + $val = Read-Host "$Prompt [$Default]" |
| 37 | + if ([string]::IsNullOrWhiteSpace($val)) { return $Default } |
| 38 | + return $val |
| 39 | +} |
| 40 | + |
| 41 | +function Read-YesNo { |
| 42 | + param([string]$Prompt, [bool]$Default = $true) |
| 43 | + $hint = if ($Default) { '[Y/n]' } else { '[y/N]' } |
| 44 | + while ($true) { |
| 45 | + $resp = Read-Host "$Prompt $hint" |
| 46 | + if ([string]::IsNullOrWhiteSpace($resp)) { return $Default } |
| 47 | + switch -Regex ($resp.Trim().ToLower()) { |
| 48 | + '^y(es)?$' { return $true } |
| 49 | + '^n(o)?$' { return $false } |
| 50 | + default { Write-Host " Please answer y or n." -ForegroundColor Yellow } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +# -------- flow -------- |
| 57 | + |
| 58 | +Write-Host "" |
| 59 | +Write-Host "=== RedSand custom toolkit installer builder ===" -ForegroundColor Green |
| 60 | +Write-Host "Generates a scoop-based installer .ps1 with your chosen tool list." |
| 61 | +Write-Host "" |
| 62 | + |
| 63 | +# Output (any path works - absolute, relative) |
| 64 | +$outFile = Read-WithDefault -Prompt 'Output path' -Default 'Utils\Scripts\AdditionalScripts\InSandbox\installCustomTools.ps1' |
| 65 | +if ($outFile -notlike '*.ps1') { $outFile = "$outFile.ps1" } |
| 66 | + |
| 67 | +$description = Read-WithDefault -Prompt 'One-line description (for the script header)' -Default 'Custom scoop tool pack' |
| 68 | + |
| 69 | +# Install scope determines --global usage + admin requirement in the emitted script |
| 70 | +$scope = Read-Option 'Install scope?' @( |
| 71 | + 'Global (sandbox / any admin install)', |
| 72 | + 'Per-user (local non-admin install)' |
| 73 | +) 0 |
| 74 | +$useGlobal = ($scope -like 'Global*') |
| 75 | +$globalFlag = if ($useGlobal) { ' --global' } else { '' } |
| 76 | + |
| 77 | +# Buckets |
| 78 | +$buckets = @() |
| 79 | +Write-Host "" |
| 80 | +Write-Host "--- Buckets ---" -ForegroundColor Cyan |
| 81 | +Write-Host "Scoop's 'main' bucket is always available; add others as needed." |
| 82 | + |
| 83 | +if (Read-YesNo -Prompt "Add 'extras' bucket?" -Default $true) { $buckets += 'extras' } |
| 84 | +foreach ($candidate in 'nirsoft', 'versions', 'java') { |
| 85 | + if (Read-YesNo -Prompt "Add '$candidate' bucket?" -Default $false) { $buckets += $candidate } |
| 86 | +} |
| 87 | +while (Read-YesNo -Prompt 'Add another custom bucket?' -Default $false) { |
| 88 | + $name = Read-Host " Bucket name (or 'name <git-url>' for a custom feed)" |
| 89 | + if (-not [string]::IsNullOrWhiteSpace($name)) { $buckets += $name.Trim() } |
| 90 | +} |
| 91 | + |
| 92 | +# Tools |
| 93 | +$tools = @() |
| 94 | +Write-Host "" |
| 95 | +Write-Host "--- Tools ---" -ForegroundColor Cyan |
| 96 | +Write-Host "Enter tool names, comma- or space-separated. Example: hxd, dnspy, x64dbg" |
| 97 | +$first = Read-Host 'Tools' |
| 98 | +if (-not [string]::IsNullOrWhiteSpace($first)) { |
| 99 | + $tools += @($first -split '[,\s]+' | Where-Object { $_ }) |
| 100 | +} |
| 101 | +while (Read-YesNo -Prompt 'Add more (one at a time)?' -Default $false) { |
| 102 | + $name = Read-Host ' Tool name' |
| 103 | + if (-not [string]::IsNullOrWhiteSpace($name)) { $tools += $name.Trim() } |
| 104 | +} |
| 105 | + |
| 106 | +if ($tools.Count -eq 0) { |
| 107 | + Write-Host "No tools listed; aborting." -ForegroundColor Yellow |
| 108 | + return |
| 109 | +} |
| 110 | + |
| 111 | +# Summary + confirm |
| 112 | +$resolvedOut = if (Test-Path -IsValid $outFile) { [System.IO.Path]::GetFullPath((Join-Path (Get-Location).Path $outFile)) } else { $outFile } |
| 113 | + |
| 114 | +Write-Host "" |
| 115 | +Write-Host "=== Summary ===" -ForegroundColor Green |
| 116 | +Write-Host "Output: $outFile" |
| 117 | +Write-Host " (full path) $resolvedOut" |
| 118 | +Write-Host "Description: $description" |
| 119 | +Write-Host "Scope: $scope" |
| 120 | +Write-Host "Buckets: $(if ($buckets.Count) { $buckets -join ', ' } else { '(none beyond main)' })" |
| 121 | +Write-Host "Tools ($($tools.Count)):" |
| 122 | +foreach ($t in $tools) { Write-Host " - $t" } |
| 123 | +Write-Host "" |
| 124 | + |
| 125 | +if (-not (Read-YesNo -Prompt 'Write this file?' -Default $true)) { |
| 126 | + Write-Host "Aborted." -ForegroundColor Yellow |
| 127 | + return |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +# -------- emit the installer script -------- |
| 132 | + |
| 133 | +$lines = @() |
| 134 | +$lines += '#Requires -Version 5.1' |
| 135 | +if ($useGlobal) { |
| 136 | + $lines += '#Requires -RunAsAdministrator' |
| 137 | +} |
| 138 | +$lines += "`$ErrorActionPreference = 'Stop'" |
| 139 | +$lines += '' |
| 140 | +$lines += "# $description" |
| 141 | +$lines += '# Generated by build-toolkit-installer.ps1' |
| 142 | +$lines += '#' |
| 143 | +if ($useGlobal) { |
| 144 | + $lines += '# Global / admin install. Run installChocoAndScoop.ps1 first inside the' |
| 145 | + $lines += '# sandbox, or install scoop manually with -RunAsAdmin.' |
| 146 | +} else { |
| 147 | + $lines += '# Per-user install. Requires scoop already installed (https://scoop.sh).' |
| 148 | +} |
| 149 | +$lines += '' |
| 150 | +$lines += 'if (-not (Get-Command scoop -ErrorAction SilentlyContinue)) {' |
| 151 | +$lines += ' throw "scoop not found - install scoop first (https://scoop.sh)"' |
| 152 | +$lines += '}' |
| 153 | +$lines += '' |
| 154 | +$lines += '# Scoop needs git to clone bucket repos; install it if missing' |
| 155 | +$lines += 'if (-not (Get-Command git -ErrorAction SilentlyContinue)) {' |
| 156 | +$lines += ' Write-Host "Installing git (required for scoop buckets)..." -ForegroundColor Cyan' |
| 157 | +$lines += " scoop install$globalFlag git" |
| 158 | +$lines += '}' |
| 159 | +$lines += '' |
| 160 | +foreach ($b in $buckets) { |
| 161 | + $lines += "scoop bucket add $b" |
| 162 | +} |
| 163 | +if ($buckets.Count -gt 0) { $lines += '' } |
| 164 | + |
| 165 | +$lines += "`$tools = @(" |
| 166 | +for ($i = 0; $i -lt $tools.Count; $i++) { |
| 167 | + $sep = if ($i -eq $tools.Count - 1) { '' } else { ',' } |
| 168 | + $lines += " '$($tools[$i])'$sep" |
| 169 | +} |
| 170 | +$lines += ')' |
| 171 | +$lines += '' |
| 172 | +$lines += 'foreach ($tool in $tools) {' |
| 173 | +$lines += ' Write-Host "Installing $tool..." -ForegroundColor Cyan' |
| 174 | +$lines += " scoop install$globalFlag `$tool" |
| 175 | +$lines += ' if ($LASTEXITCODE -ne 0) {' |
| 176 | +$lines += ' Write-Warning "scoop returned exit code $LASTEXITCODE installing $tool"' |
| 177 | +$lines += ' }' |
| 178 | +$lines += '}' |
| 179 | +$lines += '' |
| 180 | +$lines += 'Write-Host "Tools installed." -ForegroundColor Green' |
| 181 | + |
| 182 | +# Write |
| 183 | +$parentDir = Split-Path -Parent $outFile |
| 184 | +if ($parentDir -and -not (Test-Path $parentDir)) { |
| 185 | + New-Item -ItemType Directory -Path $parentDir -Force | Out-Null |
| 186 | +} |
| 187 | +Set-Content -Path $outFile -Value ($lines -join [Environment]::NewLine) -Encoding UTF8 |
| 188 | + |
| 189 | +Write-Host "" |
| 190 | +Write-Host "Wrote $outFile" -ForegroundColor Green |
| 191 | +Write-Host " (full path) $resolvedOut" |
| 192 | +Write-Host "" |
| 193 | +if ($useGlobal) { |
| 194 | + Write-Host "Run inside the sandbox (after installChocoAndScoop.ps1):" -ForegroundColor Cyan |
| 195 | +} else { |
| 196 | + Write-Host "Run from any PowerShell session (scoop must be installed):" -ForegroundColor Cyan |
| 197 | +} |
| 198 | +Write-Host " powershell.exe -ExecutionPolicy Bypass -File `"$resolvedOut`"" |
0 commit comments