-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
536 lines (458 loc) · 20.3 KB
/
Copy pathsetup.ps1
File metadata and controls
536 lines (458 loc) · 20.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# =============================================================================
# Gelegram - Onboarding Setup Script
# =============================================================================
# This script automates the full installation pipeline for Gelegram:
# 1. Checks/installs Node.js (via winget, or MSI fallback)
# 2. Checks/installs uv (Python package manager)
# 3. Checks/installs Gemini CLI (npm global)
# 4. Creates Python venv and installs dependencies
# 5. Configures .env (bot token, password, workspace path, gemini CLI path)
# 6. Optionally installs the Windows service (default: Yes)
# 7. Runs gemini auth for Google OAuth (final step)
#
# Usage:
# powershell -ExecutionPolicy Bypass -File setup.ps1
#
# The script is idempotent -- safe to re-run. Already-completed steps are
# skipped with a green [OK] message.
# =============================================================================
$ErrorActionPreference = "Stop"
# -----------------------------------------------------------------------------
# Resolve project root from the script's own location
# -----------------------------------------------------------------------------
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
# -----------------------------------------------------------------------------
# Helper Functions -- consistent with install_service.ps1 colour scheme
# -----------------------------------------------------------------------------
function Write-Step {
param([string]$Message)
Write-Host ""
Write-Host " >> $Message" -ForegroundColor Cyan
Write-Host " $('-' * 60)" -ForegroundColor DarkGray
}
function Write-Ok {
param([string]$Message)
Write-Host " [OK] $Message" -ForegroundColor Green
}
function Write-Notice {
param([string]$Message)
Write-Host " [i] $Message" -ForegroundColor Yellow
}
function Write-Err {
param([string]$Message)
Write-Host " [X] $Message" -ForegroundColor Red
}
function Write-Info {
param([string]$Message)
Write-Host " $Message" -ForegroundColor White
}
# -----------------------------------------------------------------------------
# Utility: Refresh PATH from registry so newly installed tools are visible
# in the current session without restarting the terminal.
# -----------------------------------------------------------------------------
function Refresh-Path {
$machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine")
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$env:Path = "$machinePath;$userPath"
}
# -----------------------------------------------------------------------------
# Utility: Test if a command exists on the current PATH
# -----------------------------------------------------------------------------
function Test-Command {
param([string]$Name)
try {
$null = Get-Command $Name -ErrorAction Stop
return $true
} catch {
return $false
}
}
# =============================================================================
# BANNER
# =============================================================================
Write-Host ""
Write-Host " ========================================================" -ForegroundColor Magenta
Write-Host " Gelegram - Onboarding Setup" -ForegroundColor White
Write-Host " ========================================================" -ForegroundColor Magenta
Write-Host ""
# =============================================================================
# PHASE 1: Pre-flight & Detection
# =============================================================================
Write-Step "Detecting environment"
$CurrentUser = $env:USERNAME
$CurrentDomain = $env:USERDOMAIN
$UserProfile = $env:USERPROFILE
$GeminiDir = Join-Path $UserProfile ".gemini"
Write-Info "User : $CurrentDomain\$CurrentUser"
Write-Info "Profile : $UserProfile"
Write-Info "Project : $ScriptDir"
if (Test-Path $GeminiDir) {
Write-Ok ".gemini directory found: $GeminiDir"
} else {
Write-Notice ".gemini directory not found -- will be created during gemini auth"
}
# =============================================================================
# PHASE 2: Dependency Installation
# =============================================================================
# -- 2.1 Node.js --------------------------------------------------------------
Write-Step "Checking Node.js"
if (Test-Command "node") {
$nodeVersion = & node --version 2>$null
Write-Ok "Node.js already installed: $nodeVersion"
} else {
Write-Notice "Node.js not found. Installing..."
$wingetAvailable = Test-Command "winget"
if ($wingetAvailable) {
# -- winget is available -- use it directly
Write-Notice "Installing Node.js LTS via winget..."
try {
& winget install OpenJS.NodeJS.LTS --silent --accept-source-agreements --accept-package-agreements
Refresh-Path
if (Test-Command "node") {
$nodeVersion = & node --version 2>$null
Write-Ok "Node.js installed successfully: $nodeVersion"
} else {
Write-Err "Node.js was installed but 'node' is not on PATH yet."
Write-Notice "You may need to restart your terminal after setup completes."
}
} catch {
Write-Err "winget install failed: $_"
Write-Notice "Falling back to MSI download..."
$wingetAvailable = $false
}
}
if (-not $wingetAvailable) {
# -- winget not available -- ask user
Write-Notice "winget is not available on this system."
$installWinget = Read-Host " Install winget first? (Y/n)"
if ($installWinget -eq "" -or $installWinget -match "^[Yy]") {
# Download and install winget (App Installer from Microsoft)
Write-Notice "Downloading winget (App Installer)..."
$wingetUrl = "https://aka.ms/getwinget"
$wingetMsix = Join-Path $ScriptDir "tools\Microsoft.DesktopAppInstaller.msixbundle"
New-Item -ItemType Directory -Path (Join-Path $ScriptDir "tools") -Force | Out-Null
try {
Invoke-WebRequest -Uri $wingetUrl -OutFile $wingetMsix -UseBasicParsing
Write-Notice "Installing winget..."
Add-AppxPackage -Path $wingetMsix
Remove-Item $wingetMsix -Force -ErrorAction SilentlyContinue
Refresh-Path
if (Test-Command "winget") {
Write-Ok "winget installed successfully."
Write-Notice "Installing Node.js LTS via winget..."
& winget install OpenJS.NodeJS.LTS --silent --accept-source-agreements --accept-package-agreements
Refresh-Path
} else {
Write-Err "winget installed but not detected. Falling back to MSI."
}
} catch {
Write-Err "Failed to install winget: $_"
Write-Notice "Falling back to Node.js MSI download..."
}
}
# -- MSI fallback (if winget didn't work or user declined)
if (-not (Test-Command "node")) {
Write-Notice "Downloading Node.js LTS installer from nodejs.org..."
$nodeUrl = "https://nodejs.org/dist/v22.15.0/node-v22.15.0-x64.msi"
$nodeMsi = Join-Path $ScriptDir "tools\node_installer.msi"
New-Item -ItemType Directory -Path (Join-Path $ScriptDir "tools") -Force | Out-Null
try {
Invoke-WebRequest -Uri $nodeUrl -OutFile $nodeMsi -UseBasicParsing
Write-Notice "Running Node.js installer (silent mode)..."
Start-Process msiexec.exe -ArgumentList "/i `"$nodeMsi`" /qn /norestart" -Wait -NoNewWindow
Remove-Item $nodeMsi -Force -ErrorAction SilentlyContinue
Refresh-Path
if (Test-Command "node") {
$nodeVersion = & node --version 2>$null
Write-Ok "Node.js installed via MSI: $nodeVersion"
} else {
Write-Err "Node.js MSI installed but 'node' not on PATH."
Write-Notice "You may need to restart your terminal."
}
} catch {
Write-Err "Failed to download/install Node.js: $_"
Write-Err "Please install Node.js manually from https://nodejs.org"
Read-Host "Press Enter to exit"
exit 1
}
}
}
}
# -- 2.2 uv -------------------------------------------------------------------
Write-Step "Checking uv (Python package manager)"
if (Test-Command "uv") {
$uvVersion = & uv --version 2>$null
Write-Ok "uv already installed: $uvVersion"
} else {
Write-Notice "uv not found. Installing..."
try {
# Official uv installer for Windows
Invoke-RestMethod https://astral.sh/uv/install.ps1 | Invoke-Expression
Refresh-Path
if (Test-Command "uv") {
$uvVersion = & uv --version 2>$null
Write-Ok "uv installed successfully: $uvVersion"
} else {
# uv installs to ~/.local/bin or CARGO_HOME -- add common location
$uvLocalBin = Join-Path $UserProfile ".local\bin"
if (Test-Path (Join-Path $uvLocalBin "uv.exe")) {
$env:Path = "$uvLocalBin;$env:Path"
Write-Ok "uv installed at $uvLocalBin"
} else {
$uvCargoBin = Join-Path $UserProfile ".cargo\bin"
if (Test-Path (Join-Path $uvCargoBin "uv.exe")) {
$env:Path = "$uvCargoBin;$env:Path"
Write-Ok "uv installed at $uvCargoBin"
} else {
Write-Err "uv was installed but not found on PATH."
Write-Notice "You may need to restart your terminal."
}
}
}
} catch {
Write-Err "Failed to install uv: $_"
Write-Notice "Install manually: https://github.com/astral-sh/uv"
Read-Host "Press Enter to exit"
exit 1
}
}
# -- 2.3 Gemini CLI ------------------------------------------------------------
Write-Step "Checking Gemini CLI"
# Check if gemini CLI is installed via npm global list
$geminiInstalled = $false
try {
$npmListOutput = & npm list -g @google/gemini-cli 2>$null
if ($npmListOutput -match "gemini-cli") {
$geminiInstalled = $true
}
} catch {}
# Also try the command directly (handles manual installs)
if (-not $geminiInstalled -and (Test-Command "gemini")) {
$geminiInstalled = $true
}
if ($geminiInstalled) {
Write-Ok "Gemini CLI already installed."
} else {
Write-Notice "Gemini CLI not found. Installing via npm..."
try {
& npm install -g @google/gemini-cli
Refresh-Path
Write-Ok "Gemini CLI installed."
} catch {
Write-Err "Failed to install Gemini CLI: $_"
Write-Notice "Install manually: npm install -g @google/gemini-cli"
Read-Host "Press Enter to exit"
exit 1
}
}
# =============================================================================
# PHASE 3: Python Environment
# =============================================================================
Write-Step "Setting up Python virtual environment"
$VenvPython = Join-Path $ScriptDir ".venv\Scripts\python.exe"
if (Test-Path $VenvPython) {
Write-Ok "Virtual environment already exists: .venv"
} else {
Write-Notice "Creating virtual environment with uv..."
try {
Push-Location $ScriptDir
& uv venv .venv
Pop-Location
Write-Ok "Virtual environment created."
} catch {
Write-Err "Failed to create venv: $_"
Read-Host "Press Enter to exit"
exit 1
}
}
# -- Install Python dependencies -----------------------------------------------
Write-Step "Installing Python dependencies"
$RequirementsFile = Join-Path $ScriptDir "requirements.txt"
if (-not (Test-Path $RequirementsFile)) {
Write-Err "requirements.txt not found at: $RequirementsFile"
Read-Host "Press Enter to exit"
exit 1
}
try {
Write-Notice "Running: uv pip install -r requirements.txt"
Push-Location $ScriptDir
& uv pip install -r requirements.txt --python "$VenvPython"
Pop-Location
Write-Ok "Dependencies installed."
} catch {
Write-Err "Failed to install dependencies: $_"
Read-Host "Press Enter to exit"
exit 1
}
# =============================================================================
# PHASE 4: Configuration (.env)
# =============================================================================
Write-Step "Configuring environment (.env)"
$EnvFile = Join-Path $ScriptDir ".env"
$EnvExample = Join-Path $ScriptDir ".env.example"
if (Test-Path $EnvFile) {
Write-Ok ".env file already exists."
Write-Notice "Skipping configuration prompts (edit .env manually if needed)."
} else {
if (-not (Test-Path $EnvExample)) {
Write-Err ".env.example template not found!"
Read-Host "Press Enter to exit"
exit 1
}
# -- Prompt: Telegram Bot Token
Write-Host ""
Write-Host " Please provide the following configuration values." -ForegroundColor White
Write-Host " (Get a bot token from @BotFather on Telegram)" -ForegroundColor DarkGray
Write-Host ""
$botToken = Read-Host " Enter your Telegram Bot Token"
if (-not $botToken) {
Write-Notice "No token entered -- you will need to edit .env manually."
}
# -- Prompt: Bot Password
$botPassword = Read-Host " Set a Telegram password (or press Enter to skip)"
# -- Prompt: Workspace Location
$defaultWorkspace = ".\workdir"
$workspace = Read-Host " Workspace directory (default: $defaultWorkspace)"
if (-not $workspace) {
$workspace = $defaultWorkspace
}
# -- Detect Gemini CLI Path
# Try where.exe first (works if gemini was already on PATH before this session).
# Fall back to the standard npm global install location on Windows.
$geminiCliPath = $null
try {
$whereResult = & where.exe gemini 2>$null | Select-Object -First 1
if ($whereResult -and (Test-Path $whereResult)) {
$geminiCliPath = $whereResult
Write-Ok "Gemini CLI found via where: $geminiCliPath"
}
} catch {}
if (-not $geminiCliPath) {
# Fallback: standard npm global install path on Windows
$defaultGeminiPath = Join-Path $env:APPDATA "npm\gemini.cmd"
if (Test-Path $defaultGeminiPath) {
$geminiCliPath = $defaultGeminiPath
Write-Ok "Gemini CLI found at default location: $geminiCliPath"
} else {
Write-Notice "Gemini CLI path could not be auto-detected."
Write-Notice "Setting to 'gemini' (assumes it will be on PATH)."
$geminiCliPath = "gemini"
}
}
# BUG FIX: Read template and write with prefix matching instead of regex replace to prevent backslash/regex escapes failures
$lines = Get-Content $EnvExample
$newLines = foreach ($line in $lines) {
if ($line -like "TELEGRAM_BOT_TOKEN=*") {
if ($botToken) { "TELEGRAM_BOT_TOKEN=$botToken" } else { $line }
}
elseif ($line -like "BOT_PASSWORD=*") {
if ($botPassword) { "BOT_PASSWORD=$botPassword" } else { $line }
}
elseif ($line -like "GEMINI_WORKING_DIR=*") {
"GEMINI_WORKING_DIR=$workspace"
}
elseif ($line -like "GEMINI_CLI_PATH=*") {
"GEMINI_CLI_PATH=$geminiCliPath"
}
else {
$line
}
}
$newLines | Set-Content -Path $EnvFile -Encoding UTF8
Write-Ok ".env file created successfully."
}
# =============================================================================
# PHASE 5: Service Installation
# =============================================================================
Write-Step "Windows Service Installation"
Write-Host ""
Write-Host " Install Gelegram as a Windows background service?" -ForegroundColor White
Write-Host " This makes the bot start automatically on boot and" -ForegroundColor White
Write-Host " survive crashes with automatic restarts." -ForegroundColor White
Write-Host ""
$installService = Read-Host " Install as service? [Y/n] (default: Y)"
if ($installService -eq "" -or $installService -match "^[Yy]") {
Write-Notice "Launching service installer (requesting Administrator elevation)..."
$installScript = Join-Path $ScriptDir "install_service.ps1"
if (Test-Path $installScript) {
try {
# BUG FIX: Wait for the elevated service installer to finish before resuming setup.ps1
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$installScript`"" -Verb RunAs -Wait
} catch {
Write-Err "Service installation failed: $_"
Write-Notice "You can run install_service.ps1 manually later."
}
} else {
Write-Err "install_service.ps1 not found at: $installScript"
}
} else {
Write-Ok "Skipping service installation."
Write-Host ""
Write-Host " To run the bot manually:" -ForegroundColor White
Write-Host " .venv\Scripts\activate" -ForegroundColor DarkGray
Write-Host " python gateway.py # with watchdog (recommended)" -ForegroundColor DarkGray
Write-Host " python bot.py # without watchdog" -ForegroundColor DarkGray
Write-Host ""
Write-Host " To install as a service later:" -ForegroundColor White
Write-Host " powershell -File install_service.ps1" -ForegroundColor DarkGray
Write-Host ""
}
# =============================================================================
# PHASE 6: Gemini Auth (final step)
# =============================================================================
Write-Step "Gemini CLI Authentication"
Write-Host ""
Write-Host " Gemini CLI needs Google OAuth to function." -ForegroundColor White
Write-Host " A browser window will open -- log in with your Google account." -ForegroundColor White
Write-Host " Close the gemini session after authentication completes." -ForegroundColor DarkGray
Write-Host ""
# Resolve gemini CLI path for auth (same logic as .env detection)
$geminiAuthCmd = $null
try {
$whereResult = & where.exe gemini 2>$null | Select-Object -First 1
if ($whereResult -and (Test-Path $whereResult)) {
$geminiAuthCmd = $whereResult
}
} catch {}
if (-not $geminiAuthCmd) {
$defaultGeminiPath = Join-Path $env:APPDATA "npm\gemini.cmd"
if (Test-Path $defaultGeminiPath) {
$geminiAuthCmd = $defaultGeminiPath
}
}
if ($geminiAuthCmd) {
try {
Write-Notice "Running: gemini auth"
# Run gemini auth -- this opens a browser for Google OAuth
& cmd /c "`"$geminiAuthCmd`"" auth
Write-Ok "Gemini authentication completed."
} catch {
Write-Notice "gemini auth exited (this may be normal): $_"
}
} else {
Write-Notice "Could not locate gemini CLI to run auth."
Write-Notice "After setup, run 'gemini auth' manually to authenticate."
}
Write-Host ""
Write-Host " REMINDER: Make sure you logged in with your Google account!" -ForegroundColor Yellow
Write-Host " If the browser didn't open, run 'gemini auth' manually." -ForegroundColor Yellow
Write-Host ""
# =============================================================================
# SUMMARY
# =============================================================================
Write-Host ""
Write-Host " ========================================================" -ForegroundColor Magenta
Write-Host " Gelegram Setup Complete!" -ForegroundColor Green
Write-Host " ========================================================" -ForegroundColor Magenta
Write-Host ""
Write-Host " User : $CurrentDomain\$CurrentUser" -ForegroundColor White
Write-Host " Profile : $UserProfile" -ForegroundColor White
Write-Host " Project : $ScriptDir" -ForegroundColor White
Write-Host " .gemini : $GeminiDir" -ForegroundColor White
Write-Host ""
Write-Host " IMPORTANT REMINDERS:" -ForegroundColor Yellow
Write-Host " 1. If gemini auth didn't complete, run: gemini auth" -ForegroundColor Yellow
Write-Host " 2. Message your bot on Telegram -- it will guide you" -ForegroundColor Yellow
Write-Host " through identity setup on the first message." -ForegroundColor Yellow
Write-Host ""
Read-Host "Press Enter to close"