forked from pester/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - prepare to test docs update workflow
- Loading branch information
Showing
3 changed files
with
304 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
name: Update Pester Docs | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
docs_version: | ||
description: 'Docs version to update' | ||
required: true | ||
default: 'v5' | ||
type: choice | ||
options: | ||
- Current | ||
- v5 | ||
- v4 | ||
pester_version: | ||
description: 'Pester version to use for docs generation. Format: 5.6.0 or 6.0.0-alpha1' | ||
required: true | ||
type: string | ||
|
||
jobs: | ||
generate_docs: | ||
name: Docs PR | ||
runs-on: ubuntu-latest | ||
env: | ||
PESTER: ${{ github.event.inputs.pester_version }} | ||
DOCS: ${{ github.event.inputs.docs_version }} | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
steps: | ||
- name: Validate Pester version input | ||
run: | | ||
echo "Validating that the provided pester_version input is a valid version-format (x.y.z or x.y.z-suffix)" | ||
echo "${{ env.PESTER }}" | grep -P '^\d+.\d+.\d+(?:-\w+)?$' | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
# This step will also install and import modules incl. selected Pester-version | ||
- name: Update Command Reference | ||
shell: pwsh | ||
run: | | ||
./generate-command-reference.ps1 -PesterVersion '${{ env }}' -DocsVersion '${{ env.DOCS }}' | ||
- name: Update PesterConfiguration docs | ||
if: ${{ env.DOCS != 'v4' }} | ||
shell: pwsh | ||
run: | | ||
./generate-pesterconfiguration-docs.ps1 -PesterVersion '${{ env.PESTER }}' -Style Table -DocsVersion '${{ env.DOCS }}' | ||
- name: Create Pull Request | ||
uses: peter-evans/create-pull-request@6d6857d36972b65feb161a90e484f2984215f83e # v6.0.5 | ||
env: | ||
PR_BRANCH: "docs-update/${{ env.DOCS }}-v${{ env.PESTER }}" | ||
TITLE_PREFIX: "${{ env.DOCS != 'Current' && format('({0}) ', env.DOCS) || '' }}" | ||
with: | ||
branch: ${{ env.PR_BRANCH }} | ||
branch-suffix: short-commit-hash | ||
# Set user triggering the workflow as author (default in action) | ||
author: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com> | ||
# Github Action as commit (explicit default) | ||
committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | ||
commit-message: "Update generated docs to ${{ env.PESTER }}" | ||
add-paths: | | ||
docs | ||
versioned_docs | ||
title: ${{ env.TITLE_PREFIX }}Update generated docs to Pester v${{ env.PESTER }} | ||
body: | | ||
Updates Command Reference and/or PesterConfiguration documentation. | ||
Docs version being updated: ${{ env.DOCS }} | ||
Pester version used: v${{ env.PESTER }} | ||
*PR created using '${{ github.workflow }}' workflow* | ||
draft: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<# | ||
.SYNOPSIS | ||
Updates the Configuration page with latest PesterConfiguration Sections and Options | ||
#> | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory = $False)] | ||
[string] $PesterVersion, | ||
[ValidateSet('Current','v5')] | ||
[string] $DocsVersion = 'Current', | ||
[ValidateSet('List','Table')] | ||
[string] $Style = 'List' | ||
) | ||
|
||
#region helpers | ||
function Format-NicelyMini ($value) { | ||
if ($value -is [bool]) { | ||
if ($value) { | ||
'$true' | ||
} else { | ||
'$false' | ||
} | ||
} | ||
|
||
if ($value -is [int] -or $value -is [decimal]) { | ||
return $value | ||
} | ||
|
||
if ($value -is [string]) { | ||
if ([String]::IsNullOrEmpty($value)) { | ||
return '$null' | ||
} else { | ||
return "'$value'" | ||
} | ||
} | ||
|
||
# does not work with [object[]] when building for some reason | ||
if ($value -is [System.Collections.IEnumerable]) { | ||
if (0 -eq $value.Count) { | ||
return '@()' | ||
} | ||
$v = foreach ($i in $value) { | ||
Format-NicelyMini $i | ||
} | ||
return "@($($v -join ', '))" | ||
} | ||
} | ||
|
||
function generateSectionsMarkdownAsTable { | ||
$configObject = New-PesterConfiguration | ||
foreach ($configSection in $configObject.PSObject.Properties) { | ||
$sectionName = $configSection.Name | ||
$sectionDescription = $configSection.Value -as [string] | ||
$section = $configSection.Value | ||
|
||
$options = foreach ($configOption in $section.PSObject.Properties) { | ||
$optionName = $configOption.Name | ||
$option = $configOption.Value | ||
$default = Format-NicelyMini $option.Default | ||
$type = $option.Default.GetType() -as [string] -replace '^Pester\.' | ||
"| $optionName | $($option.Description) | ``$type`` | ``$default`` |" | ||
} | ||
|
||
@" | ||
### ${sectionName} | ||
$sectionDescription | ||
| Option | Description | Type | Default | | ||
|--------|-------------|-----:|--------:| | ||
$($options -join $eol) | ||
"@ | ||
} | ||
} | ||
|
||
function generateSectionsMarkdownAsList { | ||
$configObject = New-PesterConfiguration | ||
foreach ($configSection in $configObject.PSObject.Properties) { | ||
$sectionName = $configSection.Name | ||
$sectionDescription = $configSection.Value -as [string] | ||
$section = $configSection.Value | ||
|
||
$options = foreach ($configOption in $section.PSObject.Properties) { | ||
$optionName = $configOption.Name | ||
$option = $configOption.Value | ||
$default = Format-NicelyMini $option.Default | ||
$type = $option.Default.GetType() -as [string] | ||
@" | ||
#### $sectionName.$optionName | ||
**Type:** ``$type``<br/> | ||
**Default:** ``$default`` | ||
$($option.Description) | ||
"@ | ||
} | ||
|
||
# Output markdown string per section | ||
@" | ||
### ${sectionName} | ||
$sectionDescription | ||
$($options -join $eol) | ||
"@ | ||
} | ||
} | ||
#endregion | ||
|
||
$loadedModule = if ($PSBoundParameters.ContainsKey('PesterVersion')) { | ||
Import-Module Pester -RequiredVersion ($PesterVersion -replace '-\w+$') -PassThru | ||
} else { | ||
Import-Module Pester -PassThru | ||
} | ||
|
||
$loadedVersion = if ($loadedModule.PrivateData -and $loadedModule.PrivateData.PSData -and $loadedModule.PrivateData.PSData.PreRelease) { | ||
"$($loadedModule.Version)-$($loadedModule.PrivateData.PSData.PreRelease)" | ||
} else { | ||
$loadedModule.Version | ||
} | ||
|
||
if ($PSBoundParameters.ContainsKey('PesterVersion') -and $loadedVersion -ne $PesterVersion) { | ||
throw "Pester $PesterVersion was requested, but version '$loadedVersion' was loaded. Aborting." | ||
} | ||
|
||
# generate help for config object and insert it | ||
$startComment = 'GENERATED_PESTER_CONFIGURATION_DOCS_START' | ||
$endComment = 'GENERATED_PESTER_CONFIGURATION_DOCS_END' | ||
$eol = "`n" | ||
$encoding = 'UTF8' | ||
|
||
$ConfigurationPagePath = switch ($DocsVersion) { | ||
'Current' { "$PSScriptRoot/docs/usage/configuration.mdx" } | ||
'v5' { "$PSScriptRoot/versioned_docs/version-v5/usage/configuration.mdx" } | ||
} | ||
|
||
$generatedConfigDocs = switch ($Style) { | ||
'List' { generateSectionsMarkdownAsList } | ||
'Table' { generateSectionsMarkdownAsTable } | ||
} | ||
|
||
$pageContent = Get-Content $ConfigurationPagePath -Encoding $encoding | ||
$sbf = [System.Text.StringBuilder]'' | ||
$sectionFound = $false | ||
|
||
foreach ($line in $pageContent) { | ||
if (-not $sectionFound -and $line -match $startComment) { | ||
$null = $sbf.AppendLine("$line$eol") | ||
$null = $sbf.AppendLine("*This configuration documentation is generated from Pester $loadedVersion.*$eol") | ||
$sectionFound = $true | ||
$null = $sbf.AppendJoin($eol, $generatedConfigDocs) | ||
} elseif ($sectionFound -and ($line -match $endComment)) { | ||
$sectionFound = $false | ||
} | ||
if (-not $sectionFound) { | ||
$null = $sbf.AppendLine($line) | ||
} | ||
} | ||
|
||
Set-Content -Encoding $encoding -Value $sbf.ToString().TrimEnd() -Path $ConfigurationPagePath |