Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -Path parameter to functions #47

Closed
Stephanevg opened this issue Oct 14, 2018 · 4 comments
Closed

Add -Path parameter to functions #47

Stephanevg opened this issue Oct 14, 2018 · 4 comments
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@Stephanevg
Copy link
Owner

Stephanevg commented Oct 14, 2018

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:

  • Get-CuClass
  • Get-CUClassMethod
  • Get-CUClassConstructor
  • Get-CUClassProperty (Not sure about this one though)

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.
`

@Stephanevg Stephanevg added enhancement New feature or request help wanted Extra attention is needed question Further information is requested labels Oct 14, 2018
@LxLeChat
Copy link
Collaborator

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
Copy link
Owner Author

Stephanevg commented Oct 14, 2018

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
Copy link
Collaborator

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 !

This was referenced Oct 15, 2018
@Stephanevg
Copy link
Owner Author

Closing as this has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants