Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
73 changes: 48 additions & 25 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -530,19 +530,19 @@ function get_app_name($path) {
return ''
}

function get_app_name_from_ps1_shim($shim_ps1) {
if (!(Test-Path($shim_ps1))) {
function get_app_name_from_ext_shim($shim_ext) {
if (!(Test-Path($shim_ext))) {
return ''
}
$content = (Get-Content $shim_ps1 -Encoding utf8) -join ' '
$content = (Get-Content $shim_ext -Encoding utf8) -join ' '
return get_app_name $content
}

function warn_on_overwrite($shim_ps1, $path) {
if (!(Test-Path($shim_ps1))) {
function warn_on_overwrite($shim_ext, $path) {
if (!(Test-Path($shim_ext))) {
return
}
$shim_app = get_app_name_from_ps1_shim $shim_ps1
$shim_app = get_app_name_from_ext_shim $shim_ext
$path_app = get_app_name $path
if ($shim_app -eq $path_app) {
return
Expand All @@ -558,43 +558,44 @@ function shim($path, $global, $name, $arg) {

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

warn_on_overwrite "$shim.ps1" $path

# convert to relative path
Push-Location $abs_shimdir
$relative_path = resolve-path -relative $path
Pop-Location
$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 {
# Setting PSScriptRoot in Shim if it is not defined, so the shim doesn't break in PowerShell 2.0
Write-Output "if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }" | Out-File "$shim.ps1" -Encoding utf8
write-output "`$path = join-path `"`$psscriptroot`" `"$relative_path`"" | out-file "$shim.ps1" -Encoding utf8 -Append
}

if($path -match '\.jar$') {
"if(`$myinvocation.expectingInput) { `$input | & java -jar `$path $arg @args } else { & java -jar `$path $arg @args }" | out-file "$shim.ps1" -encoding utf8 -append
} else {
"if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }" | out-file "$shim.ps1" -encoding utf8 -append
}

if($path -match '\.(exe|com)$') {
# for programs with no awareness of any shell
warn_on_overwrite "$shim.shim" $path
Copy-Item (get_shim_path) "$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
warn_on_overwrite "$shim.cmd" $path
"@`"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii

warn_on_overwrite $shim $path
"#!/bin/sh`nMSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} elseif($path -match '\.ps1$') {
# if $path points to another drive resolve-path prepends .\ which could break shims
warn_on_overwrite "$shim.ps1" $path
if($relative_path -match "^(.\\[\w]:).*$") {
"`$path = `"$path`"" | out-file "$shim.ps1" -encoding utf8
"if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }" | out-file "$shim.ps1" -encoding utf8 -append
} else {
# Setting PSScriptRoot in Shim if it is not defined, so the shim doesn't break in PowerShell 2.0
"if (!(Test-Path Variable:PSScriptRoot)) { `$PSScriptRoot = Split-Path `$MyInvocation.MyCommand.Path -Parent }" | out-file "$shim.ps1" -encoding utf8
"`$path = join-path `"`$psscriptroot`" `"$relative_path`"" | out-file "$shim.ps1" -encoding utf8 -append
"if(`$myinvocation.expectingInput) { `$input | & `$path $arg @args } else { & `$path $arg @args }" | out-file "$shim.ps1" -encoding utf8 -append
}

# TODO: Find a way to determine if pwsh.exe exists, and use that in the following batch and bash scripts

# make ps1 accessible from cmd.exe
warn_on_overwrite "$shim.cmd" $path
"@echo off
setlocal enabledelayedexpansion
set args=%*
Expand All @@ -606,20 +607,42 @@ set invalid=`"='
if !args! == !invalid! ( set args= )
powershell -noprofile -ex unrestricted `"& '$resolved_path' $arg %args%;exit `$lastexitcode`"" | out-file "$shim.cmd" -encoding ascii

warn_on_overwrite $shim $path
"#!/bin/sh`npowershell.exe -noprofile -ex unrestricted `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} elseif($path -match '\.jar$') {
warn_on_overwrite "$shim.cmd" $path
"@java -jar `"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii

warn_on_overwrite $shim $path
"#!/bin/sh`njava -jar `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} elseif($path -match '\.py$') {
warn_on_overwrite "$shim.cmd" $path
"@python `"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii

warn_on_overwrite $shim $path
"#!/bin/sh`npython `"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
} 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
}
"@$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe') `"$resolved_path`" $arg %*" | out-file "$shim.cmd" -encoding ascii

warn_on_overwrite $shim $path
"#!/bin/sh`n`"$resolved_path`" $arg `"$@`"" | out-file $shim -encoding ascii
}
}

function get_shim_path() {
$shim_path = "$(versiondir 'scoop' 'current')\supporting\shimexe\bin\shim.exe"
$shim_path = "$(versiondir 'scoop' 'current')\supporting\shims\kiennq\shim.exe"
$shim_version = get_config 'shim' 'default'
switch ($shim_version) {
'71' { $shim_path = "$(versiondir 'scoop' 'current')\supporting\shims\71\shim.exe"; Break }
'kiennq' { $shim_path = "$(versiondir 'scoop' 'current')\supporting\shims\kiennq\shim.exe"; Break }
Comment thread
rashil2000 marked this conversation as resolved.
'scoopcs' { $shim_path = "$(versiondir 'scoop' 'current')\supporting\shimexe\bin\shim.exe"; Break }
'rshim' { $shim_path = "$(versiondir 'scoop' 'current')\supporting\shims\rshim\shim.exe"; Break }
'kiennq' { Break } # for backward compatibility
Comment thread
rashil2000 marked this conversation as resolved.
'default' { Break }
default { warn "Unknown shim version: '$shim_version'" }
}
Expand Down
8 changes: 4 additions & 4 deletions test/Scoop-Core.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -221,28 +221,28 @@ describe "rm_shim" -Tag 'Scoop' {
}
}

Describe "get_app_name_from_ps1_shim" -Tag 'Scoop' {
Describe "get_app_name_from_exe_shim" -Tag 'Scoop' {
BeforeAll {
$working_dir = setup_working "shim"
$shimdir = shimdir
$(ensure_in_path $shimdir) | Out-Null
}

It "returns empty string if file does not exist" -skip:$isUnix {
get_app_name_from_ps1_shim "non-existent-file" | should -be ""
get_app_name_from_exe_shim "non-existent-file" | should -be ""
}

It "returns app name if file exists and is a shim to an app" -skip:$isUnix {
mkdir -p "$working_dir/mockapp/current/"
Write-Output "" | Out-File "$working_dir/mockapp/current/mockapp.ps1"
shim "$working_dir/mockapp/current/mockapp.ps1" $false "shim-test"
$shim_path = (get-command "shim-test.ps1").Path
get_app_name_from_ps1_shim "$shim_path" | should -be "mockapp"
get_app_name_from_exe_shim "$shim_path" | should -be "mockapp"
}

It "returns empty string if file exists and is not a shim" -skip:$isUnix {
Write-Output "lorem ipsum" | Out-File -Encoding ascii "$working_dir/mock-shim.ps1"
get_app_name_from_ps1_shim "$working_dir/mock-shim.ps1" | should -be ""
get_app_name_from_exe_shim "$working_dir/mock-shim.ps1" | should -be ""
}

AfterEach {
Expand Down