-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-start.ps1
More file actions
333 lines (277 loc) · 12.1 KB
/
quick-start.ps1
File metadata and controls
333 lines (277 loc) · 12.1 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
# Helpful Tools v2 - PowerShell Quick Start Script
# Usage: .\quick-start.ps1 {start|stop|restart|status|logs|test|install|help} [port]
param (
[Parameter(Position = 0)]
[string]$Command = "start",
[Parameter(Position = 1)]
[string]$Port = "8000",
[switch]$ForceDeps
)
# --- Configuration ---
$VenvDir = "venv"
$ProjectDir = $PSScriptRoot
$ConfigDir = Join-Path $HOME ".config\helpful-tools"
$PidFile = Join-Path $ConfigDir "helpful-tools-v2.pid"
$LogFile = Join-Path $ConfigDir "helpful-tools-v2.log"
$PortFile = Join-Path $ConfigDir ".port"
# --- Functions ---
# Function to ensure config directory exists
function Ensure-ConfigDir {
if (-not (Test-Path $ConfigDir)) {
Write-Host "Creating config directory: $ConfigDir" -ForegroundColor Yellow
New-Item -Path $ConfigDir -ItemType Directory -Force | Out-Null
Write-Host "Config directory created successfully" -ForegroundColor Green
}
}
# Function to check if the process is running
function Is-Running {
if (Test-Path $PidFile) {
$processId = Get-Content $PidFile
if (Get-Process -Id $processId -ErrorAction SilentlyContinue) {
return $true
} else {
Remove-Item $PidFile -Force
return $false
}
}
return $false
}
# Function to set up virtual environment and install dependencies
function Setup-Venv {
param([bool]$ForceCheck = $false)
$VenvPath = Join-Path $ProjectDir $VenvDir
# Create venv if it doesn't exist
if (-not (Test-Path $VenvPath)) {
Write-Host "Creating virtual environment..." -ForegroundColor Yellow
$OriginalLocation = Get-Location
Set-Location $ProjectDir
python -m venv $VenvDir
Set-Location $OriginalLocation
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to create virtual environment" -ForegroundColor Red
exit 1
}
Write-Host "Virtual environment created" -ForegroundColor Green
}
Write-Host "Installing/updating dependencies..." -ForegroundColor Yellow
$PipPath = Join-Path $VenvPath "Scripts\pip.exe"
if (-not (Test-Path $PipPath)) {
Write-Host "pip.exe not found at $PipPath" -ForegroundColor Red
exit 1
}
$OriginalLocation = Get-Location
Set-Location $ProjectDir
& $PipPath install --quiet --upgrade pip 2>&1 | Out-Null
& $PipPath install --quiet -r "requirements.txt" 2>&1 | Out-Null
Set-Location $OriginalLocation
if ($LASTEXITCODE -ne 0) {
Write-Host "Failed to install dependencies" -ForegroundColor Red
exit 1
}
Write-Host "Dependencies installed and verified" -ForegroundColor Green
}
# Function to start the application
function Start-App {
param(
[string]$StartPort = "8000"
)
Ensure-ConfigDir
if (Is-Running) {
$processId = Get-Content $PidFile
if (Test-Path $PortFile) { $currentPort = Get-Content $PortFile } else { $currentPort = $StartPort }
Write-Host "Helpful-Tools-v2 is already running (PID: $processId)" -ForegroundColor Yellow
Write-Host "Access at: http://127.0.0.1:$currentPort" -ForegroundColor Blue
return
}
Setup-Venv
Write-Host "Starting Helpful-Tools-v2 on port $StartPort in background..." -ForegroundColor Blue
Set-Content -Path $PortFile -Value $StartPort
$VenvScriptsPath = Join-Path $ProjectDir (Join-Path $VenvDir "Scripts")
$PythonPath = Join-Path $VenvScriptsPath "python.exe"
$AppPath = Join-Path $ProjectDir "app.py"
if (-not (Test-Path $PythonPath)) {
Write-Host "Python not found at $PythonPath" -ForegroundColor Red
exit 1
}
if (-not (Test-Path $AppPath)) {
Write-Host "app.py not found at $AppPath" -ForegroundColor Red
exit 1
}
# Build the command to run Python app
$CommandString = "Set-Location '$ProjectDir'; & '$PythonPath' '$AppPath' --port $StartPort *>> '$LogFile' 2>&1"
# Start the process in a hidden window
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = "powershell.exe"
$ProcessInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Command `"$CommandString`""
$ProcessInfo.UseShellExecute = $false
$ProcessInfo.CreateNoWindow = $true
$ProcessInfo.WorkingDirectory = $ProjectDir
$process = [System.Diagnostics.Process]::Start($ProcessInfo)
# Save the PID of the PowerShell wrapper
# Note: The actual Python process will be a child of this
Set-Content -Path $PidFile -Value $process.Id
Start-Sleep -Seconds 3
# Check if the Python process is actually running
$pythonProcesses = Get-Process -Name python -ErrorAction SilentlyContinue | Where-Object {
try {
$_.Path -and $_.CommandLine -like "*app.py*$StartPort*"
} catch {
$false
}
}
if ($pythonProcesses -or (Is-Running)) {
Write-Host "Helpful-Tools-v2 started successfully (PID: $($process.Id))" -ForegroundColor Green
Write-Host "Access at: http://127.0.0.1:$StartPort" -ForegroundColor Blue
Write-Host "Dashboard ready with JSON formatter, text diff, regex tester, and more!" -ForegroundColor Blue
Write-Host "Logs: $LogFile" -ForegroundColor Yellow
Write-Host "Port: $PortFile" -ForegroundColor Yellow
Write-Host "Config: $ConfigDir" -ForegroundColor Blue
} else {
Write-Host "Failed to start Helpful-Tools-v2" -ForegroundColor Red
Write-Host "Check logs: $LogFile" -ForegroundColor Yellow
if(Test-Path $PidFile) { Remove-Item $PidFile -Force }
if(Test-Path $PortFile) { Remove-Item $PortFile -Force }
}
}
# Function to stop the application
function Stop-App {
if (-not (Is-Running)) {
Write-Host "Helpful-Tools-v2 is not running" -ForegroundColor Yellow
return
}
$processId = Get-Content $PidFile
Write-Host "Stopping Helpful-Tools-v2 (PID: $processId)..." -ForegroundColor Yellow
# Stop the PowerShell wrapper process and any child Python processes
try {
# First, try to stop all Python processes running app.py
$pythonProcesses = Get-Process -Name python -ErrorAction SilentlyContinue | Where-Object {
try {
$_.Path -and $_.CommandLine -like "*app.py*"
} catch {
$false
}
}
foreach ($proc in $pythonProcesses) {
Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue
}
# Then stop the wrapper PowerShell process
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
if (-not (Get-Process -Id $processId -ErrorAction SilentlyContinue)) {
Write-Host "Helpful-Tools-v2 stopped successfully" -ForegroundColor Green
} else {
Write-Host "Could not stop process. It may need to be stopped manually." -ForegroundColor Yellow
}
} catch {
Write-Host "Error stopping process: $_" -ForegroundColor Yellow
}
if(Test-Path $PidFile) { Remove-Item $PidFile -Force }
if(Test-Path $PortFile) { Remove-Item $PortFile -Force }
}
# Function to show status
function Show-Status {
if (Is-Running) {
$processId = Get-Content $PidFile
if (Test-Path $PortFile) { $currentPort = Get-Content $PortFile } else { $currentPort = $Port }
Write-Host "Helpful-Tools-v2 is running (PID: $processId)" -ForegroundColor Green
Write-Host "Access at: http://127.0.0.1:$currentPort" -ForegroundColor Blue
Write-Host "Logs: $LogFile" -ForegroundColor Yellow
} else {
Write-Host "Helpful-Tools-v2 is not running" -ForegroundColor Red
}
}
# Function to show logs
function Show-Logs {
if (Test-Path $LogFile) {
Write-Host "Recent logs:" -ForegroundColor Blue
Get-Content $LogFile -Tail 20
} else {
Write-Host "No log file found" -ForegroundColor Red
}
}
# Function to run tests
function Run-Tests {
Write-Host "Running unit and integration tests..." -ForegroundColor Blue
Ensure-ConfigDir
# Check if application is running
if (-not (Is-Running)) {
Write-Host "WARNING: Helpful-Tools-v2 is not running!" -ForegroundColor Red
Write-Host "Some integration tests may fail. Start the application with: .\quick-start.ps1 start" -ForegroundColor Yellow
} else {
$processId = Get-Content $PidFile
$currentPort = "8000"
if (Test-Path $PortFile) {
$currentPort = Get-Content $PortFile
}
Write-Host "Application is running on port $currentPort (PID: $processId)" -ForegroundColor Green
# Set environment variables for tests
$env:HELPFUL_TOOLS_PORT = $currentPort
$env:HELPFUL_TOOLS_CONFIG_DIR = $ConfigDir
}
Setup-Venv $true # Force dependency check for testing
# Backend tests
Write-Host "Running Backend Tests (Pytest)..." -ForegroundColor Yellow
$VenvScriptsPath = Join-Path $ProjectDir (Join-Path $VenvDir "Scripts")
$PytestPath = Join-Path $VenvScriptsPath "pytest.exe"
$TestsPath = Join-Path $ProjectDir "tests"
$OriginalLocation = Get-Location
Set-Location $ProjectDir
& $PytestPath $TestsPath -v --tb=short
Set-Location $OriginalLocation
Write-Host "Tests completed" -ForegroundColor Green
}
# Function to install/update dependencies only
function Install-Dependencies {
Write-Host "Installing/updating all dependencies..." -ForegroundColor Blue
Setup-Venv $true # Force dependency check
Write-Host "Dependencies installation completed" -ForegroundColor Green
}
# Function to show help
function Show-Help {
Write-Host "Helpful Tools v2 - PowerShell Quick Start" -ForegroundColor Blue
Write-Host "Usage: .\quick-start.ps1 [command] [port] [-ForceDeps]"
Write-Host ""
Write-Host "Available commands:" -ForegroundColor Blue
Write-Host " start [port] [-ForceDeps] - Start Helpful-Tools-v2 in background (default port: 8000)" -ForegroundColor Yellow
Write-Host " stop - Stop Helpful-Tools-v2" -ForegroundColor Yellow
Write-Host " restart [port] [-ForceDeps] - Restart Helpful-Tools-v2" -ForegroundColor Yellow
Write-Host " status - Check if Helpful-Tools-v2 is running" -ForegroundColor Yellow
Write-Host " logs - Show recent logs" -ForegroundColor Yellow
Write-Host " test - Run unit and integration tests" -ForegroundColor Yellow
Write-Host " install - Install/update all dependencies" -ForegroundColor Yellow
Write-Host " help - Show this help message" -ForegroundColor Yellow
Write-Host ""
Write-Host "Examples:" -ForegroundColor Blue
Write-Host " .\quick-start.ps1 start # Start on default port (8000)" -ForegroundColor Yellow
Write-Host " .\quick-start.ps1 start 3000 # Start on port 3000" -ForegroundColor Yellow
Write-Host " .\quick-start.ps1 start -ForceDeps # Force dependency check" -ForegroundColor Yellow
Write-Host " .\quick-start.ps1 restart 5000 # Restart on port 5000" -ForegroundColor Yellow
Write-Host " .\quick-start.ps1 test # Run unit tests" -ForegroundColor Yellow
Write-Host ""
Write-Host "Features available:" -ForegroundColor Blue
Write-Host " - JSON Formatter & Validator"
Write-Host " - YAML <-> JSON Converter"
Write-Host " - XML <-> JSON Converter"
Write-Host " - Text Diff Tool"
Write-Host " - Regex Tester"
Write-Host " - Cron Parser"
Write-Host " - JWT Decoder"
Write-Host " - Scientific Calculator"
Write-Host " - Sources Manager"
}
# --- Main script logic ---
switch ($Command.ToLower()) {
"start" { Start-App -StartPort $Port }
"stop" { Stop-App }
"restart" { Stop-App; Start-Sleep -Seconds 1; Start-App -StartPort $Port }
"status" { Show-Status }
"logs" { Show-Logs }
"test" { Run-Tests }
"install" { Install-Dependencies }
"help" { Show-Help }
default {
Write-Host "Unknown command: $Command" -ForegroundColor Red
Write-Host ""
Show-Help
}
}