# build_all_par.ps1 # script per compilazione (parallela) progetti MAPO-CORE # nb: prerequisito powershell >= 7, install con # winget install Microsoft.PowerShell # --- CONFIGURAZIONE --- $pattern = "MP-*.sln" $sharedProjectPath = ".\MP.Data\MP.Data.csproj" # Controllo robusto del parametro --agent (cerca in tutti gli argomenti passati) $agentMode = $args -contains "--agent" # Avvia il cronometro per calcolare il tempo totale $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() # 1. Trova l'MSBuild ufficiale di Visual Studio 2022 $vsPaths = @(& "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -version "[17.0,19.0)" -products * -requires Microsoft.Component.MSBuild -property installationPath) # versione WKS-R9-SAM # $vsPaths = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -version "[17.0,18.0)" -products * -requires Microsoft.Component.MSBuild -property installationPath if (-not $vsPaths) { if ($agentMode) { exit 1 } Write-Host "โŒ Impossibile trovare Visual Studio 2022!" -ForegroundColor Red Exit } $msbuildPath = Join-Path $vsPaths[0] "MSBuild\Current\Bin\MSBuild.exe" if (-not $agentMode) { Write-Host "๐ŸŽฏ Usando MSBuild di VS2022: $msbuildPath" -ForegroundColor Gray } # Cerca tutte le soluzioni escludendo cartelle di build $solutions = Get-ChildItem -Path . -Filter $pattern -Recurse | Where-Object { $_.FullName -notmatch '\\(bin|obj|\\.git|\\.vs)\\.' } if ($solutions.Count -eq 0) { if ($agentMode) { exit 1 } Write-Host "โš ๏ธ Nessuna soluzione trovata che corrisponde al pattern: $pattern" -ForegroundColor Yellow Exit } if (-not $agentMode) { Write-Host "๐Ÿš€ Trovate $($solutions.Count) soluzioni univoche." -ForegroundColor Magenta } # FASE 1: Compilazione preventiva del progetto comune if (Test-Path $sharedProjectPath) { if (-not $agentMode) { Write-Host "`n๐Ÿ“ฆ Fase 1: Compilazione del progetto comune condiviso..." -ForegroundColor Cyan } # AGGIORNAMENTO: Se siamo in agentMode silenziamo completamente MSBuild (/v:q) e ridirigiamo l'output if ($agentMode) { & $msbuildPath $sharedProjectPath /p:Configuration=Release /v:q /nologo > $null 2>&1 } else { & $msbuildPath $sharedProjectPath /p:Configuration=Release /v:m /nologo } if ($LASTEXITCODE -ne 0) { if (-not $agentMode) { Write-Host "โŒ Errore critico: Impossibile compilare il progetto comune. Interruzione." -ForegroundColor Red } exit 1 } if (-not $agentMode) { Write-Host "โœ… Progetto comune pronto." -ForegroundColor Green } } $numPar=3 # FASE 2: Compilazione parallela iniziale if (-not $agentMode) { Write-Host "`n๐Ÿ› ๏ธ Fase 2: Avvio compilazione parallela delle soluzioni (Max $($numPar))..." -ForegroundColor Magenta Write-Host "==================================================" -ForegroundColor Magenta } $parallelResults = $solutions | ForEach-Object -Parallel { $solName = $_.Name $solPath = $_.FullName $msb = $using:msbuildPath # Aggiunto /nologo per evitare intestazioni ripetute nei log interni $log = & $msb $solPath /p:Configuration=Release /m:1 /p:BuildInParallel=false /v:m /nologo 2>&1 [PSCustomObject]@{ Name = $solName FullName = $solPath Success = ($LASTEXITCODE -eq 0) Log = $log } } -ThrottleLimit $numPar # --- ANALISI PRIMO ROUND E FASE 3 (RETRY SEQUENZIALE) --- $successSolutions = @() $failedToRetry = @() foreach ($res in $parallelResults) { if ($res.Success) { if (-not $agentMode) { Write-Host "โœ… $($res.Name) compilata con successo (in parallelo)!" -ForegroundColor Green } $successSolutions += $res.Name } else { if (-not $agentMode) { Write-Host "โš ๏ธ $($res.Name) fallita in parallelo. Accodata per il recupero sequenziale..." -ForegroundColor Yellow } $failedToRetry += $res } } # Se ci sono falliti, li rieseguiamo UNO ALLA VOLTA pulendo la cache if ($failedToRetry.Count -gt 0) { if (-not $agentMode) { Write-Host "`n๐Ÿ”„ Fase 3: Riesecuzione sequenziale dei task falliti ($($failedToRetry.Count) soluzioni)..." -ForegroundColor Magenta Write-Host "==================================================" -ForegroundColor Magenta } foreach ($failedRes in $failedToRetry) { if (-not $agentMode) { Write-Host "โณ Ripristino e compilazione sequenziale: $($failedRes.Name)..." -ForegroundColor Cyan } & $msbuildPath $failedRes.FullName /t:Restore /v:q /nologo > $null 2>&1 & $msbuildPath $failedRes.FullName /t:Clean /v:q /p:Configuration=Release /nologo > $null 2>&1 $retryLog = & $msbuildPath $failedRes.FullName /t:Build /p:Configuration=Release /v:m /nologo 2>&1 if ($LASTEXITCODE -eq 0) { if (-not $agentMode) { Write-Host "โœ… FALSO ALLARME: $($failedRes.Name) compilata correttamente in sequenziale!" -ForegroundColor Green } $successSolutions += $failedRes.Name } else { if (-not $agentMode) { Write-Host "โŒ ERRORE REALE: $($failedRes.Name) รจ fallita anche in sequenziale." -ForegroundColor Red } $failedRes.Log = $retryLog } } } # Ferma il cronometro e calcola il tempo trascorso $stopwatch.Stop() $elapsedTime = "{0:mm\:ss}" -f $stopwatch.Elapsed # --- ELABORAZIONE DEI RISULTATI FINALI --- $totalCount = $solutions.Count $successCount = $successSolutions.Count $failCount = $totalCount - $successCount $failColor = if ($failCount -gt 0) { "Red" } else { "Gray" } # --- RIEPILOGO FINALE --- if (-not $agentMode) { Write-Host "`n==================================================" -ForegroundColor Magenta Write-Host "๐Ÿ Processo di verifica completato in $elapsedTime!" -ForegroundColor Magenta Write-Host "==================================================" -ForegroundColor Magenta Write-Host " Soluzioni Totali: $totalCount" -ForegroundColor White Write-Host " Successi totali: $successCount" -ForegroundColor Green Write-Host " Errori reali: $failCount" -ForegroundColor $failColor Write-Host " Tempo impiegato: $elapsedTime" -ForegroundColor Cyan if ($failCount -gt 0) { Write-Host "`nโŒ Elenco delle soluzioni con ERRORI REALI:" -ForegroundColor Red foreach ($res in $parallelResults) { if ($successSolutions -notcontains $res.Name) { Write-Host " - $($res.Name)" -ForegroundColor Red Write-Host " ๐Ÿ‘‰ Ultimi dettagli errore:" -ForegroundColor DarkRed $res.Log | Where-Object { $_ -match "error" } | Select-Object -First 3 | Write-Host -ForegroundColor Gray } } } else { Write-Host "`n๐ŸŽ‰ Eccellente! Tutte le soluzioni compilano senza errori." -ForegroundColor Green } Write-Host "==================================================" -ForegroundColor Magenta } # Exit code standard per ambienti automatizzati (0 = Successo, 1 = Fallimento) if ($failCount -gt 0) { exit 1 } else { exit 0 }