Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

- **depends:** Prevent error on no URL ([#4595](https://github.com/ScoopInstaller/Scoop/issues/4595))
- **decompress:** Fix nested Zstd archive extraction ([#4608](https://github.com/ScoopInstaller/Scoop/issues/4608))
- **shim:** Fix PS1 shim error when in different drive in PS7 ([#4614](https://github.com/ScoopInstaller/Scoop/issues/4614))

### Documentation

Expand Down
132 changes: 78 additions & 54 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -577,90 +577,114 @@ function shim($path, $global, $name, $arg) {
} elseif ($path -match '\.(bat|cmd)$') {
# shim .bat, .cmd so they can be used by programs with no awareness of PSH
warn_on_overwrite "$shim.cmd" $path
"@rem $resolved_path
@`"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
@(
"@rem $resolved_path",
"@`"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII

warn_on_overwrite $shim $path
"#!/bin/sh
# $resolved_path
MSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
@(
'#!/bin/sh',
"# $resolved_path",
"MSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`""
) -join "`r`n" | Out-File $shim -Encoding ASCII
Comment thread
niheaven marked this conversation as resolved.
Outdated
} elseif ($path -match '\.ps1$') {
# if $path points to another drive resolve-path prepends .\ which could break shims
warn_on_overwrite "$shim.ps1" $path
$ps1text = if ($relative_path -match '^(.\\[\w]:).*$') {
"# $resolved_path
`$path = `"$path`"
if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }
exit `$lastexitcode"
$ps1text = if ($relative_path -match '^(\.\\)?\w:.*$') {
@(
"# $resolved_path",
"`$path = `"$path`"",
"if (`$MyInvocation.ExpectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }",
"exit `$LASTEXITCODE"
)
} else {
# Setting PSScriptRoot in Shim if it is not defined, so the shim doesn't break in PowerShell 2.0
"# $resolved_path
if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }
`$path = join-path `"`$psscriptroot`" `"$relative_path`"
if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }
exit `$lastexitcode"
@(
"# $resolved_path",
"if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }",
"`$path = Join-Path `"`$PSScriptRoot`" `"$relative_path`"",
"if (`$MyInvocation.ExpectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }",
"exit `$LASTEXITCODE"
)
}
$ps1text | Out-File "$shim.ps1" -Encoding ASCII
$ps1text -join "`r`n" | Out-File "$shim.ps1" -Encoding ASCII

# make ps1 accessible from cmd.exe
warn_on_overwrite "$shim.cmd" $path
"@rem $resolved_path
@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= )
where /q pwsh.exe
if %errorlevel% equ 0 (
pwsh -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"
) else (
powershell -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"
)" | Out-File "$shim.cmd" -Encoding ASCII
@(
"@rem $resolved_path",
'@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= )',
'where /q pwsh.exe',
'if %errorlevel% equ 0 (',
" pwsh -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"",
') else (',
" powershell -noprofile -ex unrestricted -command `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"",
')'
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII

warn_on_overwrite $shim $path
"#!/bin/sh
# $resolved_path
if command -v pwsh.exe &> /dev/null; then
pwsh.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"
else
powershell.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"
fi" | Out-File $shim -Encoding ASCII
@(
'#!/bin/sh',
"# $resolved_path",
'if command -v pwsh.exe &> /dev/null; then',
" pwsh.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"",
Comment thread
niheaven marked this conversation as resolved.
Outdated
'else',
" powershell.exe -noprofile -ex unrestricted -command `"& '$resolved_path' $arg $@;exit \`$lastexitcode`"",
Comment thread
niheaven marked this conversation as resolved.
Outdated
'fi'
) -join "`r`n" | Out-File $shim -Encoding ASCII
Comment thread
niheaven marked this conversation as resolved.
Outdated
} elseif ($path -match '\.jar$') {
warn_on_overwrite "$shim.cmd" $path
"@rem $resolved_path
@java -jar `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
@(
"@rem $resolved_path",
"@java -jar `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII

warn_on_overwrite $shim $path
"#!/bin/sh
# $resolved_path
java -jar `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
@(
'#!/bin/sh',
"# $resolved_path",
"java -jar `"$resolved_path`" $arg `"$@`""
) -join "`r`n" | Out-File $shim -Encoding ASCII
Comment thread
niheaven marked this conversation as resolved.
Outdated
} elseif ($path -match '\.py$') {
warn_on_overwrite "$shim.cmd" $path
"@rem $resolved_path
@python `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
@(
"@rem $resolved_path",
"@python `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII

warn_on_overwrite $shim $path
"#!/bin/sh
# $resolved_path
python `"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
@(
'#!/bin/sh',
"# $resolved_path",
"python `"$resolved_path`" $arg `"$@`""
) -join "`r`n" | Out-File $shim -Encoding ASCII
Comment thread
niheaven marked this conversation as resolved.
Outdated
} else {
warn_on_overwrite "$shim.cmd" $path
# find path to Git's bash so that batch scripts can run bash scripts
$gitdir = (Get-Item (Get-Command git -ErrorAction:Stop).Source -ErrorAction:Stop).Directory.Parent
if ($gitdir.FullName -imatch 'mingw') {
$gitdir = $gitdir.Parent
}
"@rem $resolved_path
@`"$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe')`" `"$resolved_path`" $arg %*" | Out-File "$shim.cmd" -Encoding ASCII
@(
"@rem $resolved_path",
"@`"$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe')`" `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII

warn_on_overwrite $shim $path
"#!/bin/sh
# $resolved_path
`"$resolved_path`" $arg `"$@`"" | Out-File $shim -Encoding ASCII
@(
'#!/bin/sh',
"# $resolved_path",
"`"$resolved_path`" $arg `"$@`""
) -join "`r`n" | Out-File $shim -Encoding ASCII
Comment thread
niheaven marked this conversation as resolved.
Outdated
}
}

Expand Down