# Build the ESP32-S3 firmware from PowerShell without executing .env as code. [CmdletBinding()] param( [string]$EnvFile = '.env', [Parameter(ValueFromRemainingArguments = $true)] [string[]]$CargoArgs = @() ) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest Push-Location (Split-Path -Parent $PSScriptRoot) $buildEnvironment = @{} foreach ($name in @('CARGO_WORKSPACE_DIR', 'ESP_IDF_GLOB_BASE', 'ESP_IDF_GLOB_PARTITIONS')) { $buildEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, 'Process') } try { if (Test-Path -LiteralPath $EnvFile) { $lineNumber = 0 foreach ($line in Get-Content -LiteralPath $EnvFile -Encoding UTF8) { $lineNumber++ if ($line -match '^\s*(#|$)') { continue } if ($line -notmatch '^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)$') { throw "Invalid assignment in ${EnvFile}:${lineNumber}" } $key = $Matches[1] $value = $Matches[2].Trim() if ($value.StartsWith("'") -or $value.StartsWith('"')) { $quote = $value.Substring(0, 1) if ($value.Length -lt 2 -or -not $value.EndsWith($quote)) { throw "Unclosed quote in ${EnvFile}:${lineNumber}" } $value = $value.Substring(1, $value.Length - 2) } else { $value = ($value -replace '\s+#.*$', '').TrimEnd() } [Environment]::SetEnvironmentVariable($key, $value, 'Process') } } if ([string]::IsNullOrWhiteSpace($env:WIFI_SSID) -or $env:WIFI_SSID -eq 'your-ssid') { throw 'Set WIFI_SSID in .env (copy .env.example) or the environment before building.' } if ($null -eq $env:WIFI_PASS) { $env:WIFI_PASS = '' } $env:CARGO_WORKSPACE_DIR = (Get-Location).Path $env:ESP_IDF_GLOB_BASE = (Get-Location).Path $env:ESP_IDF_GLOB_PARTITIONS = 'partitions.csv' & cargo build --release --locked @CargoArgs if ($LASTEXITCODE -ne 0) { throw "cargo build failed (exit $LASTEXITCODE)." } } finally { foreach ($name in $buildEnvironment.Keys) { [Environment]::SetEnvironmentVariable($name, $buildEnvironment[$name], 'Process') } Pop-Location }