1+ # Define color codes for PowerShell
2+ $Script :Green = [System.ConsoleColor ]::Green
3+ $Script :DefaultColor = [System.ConsoleColor ]::White
4+
5+ # Get the project root directory
6+ function Get-ProjectRoot {
7+ return (Get-Item $PSScriptRoot ).Parent.FullName
8+ }
9+
10+ # Create and activate virtual environment
11+ function Initialize-VirtualEnvironment {
12+ param (
13+ [string ]$ProjectRoot
14+ )
15+
16+ $VenvPath = Join-Path $ProjectRoot " venv"
17+
18+ # Create virtual environment if it doesn't exist
19+ if (-not (Test-Path $VenvPath )) {
20+ Write-Host " Creating virtual environment..."
21+ python - m venv $VenvPath
22+ }
23+
24+ # Activate virtual environment
25+ try {
26+ if ($IsWindows ) {
27+ & " $VenvPath /Scripts/Activate.ps1"
28+ } else {
29+ & bash - c " source '$VenvPath /bin/activate'"
30+ }
31+ Write-Host " Virtual environment activated"
32+ return $true
33+ } catch {
34+ Write-Warning " Failed to activate virtual environment: $_ "
35+ return $false
36+ }
37+ }
38+
39+ # Install Python dependencies
40+ function Install-PythonDependencies {
41+ param (
42+ [string ]$ProjectRoot
43+ )
44+
45+ $RequirementsPath = Join-Path $ProjectRoot " server/requirements.txt"
46+
47+ if (Test-Path $RequirementsPath ) {
48+ Write-Host " Installing Python dependencies..."
49+ pip install - r $RequirementsPath
50+ return $LASTEXITCODE -eq 0
51+ } else {
52+ Write-Warning " Requirements file not found at $RequirementsPath ; skipping pip install."
53+ return $false
54+ }
55+ }
56+
57+ # Run Python script safely
58+ function Invoke-PythonScript {
59+ param (
60+ [string ]$ScriptPath ,
61+ [string ]$WorkingDirectory = $null
62+ )
63+
64+ if (Test-Path $ScriptPath ) {
65+ Write-Host " Running Python script: $ScriptPath "
66+ if ($WorkingDirectory ) {
67+ $oldLocation = Get-Location
68+ Set-Location $WorkingDirectory
69+ }
70+
71+ try {
72+ & python $ScriptPath
73+ return $LASTEXITCODE -eq 0
74+ } finally {
75+ if ($WorkingDirectory ) {
76+ Set-Location $oldLocation
77+ }
78+ }
79+ } else {
80+ Write-Warning " Python script not found at $ScriptPath ; skipping execution."
81+ return $false
82+ }
83+ }
84+
85+ # Setup Flask environment variables
86+ function Set-FlaskEnvironment {
87+ $env: FLASK_DEBUG = 1
88+ $env: FLASK_PORT = 5100
89+ }
90+
91+ # Start a process with proper error handling
92+ function Start-ManagedProcess {
93+ param (
94+ [string ]$FilePath ,
95+ [string ]$WorkingDirectory ,
96+ [string []]$ArgumentList ,
97+ [string ]$ProcessName
98+ )
99+
100+ try {
101+ Write-Host " Starting $ProcessName ..."
102+ $process = Start-Process $FilePath `
103+ - WorkingDirectory $WorkingDirectory `
104+ - ArgumentList $ArgumentList `
105+ - PassThru `
106+ - NoNewWindow
107+
108+ return $process
109+ } catch {
110+ Write-Warning " Failed to start $ProcessName : $_ "
111+ return $null
112+ }
113+ }
114+
115+ # Cleanup function for process management
116+ function Stop-ManagedProcesses {
117+ param (
118+ [System.Diagnostics.Process []]$Processes ,
119+ [string ]$InitialDirectory
120+ )
121+
122+ Write-Host " Shutting down servers..."
123+
124+ foreach ($process in $Processes ) {
125+ if ($process -and -not $process.HasExited ) {
126+ try {
127+ Stop-Process - Id $process.Id - Force - ErrorAction SilentlyContinue
128+ } catch {
129+ Write-Warning " Failed to stop process $ ( $process.Id ) : $_ "
130+ }
131+ }
132+ }
133+
134+ # Deactivate virtual environment if it exists
135+ if (Test-Path Function:\deactivate) {
136+ deactivate
137+ }
138+
139+ # Return to initial directory
140+ if ($InitialDirectory ) {
141+ Set-Location $InitialDirectory
142+ }
143+ }
144+
145+ # Print colored success message
146+ function Write-Success {
147+ param ([string ]$Message )
148+ Write-Host $Message - ForegroundColor $Script :Green
149+ }
150+
151+ # Navigate to project root if in scripts directory
152+ function Set-ProjectRoot {
153+ if ((Split-Path - Path (Get-Location ) - Leaf) -eq " scripts" ) {
154+ Set-Location ..
155+ }
156+ }
157+
158+ # Get appropriate NPM command based on OS
159+ function Get-NpmCommand {
160+ if ($IsWindows ) {
161+ return " npm.cmd"
162+ } else {
163+ return " npm"
164+ }
165+ }
166+
167+ # Install Node.js dependencies
168+ function Install-NodeDependencies {
169+ param (
170+ [string ]$Directory
171+ )
172+
173+ $oldLocation = Get-Location
174+ try {
175+ Set-Location $Directory - ErrorAction Stop
176+ Write-Host " Installing Node.js dependencies in $Directory ..."
177+ npm install
178+ return $LASTEXITCODE -eq 0
179+ } catch {
180+ Write-Warning " Failed to install Node.js dependencies: $_ "
181+ return $false
182+ } finally {
183+ Set-Location $oldLocation
184+ }
185+ }
0 commit comments