diff --git a/scripts/setup-environment.ps1 b/scripts/setup-environment.ps1 new file mode 100644 index 0000000..37b6569 --- /dev/null +++ b/scripts/setup-environment.ps1 @@ -0,0 +1,99 @@ +# If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { +# #"No Administrative rights, it will display a popup window asking user for Admin rights" +# $arguments = "-NoProfile -ExecutionPolicy Bypass -Command & '" + $myinvocation.mycommand.definition + "'" +# Start-Process "$psHome\powershell.exe" -Verb runAs -ArgumentList $arguments #-WindowStyle Hidden +# break +# } + +# # Set execution policy remotesigned machine wide +# try { +# Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue +# } +# catch { +# # Suppress +# } + +# # Load Environment Setup Module +# $envSetupModulePath = $(Join-Path $PSScriptRoot "EnvironmentSetup.psm1") +# if (-not (Test-Path $envSetupModulePath)) { +# Write-Warning "Environment Setup Module file '$($envSetupModulePath)' not found!" +# Write-Host +# exit 1 +# } +# Import-Module -Force $envSetupModulePath + +Write-Host "Preparing environment..." -ForegroundColor Cyan + +Write-Host "Preparing settings..." -ForegroundColor Gray + +# Load and validate .env.template (INI-style key=value list) +$parentDir = Split-Path -Parent $PSScriptRoot +$templatePath = Join-Path $parentDir '.env.template' +if (-not (Test-Path $templatePath)) { + Write-Error "Template file '$templatePath' not found." + exit 1 +} + +$templateLines = Get-Content -Raw -Path $templatePath -ErrorAction Stop +$settingNames = @() +foreach ($line in $templateLines -split "`n") { + $l = $line.Trim() + if ($l -eq '' -or $l.StartsWith('#') -or $l.StartsWith(';')) { continue } + if ($l -match '=') { + $parts = $l -split '=', 2 + $name = $parts[0].Trim() + if ($name -ne '') { $settingNames += $name } + } +} +if ($settingNames.Count -eq 0) { + Write-Error "No settings found in template file '$templatePath'." + exit 1 +} + +# Load existing .env if present +$envPath = Join-Path $parentDir '.env' +$envValues = @{} +if (Test-Path $envPath) { + $envLines = Get-Content -Raw -Path $envPath -ErrorAction Stop + foreach ($line in $envLines -split "`n") { + $l = $line.Trim() + if ($l -eq '' -or $l.StartsWith('#') -or $l.StartsWith(';')) { continue } + if ($l -match '=') { + $parts = $l -split '=', 2 + $name = $parts[0].Trim() + $value = $parts[1].Trim().Trim("'").Trim('"') + $envValues[$name] = $value + } + } +} + +# Ensure all template settings are present; prompt user for missing/empty +foreach ($name in $settingNames) { + if (-not $envValues.ContainsKey($name) -or [string]::IsNullOrWhiteSpace($envValues[$name])) { + do { + Write-Host "$name value is missing!" -ForegroundColor Yellow + $input = Read-Host "Enter value for $name" + } while ([string]::IsNullOrWhiteSpace($input)) + $envValues[$name] = $input + } + else { + Write-Host "$name value already set to '$($envValues[$name])'" -ForegroundColor DarkGray + } +} + +# Recreate ../.env with the required settings and values +$out = foreach ($name in $settingNames) { "$name='$($envValues[$name])'" } +$out | Set-Content -Path $envPath -Encoding UTF8 + +Write-Host "Settings preparation complete" -ForegroundColor Gray + +Write-Host "Environment preparation complete!" -ForegroundColor Cyan + +# do { +# Write-Host "To complete the intallations you need to restart your computer" -ForegroundColor Cyan +# Write-Host "do you want to restart? [Y/n]" -ForegroundColor Cyan +# $key = ([console]::ReadKey()).Key.ToString().ToUpper() +# if ($key -eq 'Y') { +# Restart-Computer +# } +# } while ($key -ne 'N' -and $key -ne 'Y') diff --git a/setup-environment.bat b/setup-environment.bat new file mode 100644 index 0000000..c87b998 --- /dev/null +++ b/setup-environment.bat @@ -0,0 +1,12 @@ +@echo off +echo %cmdcmdline% | findstr /i /c:"%~nx0" >NUL && set iscommandline=1 +echo %PSModulePath% | findstr "%USERPROFILE%" >NUL && set ispowershell=1 +cd /D "%~dp0" + +echo. + +@powershell -NoProfile -ExecutionPolicy Bypass -Command .\scripts\setup-environment.ps1 %* + +echo. + +IF DEFINED iscommandline IF NOT DEFINED ispowershell pause