diff --git a/tools/GenerateModules.ps1 b/tools/GenerateModules.ps1 index c7d265094f..b720c863f3 100644 --- a/tools/GenerateModules.ps1 +++ b/tools/GenerateModules.ps1 @@ -94,9 +94,10 @@ $AutoRestTempFolder | ForEach-Object { $Stopwatch = [system.diagnostics.stopwatch]::StartNew() $CpuCount = (Get-CimInstance Win32_Processor).NumberOfLogicalProcessors $Throttle = [math]::Min(4, $cpuCount / 2) # Use half the CPU count but max 4 -$ModuleToGenerate | ForEach-Object -Parallel { +$Results = $ModuleToGenerate | ForEach-Object -Parallel { $Module = $_ Write-Host -ForegroundColor Green "-------------'Generating $Module'-------------" + $ServiceModuleParams = @{ Module = $Module ModulesSrc = $using:ModulesSrc @@ -111,32 +112,69 @@ $ModuleToGenerate | ForEach-Object -Parallel { ArtifactsLocation = $using:ArtifactsLocation RequiredModules = $using:RequiredGraphModules } - & $using:GenerateServiceModulePS1 @ServiceModuleParams - function Get-OpenFiles { - param ( - [string] $Path - ) - $OpenFiles = @() - $Files = Get-ChildItem -Path $Path -Recurse -Directory | Where-Object { $_.Name -match "autorest" } - $Files | ForEach-Object { - $File = $_ - try { - $FileStream = $File.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None) - $FileStream.Close() - } - catch { - $OpenFiles += $File.FullName + + try { + $Result = & $using:GenerateServiceModulePS1 @ServiceModuleParams + + # Check if the script returned an exit code (failure) + if ($null -ne $Result -and $Result -is [int] -and $Result -ne 0) { + Write-Host -ForegroundColor Red "Failed to generate module '$Module' with exit code $Result" + return @{ Module = $Module; Success = $false; ExitCode = $Result; Error = "Generation or build failed" } + } + + # Also check $LASTEXITCODE in case the script didn't return but set exit code + if ($null -ne $LASTEXITCODE -and $LASTEXITCODE -ne 0) { + Write-Host -ForegroundColor Red "Failed to generate module '$Module' with exit code $LASTEXITCODE" + return @{ Module = $Module; Success = $false; ExitCode = $LASTEXITCODE; Error = "Generation or build failed" } + } + + function Get-OpenFiles { + param ( + [string] $Path + ) + $OpenFiles = @() + $Files = Get-ChildItem -Path $Path -Recurse -Directory | Where-Object { $_.Name -match "autorest" } + $Files | ForEach-Object { + $File = $_ + try { + $FileStream = $File.Open([System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None) + $FileStream.Close() + } + catch { + $OpenFiles += $File.FullName + } } + return $OpenFiles + } + #Call a function to check if there are any open files in the temp folder. Recurse through the folder until all files are closed + $OpenFiles = Get-OpenFiles -Path $using:TempPath + if ($OpenFiles.Count -gt 0) { + $OpenFiles = Get-OpenFiles -Path $using:TempPath } - return $OpenFiles + + return @{ Module = $Module; Success = $true; ExitCode = 0; Error = $null } } - #Call a function to check if there are any open files in the temp folder. Recurse through the folder until all files are closed - $OpenFiles = Get-OpenFiles -Path $TempPath - if ($OpenFiles.Count -gt 0) { - $OpenFiles = Get-OpenFiles -Path $TempPath + catch { + Write-Host -ForegroundColor Red "Exception while generating module '$Module': $_" + return @{ Module = $Module; Success = $false; ExitCode = -1; Error = $_.Exception.Message } } - } -ThrottleLimit $Throttle $stopwatch.Stop() -Write-Host -ForegroundColor Green "Generated SDK in '$($Stopwatch.Elapsed.TotalMinutes)' minutes." +# Check if any modules failed to generate +$FailedModules = $Results | Where-Object { -not $_.Success } +if ($FailedModules.Count -gt 0) { + Write-Host "" + Write-Host -ForegroundColor Red "=========================================" + Write-Host -ForegroundColor Red "Failed to generate the following modules:" + Write-Host -ForegroundColor Red "=========================================" + $FailedModules | ForEach-Object { + Write-Host -ForegroundColor Red " - $($_.Module) (Exit Code: $($_.ExitCode)) - $($_.Error)" + } + Write-Host -ForegroundColor Red "=========================================" + Write-Host "" + Write-Error "Module generation failed. $($FailedModules.Count) of $($ModuleToGenerate.Count) module(s) failed to generate." +} +else { + Write-Host -ForegroundColor Green "All modules generated successfully in '$($Stopwatch.Elapsed.TotalMinutes)' minutes." +} diff --git a/tools/GenerateServiceModule.ps1 b/tools/GenerateServiceModule.ps1 index 8db9478cf9..d5385173ac 100644 --- a/tools/GenerateServiceModule.ps1 +++ b/tools/GenerateServiceModule.ps1 @@ -72,7 +72,7 @@ $ApiVersion | ForEach-Object { npx autorest --max-memory-size=$MaxMemorySize --module-version:$FullModuleVersion --module-name:$ModuleFullName --service-name:$Module --input-file:$OpenApiFile $AutoRestModuleConfig --max-cpu=2 --network-calls=2 if ($LastExitCode -ne 0) { Write-Host -ForegroundColor Red "AutoREST failed to generate '$ModuleFullName' module." - exit $LastExitCode + return $LastExitCode } Write-Debug "AutoRest generated '$ModuleFullName' successfully." @@ -87,7 +87,7 @@ $ApiVersion | ForEach-Object { & $CleanUpPsm1 -ModuleProjectPath $ModuleProjectPath -FullyQualifiedModuleName $ModuleFullName if ($LastExitCode -ne 0) { Write-Host -ForegroundColor Red "Failed to build '$ModuleFullName' module." - exit $LastExitCode + return $LastExitCode } }