Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 61 additions & 23 deletions tools/GenerateModules.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."
}
4 changes: 2 additions & 2 deletions tools/GenerateServiceModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand All @@ -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
}
}

Expand Down