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

Get-CUEnum doesn't list currently in memory loaded enums #104

Open Stephanevg opened 5 years ago

Stephanevg commented 5 years ago

Enum ComputerType {
    Server
    Client
}

Class Computer {
    [String]$Name
    [ComputerType]$Type
}

Function Get-InternalStuff {
    #Does internal stuff 
}

Function Get-ComputerData {
    #Does stuff
}

Export-ModuleMember -Function Get-ComputerData 

It doesn't list the ComputerType Enum when called as followed.

import-module psclassutils 
Get-CUEnum
LxLeChat commented 5 years ago

Hi, If we revamp the code from Get-LoadedClass and maybe create a Get-LoadedEnums i think we might be able to do want you want!

Here is part of the code from Get-LoadedClass


[Array]$LoadedClasses = [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetCustomAttributes($false) | Where-Object { $_ -is [System.Management.Automation.DynamicClassImplementationAssemblyAttribute]} } | ForEach-Object { $_.GetTypes() | Where-Object IsPublic}

$LoadedClasses | %{if($_.baseType.Name -eq 'Enum'){$_}} | select name,@{l = 'Path'; e = {($_.Module.ScopeName.Replace([char]0x29F9, '\').replace([char]0x589, ':')) -replace '^\\', ''}}
Name              Path
----              ----
ComputerType      powershell
GraphOutputFormat C:\Users\Lx\GitPerso\PSClassUtils\PSClassUtils\PSClassUtils.psm1
PesterType        C:\Users\Lx\GitPerso\PSClassUtils\PSClassUtils\PSClassUtils.psm1```

This can be a good starting point
LxLeChat commented 5 years ago

I Created a function Get-LoadedClass and modified Get-CUEnum (first draft), got the following result: Creating a new function was not necessary since Get-LoadedClass doesnt make the difference between Classes & Enums In fact we only need to modify Get-CUEnum since Get-LoadedClass doesnt make the difference between classes and enums.

The important bit is this:

           Foreach ( $Enum in (Get-CULoadedClass ) ) {
                If($Enum.IsEnum){
                    [ClassEnum]::New($Enum.Name,$Enum.members.Name)
                }
            }

Complete Function

Function Get-CUEnum{
    <#
    .SYNOPSIS
        This function returns enums existing in a document.
    .DESCRIPTION
        Returns a custom type [ClassEnum]
    .EXAMPLE
        Get-CuEnum -Path C:\plop\enum.ps1

        Returns:

        Name Member
        ---- ------
        woop {Absent, Present}

    .INPUTS
        String
    .OUTPUTS
        Classenum
    .NOTES   
        Author: Stéphane van Gulick
        Version: 0.2.0

    .LINK
        https://github.com/Stephanevg/PowerShellClassUtils
    #>
    [cmdletBinding()]
    Param(

        [Parameter(Mandatory=$false,ValueFromPipeline=$true)]
        [System.IO.FileInfo[]]$Path
    )

   begin{

   }

   Process{
        If ( $null -ne $PSBoundParameters['Path']) {
            foreach($p in $Path){

                $AST = Get-cuast -Path $p | ? {$_.IsEnum -eq $True}

                foreach($enum in $AST){
                    [ClassEnum]::New($enum.Name,$enum.members.Name)
                }
            }
        } Else {
            Foreach ( $Enum in (Get-CULoadedClass ) ) {
                If($Enum.IsEnum){
                    [ClassEnum]::New($Enum.Name,$Enum.members.Name)
                }
            }
        }
   }
   End{

   }
}

For the following Result

PS C:\Users\Lx\GitPerso\PSClassUtils\PSClassUtils> Get-CUEnum

Name              Member
----              ------
PesterType        {It, Describe, Context}
GraphOutputFormat {jpg, png, gif, imap...}