Skip to content

Commit

Permalink
allow alias and arguments for shims
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesampson committed Jan 12, 2014
1 parent ac75cb5 commit 3b90306
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
17 changes: 13 additions & 4 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,38 @@ function unzip($path,$to) {
[io.compression.zipfile]::extracttodirectory($path,$to)
}

function shim($path, $global) {
function shim($path, $global, $name, $arg) {
if(!(test-path $path)) { abort "can't shim $(fname $path): couldn't find $path" }
$abs_shimdir = ensure (shimdir $global)
$shim = "$abs_shimdir\$(strip_ext(fname $path).tolower()).ps1"
if(!$name) { $name = strip_ext (fname $path) }

$shim = "$abs_shimdir\$($name.tolower()).ps1"

# note: use > for first line to replace file, then >> to append following lines
echo '# ensure $HOME is set for MSYS programs' > $shim
echo "if(!`$env:home) { `$env:home = `"`$home\`" }" >> $shim
echo 'if($env:home -eq "\") { $env:home = $env:allusersprofile }' >> $shim
echo "`$path = '$path'" >> $shim
if($arg) {
echo "`$args = '$($arg -join "', '")', `$args" >> $shim
}
echo 'if($myinvocation.expectingInput) { $input | & $path @args } else { & $path @args }' >> $shim

if($path -match '\.exe$') {
# for programs with no awareness of any shell
$shim_exe = "$(strip_ext($shim)).shim"
cp "$(versiondir 'scoop' 'current')\supporting\shimexe\shim.exe" "$(strip_ext($shim)).exe" -force
echo "path = $(resolve-path $path)" | out-file "$(strip_ext($shim)).shim" -encoding oem
echo "path = $(resolve-path $path)" | out-file $shim_exe -encoding oem
if($arg) {
echo "args = $arg" | out-file $shim_exe -encoding oem -append
}
} elseif($path -match '\.((bat)|(cmd))$') {
# shim .bat, .cmd so they can be used by programs with no awareness of PSH
$shim_cmd = "$(strip_ext($shim)).cmd"
':: ensure $HOME is set for MSYS programs' | out-file $shim_cmd -encoding oem
'@if "%home%"=="" set home=%homedrive%%homepath%\' | out-file $shim_cmd -encoding oem -append
'@if "%home%"=="\" set home=%allusersprofile%\' | out-file $shim_cmd -encoding oem -append
"@`"$(resolve-path $path)`" %*" | out-file $shim_cmd -encoding oem -append
"@`"$(resolve-path $path)`" $arg %*" | out-file $shim_cmd -encoding oem -append
} elseif($path -match '\.ps1$') {
# make ps1 accessible from cmd.exe
$shim_cmd = "$(strip_ext($shim)).cmd"
Expand Down
28 changes: 18 additions & 10 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -430,35 +430,43 @@ function run_uninstaller($manifest, $architecture, $dir) {
}
}

# get target, name, arguments for shim
function shim_def($item) {
if($item -is [array]) { return $item }
return $item, (strip_ext (fname $item)), $null
}

function create_shims($manifest, $dir, $global) {
$manifest.bin | ?{ $_ -ne $null } | % {
echo "creating shim for $_"
$target, $name, $arg = shim_def $_
echo "creating $name shim"

# check valid bin
$bin = "$dir\$_"
$bin = "$dir\$target"
if(!(is_in_dir $dir $bin)) {
abort "error in manifest: bin '$_' is outside the app directory"
abort "error in manifest: bin '$target' is outside the app directory"
}
if(!(test-path $bin)) { abort "can't shim $_`: file doesn't exist"}
if(!(test-path $bin)) { abort "can't shim $target`: file doesn't exist"}

shim "$dir\$_" $global
shim "$dir\$target" $global $name $arg
}
}
function rm_shims($manifest, $global) {
$manifest.bin | ?{ $_ -ne $null } | % {
$shim = "$(shimdir $global)\$(strip_ext(fname $_)).ps1"
$target, $name, $null = shim_def $_
$shimdir = shimdir $global
$shim = "$shimdir\$name.ps1"

if(!(test-path $shim)) { # handle no shim from failed install
warn "shim for $_ is missing, skipping"
warn "shim for $name is missing, skipping"
} else {
echo "removing shim for $_"
echo "removing shim for $name"
rm $shim
}

# other shim types might be present
$base = strip_ext $shim
'.exe', '.shim', '.cmd' | % {
if(test-path "$base$_") { rm "$base$_" }
if(test-path "$shimdir\$name$_") { rm "$shimdir\$name$_" }
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions supporting/shimexe/build.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$fwdir = gci C:\Windows\Microsoft.NET\Framework\ -dir | sort -desc | select -first 1

pushd $psscriptroot
& "$($fwdir.fullname)\csc.exe" /nologo shim.cs
popd
8 changes: 7 additions & 1 deletion supporting/shimexe/shim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ class Program {

var config = Config(configPath);
var path = Get(config, "path");
var add_args = Get(config, "args");
if(!string.IsNullOrEmpty(add_args)) {
add_args += " ";
} else {
add_args = "";
}

var p = new Process();
p.StartInfo.FileName = path;
p.StartInfo.Arguments = Serialize(args);
p.StartInfo.Arguments = add_args + Serialize(args);

p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
Expand Down
Binary file modified supporting/shimexe/shim.exe
Binary file not shown.

0 comments on commit 3b90306

Please sign in to comment.