From f00b002c8d00469f109e28f8d879ca2b12b8228f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Sun, 20 Jun 2021 21:01:59 +0200 Subject: [PATCH 01/14] Generalize Get-AdditionalArguments and provide diagnostic output for Install-Package --- src/private/MiscHelpers.ps1 | 11 ++++++----- src/public/Install-Package.ps1 | 5 ++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/private/MiscHelpers.ps1 b/src/private/MiscHelpers.ps1 index 72452df..ca05c1a 100644 --- a/src/private/MiscHelpers.ps1 +++ b/src/private/MiscHelpers.ps1 @@ -1,18 +1,19 @@ # Get AdditionalArguments property from the input cmdline -function Get-AdditionalArguments { +function Get-ProviderAdditionalParameter { [CmdletBinding()] [OutputType([string])] param ( + [Parameter(Mandatory)] [string] $Name ) - $additionalArgs = $null + $value = $null $options = $request.Options - if($options.ContainsKey($script:additionalArguments)) { - $additionalArgs = $options[$script:additionalArguments] + if($options.ContainsKey($Name)) { + $value = $options[$Name] } - $additionalArgs + $value } # Find whether a user specifies -force diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index cbd1bce..ef63ecc 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -8,7 +8,7 @@ function Install-Package { $FastPackageReference, [string] - $AdditionalArgs = (Get-AdditionalArguments) + $AdditionalArgs = (Get-ProviderAdditionalParameter $script:additionalArguments) ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) @@ -62,6 +62,9 @@ function Install-Package { } } + Write-Debug "chocoParams =" + $chocoParams | ConvertTo-Json -Depth 10 | Out-String -Width 200 | ForEach-Object { Write-Debug "$_" } + $swid = $( $result = Install-ChocoPackage @chocoParams if (-not $result) { From 108886e7809af25f1c0935abeb1e975bab0acab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Sun, 20 Jun 2021 21:07:04 +0200 Subject: [PATCH 02/14] Define PackageParameters and InstallArguments dynamic options for Install-Package This sidesteps all issues caused by having a list of parameters embedded inside another list of parameters. In particular, it provides support for package parameters/install arguments which start with dashes (such as used by the visualstudio2019buildtools [1] package). If the user sets PackageParameters/InstallArguments and at the same time provides values for them in AdditionalArguments (the existing way), the values from AdditionalArguments take precedence. [1] https://community.chocolatey.org/packages/visualstudio2019buildtools --- src/ChocolateyGet.psm1 | 2 ++ src/public/BaseFunctions.ps1 | 2 ++ src/public/Install-Package.ps1 | 10 +++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ChocolateyGet.psm1 b/src/ChocolateyGet.psm1 index 1e177fb..0153c92 100644 --- a/src/ChocolateyGet.psm1 +++ b/src/ChocolateyGet.psm1 @@ -6,6 +6,8 @@ $script:PackageSource = "Chocolatey" $script:additionalArguments = "AdditionalArguments" $script:AllVersions = "AllVersions" $script:AcceptLicense = "AcceptLicense" +$script:ProviderAdditionalParameterPackageParameters = "PackageParameters" +$script:ProviderAdditionalParameterInstallArguments = "InstallArguments" # Define choco related variables $script:ChocoExeName = 'choco.exe' diff --git a/src/public/BaseFunctions.ps1 b/src/public/BaseFunctions.ps1 index f890749..152f51d 100644 --- a/src/public/BaseFunctions.ps1 +++ b/src/public/BaseFunctions.ps1 @@ -25,6 +25,8 @@ function Get-DynamicOptions { Install { Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:additionalArguments -ExpectedType String -IsRequired $false) Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:AcceptLicense -ExpectedType Switch -IsRequired $false) + Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:ProviderAdditionalParameterPackageParameters -ExpectedType String -IsRequired $false) + Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:ProviderAdditionalParameterInstallArguments -ExpectedType String -IsRequired $false) } } } diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index ef63ecc..bdd37a9 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -8,7 +8,13 @@ function Install-Package { $FastPackageReference, [string] - $AdditionalArgs = (Get-ProviderAdditionalParameter $script:additionalArguments) + $AdditionalArgs = (Get-ProviderAdditionalParameter $script:additionalArguments), + + [string] + $ChocoPackageParameters = (Get-ProviderAdditionalParameter $script:ProviderAdditionalParameterPackageParameters), + + [string] + $ChocoInstallArguments = (Get-ProviderAdditionalParameter $script:ProviderAdditionalParameterInstallArguments) ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) @@ -36,6 +42,8 @@ function Install-Package { Version = $Matches.version Source = $Matches.source Force = Get-ForceProperty + Parameters = $ChocoPackageParameters + InstallArguments = $ChocoInstallArguments } # Split on the first hyphen of each option/switch From d6de3079b0af8d7904877b89f3fc3332bbd6a005 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:17:14 -0500 Subject: [PATCH 03/14] Update Install-Package.ps1 --- src/public/Install-Package.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 7bcb9eb..1d3ebd1 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -14,11 +14,11 @@ function Install-Package { [Parameter()] [string] - $InstallArguments = ($request.Options[$script:InstallArguments]), + $InstallArguments = ($request.Options[$script:InstallArguments]), [Parameter()] [string] - $PackageParameters = ($request.Options[$script:PackageParameters]) + $PackageParameters = ($request.Options[$script:PackageParameters]) ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) From 9dae3e6a83e76ff46e2e5415201d395bddd2ac57 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:18:21 -0500 Subject: [PATCH 04/14] Update BaseFunctions.ps1 --- src/public/BaseFunctions.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/BaseFunctions.ps1 b/src/public/BaseFunctions.ps1 index ca4abba..e32e83a 100644 --- a/src/public/BaseFunctions.ps1 +++ b/src/public/BaseFunctions.ps1 @@ -26,8 +26,8 @@ function Get-DynamicOptions { Install { Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:AdditionalArguments -ExpectedType String -IsRequired $false) Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:AcceptLicense -ExpectedType Switch -IsRequired $false) - Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:ProviderAdditionalParameterPackageParameters -ExpectedType String -IsRequired $false) - Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:ProviderAdditionalParameterInstallArguments -ExpectedType String -IsRequired $false) + Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:PackageParameters -ExpectedType String -IsRequired $false) + Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:InstallArguments -ExpectedType String -IsRequired $false) } } } From 6850de4b2bc298027db221d1b45229d2a96deb11 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:18:52 -0500 Subject: [PATCH 05/14] Update ChocolateyGet.psm1 --- src/ChocolateyGet.psm1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ChocolateyGet.psm1 b/src/ChocolateyGet.psm1 index e1d1908..a737e09 100644 --- a/src/ChocolateyGet.psm1 +++ b/src/ChocolateyGet.psm1 @@ -3,13 +3,12 @@ # Define provider related variables $script:AcceptLicense = "AcceptLicense" - $script:AdditionalArguments = "AdditionalArguments" $script:AllVersions = "AllVersions" $script:Force = "Force" +$script:InstallArguments = "InstallArguments" $script:PackageSource = "Chocolatey" $script:PackageParameters = "PackageParameters" -$script:InstallArguments = "InstallArguments" # Utility variables $script:FastReferenceRegex = "(?[^#]*)#(?[^\s]*)#(?[^#]*)" From f5bdf9e68fcf4f3b70332497921aae15a792d584 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:19:55 -0500 Subject: [PATCH 06/14] Update Install-Package.ps1 --- src/public/Install-Package.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 1d3ebd1..4d3d37b 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -45,7 +45,7 @@ function Install-Package { Name = $Matches.name Version = $Matches.version Source = $Matches.source - Force = Get-ForceProperty + Force = Get-ProviderDynamicFlag -Name $script:Force Parameters = $PackageParameters InstallArguments = $InstallArguments } From 359e0bf38bc2d50f3c6a108177755a8f90db8ce5 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:20:23 -0500 Subject: [PATCH 07/14] Update Install-Package.ps1 --- src/public/Install-Package.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 4d3d37b..fcad020 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -74,9 +74,6 @@ function Install-Package { } } - Write-Debug "chocoParams =" - $chocoParams | ConvertTo-Json -Depth 10 | Out-String -Width 200 | ForEach-Object { Write-Debug "$_" } - $swid = $( $result = Foil\Install-ChocoPackage @chocoParams if (-not $result) { From a417cfe8121503fca0e6273e335c8cfd8909d4e1 Mon Sep 17 00:00:00 2001 From: Ethan Bergstrom <13603162+ethanbergstrom@users.noreply.github.com> Date: Sun, 18 Jul 2021 11:29:26 -0500 Subject: [PATCH 08/14] Update ChocolateyGet.psm1 --- src/ChocolateyGet.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ChocolateyGet.psm1 b/src/ChocolateyGet.psm1 index a737e09..2c327ab 100644 --- a/src/ChocolateyGet.psm1 +++ b/src/ChocolateyGet.psm1 @@ -7,8 +7,8 @@ $script:AdditionalArguments = "AdditionalArguments" $script:AllVersions = "AllVersions" $script:Force = "Force" $script:InstallArguments = "InstallArguments" -$script:PackageSource = "Chocolatey" $script:PackageParameters = "PackageParameters" +$script:PackageSource = "Chocolatey" # Utility variables $script:FastReferenceRegex = "(?[^#]*)#(?[^\s]*)#(?[^#]*)" From 5b88ad3a383e38dde57f2defca3dafd89e16950d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Sun, 19 Sep 2021 22:49:38 +0200 Subject: [PATCH 09/14] Install-Package: Do not pass empty optional parameters This works around https://github.com/ethanbergstrom/Foil/issues/3 --- src/public/Install-Package.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 8136b9b..719469a 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -78,6 +78,12 @@ function Install-Package { } } + foreach ($possiblyEmptyParam in @('Parameters', 'InstallArguments')) { + if ([string]::IsNullOrEmpty($chocoParams[$possiblyEmptyParam])) { + $chocoParams.Remove($possiblyEmptyParam) + } + } + # Convert the PSCustomObject output from Foil into PackageManagement SWIDs, then validate what Chocolatey installed matched what we requested $swid = $( $result = Foil\Install-ChocoPackage @chocoParams From e245101b8a8604b08530dfcd96c53f957fc5b3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Sun, 19 Sep 2021 23:06:25 +0200 Subject: [PATCH 10/14] Fix typo --- test/ChocolateyGet.Unit.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ChocolateyGet.Unit.Tests.ps1 b/test/ChocolateyGet.Unit.Tests.ps1 index 50679af..8da9c1e 100644 --- a/test/ChocolateyGet.Unit.Tests.ps1 +++ b/test/ChocolateyGet.Unit.Tests.ps1 @@ -84,7 +84,7 @@ Describe 'DSC-compliant package installation and uninstallation' { } } -Describe 'pipline-based package installation and uninstallation' { +Describe 'pipeline-based package installation and uninstallation' { Context 'without additional arguments' { BeforeAll { $package = 'cpu-z' From 85759a0401d1470535e7e745b071372e09449ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bere=C5=BCa=C5=84ski?= Date: Sun, 19 Sep 2021 23:06:52 +0200 Subject: [PATCH 11/14] Add tests for Install-Package -PackageParameters --- test/ChocolateyGet.Unit.Tests.ps1 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/ChocolateyGet.Unit.Tests.ps1 b/test/ChocolateyGet.Unit.Tests.ps1 index 8da9c1e..6d0967a 100644 --- a/test/ChocolateyGet.Unit.Tests.ps1 +++ b/test/ChocolateyGet.Unit.Tests.ps1 @@ -82,6 +82,35 @@ Describe 'DSC-compliant package installation and uninstallation' { Uninstall-Package -Provider $ChocolateyGet -Name $package -AdditionalArguments $params | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty } } + Context 'with package parameters passed explicitly' { + BeforeAll { + $package = 'kitty' + $installDir = Join-Path -Path $env:ChocolateyInstall -ChildPath (Join-Path -Path 'lib' -ChildPath $package) + $kittyIniPath = Join-Path -Path $installDir -ChildPath (Join-Path -Path 'tools' -ChildPath 'kitty.ini') + $packageParams = "/Portable" + $wrappedParams = "--params ""/Dummy""" + Remove-Item -Force -Recurse -Path $installDir -ErrorAction SilentlyContinue + } + It 'silently installs the latest version of a package with explicit parameters' { + Install-Package -Force -Provider $ChocolateyGet -Name $package -PackageParameters $packageParams | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty + } + It 'correctly passed explicit parameters to the package' { + $kittyIniPath | Should -Exist + $kittyIniPath | Should -FileContentMatch 'savemode=dir' + } + It 'silently uninstalls the locally installed package just installed' { + Uninstall-Package -Provider $ChocolateyGet -Name $package | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty + } + It 'silently installs the latest version of a package with explicit and wrapped parameters' { + Install-Package -Force -Provider $ChocolateyGet -Name $package -PackageParameters $packageParams -AdditionalArguments $wrappedParams | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty + } + It 'correctly passed wrapped parameters to the package' { + $kittyIniPath | Should -Not -Exist + } + It 'silently uninstalls the locally installed package just installed again' { + Uninstall-Package -Provider $ChocolateyGet -Name $package | Where-Object {$_.Name -contains $package} | Should -Not -BeNullOrEmpty + } + } } Describe 'pipeline-based package installation and uninstallation' { From d501f88c0f4084646f876716268dbb16e7c8ec9e Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Fri, 24 Sep 2021 10:33:41 -0500 Subject: [PATCH 12/14] Fixed in Foil v0.0.8 --- src/public/Install-Package.ps1 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 719469a..8136b9b 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -78,12 +78,6 @@ function Install-Package { } } - foreach ($possiblyEmptyParam in @('Parameters', 'InstallArguments')) { - if ([string]::IsNullOrEmpty($chocoParams[$possiblyEmptyParam])) { - $chocoParams.Remove($possiblyEmptyParam) - } - } - # Convert the PSCustomObject output from Foil into PackageManagement SWIDs, then validate what Chocolatey installed matched what we requested $swid = $( $result = Foil\Install-ChocoPackage @chocoParams From d272ae52d75a34f573762ec7927229253865c268 Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Fri, 24 Sep 2021 10:38:11 -0500 Subject: [PATCH 13/14] Make variable names less confusing --- src/public/Install-Package.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index 8136b9b..d0da130 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -14,11 +14,11 @@ function Install-Package { [Parameter()] [string] - $InstallArguments = ($request.Options[$script:InstallArguments]), + $InstallArgs = ($request.Options[$script:InstallArguments]), [Parameter()] [string] - $PackageParameters = ($request.Options[$script:PackageParameters]) + $PackageParams = ($request.Options[$script:PackageParameters]) ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) @@ -46,8 +46,8 @@ function Install-Package { Version = $Matches.version Source = $Matches.source Force = $request.Options.ContainsKey($script:Force) - Parameters = $PackageParameters - InstallArguments = $InstallArguments + Parameters = $PackageParams + InstallArguments = $InstallArgs } # Split on the first hyphen of each option/switch From ce6dca4aafa6c34c3b8e8fd4e8b80be486c5e61c Mon Sep 17 00:00:00 2001 From: ethanbergstrom Date: Fri, 24 Sep 2021 11:05:11 -0500 Subject: [PATCH 14/14] Tighten up Install-Package API and readme docs --- README.md | 6 +++++- src/public/Install-Package.ps1 | 21 +++++---------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6d03d29..3287b7a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,11 @@ Unregister-PackageSource privateRepo -Provider ChocolateyGet ChocolateyGet integrates with Choco.exe to manage and store source information ## Pass in choco arguments -If you need to pass in some of choco arguments to the Find, Install, Get and Uninstall-Package cmdlets, you can use AdditionalArguments PowerShell property. +If you need to pass in additional package installation options, you can use either the dedicated package parameter and argument properties or the combined AdditionalArguments property. + +```powershell +Install-Package sysinternals -Provider ChocolateyGet -AcceptLicense -AdditionalArguments '--paramsglobal' -PackageParameters '/InstallDir:c:\windows\temp\sysinternals /QuickLaunchShortcut:false' -InstallArguments 'MaintenanceService=false' -Verbose +``` ```powershell Install-Package sysinternals -Provider ChocolateyGet -AcceptLicense -AdditionalArguments '--paramsglobal --params "/InstallDir:c:\windows\temp\sysinternals /QuickLaunchShortcut:false" -y --installargs MaintenanceService=false' -Verbose diff --git a/src/public/Install-Package.ps1 b/src/public/Install-Package.ps1 index d0da130..2c1a5cd 100644 --- a/src/public/Install-Package.ps1 +++ b/src/public/Install-Package.ps1 @@ -6,19 +6,7 @@ function Install-Package { [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] - $FastPackageReference, - - [Parameter()] - [string] - $AdditionalArgs = ($request.Options[$script:AdditionalArguments]), - - [Parameter()] - [string] - $InstallArgs = ($request.Options[$script:InstallArguments]), - - [Parameter()] - [string] - $PackageParams = ($request.Options[$script:PackageParameters]) + $FastPackageReference ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) @@ -46,12 +34,13 @@ function Install-Package { Version = $Matches.version Source = $Matches.source Force = $request.Options.ContainsKey($script:Force) - Parameters = $PackageParams - InstallArguments = $InstallArgs + Parameters = $request.Options[$script:PackageParameters] + InstallArguments = $request.Options[$script:InstallArguments] } # Split on the first hyphen of each option/switch - [regex]::Split($AdditionalArgs,'(?:^|\s)-') | ForEach-Object { + # Overrides values passed through explicit package parameter/argument options + [regex]::Split($request.Options[$script:AdditionalArguments],'(?:^|\s)-') | ForEach-Object { Write-Debug "AdditionalArgs: $_" # Check each option/switch against known patterns that we can pass to Foil switch -Regex ($_) {