Skip to content

Commit

Permalink
fix(shim): Manipulating shims with UTF8 encoding (#4791)
Browse files Browse the repository at this point in the history
  • Loading branch information
chawyehsu authored Mar 10, 2022
1 parent 476b507 commit 5025661
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Bug Fixes

- **shim:** Manipulating shims with UTF8 encoding ([#4791](https://github.com/ScoopInstaller/Scoop/issues/4791))
- **installed:** If no `$global`, check both local and global installed ([#4798](https://github.com/ScoopInstaller/Scoop/issues/4798))
- **scoop-prefix:** Fix typo that breaks global installed apps ([#4795](https://github.com/ScoopInstaller/Scoop/issues/4795))
- **update:** Skip logs starting with `(chore)` ([#4800](https://github.com/ScoopInstaller/Scoop/issues/4800))
Expand Down
49 changes: 30 additions & 19 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ function Invoke-ExternalCommand {
return $false
}
if ($LogPath -and ($FilePath -notmatch '(^|\W)msiexec($|\W)')) {
Out-File -FilePath $LogPath -Encoding Default -Append -InputObject $Process.StandardOutput.ReadToEnd()
Out-File -FilePath $LogPath -Encoding Default -Append -InputObject $Process.StandardError.ReadToEnd()
Out-UTF8File -FilePath $LogPath -Append -InputObject $Process.StandardOutput.ReadToEnd()
Out-UTF8File -FilePath $LogPath -Append -InputObject $Process.StandardError.ReadToEnd()
}
$Process.WaitForExit()
if ($Process.ExitCode -ne 0) {
Expand Down Expand Up @@ -628,24 +628,24 @@ function shim($path, $global, $name, $arg) {
# 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 ASCII
Write-Output "path = `"$resolved_path`"" | Out-UTF8File "$shim.shim"
if ($arg) {
Write-Output "args = $arg" | Out-File "$shim.shim" -Encoding ASCII -Append
Write-Output "args = $arg" | Out-UTF8File "$shim.shim" -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
@(
"@rem $resolved_path",
"@`"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
) -join "`r`n" | Out-UTF8File "$shim.cmd"

warn_on_overwrite $shim $path
@(
"#!/bin/sh",
"# $resolved_path",
"MSYS2_ARG_CONV_EXCL=/C cmd.exe /C `"$resolved_path`" $arg `"$@`""
) -join "`n" | Out-File $shim -Encoding ASCII -NoNewline
) -join "`n" | Out-UTF8File $shim -NoNewLine
} elseif ($path -match '\.ps1$') {
# if $path points to another drive resolve-path prepends .\ which could break shims
warn_on_overwrite "$shim.ps1" $path
Expand All @@ -664,7 +664,7 @@ function shim($path, $global, $name, $arg) {
"exit `$LASTEXITCODE"
)
}
$ps1text -join "`r`n" | Out-File "$shim.ps1" -Encoding ASCII
$ps1text -join "`r`n" | Out-UTF8File "$shim.ps1"

# make ps1 accessible from cmd.exe
warn_on_overwrite "$shim.cmd" $path
Expand All @@ -685,7 +685,7 @@ function shim($path, $global, $name, $arg) {
") else (",
" powershell -noprofile -ex unrestricted -file `"$resolved_path`" $arg %args%",
")"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
) -join "`r`n" | Out-UTF8File "$shim.cmd"

warn_on_overwrite $shim $path
@(
Expand All @@ -696,33 +696,33 @@ function shim($path, $global, $name, $arg) {
"else",
" powershell.exe -noprofile -ex unrestricted -file `"$resolved_path`" $arg $@",
"fi"
) -join "`n" | Out-File $shim -Encoding ASCII -NoNewline
) -join "`n" | Out-UTF8File $shim -NoNewLine
} elseif ($path -match '\.jar$') {
warn_on_overwrite "$shim.cmd" $path
@(
"@rem $resolved_path",
"@java -jar `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
) -join "`r`n" | Out-UTF8File "$shim.cmd"

warn_on_overwrite $shim $path
@(
"#!/bin/sh",
"# $resolved_path",
"java.exe -jar `"$resolved_path`" $arg `"$@`""
) -join "`n" | Out-File $shim -Encoding ASCII -NoNewline
) -join "`n" | Out-UTF8File $shim -NoNewLine
} elseif ($path -match '\.py$') {
warn_on_overwrite "$shim.cmd" $path
@(
"@rem $resolved_path",
"@python `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
) -join "`r`n" | Out-UTF8File "$shim.cmd"

warn_on_overwrite $shim $path
@(
"#!/bin/sh",
"# $resolved_path",
"python.exe `"$resolved_path`" $arg `"$@`""
) -join "`n" | Out-File $shim -Encoding ASCII -NoNewline
) -join "`n" | Out-UTF8File $shim -NoNewLine
} else {
warn_on_overwrite "$shim.cmd" $path
# find path to Git's bash so that batch scripts can run bash scripts
Expand All @@ -733,14 +733,14 @@ function shim($path, $global, $name, $arg) {
@(
"@rem $resolved_path",
"@`"$(Join-Path (Join-Path $gitdir.FullName 'bin') 'bash.exe')`" `"$resolved_path`" $arg %*"
) -join "`r`n" | Out-File "$shim.cmd" -Encoding ASCII
) -join "`r`n" | Out-UTF8File "$shim.cmd"

warn_on_overwrite $shim $path
@(
"#!/bin/sh",
"# $resolved_path",
"`"$resolved_path`" $arg `"$@`""
) -join "`n" | Out-File $shim -Encoding ASCII -NoNewline
) -join "`n" | Out-UTF8File $shim -NoNewLine
}
}

Expand Down Expand Up @@ -1104,14 +1104,25 @@ function Out-UTF8File {
[Parameter(Mandatory = $True, Position = 0)]
[Alias("Path")]
[String] $FilePath,
[Switch] $Append,
[Switch] $NoNewLine,
[Parameter(ValueFromPipeline = $True)]
[PSObject] $InputObject
)
process {
# Ref: https://stackoverflow.com/questions/5596982
# Performance Note: `WriteAllLines` throttles memory usage while
# `WriteAllText` needs to keep the complete string in memory.
[System.IO.File]::WriteAllLines($FilePath, $InputObject)
if ($Append) {
[System.IO.File]::AppendAllText($FilePath, $InputObject)
} else {
if (!$NoNewLine) {
# Ref: https://stackoverflow.com/questions/5596982
# Performance Note: `WriteAllLines` throttles memory usage while
# `WriteAllText` needs to keep the complete string in memory.
[System.IO.File]::WriteAllLines($FilePath, $InputObject)
} else {
# However `WriteAllText` does not add ending newline.
[System.IO.File]::WriteAllText($FilePath, $InputObject)
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion libexec/scoop-alias.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function add_alias($name, $command) {
# Summary: $description
$command
"@
$script | Out-File "$shimdir\$alias_file.ps1" -Encoding ASCII
$script | Out-UTF8File "$shimdir\$alias_file.ps1"

# add alias to config
$aliases | Add-Member -MemberType NoteProperty -Name $name -Value $alias_file
Expand Down
10 changes: 5 additions & 5 deletions test/Scoop-Alias.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
reset_aliases

Describe 'add_alias' -Tag 'Scoop' {
Mock shimdir { 'TestDrive:\shim' }
Mock shimdir { "$env:TEMP\shims" }
Mock set_config { }
Mock get_config { @{} }

$shimdir = shimdir
mkdir $shimdir
New-Item -ItemType Directory -Path $shimdir -ErrorAction Ignore

Context "alias doesn't exist" {
It 'creates a new alias' {
Expand All @@ -23,7 +23,7 @@ Describe 'add_alias' -Tag 'Scoop' {
Context 'alias exists' {
It 'does not change existing alias' {
$alias_file = "$shimdir\scoop-rm.ps1"
New-Item $alias_file -type file
New-Item $alias_file -Type File -Force
$alias_file | Should -Exist

add_alias 'rm' 'test'
Expand All @@ -33,12 +33,12 @@ Describe 'add_alias' -Tag 'Scoop' {
}

Describe 'rm_alias' -Tag 'Scoop' {
Mock shimdir { 'TestDrive:\shim' }
Mock shimdir { "$env:TEMP\shims" }
Mock set_config { }
Mock get_config { @{} }

$shimdir = shimdir
mkdir $shimdir
New-Item -ItemType Directory -Path $shimdir -ErrorAction Ignore

Context 'alias exists' {
It 'removes an existing alias' {
Expand Down

0 comments on commit 5025661

Please sign in to comment.