Skip to content
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

fix(core): Use Invoke-Command instead of Invoke-Expression #4941

Merged
merged 4 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -2,6 +2,7 @@

### Bug Fixes

- **core:** Using `Invoke-Command` instead of `Invoke-Expression` ([#4941](https://github.com/ScoopInstaller/Scoop/issues/4941))
- **core:** Load config file before initialization ([#4932](https://github.com/ScoopInstaller/Scoop/issues/4932))
- **depends:** Avoid digits in archive file extension (except for .7z and .001) ([#4915](https://github.com/ScoopInstaller/Scoop/issues/4915))
- **bucket:** Don't check remote URL of non-git buckets ([#4923](https://github.com/ScoopInstaller/Scoop/issues/4923))
Expand Down
2 changes: 1 addition & 1 deletion bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ if (is_unix) {

function execute($cmd) {
Write-Host $cmd -ForegroundColor Green
$output = Invoke-Expression $cmd
$output = Invoke-Command ([scriptblock]::Create($cmd))

if ($LASTEXITCODE -gt 0) {
abort "^^^ Error! See above ^^^ (last command: $cmd)"
Expand Down
2 changes: 1 addition & 1 deletion bin/checkver.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ while ($in_progress -gt 0) {
$page = $ev.SourceEventArgs.Result
$err = $ev.SourceEventArgs.Error
if ($json.checkver.script) {
$page = $json.checkver.script -join "`r`n" | Invoke-Expression
$page = Invoke-Command ([scriptblock]::Create($json.checkver.script -join "`r`n"))
}

if ($err) {
Expand Down
4 changes: 2 additions & 2 deletions bin/scoop.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ switch ($subCommand) {
({ $subCommand -in @('-v', '--version') }) {
Write-Host 'Current Scoop version:'
if ((Test-CommandAvailable git) -and (Test-Path "$PSScriptRoot\..\.git") -and (get_config SCOOP_BRANCH 'master') -ne 'master') {
Invoke-Expression "git -C '$PSScriptRoot\..' --no-pager log --oneline HEAD -n 1"
git -C "$PSScriptRoot\.." --no-pager log --oneline HEAD -n 1
} else {
$version = Select-String -Pattern '^## \[(v[\d.]+)\].*?([\d-]+)$' -Path "$PSScriptRoot\..\CHANGELOG.md"
Write-Host $version.Matches.Groups[1].Value -ForegroundColor Cyan -NoNewline
Expand All @@ -33,7 +33,7 @@ switch ($subCommand) {
$bucketLoc = Find-BucketDirectory $_ -Root
if ((Test-Path "$bucketLoc\.git") -and (Test-CommandAvailable git)) {
Write-Host "'$_' bucket:"
Invoke-Expression "git -C '$bucketLoc' --no-pager log --oneline HEAD -n 1"
git -C "$bucketLoc" --no-pager log --oneline HEAD -n 1
Write-Host ''
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/buckets.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function new_issue_msg($app, $bucket, $title, $body) {
$bucket_path = "$bucketsdir\$bucket"

if (Test-Path $bucket_path) {
$remote = Invoke-Expression "git -C '$bucket_path' config --get remote.origin.url"
$remote = git -C "$bucket_path" config --get remote.origin.url
# Support ssh and http syntax
# git@PROVIDER:USER/REPO.git
# https://PROVIDER/USER/REPO.git
Expand Down
2 changes: 1 addition & 1 deletion lib/commands.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function command_path($cmd) {
$shim_path = "$scoopdir\shims\scoop-$cmd.ps1"
$line = ((Get-Content $shim_path) | Where-Object { $_.startswith('$path') })
if($line) {
Invoke-Expression -command "$line"
Invoke-Command ([scriptblock]::Create($line)) -NoNewScope
$cmd_path = $path
}
else { $cmd_path = $shim_path }
Expand Down
10 changes: 5 additions & 5 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
$oriConsoleEncoding = [Console]::OutputEncoding
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding

Invoke-Expression $aria2 | ForEach-Object {
Invoke-Command ([scriptblock]::Create($aria2)) | ForEach-Object {
# Skip blank lines
if ([String]::IsNullOrWhiteSpace($_)) { return }

Expand Down Expand Up @@ -718,7 +718,7 @@ function run_installer($fname, $manifest, $architecture, $dir, $global) {
$installer = installer $manifest $architecture
if($installer.script) {
write-output "Running installer script..."
Invoke-Expression (@($installer.script) -join "`r`n")
Invoke-Command ([scriptblock]::Create($installer.script -join "`r`n"))
return
}

Expand Down Expand Up @@ -798,7 +798,7 @@ function run_uninstaller($manifest, $architecture, $dir) {
$version = $manifest.version
if($uninstaller.script) {
write-output "Running uninstaller script..."
Invoke-Expression (@($uninstaller.script) -join "`r`n")
Invoke-Command ([scriptblock]::Create($uninstaller.script -join "`r`n"))
return
}

Expand Down Expand Up @@ -1035,15 +1035,15 @@ function pre_install($manifest, $arch) {
$pre_install = arch_specific 'pre_install' $manifest $arch
if($pre_install) {
write-output "Running pre-install script..."
Invoke-Expression (@($pre_install) -join "`r`n")
Invoke-Command ([scriptblock]::Create($pre_install -join "`r`n"))
}
}

function post_install($manifest, $arch) {
$post_install = arch_specific 'post_install' $manifest $arch
if($post_install) {
write-output "Running post-install script..."
Invoke-Expression (@($post_install) -join "`r`n")
Invoke-Command ([scriptblock]::Create($post_install -join "`r`n"))
}
}

Expand Down
18 changes: 9 additions & 9 deletions libexec/scoop-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,35 @@ function update_scoop() {
Remove-Item -r -force $currentdir -ea stop
Move-Item $newdir $currentdir
} else {
$previousCommit = Invoke-Expression "git -C '$currentdir' rev-parse HEAD"
$currentRepo = Invoke-Expression "git -C '$currentdir' config remote.origin.url"
$currentBranch = Invoke-Expression "git -C '$currentdir' branch"
$previousCommit = git -C "$currentdir" rev-parse HEAD
$currentRepo = git -C "$currentdir" config remote.origin.url
$currentBranch = git -C "$currentdir" branch

$isRepoChanged = !($currentRepo -match $configRepo)
$isBranchChanged = !($currentBranch -match "\*\s+$configBranch")

# Change remote url if the repo is changed
if ($isRepoChanged) {
Invoke-Expression "git -C '$currentdir' config remote.origin.url '$configRepo'"
git -C "$currentdir" config remote.origin.url "$configRepo"
}

# Fetch and reset local repo if the repo or the branch is changed
if ($isRepoChanged -or $isBranchChanged) {
# Reset git fetch refs, so that it can fetch all branches (GH-3368)
Invoke-Expression "git -C '$currentdir' config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'"
git -C "$currentdir" config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
# fetch remote branch
git_cmd -C "`"$currentdir`"" fetch --force origin "refs/heads/`"$configBranch`":refs/remotes/origin/$configBranch" -q
# checkout and track the branch
git_cmd -C "`"$currentdir`"" checkout -B $configBranch -t origin/$configBranch -q
# reset branch HEAD
Invoke-Expression "git -C '$currentdir' reset --hard origin/$configBranch -q"
git -C "$currentdir" reset --hard origin/$configBranch -q
} else {
git_cmd -C "`"$currentdir`"" pull -q
}

$res = $lastexitcode
if ($show_update_log) {
Invoke-Expression "git -C '$currentdir' --no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
git -C "$currentdir" --no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' "$previousCommit..HEAD"
}

if ($res -ne 0) {
Expand Down Expand Up @@ -148,10 +148,10 @@ function update_scoop() {
continue
}

$previousCommit = (Invoke-Expression "git -C '$bucketLoc' rev-parse HEAD")
$previousCommit = git -C "$bucketLoc" rev-parse HEAD
git_cmd -C "`"$bucketLoc`"" pull -q
if ($show_update_log) {
Invoke-Expression "git -C '$bucketLoc' --no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' '$previousCommit..HEAD'"
git -C "$bucketLoc" --no-pager log --no-decorate --grep='^(chore)' --invert-grep --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s %C(cyan)%cr%Creset' "$previousCommit..HEAD"
}
}

Expand Down
3 changes: 2 additions & 1 deletion supporting/shimexe/build.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Param([Switch]$Fast)
Push-Location $PSScriptRoot
. "$PSScriptRoot\..\..\lib\core.ps1"
. "$PSScriptRoot\..\..\lib\install.ps1"

if (!$Fast) {
Write-Host "Install dependencies ..."
Invoke-Expression "$PSScriptRoot\install.ps1"
& "$PSScriptRoot\install.ps1"
}

$output = "$PSScriptRoot\bin"
Expand Down
3 changes: 2 additions & 1 deletion supporting/validator/build.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Param([Switch]$Fast)
Push-Location $PSScriptRoot
. "$PSScriptRoot\..\..\lib\core.ps1"
. "$PSScriptRoot\..\..\lib\install.ps1"

if (!$Fast) {
Write-Host "Install dependencies ..."
Invoke-Expression "$PSScriptRoot\install.ps1"
& "$PSScriptRoot\install.ps1"
}

$output = "$PSScriptRoot\bin"
Expand Down
2 changes: 1 addition & 1 deletion test/Scoop-Alias.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Describe 'add_alias' -Tag 'Scoop' {
$alias_file | Should -Not -Exist

add_alias 'rm' '"hello, world!"'
Invoke-Expression $alias_file | Should -Be 'hello, world!'
& $alias_file | Should -Be 'hello, world!'
}
}

Expand Down