Skip to content

LaurentDardenne/ExtensionMethod

Repository files navigation

ExtensionMethod

Creation of ps1xml file dedicated to the extension methods contained in an assembly. From an idea of Bart De Smet's

To install this module :

$PSGalleryPublishUri = 'https://www.myget.org/F/ottomatt/api/v2/package'
$PSGallerySourceUri = 'https://www.myget.org/F/ottomatt/api/v2'

Register-PSRepository -Name OttoMatt -SourceLocation $PSGallerySourceUri -PublishLocation $PSGalleryPublishUri #-InstallationPolicy Trusted
Install-Module ExtensionMethod -Repository OttoMatt

Note : this module depends on the UncommonSense.PowerShell.TypeData module, available on PSGallery.

This code return all types containing extension methods :

 [psobject].Assembly.ExportedTypes|Find-ExtensionMethod -ExcludeGeneric|%  {$_.ToString()}

#System.Management.Automation.Language.TokenFlags GetTraits(System.Management.Automation.Language.TokenKind)
#Boolean HasTrait(System.Management.Automation.Language.TokenKind, System.Management.Automation.Language.TokenFlags)
#System.String Text(System.Management.Automation.Language.TokenKind)

By default Powershell can not use them, but with ETS it is possible to make extension methods available.

The goal is to adapt each method :

<?xml version="1.0" encoding="utf-8"?>
<Types>
  <Type>
    <Name>System.Management.Automation.Language.TokenKind</Name>
    <Members>
      <ScriptMethod>
         <Name>HasTrait</Name>
       <Script>
         switch ($args.Count) {
            1 { [System.Management.Automation.Language.TokenTraits]::HasTrait($this,$args[0])}

            default { throw "No overload for 'HasTrait' takes the specified number of parameters ($($args.Count))." }
         }</Script>
      </ScriptMethod>
      <ScriptMethod>
        <Name>GetTrait</Name>
             ...

Thus it is possible to write :

$code.Ast.EndBlock.BlockKind.HasTrait('MemberName')

The New-ExtendedTypeData function create one or many files from the extension methods contained in an assembly

Add-Type -Path $AssemblyPath -Pass|
 New-ExtendedTypeData -Path c:\temp\TestPs1Xml\All.ps1xml -All

WARNING: Excluded method : System.Boolean.ToString()
WARNING: Excluded method : System.Object.ToString()
WARNING: Excluded method : System.Char.ToString()

NOTE: The ToString() method can be generate recursiv call, they are excluded. The generic methods and those returning a type Interface are excluded.

The -All parameter group all definitions to a single file :

dir  -Path c:\temp\TestPs1Xml

    Directory: C:\temp\TestPs1Xml

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       16/02/2017     12:57         269651 All.ps1xml

The absence of the -All parameter creates a file by type, the filename is the name of the corresponding type :

Add-Type -Path $AssemblyPath -Pass|
 New-ExtendedTypeData -Path c:\temp\TestPs1Xml\All.ps1xml

dir  -Path c:\temp\TestPs1Xml|more

    Directory: C:\temp\TestPs1Xml

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       16/02/2017     12:57         269651 All.ps1xml
-a----       16/02/2017     13:07           5636 System.Array.ps1xml
-a----       16/02/2017     13:07           1098 System.Boolean.ps1xml
-a----       16/02/2017     13:07           1122 System.Byte.ps1xml
-a----       16/02/2017     13:07           4404 System.Byte.Array.ps1xml
-a----       16/02/2017     13:07           8636 System.Char.ps1xml
-a----       16/02/2017     13:07            509 System.Collections.Specialized.NameValueCollection.ps1xml
-a----       16/02/2017     13:07            864 System.Data.Common.DbCommand.ps1xml
-a----       16/02/2017     13:07           3736 System.Data.Common.DbConnection.ps1xml
...

About

Creation of ps1xml file dedicated to the extension methods contained in an assembly.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •