Skip to content

Commit 5a9457b

Browse files
committed
SupportsShouldProcess in Set-Function, implement some PSScriptAnalyzer suggestions
1 parent a0c9d86 commit 5a9457b

File tree

4 files changed

+58
-75
lines changed

4 files changed

+58
-75
lines changed

Aliases.ps1

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
<#
2-
.SYNOPSIS
3-
Aliases ¯\_(ツ)_/¯
4-
#>
1+
. "$PSScriptRoot\Set-Function.ps1"
52

6-
# Make sure Define-Command is available
7-
. "$PSScriptRoot\Define-Function.ps1"
8-
9-
Define-Function ga {git add @ARGS} -Force
10-
Define-Function gd {git diff @ARGS} -Force
11-
Define-Function gc {git commit @ARGS} -Force
12-
Define-Function gs {git status @ARGS} -Force
3+
Set-Function ga {git add @ARGS}
4+
Set-Function gd {git diff @ARGS}
5+
Set-Function gc {git commit @ARGS}
6+
Set-Function gs {git status @ARGS}
137

148
Function Find-ProgramPath ([String] $name, [uint32] $depth=2) {
159
Function find-in([String] $path) {

Define-Function.ps1

Lines changed: 0 additions & 63 deletions
This file was deleted.

Set-Function.ps1

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function Set-Function {
2+
<#
3+
.SYNOPSIS
4+
Create a PowerShell function
5+
6+
.DESCRIPTION
7+
This is useful if you want to create a function where the name
8+
is given by an arbitrary string expression.
9+
10+
When defining a function with the PowerShell "Function" keyword
11+
then a fixed name has to be given.
12+
13+
.PARAMETER RemoveAlias
14+
If an alias exists with the given name, remove it. This is neccessary because
15+
aliases have higher precedence than functions.
16+
17+
.EXAMPLE
18+
Set-Function "${funname}-asdf" {Write-Output "Hello World"}
19+
20+
.EXAMPLE
21+
Set-Function "existingfunction" {Write-Output "new implementation"} -Confirm
22+
#>
23+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
24+
param(
25+
[Parameter(Mandatory)] [String] $Name,
26+
[Parameter(Mandatory)] [ScriptBlock] $Implementation,
27+
[Switch] $NoRemoveAlias
28+
)
29+
30+
# Check if the function already exists
31+
if (Test-Path "Function:$Name") {
32+
if ($PSCmdlet.ShouldProcess("Function:$Name", 'Overwrite the function.')) {
33+
Remove-Item -Path "Function:$Name" -Confirm:$false
34+
} else {
35+
return
36+
}
37+
}
38+
39+
# Check if an alias with this name exists
40+
if ((-not $NoRemoveAlias) -and (Test-Path "Alias:$Name")) {
41+
if ($PSCmdlet.ShouldProcess("Alias:$Name", 'Remove the alias.')) {
42+
Remove-Item "Alias:$Name" -Force -Confirm:$False
43+
44+
# We need to run this a second time because if a local alias exists
45+
# inside the function scope then the first time we only remove
46+
# the local alias, the second time the global alias is removed.
47+
if (Test-Path "Alias:$Name") {Remove-Item "Alias:$Name" -Force}
48+
}
49+
}
50+
51+
New-Item -Path "Function:global:$Name" -Value $Implementation -Confirm:$False | Out-Null
52+
}

Utils.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Function Search-Google {
1313
$query = 'https://www.google.com/search?q='
1414
foreach ($arg in $ARGS) { $query += "${arg}+" }
1515
$url = $query.Substring(0, $query.Length - 1)
16-
start "$url"
16+
Start-Process "$url"
1717
}
1818

1919
Set-Alias glg Search-Google

0 commit comments

Comments
 (0)