|
| 1 | +function Initialize-ResourceFile { |
| 2 | + |
| 3 | + [CmdletBinding()] |
| 4 | + param ( |
| 5 | + [Parameter(Mandatory = $true)] |
| 6 | + [string] $ModuleName, |
| 7 | + |
| 8 | + [Parameter(Mandatory = $true)] |
| 9 | + [string] $ResourceName, |
| 10 | + |
| 11 | + [Parameter(Mandatory = $true)] |
| 12 | + [string] $Synopsis, |
| 13 | + |
| 14 | + [Parameter(Mandatory = $true)] |
| 15 | + [string] $Description |
| 16 | + ) |
| 17 | + |
| 18 | + $initialContent = @( |
| 19 | + '---', |
| 20 | + "external help file: $ModuleName-Help.xml", |
| 21 | + "Module Name: $ModuleName", |
| 22 | + "ms.date: $(Get-Date -Format 'MM/dd/yyyy')", |
| 23 | + "online version: $null", |
| 24 | + 'schema: 2.0.0', |
| 25 | + "title: $ResourceName", |
| 26 | + '---', |
| 27 | + '', |
| 28 | + "# $ResourceName", |
| 29 | + '', |
| 30 | + '## SYNOPSIS', |
| 31 | + '', |
| 32 | + $Synopsis, |
| 33 | + '', |
| 34 | + '## DESCRIPTION', |
| 35 | + '', |
| 36 | + $Description, |
| 37 | + '' |
| 38 | + ) | Where-Object { $null -ne $_ } # Filter null values |
| 39 | + $readMeFileContent = $initialContent |
| 40 | + |
| 41 | + return $readMeFileContent |
| 42 | +} |
| 43 | + |
| 44 | +function Get-TypeInstanceFromModule { |
| 45 | + param( |
| 46 | + [Parameter(Mandatory = $true)] |
| 47 | + [string] $ModuleName, |
| 48 | + [Parameter(Mandatory = $true)] |
| 49 | + [string] $ClassName |
| 50 | + ) |
| 51 | + $instance = & (Import-Module $ModuleName -PassThru) ([scriptblock]::Create("'$ClassName' -as 'type'")) |
| 52 | + return $instance |
| 53 | +} |
| 54 | + |
| 55 | +function Set-ResourcePropertyParameters { |
| 56 | + [CmdletBinding()] |
| 57 | + param ( |
| 58 | + [Parameter(Mandatory = $true)] |
| 59 | + [hashtable] $ParameterFileContent, |
| 60 | + |
| 61 | + [Parameter(Mandatory = $true)] |
| 62 | + [object[]] $FileContent |
| 63 | + ) |
| 64 | + |
| 65 | + $parameterSection = @( |
| 66 | + '## PARAMETERS', |
| 67 | + '', |
| 68 | + '| **Parameter** | **Attribute** | **DataType** | **Description** | **Allowed values** |', |
| 69 | + '| :------------ | :------------ | :----------- | :-------------- | :----------------- |' |
| 70 | + ) |
| 71 | + |
| 72 | + $ParameterFileContent.GetEnumerator() | ForEach-Object { |
| 73 | + $Attribute = $_.Value.Required ? 'Mandatory' : 'Optional' |
| 74 | + $DataType = $_.Value.Type |
| 75 | + $Description = $_.Value.Description |
| 76 | + $AllowedValues = $null # TOCO: Figure a way to get allowed values |
| 77 | + $parameterSection += '| {0} | {1} | {2} | {3} | {4} |' -f $_.Key, $Attribute, $DataType, $Description, $AllowedValues |
| 78 | + } |
| 79 | + |
| 80 | + $FileContent += $parameterSection |
| 81 | + return $FileContent |
| 82 | +} |
| 83 | + |
| 84 | +function Set-ResourceReadme { |
| 85 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 86 | + param ( |
| 87 | + [Parameter(Mandatory = $true)] |
| 88 | + [string] $ResourceFilePath, |
| 89 | + |
| 90 | + [Parameter(Mandatory = $false)] |
| 91 | + [string] $ReadMeFilePath = (Join-Path (Split-Path $ResourceFilePath -Parent) 'Help'), |
| 92 | + |
| 93 | + [Parameter(Mandatory = $false)] |
| 94 | + [ValidateSet( |
| 95 | + 'Parameters' |
| 96 | + )] |
| 97 | + [string[]] $SectionsToRefresh = @( |
| 98 | + 'Parameters' |
| 99 | + ) |
| 100 | + ) |
| 101 | + |
| 102 | + # Check resource file Path |
| 103 | + $ResourceFilePath = Resolve-Path -Path $ResourceFilePath -ErrorAction Stop |
| 104 | + |
| 105 | + if (-not (Test-Path $ResourceFilePath -PathType 'Leaf')) { |
| 106 | + throw "[$ResourceFilePath] is no valid file path." |
| 107 | + } |
| 108 | + |
| 109 | + # Check the resources that are exported |
| 110 | + $moduleManifest = Import-PowerShellDataFile -Path ($ResourceFilePath.Replace('.psm1', '.psd1')) |
| 111 | + $moduleName = $moduleManifest.RootModule.Replace('.psm1', '') |
| 112 | + $resources = $moduleManifest.DscResourcesToExport |
| 113 | + $resources | ForEach-Object { Write-Verbose -Message ('Resource: {0}' -f $_) } |
| 114 | + |
| 115 | + # TODO: Grab manual notes if needed |
| 116 | + |
| 117 | + foreach ($resource in $resources) { |
| 118 | + $docFile = Join-Path -Path $ReadMeFilePath -ChildPath ('{0}.md' -f $resource) |
| 119 | + |
| 120 | + $resourceInstance = Get-TypeInstanceFromModule -ModuleName $ResourceFilePath -ClassName $resource |
| 121 | + $dscResourceInstance = $resourceInstance::new() |
| 122 | + $t = $dscResourceInstance.GetType() |
| 123 | + |
| 124 | + $resourceInfoMethod = $t.GetMethod('GetResourceInfo') |
| 125 | + if ($null -eq $resourceInfoMethod) { |
| 126 | + Write-Warning -Message ('Resource [{0}] does not have a GetResourceInfo method.' -f $resource) |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + $resourceInfo = $resourceInfoMethod.Invoke($null, $null) |
| 131 | + |
| 132 | + $resourcePropertyInfoMethod = $t.GetMethod('GetResourcePropertyInfo') |
| 133 | + if ($null -eq $resourcePropertyInfoMethod) { |
| 134 | + Write-Warning -Message ('Resource [{0}] does not have a GetResourcePropertyInfo method.' -f $resource) |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + $resourcePropertyInfo = $resourcePropertyInfoMethod.Invoke($null, $null) |
| 139 | + |
| 140 | + # Initialize the resource file |
| 141 | + $inputObject = @{ |
| 142 | + ModuleName = $moduleName |
| 143 | + ResourceName = $resource |
| 144 | + Synopsis = $resourceInfo.Description |
| 145 | + Description = $resourceInfo.Description |
| 146 | + } |
| 147 | + $fileContent = Initialize-ResourceFile @inputObject |
| 148 | + |
| 149 | + # =============== # |
| 150 | + # Set content # |
| 151 | + # =============== # |
| 152 | + |
| 153 | + if ($SectionsToRefresh -contains 'Parameters') { |
| 154 | + $fileContent = Set-ResourcePropertyParameters -ParameterFileContent $resourcePropertyInfo -FileContent $fileContent |
| 155 | + } |
| 156 | + |
| 157 | + return $fileContent |
| 158 | + |
| 159 | + # $commandHelper = [Microsoft.PowerShell.PlatyPS.Model.CommandHelp]::new() |
| 160 | + # $commandHelper.Title = $resource |
| 161 | + # $commandHelper.Synopsis = $resourceInfo.Description |
| 162 | + # $commandHelper.Description = ($resourceInfo.Description + "`n`n## PARAMETERS`n`n" + ($parameterSection -join "`n")) |
| 163 | + # $commandHelper.Metadata = [ordered]@{ |
| 164 | + # 'external help file' = ('{0}-Help.xml' -f $moduleManifest.RootModule) |
| 165 | + # 'Module Name' = 'test' |
| 166 | + # 'ms.date' = (Get-Date).ToString('MM/dd/yyyy') |
| 167 | + # 'online version' = $null |
| 168 | + # schema = '2.0.0' |
| 169 | + # title = $resource |
| 170 | + # } |
| 171 | + } |
| 172 | +} |
0 commit comments