diff --git a/utilities/tools/Set-Module.ps1 b/utilities/tools/Set-Module.ps1 index cd451ad8e0..d0435f66a7 100644 --- a/utilities/tools/Set-Module.ps1 +++ b/utilities/tools/Set-Module.ps1 @@ -15,6 +15,9 @@ Mandatory. The path to the module folder to generate the content for. .PARAMETER Recurse Optional. Set this parameter if you not only want to generate the content for one module, but also any nested module in the same path. +.PARAMETER Depth +Optional. Recursion depth for the module search. + .PARAMETER SkipBuild Optional. Set this parameter if you don't want to build/compile the JSON template(s) for the contained `main.bicep` file(s). @@ -67,7 +70,10 @@ function Set-Module { [switch] $SkipFileAndFolderSetup, [Parameter(Mandatory = $false)] - [int] $ThrottleLimit = 5 + [int] $ThrottleLimit = 5, + + [Parameter(Mandatory = $false)] + [int] $Depth ) # # Load helper scripts @@ -83,7 +89,16 @@ function Set-Module { # } if ($Recurse) { - $relevantTemplatePaths = (Get-ChildItem -Path $resolvedPath -Recurse -File -Filter 'main.bicep').FullName + $childInput = @{ + Path = $resolvedPath + Recurse = $Recurse + File = $true + Filter = 'main.bicep' + } + if ($Depth) { + $childInput.Depth = $Depth + } + $relevantTemplatePaths = (Get-ChildItem @childInput).FullName } else { $relevantTemplatePaths = Join-Path $resolvedPath 'main.bicep' } @@ -100,30 +115,54 @@ function Set-Module { # Using threading to speed up the process if ($PSCmdlet.ShouldProcess(('Building & generation of [{0}] modules in path [{1}]' -f $relevantTemplatePaths.Count, $resolvedPath), 'Execute')) { - $relevantTemplatePaths | ForEach-Object -ThrottleLimit $ThrottleLimit -Parallel { - $resourceTypeIdentifier = ((Split-Path $_) -split '[\/|\\]{1}modules[\/|\\]{1}')[1] # avm/res// + try { + $job = $relevantTemplatePaths | ForEach-Object -ThrottleLimit $ThrottleLimit -AsJob -Parallel { + $resourceTypeIdentifier = ((Split-Path $_) -split '[\/|\\]{1}modules[\/|\\]{1}')[1] # avm/res// + + . $using:ReadMeScriptFilePath + + ############### + ## Build ## + ############### + if (-not $using:SkipBuild) { + Write-Output "Building [$resourceTypeIdentifier]" + bicep build $_ + } + + ################ + ## ReadMe ## + ################ + if (-not $using:SkipReadMe) { + Write-Output "Generating readme for [$resourceTypeIdentifier]" + + # If the template was just build, we can pass the JSON into the readme script to be more efficient + $readmeTemplateFilePath = (-not $using:SkipBuild) ? (Join-Path (Split-Path $_ -Parent) 'main.json') : $_ + + Set-ModuleReadMe -TemplateFilePath $readmeTemplateFilePath -CrossReferencedModuleList $using:crossReferencedModuleList + } + } - . $using:ReadMeScriptFilePath + do { + # Sleep a bit to allow the threads to run - adjust as desired. + Start-Sleep -Seconds 0.5 - ############### - ## Build ## - ############### - if (-not $using:SkipBuild) { - Write-Output "Building [$resourceTypeIdentifier]" - bicep build $_ - } + # Determine how many jobs have completed so far. + $completedJobsCount = ($job.ChildJobs | Where-Object { $_.State -notin @('NotStarted', 'Running') }).Count - ################ - ## ReadMe ## - ################ - if (-not $using:SkipReadMe) { - Write-Output "Generating readme for [$resourceTypeIdentifier]" + # Relay any pending output from the child jobs. + $job | Receive-Job - # If the template was just build, we can pass the JSON into the readme script to be more efficient - $readmeTemplateFilePath = (-not $using:SkipBuild) ? (Join-Path (Split-Path $_ -Parent) 'main.json') : $_ + # Update the progress display. + [int] $percent = ($completedJobsCount / $job.ChildJobs.Count) * 100 + Write-Progress -Activity ('Processed [{0}] files' -f $relevantTemplatePaths.Count) -Status "$percent% complete" -PercentComplete $percent - Set-ModuleReadMe -TemplateFilePath $readmeTemplateFilePath -CrossReferencedModuleList $using:crossReferencedModuleList - } + } while ($completedJobsCount -lt $job.ChildJobs.Count) + + # Clean up the job. + $job | Remove-Job + } finally { + # In case the user cancelled the process, we need to make sure to stop all running jobs + $job | Remove-Job -Force -ErrorAction 'SilentlyContinue' } } }