Skip to content

Commit

Permalink
Refactor Wiki Functions (#81)
Browse files Browse the repository at this point in the history
- Added private functions:
  - Get-TemporaryPath - returns the appropriate temp path for the OS.
- New-DscResourceWikiPage
  - Refactored to split into two private functions New-DscMofResourceWikiPage and
    New-DscClassResourceWikiPage.
- Get-MofSchemaObject
  - Refactored to reduce code duplication when adding functions for supporting
    composite resources.
  • Loading branch information
PlagueHO authored Jun 16, 2021
1 parent 210424f commit 3a8ba11
Show file tree
Hide file tree
Showing 14 changed files with 2,442 additions and 1,943 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Get-ClassResourceProperty` - Returns DSC class resource properties
from the provided class or classes.
- `Format-Text` - Format a string according to predefined options.
- `Get-TemporaryPath` - returns the appropriate temp path for the OS.

### Changed

- `New-DscResourceWikiPage`
- If a class-based resource has a parent class that contains DSC resource
properties they will now also be returned as part of the DSC resource
parameters ([issue #62](https://github.com/dsccommunity/DscResource.DocGenerator/issues/62)).
- Refactored to split into two private functions `New-DscMofResourceWikiPage` and
`New-DscClassResourceWikiPage`.
- `Get-MofSchemaObject`
- Refactored to reduce code duplication when adding functions for supporting
composite resources.

### Fixed

Expand Down
37 changes: 11 additions & 26 deletions source/Private/Get-MofSchemaObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
$mof = Get-MofSchemaObject -FileName C:\repos\SharePointDsc\DSCRescoures\MSFT_SPSite\MSFT_SPSite.schema.mof
This example parses a MOF schema file.
.NOTES
The function will thrown when run on MacOS because currently there is an
issue using the type
[Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache]
on macOS. See issue https://github.com/PowerShell/PowerShell/issues/5970
and issue https://github.com/PowerShell/MMI/issues/33.
#>
function Get-MofSchemaObject
{
Expand All @@ -27,35 +34,13 @@ function Get-MofSchemaObject
$FileName
)

$temporaryPath = $null

# Determine the correct $env:TEMP drive
switch ($true)
if ($IsMacOS)
{
(-not (Test-Path -Path variable:IsWindows) -or $IsWindows)
{
# Windows PowerShell or PowerShell 6+
$temporaryPath = $env:TEMP
}

$IsMacOS
{
$temporaryPath = $env:TMPDIR

throw 'NotImplemented: Currently there is an issue using the type [Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache] on macOS. See issue https://github.com/PowerShell/PowerShell/issues/5970 and issue https://github.com/PowerShell/MMI/issues/33.'
}

$IsLinux
{
$temporaryPath = '/tmp'
}

Default
{
throw 'Cannot set the temporary path. Unknown operating system.'
}
throw 'NotImplemented: Currently there is an issue using the type [Microsoft.PowerShell.DesiredStateConfiguration.Internal.DscClassCache] on macOS. See issue https://github.com/PowerShell/PowerShell/issues/5970 and issue https://github.com/PowerShell/MMI/issues/33.'
}

$temporaryPath = Get-TemporaryPath

#region Workaround for OMI_BaseResource inheritance not resolving.

$filePath = (Resolve-Path -Path $FileName).Path
Expand Down
50 changes: 50 additions & 0 deletions source/Private/Get-TemporaryPath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<#
.SYNOPSIS
Get-TemporaryPath returns the temporary path for the OS.
.DESCRIPTION
The Get-TemporaryPath function will return the temporary
path specific to the OS. It will return $ENV:Temp when run
on Windows OS, '/tmp' when run in Linux and $ENV:TMPDIR when
run on MacOS.
.NOTES
We use Get-Variable to determine if the OS specific variables are
defined so that we can mock these during testing. We also use Get-Item
to get the environment variables so we can also mock these for testing.
#>

function Get-TemporaryPath
{
[CmdletBinding()]
[OutputType([System.String])]
param ()

$temporaryPath = $null

switch ($true)
{
(-not (Test-Path -Path variable:IsWindows) -or ((Get-Variable -Name 'IsWindows' -ValueOnly -ErrorAction SilentlyContinue) -eq $true))
{
# Windows PowerShell or PowerShell 6+
$temporaryPath = (Get-Item -Path env:TEMP).Value
}

((Get-Variable -Name 'IsMacOs' -ValueOnly -ErrorAction SilentlyContinue) -eq $true)
{
$temporaryPath = (Get-Item -Path env:TMPDIR).Value
}

((Get-Variable -Name 'IsLinux' -ValueOnly -ErrorAction SilentlyContinue) -eq $true)
{
$temporaryPath = '/tmp'
}

Default
{
throw 'Cannot set the temporary path. Unknown operating system.'
}
}

return $temporaryPath
}
148 changes: 148 additions & 0 deletions source/Private/New-DscClassResourceWikiPage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<#
.SYNOPSIS
New-DscClassResourceWikiPage generates wiki pages for class-based resources
that can be uploaded to GitHub to use as public documentation for a module.
.DESCRIPTION
The New-DscClassResourceWikiPage cmdlet will review all of the class-based and
in a specified module directory and will output the Markdown files to the
specified directory. These help files include details on the property types
for each resource, as well as a text description and examples where they exist.
.PARAMETER OutputPath
Where should the files be saved to.
.PARAMETER SourcePath
The path to the root of the DSC resource module (where the PSD1 file is found,
not the folder for and individual DSC resource).
.PARAMETER BuiltModulePath
The path to the root of the built DSC resource module, e.g.
'output/MyResource/1.0.0'.
.EXAMPLE
New-DscClassResourceWikiPage `
-SourcePath C:\repos\MyResource\source `
-BuiltModulePath C:\repos\MyResource\output\MyResource\1.0.0 `
-OutputPath C:\repos\MyResource\output\WikiContent
This example shows how to generate wiki documentation for a specific module.
#>
function New-DscClassResourceWikiPage
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$OutputPath,

[Parameter(Mandatory = $true)]
[System.String]
$SourcePath,

[Parameter(Mandatory = $true)]
[System.String]
$BuiltModulePath,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)

