Skip to content

Commit

Permalink
chocolatey-visualstudio.extension: Support default package parameter …
Browse files Browse the repository at this point in the history
…values

Related issues: GH-139 GH-111
  • Loading branch information
jberezanski committed May 5, 2023
1 parent a7ff788 commit 835c975
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 42 deletions.
2 changes: 2 additions & 0 deletions chocolatey-visualstudio.extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Version 1.11.0

- Arguments `--path install=...`, `--path cache=...` and `--path shared=...` are now supported ([GH-142](https://github.com/jberezanski/ChocolateyPackages/issues/142)). `--path install=...` is converted to `--installPath`; those two must not be specified at the same time with different values.
- Packages may now specify default values for some package parameters and pass them to Install-VisualStudio, Add-VisualStudioWorkload/Component and Remove-VisualStudioWorkload/Component (via a new -DefaultParameterValues parameter). This, in particular, supports packages which embed the VS bootstrapper and manifests in order to install a specific VS version, or packages which want to install/update VS from the LTSC channel ([GH-139](https://github.com/jberezanski/ChocolateyPackages/pull/139)).
- The parameters parser now understands the syntax `--reset-parameter-xyz` as a directive to forget the parameter `--xyz` if it has already been specified (possibly via default values set by the package). This is intended to be a way to override some defaults set in the package wihout having to provide an explicit value (i.e. allow the normal VS Installer and/or extension script logic to determine the value).

## Version 1.10.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>chocolatey-visualstudio.extension</id>
<version>1.11.0-preview2</version>
<version>1.11.0-preview3</version>
<packageSourceUrl>https://github.com/jberezanski/ChocolateyPackages/tree/master/chocolatey-visualstudio.extension</packageSourceUrl>
<owners>jberezanski</owners>
<title>Chocolatey Visual Studio servicing extensions</title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function Add-VisualStudioComponent
[Parameter(Mandatory = $true)] [string] $VisualStudioYear,
[Parameter(Mandatory = $true)] [string[]] $ApplicableProducts,
[version] $RequiredProductVersion,
[bool] $Preview
[bool] $Preview,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -20,5 +21,13 @@ function Add-VisualStudioComponent
$argumentList = @('add', "$Component")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
Start-VSModifyOperation -PackageName $PackageName -ArgumentList $argumentList -ChannelReference $channelReference -ApplicableProducts $ApplicableProducts -RequiredProductVersion $RequiredProductVersion -OperationTexts @('installed', 'installing', 'installation')
}
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
-ArgumentList $argumentList `
-ChannelReference $channelReference `
-ApplicableProducts $ApplicableProducts `
-RequiredProductVersion $RequiredProductVersion `
-OperationTexts @('installed', 'installing', 'installation')
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
[switch] $IncludeRecommendedComponentsByDefault,
[switch] $IncludeOptionalComponentsByDefault,
[version] $RequiredProductVersion,
[bool] $Preview
[bool] $Preview,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -30,5 +31,13 @@
}

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
Start-VSModifyOperation -PackageName $PackageName -ArgumentList $argumentList -ChannelReference $channelReference -ApplicableProducts $ApplicableProducts -RequiredProductVersion $RequiredProductVersion -OperationTexts @('installed', 'installing', 'installation')
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
-ArgumentList $argumentList `
-ChannelReference $channelReference `
-ApplicableProducts $ApplicableProducts `
-RequiredProductVersion $RequiredProductVersion `
-OperationTexts @('installed', 'installing', 'installation')
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ Install-ChocolateyPackage
[string] $VisualStudioYear,
[string] $Product,
[bool] $Preview,
[version] $DesiredProductVersion
[version] $DesiredProductVersion,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -51,7 +52,7 @@ Install-ChocolateyPackage
}
Write-Debug "Running 'Install-VisualStudio' for $PackageName with ApplicationName:'$ApplicationName' Url:'$Url' Checksum:$Checksum ChecksumType:$ChecksumType InstallerTechnology:'$InstallerTechnology' ProgramsAndFeaturesDisplayName:'$ProgramsAndFeaturesDisplayName' VisualStudioYear:'$VisualStudioYear' Product:'$Product' Preview:'$Preview' DesiredProductVersion:'$DesiredProductVersion'";

$packageParameters = Parse-Parameters $env:chocolateyPackageParameters
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$creatingLayout = $packageParameters.ContainsKey('layout')
$assumeNewVS2017Installer = $InstallerTechnology -eq 'WillowVS2017OrLater'

Expand Down
31 changes: 24 additions & 7 deletions chocolatey-visualstudio.extension/extensions/Parse-Parameters.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ function Parse-Parameters
{
[CmdletBinding()]
Param (
[string] $s
[string] $s,
[hashtable] $DefaultValues
)
Write-Debug "Running 'Parse-Parameters' with s:'$s'";
$parameters = @{}
if ($null -ne $DefaultValues)
{
Write-Debug "Running 'Parse-Parameters' with s:'$s' DefaultValues:'$($DefaultValues.GetEnumerator() | ForEach-Object { $kvp = $_; $_.Value | ForEach-Object { '--{0} {1}' -f $kvp.Key, $_ } })'";
$parameters = $DefaultValues
}
else
{
Write-Debug "Running 'Parse-Parameters' with s:'$s' DefaultValues:''";
$parameters = @{}
}

if ($s -eq '')
{
Expand All @@ -21,6 +30,7 @@ function Parse-Parameters
$s = ' ' + $s
[String[]] $kvpPrefix = @(" --")
$kvpDelimiter = ' '
$rxResetParameter = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList @('^reset-param(eter)?-(?=.)', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)

$kvps = $s.Split($kvpPrefix, [System.StringSplitOptions]::RemoveEmptyEntries)
foreach ($kvp in $kvps)
Expand All @@ -41,7 +51,13 @@ function Parse-Parameters
}

Write-Debug "Package parameter: key=$key, value=$value"
if ($multiValuedParameterNames.ContainsKey($key) -and $parameters.ContainsKey($key))
if ($rxResetParameter.IsMatch($key))
{
$resetParameterName = $rxResetParameter.Replace($key, '')
Write-Debug "Removing existing value of --$resetParameterName parameter, if any."
$parameters.Remove($resetParameterName)
}
elseif ($multiValuedParameterNames.ContainsKey($key) -and $parameters.ContainsKey($key))
{
$existingValue = $parameters[$key]
if ($existingValue -is [System.Collections.IList])
Expand All @@ -66,7 +82,7 @@ function Parse-Parameters
{
$pathInstallValue = $null
$pathParameterValue = $parameters['path']
$rxInstallEquals = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList @('^install=', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
$rxInstallEquals = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList @('^install=(?=.)', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
if ($pathParameterValue -is [string] -and $rxInstallEquals.IsMatch($pathParameterValue))
{
Write-Debug "Found --path install=... in package parameters (as the only --path value), removing the --path parameter."
Expand All @@ -75,7 +91,7 @@ function Parse-Parameters
}
elseif ($pathParameterValue -is [System.Collections.IList])
{
$pathInstallValue = $pathParameterValue | Where-Object { $rxInstallEquals.IsMatch($_) } | Select-Object -First 1
$pathInstallValue = $pathParameterValue | Where-Object { $rxInstallEquals.IsMatch($_) } | Select-Object -Last 1
if ($null -ne $pathInstallValue)
{
Write-Debug "Found --path install=... in package parameters (among other --path values)."
Expand Down Expand Up @@ -107,7 +123,8 @@ function Parse-Parameters
{
if ($parameters['installPath'] -ne $installPathValue)
{
throw "Package parameters contain both '--installPath ...' and '--path install=...' with different values. Please provide one or the other, but not both. Provided installPath: [$($parameters['installPath'])]. provided path install=: [${installPathValue}]."
Write-Error "Package parameters contain both '--installPath ...' and '--path install=...' with different values. Please provide one or the other, but not both. Provided installPath: [$($parameters['installPath'])]. provided path install=: [${installPathValue}]. Using the value of installPath."
$installPathValue = $parameters['installPath']
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function Remove-VisualStudioComponent
[Parameter(Mandatory = $true)] [string] $Component,
[Parameter(Mandatory = $true)] [string] $VisualStudioYear,
[Parameter(Mandatory = $true)] [string[]] $ApplicableProducts,
[bool] $Preview
[bool] $Preview,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -19,5 +20,12 @@ function Remove-VisualStudioComponent
$argumentList = @('remove', "$Component")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
Start-VSModifyOperation -PackageName $PackageName -ArgumentList $argumentList -ChannelReference $channelReference -ApplicableProducts $ApplicableProducts -OperationTexts @('uninstalled', 'uninstalling', 'uninstallation')
}
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
-ArgumentList $argumentList `
-ChannelReference $channelReference `
-ApplicableProducts $ApplicableProducts `
-OperationTexts @('uninstalled', 'uninstalling', 'uninstallation')
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Write-Debug "Running 'Remove-VisualStudioProduct' with PackageName:'$PackageName' Product:'$Product' VisualStudioYear:'$VisualStudioYear' Preview:'$Preview'";
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
$productReference = Get-VSProductReference -ChannelReference $channelReference -Product $Product
$packageParameters = @{ channelId = $channelReference.ChannelId; productId = $productReference.ProductId }
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues @{ channelId = $channelReference.ChannelId; productId = $productReference.ProductId }
Start-VSModifyOperation `
-PackageName $PackageName `
-ArgumentList @() `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
[Parameter(Mandatory = $true)] [string] $Workload,
[Parameter(Mandatory = $true)] [string] $VisualStudioYear,
[Parameter(Mandatory = $true)] [string[]] $ApplicableProducts,
[bool] $Preview
[bool] $Preview,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -19,5 +20,12 @@
$argumentList = @('remove', "Microsoft.VisualStudio.Workload.$Workload")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
Start-VSModifyOperation -PackageName $PackageName -ArgumentList $argumentList -ChannelReference $channelReference -ApplicableProducts $ApplicableProducts -OperationTexts @('uninstalled', 'uninstalling', 'uninstallation')
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
-ArgumentList $argumentList `
-ChannelReference $channelReference `
-ApplicableProducts $ApplicableProducts `
-OperationTexts @('uninstalled', 'uninstalling', 'uninstallation')
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[ValidateSet('modify', 'uninstall', 'update')] [string] $Operation = 'modify',
[version] $RequiredProductVersion,
[version] $DesiredProductVersion,
[hashtable] $PackageParameters,
[Parameter(Mandatory = $true)] [hashtable] $PackageParameters,
[string] $BootstrapperUrl,
[string] $BootstrapperChecksum,
[string] $BootstrapperChecksumType,
Expand All @@ -27,14 +27,7 @@

$frobbed, $frobbing, $frobbage = $OperationTexts

if ($null -eq $PackageParameters)
{
$PackageParameters = Parse-Parameters $env:chocolateyPackageParameters
}
else
{
$PackageParameters = $PackageParameters.Clone()
}
$PackageParameters = $PackageParameters.Clone()

$argumentSetFromArgumentList = @{}
for ($i = 0; $i -lt $ArgumentList.Length; $i += 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Set-StrictMode -Version 5
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
$DebugPreference = 'Continue'
. $PSScriptRoot\..\extensions\ConvertTo-ArgumentString.ps1
. $PSScriptRoot\..\extensions\Parse-Parameters.ps1
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
. $root\..\extensions\ConvertTo-ArgumentString.ps1
. $root\..\extensions\Parse-Parameters.ps1

ConvertTo-ArgumentString -Arguments @{} -Syntax VSIXInstaller
ConvertTo-ArgumentString -Arguments @{} -Syntax VSIXInstaller -InitialUnstructuredArguments @()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Set-StrictMode -Version 5
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
$DebugPreference = 'Continue'
. $PSScriptRoot\..\extensions\Merge-AdditionalArguments.ps1
. $PSScriptRoot\..\extensions\Parse-Parameters.ps1
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
. $root\..\extensions\Merge-AdditionalArguments.ps1
. $root\..\extensions\Parse-Parameters.ps1

$pp = Parse-Parameters '--installPath C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional --add Microsoft.VisualStudio.Workload.Data --add Microsoft.VisualStudio.Workload.ManagedDesktop --includeOptional --addProductLang de-DE --locale en-US --passive'
$ar = @{ passive = ''; norestart = ''; add = 'xyz'; addProductLang = @('l1', 'l2') }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Set-StrictMode -Version 5
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
$DebugPreference = 'Continue'
. $PSScriptRoot\..\extensions\Parse-Parameters.ps1
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
. $root\..\extensions\Parse-Parameters.ps1

Parse-Parameters ''
Parse-Parameters '--argnoval'
Expand All @@ -13,3 +14,9 @@ Parse-Parameters '--add Microsoft.VisualStudio.Workload.Data;includeOptional --l
Parse-Parameters '--arg someval --add Microsoft.VisualStudio.Workload.Data --add Microsoft.VisualStudio.Workload.ManagedDesktop'
Parse-Parameters '--addProductLang de-DE --addProductLang fr-FR --locale en-US'
Parse-Parameters '--installPath C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional --add Microsoft.VisualStudio.Workload.Data --add Microsoft.VisualStudio.Workload.ManagedDesktop --includeOptional --addProductLang de-DE --locale en-US --passive'
Parse-Parameters '--installPath C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional --path install=C:\VSPro' -ErrorAction Continue # should explode
Parse-Parameters '--installPath C:\VSPro --path install=C:\VSPro'
Parse-Parameters '--path install=C:\VSPro --path shared=C:\VS Shared --path cache=C:\VS Cache'
Parse-Parameters '' -DefaultValues $null
Parse-Parameters '' -DefaultValues @{ installChannelUri = 'https://aka.ms/vs/17/release.ltsc.17.2/204262982_1288348623/channel'; bootstrapperPath = '.\vs_Setup.exe' }
Parse-Parameters '--reset-parameter-path --installPath C:\OtherPath' -DefaultValues @{ path = @('install=C:\VSPro', 'shared=C:\VS Shared', 'cache=C:\VS Cache') }
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Set-StrictMode -Version 5
Set-StrictMode -Version 2
$ErrorActionPreference = 'Stop'
$DebugPreference = 'Continue'
. $PSScriptRoot\..\extensions\Parse-Parameters.ps1
. $PSScriptRoot\..\extensions\Merge-AdditionalArguments.ps1
. $PSScriptRoot\..\extensions\Remove-NegatedArguments.ps1
$root = Split-Path -Parent -Path $MyInvocation.MyCommand.Path
. $root\..\extensions\Merge-AdditionalArguments.ps1
. $root\..\extensions\Parse-Parameters.ps1
. $root\..\extensions\Remove-NegatedArguments.ps1

Write-Warning '=========== test bit 1 ==========='

Expand Down

0 comments on commit 835c975

Please sign in to comment.