122 lines
5.1 KiB
PowerShell
122 lines
5.1 KiB
PowerShell
$ErrorActionPreference = "Stop"
|
||
|
||
function Set-WasmToolchainEnv {
|
||
param()
|
||
$clang = $env:CC
|
||
if (-not $clang -or -not (Test-Path $clang)) {
|
||
$defaultClang = "C:\\Program Files\\LLVM\\bin\\clang.exe"
|
||
if (Test-Path $defaultClang) {
|
||
$clang = $defaultClang
|
||
} else {
|
||
$cmd = Get-Command clang.exe -ErrorAction SilentlyContinue
|
||
if ($cmd) { $clang = $cmd.Path }
|
||
}
|
||
}
|
||
if (-not $clang) { throw "Clang introuvable. Installez LLVM/Clang et relancez." }
|
||
|
||
$env:CC = $clang
|
||
$llvmBin = Split-Path $clang -Parent
|
||
$env:AR = Join-Path $llvmBin "llvm-ar.exe"
|
||
$env:NM = Join-Path $llvmBin "llvm-nm.exe"
|
||
|
||
$env:TARGET_CC = $env:CC
|
||
$env:CC_wasm32_unknown_unknown = $env:CC
|
||
$env:AR_wasm32_unknown_unknown = $env:AR
|
||
$env:NM_wasm32_unknown_unknown = $env:NM
|
||
[System.Environment]::SetEnvironmentVariable('CC_wasm32-unknown-unknown', $env:CC, 'Process')
|
||
[System.Environment]::SetEnvironmentVariable('AR_wasm32-unknown-unknown', $env:AR, 'Process')
|
||
[System.Environment]::SetEnvironmentVariable('NM_wasm32-unknown-unknown', $env:NM, 'Process')
|
||
}
|
||
|
||
function Invoke-WasmPackTests {
|
||
param(
|
||
[switch]$Chrome,
|
||
[switch]$Firefox,
|
||
[switch]$Node
|
||
)
|
||
if ($Chrome) { wasm-pack test --headless --chrome }
|
||
if ($Firefox) { wasm-pack test --headless --firefox }
|
||
if ($Node) { wasm-pack test --node }
|
||
}
|
||
|
||
$runnerSet = $false
|
||
function Ensure-WasmBindgenRunner {
|
||
param()
|
||
# Cherche un runner dans le cache wasm-pack
|
||
$localWp = Join-Path $env:LOCALAPPDATA ".wasm-pack"
|
||
$cachedRunner = $null
|
||
if (Test-Path $localWp) {
|
||
$candidates = Get-ChildItem -Path $localWp -Recurse -Filter "wasm-bindgen-test-runner.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
|
||
if ($candidates) { $cachedRunner = $candidates.FullName }
|
||
}
|
||
|
||
if (-not $cachedRunner) {
|
||
Write-Host "Aucun runner trouvé. Téléchargement de l’archive officielle (tar.gz) pour Windows..." -ForegroundColor Yellow
|
||
$wbgVersion = "0.2.100"
|
||
$arch = "x86_64-pc-windows-msvc"
|
||
$tarName = "wasm-bindgen-$wbgVersion-$arch.tar.gz"
|
||
$downloadUrl = "https://github.com/rustwasm/wasm-bindgen/releases/download/$wbgVersion/$tarName"
|
||
$destParent = $localWp
|
||
$tarPath = Join-Path $env:TEMP $tarName
|
||
try {
|
||
if (-not (Test-Path $destParent)) { New-Item -ItemType Directory -Force -Path $destParent | Out-Null }
|
||
Invoke-WebRequest -Uri $downloadUrl -OutFile $tarPath -UseBasicParsing -ErrorAction Stop
|
||
Push-Location $destParent
|
||
tar -xzf $tarPath
|
||
Pop-Location
|
||
} catch {
|
||
Write-Warning "Échec du téléchargement/extraction du runner: $($_.Exception.Message)"
|
||
} finally {
|
||
if (Test-Path $tarPath) { Remove-Item -Force $tarPath }
|
||
}
|
||
# Recherche récursive du binaire extrait
|
||
$found = Get-ChildItem -Path (Join-Path $destParent "wasm-bindgen-$wbgVersion-$arch") -Recurse -Filter "wasm-bindgen-test-runner.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
|
||
if ($found) { $cachedRunner = $found.FullName }
|
||
}
|
||
|
||
if ($cachedRunner -and (Test-Path $cachedRunner)) {
|
||
$script:runnerSet = $true
|
||
$env:WASM_BINDGEN_TEST_RUNNER = $cachedRunner
|
||
$runnerDir = Split-Path $cachedRunner -Parent
|
||
if ($env:PATH -notlike "*$runnerDir*") { $env:PATH = "$runnerDir;$env:PATH" }
|
||
# Force cargo/wasm-pack à utiliser ce runner pour wasm32-unknown-unknown
|
||
[System.Environment]::SetEnvironmentVariable('CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER', $cachedRunner, 'Process')
|
||
# Copie de secours dans les dossiers cache wasm-pack attendus (hashés)
|
||
try {
|
||
$wpDirs = Get-ChildItem -Path $localWp -Directory -Filter "wasm-bindgen-*" -ErrorAction SilentlyContinue
|
||
foreach ($d in $wpDirs) {
|
||
$destRunner = Join-Path $d.FullName "wasm-bindgen-test-runner.exe"
|
||
if (-not (Test-Path $destRunner)) {
|
||
Copy-Item -Force $cachedRunner $destRunner -ErrorAction SilentlyContinue
|
||
}
|
||
$wbExeSrc = Join-Path $runnerDir "wasm-bindgen.exe"
|
||
$wbExeDst = Join-Path $d.FullName "wasm-bindgen.exe"
|
||
if ((Test-Path $wbExeSrc) -and -not (Test-Path $wbExeDst)) {
|
||
Copy-Item -Force $wbExeSrc $wbExeDst -ErrorAction SilentlyContinue
|
||
}
|
||
}
|
||
} catch {}
|
||
Write-Host "WASM_BINDGEN_TEST_RUNNER défini vers: $cachedRunner" -ForegroundColor Green
|
||
return
|
||
}
|
||
|
||
Write-Warning "wasm-bindgen-test-runner introuvable. wasm-pack tentera de le télécharger lors de l'exécution des tests."
|
||
}
|
||
|
||
$scriptsDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||
$repoRoot = Split-Path -Parent $scriptsDir
|
||
Push-Location $repoRoot
|
||
try {
|
||
Set-WasmToolchainEnv
|
||
Ensure-WasmBindgenRunner
|
||
try {
|
||
# D'abord Node (plus robuste sur Windows)
|
||
Invoke-WasmPackTests -Node
|
||
} catch {
|
||
Write-Warning "Tests Node échoués, tentative avec navigateurs headless."
|
||
Invoke-WasmPackTests -Chrome -Firefox
|
||
}
|
||
} finally {
|
||
Pop-Location
|
||
}
|