Stephanevg / PSClassUtils

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

Add -Path parameter to functions #47

Closed Stephanevg closed 5 years ago

Stephanevg commented 5 years ago

I would like to add the possiblity to get class information of classes located in files. For this, adding -Path parameter seems to be the best solution.

The parameter should be added to the following functions:

The -Path parameter should be allowed to receive only either .ps1 or .psm1 files, any other types should be ignored.

edits after discussions

As suggested by @LxLeChat we could make -Path accept pipelineinput, and set an alias on it. It will allow us to avoid an extra complicated design choise, and handle folder input. It will also make the cmdlet more user friendly, as one could use it like this:


Get-ChildItem -Path C:\MyFolderWithClasses\ -Recurse | Get-CuXXX

Also, what should not be forgoten, is that currently, the Get-CUXXX cmdlets have only one parameter -ClassName and it should be kept.

When no -Path is passed, the -ClassName should look into the current loaded classes (current existing code). When -ClassName & -Path are together, -ClassName should filter the file out, that has a class having the name $ClassName

end of edits

This functionality has already been implmemented in Write-CUClassDiagram as in the following snippet:


$PathObject = Get-Item $Path
if ($PathObject -is [System.IO.DirectoryInfo]) {
$ExportFileName = "Diagram" + "." + $OutputFormat
$FolderPath = $Path
    if ($Recurse) {

        $AllItems = Get-ChildItem -path "$($Path)\*" -Include "*.ps1", "*.psm1" -Recurse
    } else {
        $AllItems = Get-ChildItem -path "$($Path)\*" -Include "*.ps1", "*.psm1"
    }
    #$Path = $null
}
elseif ($PathObject -is [System.IO.FileInfo]) {
    [System.IO.FileInfo]$File = (Resolve-Path -Path $Path).Path
    $ExportFileName = $File.BaseName + "." + $OutputFormat
    $AllItems = $File
}
else {
    throw 'Path provided was not a file or folder'
}

> and we could reuse this to save some time.

(A switch would be even better ;))

## Return type

i would imagine that the return type should be extended as well. I could see something similar to this:

|Source| ClassName | Object
|---|---|---|
[System.IO.FileInfo]|`TheNameOfTheClass`| `The object request`
|C:\Files\Myclass.ps1| CustomUser | [CUClass]

Once we agree on the design, I will create individual issues per function, so work could be done by severals persons in parallel.
`
LxLeChat commented 5 years ago

Hello @Stephanevg as discussed on the chat, maybe the path parameter of the cmdlet shoud only be a fileinfo type. With the correct alias for Path (example fullname), and the following properties in your parameter: acceptvaluefrompipelinebypropertyname this could do the trick

Stephanevg commented 5 years ago

I agree, I think it is quite elegant actually. I'll update the first comment I made to take this into consideration. (See edits parts above)

LxLeChat commented 5 years ago

I rewrote the Get-CUClass and the return is an ASTDocument type. You can review the code is you like it in my dev_branch: Get-CUClass

PS C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils> Get-CUClass

Classes            Enums Source
-------            ----- ------
{ClassProperty}          C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\01_ClassProperty.ps1
{ClassMethod}            C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\02_ClassMethod.ps1
{ClassConstructor}       C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\03_ClassConstructor.ps1
{ASTDocument}            C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\04_ASTDocument.ps1
{ClassEnum}              C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\05_ClassEnum.ps1

With Path parameter

PS C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private> Get-CUClass -Path .\01_ClassProperty.ps1

Classes         Enums Source
-------         ----- ------
{ClassProperty}       C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\01_ClassProperty.ps1

From the pipeline

PS C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes> gci -Recurse | Get-CUClass

Classes            Enums Source
-------            ----- ------
{ClassProperty}          C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\01_ClassProperty.ps1
{ClassMethod}            C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\02_ClassMethod.ps1
{ClassConstructor}       C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\03_ClassConstructor.ps1
{ASTDocument}            C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\04_ASTDocument.ps1
{ClassEnum}              C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\05_ClassEnum.ps1

With an array of paths

PS C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils> Get-CUClass -Path ".\Classes\Private\01_ClassProperty.ps1","C:\Users\Lxlechat\GitPerso\HostsFileManagement\HostsFileManagement\Classes\Public\HostsEntry.ps1"

Classes         Enums Source
-------         ----- ------
{ClassProperty}       C:\Users\Lxlechat\GitPerso\PSClassUtils\PsClassUtils\Classes\Private\01_ClassProperty.ps1
{HostsEntry}          C:\Users\Lxlechat\GitPerso\HostsFileManagement\HostsFileManagement\Classes\Public\HostsEntry.ps1

The main change i did was modyfying the default behavior where i replace the name property that was firest return, to a path

$LoadedClasses = [AppDomain]::CurrentDomain.GetAssemblies() |
            Where-Object { $_.GetCustomAttributes($false) |
            Where-Object { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute]} } |
            ForEach-Object { $_.GetTypes() |
                Where-Object IsPublic |
                Where-Object { $_.Name -like $Name } |
            Select-Object @{l='Path';e={($_.Module.ScopeName.Replace([char]0x29F9,'\').replace([char]0x589,':')) -replace '^\\',''}}
            }

Thanks to @NicolasBn for helping me resolve the conversion of the strange ScopeName encoding! and his help for the DefaultParameterSetName !! Team Work !

Stephanevg commented 5 years ago

Closing as this has been merged.