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
> nuget.exe install ExtensionMethod -Version 1.3.2 -Source https://www.myget.org/F/ottomatt/api/v3/index.json
> dotnet add package ExtensionMethod --version 1.3.2 --source https://www.myget.org/F/ottomatt/api/v3/index.json
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
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 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
...
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
OwnersLaurent Dardenne |
AuthorsDardenne Laurent |
Project URLhttps://github.com/LaurentDardenne/ExtensionMethod |
LicenseUnknown |
TagsTemplate Conditionnal Directive Regex |
Info55 total downloads |
46 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 | 46 | ||||
1.3.1 | 12.17 KB | Sun, 16 Apr 2017 12:45:28 GMT | 4 | ||||
1.3.0 | 12.17 KB | Thu, 06 Apr 2017 14:30:52 GMT | 3 | ||||
1.2.0 | 11.95 KB | Thu, 16 Feb 2017 18:48:03 GMT | 2 |