-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create sh shim #1951
Create sh shim #1951
Conversation
lib/install.ps1
Outdated
@@ -701,7 +701,7 @@ function rm_shim($name, $shimdir) { | |||
} | |||
|
|||
# other shim types might be present | |||
'.exe', '.shim', '.cmd' | % { | |||
'', ''.exe', '.shim', '.cmd' | % { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Syntax error - there's an extra '
before the .exe
I would like to see something like this implemented too, and after correcting the syntax error it works with the (limited) testing I've one. However I think the shellscript shims should only be created if the target is a .ps1 or a .cmd/.bat file. For the .exe's they're just redundant. |
@Fleshgrinder 👍 I would have put the shellscript shim creation in both the .bat and the .ps1 blocks instead, even if it's a bit of repetition, but your suggestion works just as well. I don't really have a preference. |
Wait sorry I just noticed something, we do need separate shim code for the .bat/.cmd and the .ps1 paths, otherwise we get this for .cmd shims:
Now starting powershell to run a .cmd file does work, but it's not really the best, especially given how slow PS is starting up :) Just this is apparently enough for .cmd shims
Testing this with Git Bash btw, I don't know if any of this stuff works in WSL or Cygwin |
Your proposed CMD shim cannot work in WSL because CMD files have no shebang, hence, Linux won't be able to figure out what to start it with. However, we can simply use |
lib/core.ps1
Outdated
@@ -234,6 +234,9 @@ function shim($path, $global, $name, $arg) { | |||
# shim .bat, .cmd so they can be used by programs with no awareness of PSH | |||
$shim_cmd = "$(strip_ext($shim)).cmd" | |||
"@`"$(resolve-path $path)`" $arg %*" | out-file $shim_cmd -encoding ascii | |||
|
|||
$shim_sh = "$(strip_ext($shim))" | |||
"#!/bin/sh`ncmd `"$(resolve-path $path)`" `"$@`"" | out-file $shim_sh -encoding ascii |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't $arg be in here too?
"#!/bin/sh`ncmd `"$(resolve-path $path)`" $arg `"$@`""
lib/core.ps1
Outdated
$shim_cmd = $(strip_ext($shim)) | ||
"#!/bin/sh`npowershell -ex unrestricted `"$(resolve-path $path)`" `"$@`"" | out-file $shim_cmd -encoding ascii | ||
$shim_sh = "$(strip_ext($shim))" | ||
"#!/bin/sh`npowershell -ex unrestricted `"$(resolve-path $path)`" `"$@`"" | out-file $shim_sh -encoding ascii |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too
"#!/bin/sh`npowershell -ex unrestricted `"$(resolve-path $path)`" $arg `"$@`""
Nice. We just forgot to put in $arg there for the shim args I think (haven't tested my suggestions) |
Only question left is if we have to quote |
It's not a very good name IMO, but it's either a single string or an array of strings.* * There's some code that treats |
Yeah, |
Ok one more thing: I don't know about WSL but just "#!/bin/sh`ncmd //C `"$(resolve-path $path)`" $arg `"$@`"" | out-file $shim_sh -encoding ascii |
I added the switch for script execution and immediate termination. I guess we're fine now. 👍 |
👍 (cc @r15ch13) |
Some more changes:
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)
if(!$name) { $name = strip_ext (fname $path) }
$shim = "$abs_shimdir\$($name.tolower())"
# convert to relative path
pushd $abs_shimdir
$relative_path = resolve-path -relative $path
popd
$resolved_path = resolve-path $path
# if $path points to another drive resolve-path prepends .\ which could break shims
if($relative_path -match "^(.\\[\w]:).*$") {
write-output "`$path = `"$path`"" | out-file "$shim.ps1" -encoding utf8
} else {
write-output "`$path = join-path `"`$psscriptroot`" `"$relative_path`"" | out-file "$shim.ps1" -encoding utf8
}
if($arg) {
write-output "`$args = '$($arg -join "', '")', `$args" | out-file "$shim.ps1" -encoding utf8 -append
}
if($path -match '\.jar$') {
"if(`$myinvocation.expectingInput) { `$input | & java -jar `$path @args } else { & java -jar `$path @args }" | out-file "$shim.ps1" -encoding utf8 -append
} else {
"if(`$myinvocation.expectingInput) { `$input | & `$path @args } else { & `$path @args }" | out-file "$shim.ps1" -encoding utf8 -append
}
if($path -match '\.exe$') {
# for programs with no awareness of any shell
cp "$(versiondir 'scoop' 'current')\supporting\shimexe\shim.exe" "$shim.exe" -force
write-output "path = $resolved_path" | out-file "$shim.shim" -encoding utf8
if($arg) {
write-output "args = $arg" | out-file "$shim.shim" -encoding utf8 -append
}
} elseif($path -match '\.((bat)|(cmd))$') {
# shim .bat, .cmd so they can be used by programs with no awareness of PSH
"@`"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii
"#!/bin/sh`ncmd //C `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} elseif($path -match '\.ps1$') {
# make ps1 accessible from cmd.exe
"@echo off
setlocal enabledelayedexpansion
set args=%*
:: replace problem characters in arguments
set args=%args:`"='%
set args=%args:(=``(%
set args=%args:)=``)%
set invalid=`"='
if !args! == !invalid! ( set args= )
powershell -noprofile -ex unrestricted `"& '$resolved_path' %args%;exit `$lastexitcode`"" | out-file "$shim.cmd" -encoding ascii
"#!/bin/sh`npowershell -ex unrestricted `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} elseif($path -match '\.jar$') {
"@java -jar `"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii
"#!/bin/sh`njava -jar `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
}
} |
Add "bin": [
["main.exe", "", "-somearg"]
], # other shim types might be present
'', '.exe', '.shim', '.cmd' | % {
if(test-path -Path "$shimdir\$name$_" -PathType leaf ) {
rm "$shimdir\$name$_"
}
} |
@r15ch13 two questions:
|
@Fleshgrinder yeah, there are some programs that only provide a |
Oh, missed the last condition and only looked at the first |
Closes #1949