Last active
August 29, 2015 14:27
-
-
Save ChaseFlorell/da5319be378b6bbd8922 to your computer and use it in GitHub Desktop.
First stab at translating functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true, Position=0)]$key | |
) | |
Write-Verbose "Attempting to establish new function '$key'" | |
$keyParts = ($key -split '-') | |
if($keyParts.length -gt 0){ | |
$validVerb = Get-Verb $keyParts[0] | |
if(!$validVerb){ | |
Write-Warning "'$($keyParts[0])' is an unapproved verb which might make it less discoverable. Use the Verbose parameter for more detail or type Get-Verb to see the list of approved verbs." | |
} | |
} | |
Write-Output "Function:$key" | |
} | |
function As{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [ValidateScript({$_.StartsWith('Function:')})][string] $funcKey, | |
[Parameter(Mandatory=$true, Position=0)]$function | |
) | |
$funcImp = "Function:script:$function" | |
if(Test-Path $funcKey){ | |
Write-Verbose "Original function: $funcKey" | |
Write-Verbose "Implemented as: $funcImp" | |
Copy $funcKey $funcImp | |
Write-Verbose "Copied: $(Test-Path $funcImp)" | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define functions | |
function Invoke-Nunit($param) { | |
Write-Host "Nunit: $param" | |
} | |
function Invoke-Xunit($param) { | |
Write-Host "Xunit: $param" | |
} | |
# register as specific function name | |
Register-Function Invoke-NUnit | As Invoke-UnitTests | |
# invoke new function | |
Invoke-UnitTests -param 'C:\path\to\unit.tests.dll' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Rename-Function { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, Position=0)] [string] $key | |
) | |
Write-Verbose "Attempting to establish new function '$key'" | |
$keyParts = ($key -split '-') | |
if($keyParts.length -gt 0){ | |
$validVerb = Get-Verb $keyParts[0] | |
if(!$validVerb){ | |
Write-Warning "'$($keyParts[0])' is an unapproved verb which might make it less discoverable. Use the Verbose parameter for more detail or type Get-Verb to see the list of approved verbs." | |
} | |
} | |
Write-Output "Function:$key" | |
} | |
function To{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [ValidateScript({$_.StartsWith('Function:')})][string] $funcKey, | |
[Parameter(Mandatory=$true, Position=0)]$function | |
) | |
$funcImp = "Function:script:$function" | |
if(Test-Path $funcKey){ | |
Write-Verbose "Original function: $funcKey" | |
Write-Verbose "Implemented as: $funcImp" | |
Copy $funcKey $funcImp | |
Write-Verbose "Copied: $(Test-Path $funcImp)" | |
} | |
Write-Output $function | |
} | |
function WithAlias { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] [string] $functionName, | |
[Parameter(Mandatory=$true, Position=0)] [Array] $aliases | |
) | |
$aliases | % { | |
Set-Alias -name $_ -value $functionName -Scope Script | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define functions | |
function Invoke-Nunit($param) { | |
Write-Host "Nunit: $param" | |
} | |
function Invoke-Xunit($param) { | |
Write-Host "Xunit: $param" | |
} | |
# register as specific function name | |
Rename-Function Invoke-XUnit | To Invoke-UnitTests | WithAlias 'test' | |
# invoke new function | |
Test -param 'C:\path\to\unit.tests.dll' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Written because of this poshBAR issue