Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active August 29, 2015 14:27
Show Gist options
  • Save ChaseFlorell/da5319be378b6bbd8922 to your computer and use it in GitHub Desktop.
Save ChaseFlorell/da5319be378b6bbd8922 to your computer and use it in GitHub Desktop.
First stab at translating functions.
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)"
}
}
# 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'
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
}
}
# 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'
@ChaseFlorell
Copy link
Author

Written because of this poshBAR issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment