-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_dependencies.ps1
More file actions
246 lines (210 loc) · 7.34 KB
/
Copy pathinstall_dependencies.ps1
File metadata and controls
246 lines (210 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
param(
[switch]$Yes
)
$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectCommand = Join-Path $ProjectRoot "retrocore.cmd"
$ShimRoot = Join-Path $env:LOCALAPPDATA "Retrocore\bin"
$ShimPath = Join-Path $ShimRoot "retrocore.cmd"
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host $Message -ForegroundColor Cyan
}
function Ask-YesNo {
param(
[string]$Prompt,
[bool]$Default = $true
)
if ($Yes) {
return $true
}
$suffix = if ($Default) { "[Y/n]" } else { "[y/N]" }
$response = Read-Host "$Prompt $suffix"
if ([string]::IsNullOrWhiteSpace($response)) {
return $Default
}
switch ($response.Trim().ToLowerInvariant()) {
"y" { return $true }
"yes" { return $true }
"n" { return $false }
"no" { return $false }
default {
Write-Host "Please answer y or n." -ForegroundColor Yellow
return Ask-YesNo -Prompt $Prompt -Default $Default
}
}
}
function Get-PythonCommand {
$pyCommand = Get-Command py -ErrorAction SilentlyContinue
if ($pyCommand) {
try {
& py -3 -c "import sys" *> $null
if ($LASTEXITCODE -eq 0) {
return "py -3"
}
}
catch {
}
}
$pythonCommand = Get-Command python -ErrorAction SilentlyContinue
if ($pythonCommand) {
try {
& python -c "import sys" *> $null
if ($LASTEXITCODE -eq 0) {
return "python"
}
}
catch {
}
}
return $null
}
function Show-PythonVersion {
param([string]$PythonCommand)
if ($PythonCommand -eq "py -3") {
& py -3 --version
return
}
& python --version
}
function Test-TkAvailability {
param([string]$PythonCommand)
if ($PythonCommand -eq "py -3") {
& py -3 -c "import tkinter" *> $null
return ($LASTEXITCODE -eq 0)
}
& python -c "import tkinter" *> $null
return ($LASTEXITCODE -eq 0)
}
function Install-Python {
$winget = Get-Command winget -ErrorAction SilentlyContinue
if (-not $winget) {
throw "Python was not found and winget is unavailable. Install Python 3 manually, then rerun this script."
}
Write-Host "Installing Python 3.11 with winget..." -ForegroundColor Cyan
& winget install --exact --id Python.Python.3.11 --scope user --accept-package-agreements --accept-source-agreements
if ($LASTEXITCODE -ne 0) {
throw "winget failed to install Python."
}
}
function Ensure-PathContains {
param([string]$Directory)
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$parts = @()
if ($userPath) {
$parts = $userPath -split ";" | Where-Object { $_ }
}
$alreadyPresent = $false
foreach ($part in $parts) {
if ($part.Trim().TrimEnd("\") -ieq $Directory.Trim().TrimEnd("\")) {
$alreadyPresent = $true
break
}
}
if (-not $alreadyPresent) {
$newParts = @($parts + $Directory)
[Environment]::SetEnvironmentVariable("Path", ($newParts -join ";"), "User")
if (-not (($env:Path -split ";") | Where-Object { $_.Trim().TrimEnd("\") -ieq $Directory.Trim().TrimEnd("\") })) {
$env:Path = "$Directory;$env:Path"
}
Write-Host "Added to user PATH: $Directory" -ForegroundColor Green
}
else {
Write-Host "User PATH already contains: $Directory" -ForegroundColor DarkGray
}
}
function Write-RetrocoreShim {
param(
[Parameter(Mandatory = $true)][string]$DestinationPath
)
$destinationDir = Split-Path -Parent $DestinationPath
New-Item -ItemType Directory -Force -Path $destinationDir | Out-Null
$shimContent = @"
@echo off
setlocal
call "$ProjectCommand" %*
"@
Set-Content -LiteralPath $DestinationPath -Value $shimContent -Encoding Ascii
Write-Host "Created shim: $DestinationPath" -ForegroundColor Green
}
function Update-LegacyShimIfNeeded {
$pathEntries = @()
if ($env:Path) {
$pathEntries += ($env:Path -split ";" | Where-Object { $_ })
}
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath) {
$pathEntries += ($userPath -split ";" | Where-Object { $_ })
}
$seen = @{}
foreach ($entry in $pathEntries) {
$normalized = $entry.Trim().TrimEnd("\")
if (-not $normalized) {
continue
}
if ($seen.ContainsKey($normalized.ToLowerInvariant())) {
continue
}
$seen[$normalized.ToLowerInvariant()] = $true
if (-not ($normalized -like "$env:USERPROFILE\\*")) {
continue
}
$candidate = Join-Path $normalized "retrocore.cmd"
if (-not (Test-Path -LiteralPath $candidate)) {
continue
}
if ($candidate -ieq $ShimPath) {
continue
}
Write-Host "Updating existing user shim that could shadow the new install: $candidate" -ForegroundColor Yellow
Write-RetrocoreShim -DestinationPath $candidate
}
}
Write-Host "Retrocore guided install" -ForegroundColor Green
Write-Host "This script will check Python, create a retrocore command shim, and optionally add it to your user PATH." -ForegroundColor DarkGray
Write-Host "Project folder: $ProjectRoot" -ForegroundColor DarkGray
Write-Host "If you move this project later, rerun this installer so the shim points at the new location." -ForegroundColor DarkGray
if (-not (Ask-YesNo -Prompt "Continue with setup?" -Default $true)) {
Write-Host "Setup cancelled." -ForegroundColor Yellow
exit 0
}
Write-Step "Checking Python"
$pythonCommand = Get-PythonCommand
if (-not $pythonCommand) {
Write-Host "Python 3 was not found." -ForegroundColor Yellow
if (-not (Ask-YesNo -Prompt "Install Python 3.11 with winget now?" -Default $true)) {
throw "Python is required for Retrocore."
}
Install-Python
$pythonCommand = Get-PythonCommand
if (-not $pythonCommand) {
throw "Python still was not detected after installation."
}
}
Write-Host "Python detected via: $pythonCommand" -ForegroundColor Green
Show-PythonVersion -PythonCommand $pythonCommand
if (Test-TkAvailability -PythonCommand $pythonCommand) {
Write-Host "Tkinter support detected. Retrocore Manager UI is available." -ForegroundColor Green
}
else {
Write-Host "Tkinter was not detected in this Python install. CLI commands will work, but 'retrocore manager' will not launch." -ForegroundColor Yellow
}
if (Ask-YesNo -Prompt "Create a global 'retrocore' command shim?" -Default $true) {
Write-Step "Creating retrocore command shim"
Write-RetrocoreShim -DestinationPath $ShimPath
Update-LegacyShimIfNeeded
if (Ask-YesNo -Prompt "Add the shim directory to your user PATH?" -Default $true) {
Ensure-PathContains -Directory $ShimRoot
}
else {
Write-Host "Skipped PATH update. You can still run the shim directly at $ShimPath" -ForegroundColor Yellow
}
}
if (Ask-YesNo -Prompt "Run a quick Retrocore status check now?" -Default $true) {
Write-Step "Running verification"
& $ProjectCommand status
}
Write-Host ""
Write-Host "Retrocore install setup is complete." -ForegroundColor Green
Write-Host "Open a new terminal window if PATH was changed during this run." -ForegroundColor DarkGray