Skip to content

Commit

Permalink
[Utilities] Enabled progress bar on module update & depth param (#4098)
Browse files Browse the repository at this point in the history
* Tested logic & added depth param

* Added finally block

* Enabled cancel

* ReadMe fallback

* Added docs

* Added silent continue on finally for non-cancel

* Update utilities/tools/Set-Module.ps1

Co-authored-by: Ahmad Abdalla <28486158+ahmadabdalla@users.noreply.github.com>

---------

Co-authored-by: Ahmad Abdalla <28486158+ahmadabdalla@users.noreply.github.com>
  • Loading branch information
AlexanderSehr and ahmadabdalla authored Oct 16, 2023
1 parent 9ba9712 commit e1aa127
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions utilities/tools/Set-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand All @@ -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'
}
Expand All @@ -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/<provider>/<resourceType>
try {
$job = $relevantTemplatePaths | ForEach-Object -ThrottleLimit $ThrottleLimit -AsJob -Parallel {
$resourceTypeIdentifier = ((Split-Path $_) -split '[\/|\\]{1}modules[\/|\\]{1}')[1] # avm/res/<provider>/<resourceType>

. $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'
}
}
}

0 comments on commit e1aa127

Please sign in to comment.