if (Test-Path -Path $BuiltModulePath)
{
<#
This must not use Recurse. Then it could potentially find resources
that are part of common modules in the Modules folder.
#>
$getChildItemParameters = @{
Path = Join-Path -Path $BuiltModulePath -ChildPath '*'
Include = '*.psm1'
ErrorAction = 'Stop'
File = $true
}

$builtModuleScriptFiles = Get-ChildItem @getChildItemParameters

# Looping through each module file (normally just one).
foreach ($builtModuleScriptFile in $builtModuleScriptFiles)
{
$dscResourceAsts = Get-ClassResourceAst -ScriptFile $builtModuleScriptFile.FullName

Write-Verbose -Message ($script:localizedData.FoundClassBasedMessage -f $dscResourceAsts.Count, $builtModuleScriptFile.FullName)

# Looping through each class-based resource.
foreach ($dscResourceAst in $dscResourceAsts)
{
Write-Verbose -Message ($script:localizedData.GenerateWikiPageMessage -f $dscResourceAst.Name)

$output = New-Object -TypeName 'System.Text.StringBuilder'

$null = $output.AppendLine("# $($dscResourceAst.Name)")
$null = $output.AppendLine()
$null = $output.AppendLine('## Parameters')
$null = $output.AppendLine()

$sourceFilePath = Join-Path -Path $SourcePath -ChildPath ('Classes/*{0}.ps1' -f $dscResourceAst.Name)

$className = @()

if ($dscResourceAst.BaseTypes.Count -gt 0)
{
$className += @($dscResourceAst.BaseTypes.TypeName.Name)
}

$className += $dscResourceAst.Name

# Returns the properties for class and any existing parent class(es).
$resourceProperty = Get-ClassResourceProperty -ClassName $className -SourcePath $SourcePath -BuiltModuleScriptFilePath $builtModuleScriptFile.FullName

$propertyContent = Get-DscResourceSchemaPropertyContent -Property $resourceProperty -UseMarkdown

foreach ($line in $propertyContent)
{
$null = $output.AppendLine($line)
}

$null = $output.AppendLine()

$dscResourceCommentBasedHelp = Get-ClassResourceCommentBasedHelp -Path $sourceFilePath

$description = $dscResourceCommentBasedHelp.Description
$description = $description -replace '[\r|\n]+$' # Removes all blank rows and whitespace at the end

$null = $output.AppendLine('## Description')
$null = $output.AppendLine()
$null = $output.AppendLine($description)
$null = $output.AppendLine()

$examplesPath = Join-Path -Path $SourcePath -ChildPath ('Examples\Resources\{0}' -f $dscResourceAst.Name)

$examplesOutput = Get-ResourceExampleAsMarkdown -Path $examplesPath

if ($examplesOutput.Length -gt 0)
{
$null = $output.Append($examplesOutput)
}

$outputFileName = '{0}.md' -f $dscResourceAst.Name

$savePath = Join-Path -Path $OutputPath -ChildPath $outputFileName

Write-Verbose -Message ($script:localizedData.OutputWikiPageMessage -f $savePath)

$outputToWrite = $output.ToString()
$outputToWrite = $outputToWrite -replace '[\r|\n]+$' # Removes all blank rows and whitespace at the end
$outputToWrite = $outputToWrite -replace '\r?\n', "`r`n" # Normalize to CRLF
$outputToWrite = $outputToWrite -replace '[ ]+\r\n', "`r`n" # Remove indentation from blank rows

$null = Out-File `
-InputObject $outputToWrite `
-FilePath $savePath `
-Encoding utf8 `
-Force:$Force
}
}
}
}
Loading

0 comments on commit 3a8ba11

Please sign in to comment.