ottomatt - ExtensionMethod 1.3.2

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

PM> Install-Package ExtensionMethod -Version 1.3.2 -Source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard

> nuget.exe install ExtensionMethod -Version 1.3.2 -Source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard

> dotnet add package ExtensionMethod --version 1.3.2 --source https://www.myget.org/F/ottomatt/api/v3/index.json

Copy to clipboard
<PackageReference Include="ExtensionMethod" Version="1.3.2" />
Copy to clipboard
source https://www.myget.org/F/ottomatt/api/v3/index.json

nuget ExtensionMethod  ~> 1.3.2
Copy to clipboard

> choco install ExtensionMethod --version 1.3.2 --source https://www.myget.org/F/ottomatt/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "ottomatt" -SourceLocation "https://www.myget.org/F/ottomatt/api/v2"
Install-Module -Name "ExtensionMethod" -RequiredVersion "1.3.2" -Repository "ottomatt" 
Copy to clipboard

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 package install an unofficial version of the UncommonSense.PowerShell.TypeData module.

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

2017-06-04 Version 1.3.2

Change  New-ExtensionMethodType
         Adding method signature as comment
           version 1.3.1 :
              1 { [Nager.Date.Extensions.DateTimeExtension]::IsWeekend($this,$args[0]) }
           now :                
               # IsWeekend([System.DateTime] $dateTime, [Nager.Date.CountryCode] $countryCode)
	              1 { [Nager.Date.Extensions.DateTimeExtension]::IsWeekend($this,$args[0]) }
        

2017-04-06 Version 1.3.0

Add UncommonSense.PowerShell.TypeData Module v1.1 Fix New-Hastable

2017-02-12 Version 1.2.0

Refactoring New-ExtensionMethodTypeData function with UncommonSense.PowerShell.TypeData Module

2014-01-29 Version 1.1.0 Add Split feature on New-ExtensionMethodTypeData function

2010-07-31 Version 1.0.0 Original version

  • Any 0.0
    • Log4Posh (>= 2.0.1)
    • UncommonSense.PowerShell.TypeData (>= 1.0.2)

Owners

Laurent Dardenne

Authors

Dardenne Laurent

Project URL

https://github.com/LaurentDardenne/ExtensionMethod

License

Unknown

Tags

Template Conditionnal Directive Regex

Info

39 total downloads
34 downloads for version 1.3.2
Download (12.44 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
1.3.2 12.44 KB Sun, 04 Jun 2017 10:58:40 GMT 34
1.3.1 12.17 KB Sun, 16 Apr 2017 12:45:28 GMT 2
1.3.0 12.17 KB Thu, 06 Apr 2017 14:30:52 GMT 2
1.2.0 11.95 KB Thu, 16 Feb 2017 18:48:03 GMT 1