From b4e0ff16a61f5d3f083808e6b608e4a52a561721 Mon Sep 17 00:00:00 2001 From: zStruCat <98123484+zStruCat@users.noreply.github.com> Date: Thu, 7 Jul 2022 13:19:53 +0800 Subject: [PATCH] fix(scoop-import): Use `foreach` instead of `ForEach-Object` for nullity check (#5034) * Add nullity check and alread_in_local_bucket check * update CHANGELOG * Use foreach instead of ForEach-Object * changelog * update help * refine the info and warning when adding an already-added bucket * Update lib/buckets.ps1 Make warning clearer Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> Co-authored-by: Rashil Gandhi Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com> --- CHANGELOG.md | 2 +- lib/buckets.ps1 | 2 +- libexec/scoop-import.ps1 | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46eecae04d..17116dee81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - **chore:** Add missing -a/--all param to all commands ([#5004](https://github.com/ScoopInstaller/Scoop/issues/5004)) - **scoop-status:** Check bucket status, improve output ([#5011](https://github.com/ScoopInstaller/Scoop/issues/5011)) - **scoop-info:** Show app installed/download size ([#4886](https://github.com/ScoopInstaller/Scoop/issues/4886)) -- **scoop-import:** Import a Scoop installation from JSON ([#5014](https://github.com/ScoopInstaller/Scoop/issues/5014)) +- **scoop-import:** Import a Scoop installation from JSON ([#5014](https://github.com/ScoopInstaller/Scoop/issues/5014), [#5034](https://github.com/ScoopInstaller/Scoop/issues/5034)) ### Bug Fixes diff --git a/lib/buckets.ps1 b/lib/buckets.ps1 index fa3fc8d6c5..c22c291220 100644 --- a/lib/buckets.ps1 +++ b/lib/buckets.ps1 @@ -120,7 +120,7 @@ function add_bucket($name, $repo) { $dir = Find-BucketDirectory $name -Root if (Test-Path $dir) { - warn "The '$name' bucket already exists. Use 'scoop bucket rm $name' to remove it." + warn "The '$name' bucket already exists. To add this bucket again, first remove it by running 'scoop bucket rm $name'." return 2 } diff --git a/libexec/scoop-import.ps1 b/libexec/scoop-import.ps1 index bc6a67a60f..b6a0ba5d6b 100644 --- a/libexec/scoop-import.ps1 +++ b/libexec/scoop-import.ps1 @@ -1,5 +1,7 @@ # Usage: scoop import # Summary: Imports apps, buckets and configs from a Scoopfile in JSON format +# Help: To replicate a Scoop installation from a file stored on Desktop, run +# scoop import Desktop\scoopfile.json param( [Parameter(Mandatory)] @@ -21,18 +23,18 @@ if (Test-Path $scoopfile) { if (!$import) { abort 'Input file not a valid JSON.' } -$import.config.PSObject.Properties | ForEach-Object { - set_config $_.Name $_.Value | Out-Null - Write-Host "'$($_.Name)' has been set to '$($_.Value)'" +foreach ($item in $import.config.PSObject.Properties) { + set_config $item.Name $item.Value | Out-Null + Write-Host "'$($item.Name)' has been set to '$($item.Value)'" } -$import.buckets | ForEach-Object { - add_bucket $_.Name $_.Source | Out-Null - $bucket_names += $_.Name +foreach ($item in $import.buckets) { + add_bucket $item.Name $item.Source | Out-Null + $bucket_names += $item.Name } -$import.apps | ForEach-Object { - $info = $_.Info -Split ', ' +foreach ($item in $import.apps) { + $info = $item.Info -Split ', ' $global = if ('Global install' -in $info) { ' --global' } else { @@ -46,17 +48,17 @@ $import.apps | ForEach-Object { '' } - $app = if ($_.Source -in $bucket_names) { - "$($_.Source)/$($_.Name)" - } elseif ($_.Source -eq '') { - "$($_.Name)@$($_.Version)" + $app = if ($item.Source -in $bucket_names) { + "$($item.Source)/$($item.Name)" + } elseif ($item.Source -eq '') { + "$($item.Name)@$($item.Version)" } else { - $_.Source + $item.Source } & "$PSScriptRoot\scoop-install.ps1" $app$global$arch if ('Held package' -in $info) { - & "$PSScriptRoot\scoop-hold.ps1" $($_.Name)$global + & "$PSScriptRoot\scoop-hold.ps1" $($item.Name)$global } }