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

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

Open
Stephanevg opened this issue Mar 30, 2019 · 2 comments
Open

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

Stephanevg opened this issue Mar 30, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@Stephanevg
Copy link
Owner

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
@Stephanevg Stephanevg added the bug Something isn't working label Mar 30, 2019
@LxLeChat
Copy link
Collaborator

LxLeChat commented Apr 19, 2019

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 added a commit to LxLeChat/PSClassUtils that referenced this issue Apr 19, 2019
Fixing issue Stephanevg#104
@LxLeChat
Copy link
Collaborator

LxLeChat commented Apr 19, 2019

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

LxLeChat added a commit to LxLeChat/PSClassUtils that referenced this issue Apr 20, 2019
Fixing issue Stephanevg#104
Fixing issue Stephanevg#103
Add Pipeline support for Get-CUEnum Stephanevg#102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants