forked from lukesampson/psutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.ps1
53 lines (39 loc) · 1.59 KB
/
shim.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
param($path, [switch]$help)
Set-StrictMode -Off;
$usage = "usage: shim <path>"
function create_shim($path) {
if(!(test-path $path)) { "shim: couldn't find $path"; exit 1 }
$path = resolve-path $path # ensure full path
$shimdir = "~/appdata/local/shims"
if(!(test-path $shimdir)) { mkdir $shimdir > $null }
$shimdir = resolve-path $shimdir
ensure_in_path $shimdir
$fname_stem = [io.path]::getfilenamewithoutextension($path).tolower()
$shim = "$shimdir\$fname_stem.ps1"
echo "`$path = '$path'" > $shim
echo 'if($myinvocation.expectingInput) { $input | & $path @args } else { & $path @args }' >> $shim
if($path -match '\.((exe)|(bat)|(cmd))$') {
# shim .exe, .bat, .cmd so they can be used by programs with no awareness of PSH
"@`"$path`" %*" | out-file "$shimdir\$fname_stem.cmd" -encoding oem
} elseif($path -match '\.ps1$') {
# make ps1 accessible from cmd.exe
"@powershell -noprofile -ex unrestricted `"& '$path' %*;exit `$lastexitcode`"" | out-file "$shimdir\$fname_stem.cmd" -encoding oem
}
}
function env($name,$val='__get') {
$target = 'User'
if($val -eq '__get') { [environment]::getEnvironmentVariable($name,$target) }
else { [environment]::setEnvironmentVariable($name,$val,$target) }
}
function ensure_in_path($dir) {
$path = env 'path'
$dir = resolve-path $dir
if($path -notmatch [regex]::escape($dir)) {
echo "adding $dir to your path"
env 'path' "$dir;$path" # for future sessions...
$env:path = "$dir;$env:path" # for this session
}
}
if('/?', '--help' -contains $path -or $help) { $usage; exit }
if(!$path) { "shim: path missing"; $usage; exit 1; }
create_shim $path