Stephanevg / PSClassUtils

A set of utilities to work with Powershell Classes
http://powershelldistrict.com/how-to-generate-a-uml-diagram-using-powershell/
91 stars 23 forks source link

Refactor Write-CUClassDiagram #98

Open Stephanevg opened 5 years ago

Stephanevg commented 5 years ago

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

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:

Stephanevg commented 5 years ago

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: