PSClassUtils
PSClassUtils copied to clipboard
Refactor Write-CUClassDiagram
The cmdlet is very big, and contains a lot of repetitive code. It also calls two child functions, which are probably not necessary (New-GraphPArameters and New-CUGraphExport)
New-CUGraphParameters
- Doesnt gatehr parameters, but actually generates the Graph (graphviz format).
this is the code of the function:
function New-CUGraphParameters {
<#
.SYNOPSIS
Helper function to generate a Graph, wrap Out-CUPSGraph.
.DESCRIPTION
Helper function to generate a Graph, wrap Out-CUPSGraph.
.NOTES
Private function for PSClassUtils, used in Write-CUClassDiagram
#>
Param (
$inputobject,
$ignorecase,
$showcomposition
)
$GraphParams = @{
InputObject = $inputobject
}
If ( $ignorecase ) { $GraphParams.Add('IgnoreCase',$ignorecase) }
If ( $showcomposition ) { $GraphParams.Add('ShowComposition',$showcomposition) }
Out-CUPSGraph @GraphParams
}
New-CUGraphExport
This function is actually just a wrapper for Export-PSGraph, which needs a 'graph' as input.
In the current version, it is generated using Out-CUGraphParameters. (see above).
This is a wrapper for Out-CUPsGraph.
function New-CUGraphExport {
<#
.SYNOPSIS
Helper function to generate a Graph export file, wraps Export-PSGraph.
.DESCRIPTION
Helper function to generate a Graph export file , wraps Export-PSGraph.
.NOTES
Private function for PSClassUtils, used in Write-CUClassDiagram
#>
param (
$Graph,
$PassThru,
$Path,
$ChildPath,
$OutPutFormat,
[Switch]$Show
)
$ExportParams = @{
ShowGraph = $Show
OutPutFormat = $OutPutFormat
DestinationPath = Join-Path -Path $Path -ChildPath ($ChildPath+'.'+$OutPutFormat)
}
If ( $PassThru ) {
$Graph
$null = $Graph | Export-PSGraph @ExportParams
} Else {
$Graph | Export-PSGraph @ExportParams
}
}
It would be nice to simplify this function, to enable potential futur extensions to it. Some thoughts on how to achiev this:
- Use Splatting
- remove the two functions above.
- Add a class CUDiagram --> See Here
I have advanced pretty well.
It seems like most of the work for [CUDiagram] is done. I managed to keep things simple in this first version by reusing existing functions (Export-PSGraph).
Ideally, Export-PSGraph should also be refactored into a method in CUDiagram.
Open points:
Tests done:
-
[x] One Class in one file
-
[ ] Multiple classes in one file Every class is displayed as beening in their own subgraph, with the subgraph as name. this is wrong. It should be grouped by the file name.

-
[ ] Classses throughout different files Every class is in a subgraph with their own name. This is wrong, and should be the name of the file they were in. Also, several classes are in the same file (Pester.ps1 for example) and all these classes should be displayed as such.
