Skip to content

Commit

Permalink
Support package parameters and install arguments starting with dashes (
Browse files Browse the repository at this point in the history
…#29)

* Define PackageParameters and InstallArguments dynamic options for Install-Package
* Add tests for Install-Package -PackageParameters
  • Loading branch information
jberezanski authored Sep 24, 2021
1 parent b81c97e commit 1155395
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/ChocolateyGet.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ $script:AcceptLicense = "AcceptLicense"
$script:AdditionalArguments = "AdditionalArguments"
$script:AllVersions = "AllVersions"
$script:Force = "Force"
$script:InstallArguments = "InstallArguments"
$script:PackageParameters = "PackageParameters"
$script:PackageSource = "Chocolatey"

# Utility variables
Expand Down
2 changes: 2 additions & 0 deletions src/public/BaseFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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:PackageParameters -ExpectedType String -IsRequired $false)
Write-Output -InputObject (New-DynamicOption -Category $category -Name $script:InstallArguments -ExpectedType String -IsRequired $false)
}
}
}
11 changes: 5 additions & 6 deletions src/public/Install-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ function Install-Package {
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$FastPackageReference,

[Parameter()]
[string]
$AdditionalArgs = ($request.Options[$script:AdditionalArguments])
$FastPackageReference
)

Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package'))
Expand Down Expand Up @@ -38,10 +34,13 @@ function Install-Package {
Version = $Matches.version
Source = $Matches.source
Force = $request.Options.ContainsKey($script:Force)
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 ($_) {
Expand Down
31 changes: 30 additions & 1 deletion test/ChocolateyGet.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,38 @@ 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 'pipline-based package installation and uninstallation' {
Describe 'pipeline-based package installation and uninstallation' {
Context 'without additional arguments' {
BeforeAll {
$package = 'cpu-z'
Expand Down

0 comments on commit 1155395

Please sign in to comment.