Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/laurentdardenne/extensionmethod

Creation of ps1xml file dedicated to the extension methods contained in an assembly.
https://github.com/laurentdardenne/extensionmethod

ets-xml extension-methods powershell powershell-modules

Last synced: 10 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# ExtensionMethod

Creation of ps1xml file dedicated to the extension methods contained in an assembly.
From an idea of [Bart De Smet's](http://bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx)

To install this module :
```Powershell

$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](https://www.powershellgallery.com/packages/UncommonSense.PowerShell.TypeData) module, available on PSGallery.

This code return all types containing extension methods :

```Powershell
[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](https://github.com/MicrosoftDocs/PowerShell-Docs/tree/main/reference/docs-conceptual/developer/ets) it is possible to make extension methods available.

The goal is to adapt each method :

```xml


System.Management.Automation.Language.TokenKind


HasTrait

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


GetTrait
...
```

Thus it is possible to write :

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

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

```Powershell
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 :

```Powershell
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 :

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