Skip to content

Commit

Permalink
chocolatey-visualstudio.extension: Rework handling of channel and pro…
Browse files Browse the repository at this point in the history
…duct ids to support packages which set a custom channel

Related issue: GH-139
  • Loading branch information
jberezanski committed May 5, 2023
1 parent 3a36c96 commit 599da8b
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>chocolatey-visualstudio.extension</id>
<version>1.11.0-preview5</version>
<version>1.11.0-preview6</version>
<packageSourceUrl>https://github.com/jberezanski/ChocolateyPackages/tree/master/chocolatey-visualstudio.extension</packageSourceUrl>
<owners>jberezanski</owners>
<title>Chocolatey Visual Studio servicing extensions</title>
<authors>Jakub Bereżański</authors>
<projectUrl>https://github.com/jberezanski/ChocolateyPackages/tree/master/chocolatey-visualstudio.extension</projectUrl>
<copyright>© 2017-2022 Jakub Bereżański</copyright>
<copyright>© 2017-2023 Jakub Bereżański</copyright>
<licenseUrl>https://rawcdn.githack.com/jberezanski/ChocolateyPackages/8086c84fed16d4150a50ba1e97fd6b75e3c4f511/LICENSE</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<docsUrl>https://github.com/jberezanski/ChocolateyPackages/tree/master/chocolatey-visualstudio.extension/README.md</docsUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function Add-VisualStudioComponent
Write-Debug "Running 'Add-VisualStudioComponent' with PackageName:'$PackageName' Component:'$Component' VisualStudioYear:'$VisualStudioYear' RequiredProductVersion:'$RequiredProductVersion' Preview:'$Preview'";
$argumentList = @('add', "$Component")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
$argumentList += @('includeOptional', '')
}

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,45 @@ function Get-VSChannelReference
Param
(
[Parameter(Mandatory = $true)] [ValidateSet('2017', '2019', '2022')] [string] $VisualStudioYear,
[bool] $Preview
[bool] $Preview,
[hashtable] $PackageParameters
)

switch ($VisualStudioYear)
$channelId = $null
$channelUri = $null
$installChannelUri = $null
if ($null -ne $PackageParameters)
{
'2017' { $majorVersion = 15 }
'2019' { $majorVersion = 16 }
'2022' { $majorVersion = 17 }
default { throw "Unsupported VisualStudioYear: $VisualStudioYear"}
if ($PackageParameters.ContainsKey('channelId'))
{
$channelId = $PackageParameters['channelId']
}

if ($PackageParameters.ContainsKey('channelUri'))
{
$channelUri = $PackageParameters['channelUri']
}

if ($PackageParameters.ContainsKey('installChannelUri'))
{
$installChannelUri = $PackageParameters['installChannelUri']
}
}

$mapPreviewOrReleaseToChannelTypeSuffix = @{ $true = 'Preview'; $false = 'Release' }
$channelId = 'VisualStudio.{0}.{1}' -f $majorVersion, $mapPreviewOrReleaseToChannelTypeSuffix[$Preview]
if ($null -eq $channelId)
{
switch ($VisualStudioYear)
{
'2017' { $majorVersion = 15 }
'2019' { $majorVersion = 16 }
'2022' { $majorVersion = 17 }
default { throw "Unsupported VisualStudioYear: $VisualStudioYear"}
}

$mapPreviewOrReleaseToChannelTypeSuffix = @{ $true = 'Preview'; $false = 'Release' }
$channelId = 'VisualStudio.{0}.{1}' -f $majorVersion, $mapPreviewOrReleaseToChannelTypeSuffix[$Preview]
}

$obj = New-VSChannelReference -ChannelId $channelId
$obj = New-VSChannelReference -ChannelId $channelId -ChannelUri $channelUri -InstallChannelUri $installChannelUri
return $obj
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@
Param
(
[Parameter(Mandatory = $true)] [PSObject] $ChannelReference,
[Parameter(Mandatory = $true)] [string] $Product
[Parameter(Mandatory = $true)] [string] $Product,
[hashtable] $PackageParameters
)

$productId = "Microsoft.VisualStudio.Product." + $Product
$obj = New-VSProductReference -ChannelId $ChannelReference.ChannelId -ProductId $productId
$productId = $null
if ($null -ne $PackageParameters)
{
if ($PackageParameters.ContainsKey('productId'))
{
$productId = $PackageParameters['productId']
}
}

if ($null -eq $productId)
{
$productId = "Microsoft.VisualStudio.Product." + $Product
}

$obj = New-VSProductReference -ChannelId $ChannelReference.ChannelId -ProductId $ProductId -ChannelUri $ChannelReference.ChannelUri -InstallChannelUri $ChannelReference.InstallChannelUri
return $obj
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ function Get-VSRequiredInstallerVersion
Write-Debug 'Obtaining the channel manifest in order to determine the required installer version'
$channelManifest = Get-VSChannelManifest -PackageParameters $PackageParameters -ChannelReference $ChannelReference -UseInstallChannelUri:$UseInstallChannelUri

# VS 2022+
# VS 2022 17.4+
$version = Get-VSChannelManifestItemVersion -Manifest $channelManifest -ChannelItemType 'Bootstrapper' -PropertyName 'installerVersion'
if ($null -ne $version)
{
Write-Verbose "Required installer version determined from the channel manifest (as bootstrapper installerVersion property): '$version'"
}
else
{
# VS 2017-2019
# VS <= 2022 17.3
$version = Get-VSChannelManifestItemVersion -Manifest $channelManifest -ChannelItemType 'Bootstrapper' -PropertyName 'version'
if ($null -ne $version)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,29 @@ Install-ChocolateyPackage

$channelReference = $null
$productReference = $null
if ($packageParameters.ContainsKey('channelId'))
if ($VisualStudioYear -ne '')
{
$channelReference = New-VSChannelReference -ChannelId $packageParameters['channelId']
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview -PackageParameters $packageParameters
}
elseif ($VisualStudioYear -ne '')
elseif ($packageParameters.ContainsKey('channelId'))
{
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
# Fallback for old packages, which did not specify VisualStudioYear.
# The actual year value passed here does not matter, because the function will use the channelId from package parameters.
$channelReference = Get-VSChannelReference -VisualStudioYear '2017' -Preview $Preview -PackageParameters $packageParameters
}

if ($null -ne $channelReference -and $Product -ne '')
{
$productReference = Get-VSProductReference -ChannelReference $channelReference -Product $Product
if ($Product -ne '')
{
$productReference = Get-VSProductReference -ChannelReference $channelReference -Product $Product -PackageParameters $packageParameters
}
elseif ($packageParameters.ContainsKey('productId'))
{
# Fallback for old packages, which did not specify VisualStudioYear.
# The actual product name passed here does not matter, because the function will use the productId from package parameters.
$productReference = Get-VSProductReference -ChannelReference $channelReference -Product 'Ignored' -PackageParameters $packageParameters
}
}

if (-not $creatingLayout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ If the Installer is present, it will be updated/reinstalled if:
[version] $RequiredEngineVersion,
[ValidateSet('2017', '2019', '2022')] [string] $VisualStudioYear = '2017',
[switch] $Preview,
[switch] $Force
[switch] $Force,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -35,9 +36,9 @@ If the Installer is present, it will be updated/reinstalled if:
}
Write-Debug "Running 'Install-VisualStudioInstaller' for $PackageName with Url:'$Url' Checksum:$Checksum ChecksumType:$ChecksumType RequiredInstallerVersion:'$RequiredInstallerVersion' RequiredEngineVersion:'$RequiredEngineVersion' Force:'$Force'";

$packageParameters = Parse-Parameters $env:chocolateyPackageParameters
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview
$PSBoundParameters.Remove('VisualStudioYear')
$PSBoundParameters.Remove('Preview')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ function Remove-VisualStudioComponent
Write-Debug "Running 'Remove-VisualStudioComponent' with PackageName:'$PackageName' Component:'$Component' VisualStudioYear:'$VisualStudioYear' Preview:'$Preview'";
$argumentList = @('remove', "$Component")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
[Parameter(Mandatory = $true)] [string] $PackageName,
[Parameter(Mandatory = $true)] [string] $Product,
[Parameter(Mandatory = $true)] [string] $VisualStudioYear,
[bool] $Preview
[bool] $Preview,
[hashtable] $DefaultParameterValues
)
if ($null -ne $Env:ChocolateyPackageDebug)
{
Expand All @@ -15,9 +16,9 @@
}

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 = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues @{ channelId = $channelReference.ChannelId; productId = $productReference.ProductId }
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters
$productReference = Get-VSProductReference -ChannelReference $channelReference -Product $Product -PackageParameters $packageParameters
Start-VSModifyOperation `
-PackageName $PackageName `
-ArgumentList @() `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
Write-Debug "Running 'Remove-VisualStudioWorkload' with PackageName:'$PackageName' Workload:'$Workload' VisualStudioYear:'$VisualStudioYear' Preview:'$Preview'";
$argumentList = @('remove', "Microsoft.VisualStudio.Workload.$Workload")

$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview $Preview
$packageParameters = Parse-Parameters $env:chocolateyPackageParameters -DefaultValues $DefaultParameterValues
$channelReference = Get-VSChannelReference -VisualStudioYear $VisualStudioYear -Preview:$Preview -PackageParameters $packageParameters
Start-VSModifyOperation `
-PackageName $PackageName `
-PackageParameters $packageParameters `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@
)
Write-Debug "Running 'Start-VSModifyOperation' with PackageName:'$PackageName' ArgumentList:'$ArgumentList' ChannelReference:'$ChannelReference' ApplicableProducts:'$ApplicableProducts' OperationTexts:'$OperationTexts' Operation:'$Operation' RequiredProductVersion:'$RequiredProductVersion' BootstrapperUrl:'$BootstrapperUrl' BootstrapperChecksum:'$BootstrapperChecksum' BootstrapperChecksumType:'$BootstrapperChecksumType' ProductReference:'$ProductReference' UseBootstrapper:'$UseBootstrapper'";

if ($null -eq $ProductReference -and $Operation -eq 'update')
if ($null -eq $ProductReference)
{
throw 'ProductReference is mandatory for update operations.'
if ($PackageParameters.ContainsKey('productId'))
{
# Workload/component packages do not pass a ProductReference, because they may apply to several products.
# However, the user can explicitly narrow the operation scope via package parameters.
# The actual product name passed here does not matter, because the function will use the productId from package parameters.
$ProductReference = Get-VSProductReference -ChannelReference $channelReference -Product 'Ignored' -PackageParameters $packageParameters
}
elseif ($Operation -eq 'update')
{
throw 'ProductReference is mandatory for update operations.'
}
}

$frobbed, $frobbing, $frobbage = $OperationTexts
Expand Down Expand Up @@ -62,16 +72,6 @@
$argumentSets = ,$baseArgumentSet
if ($baseArgumentSet.ContainsKey('installPath'))
{
if ($baseArgumentSet.ContainsKey('productId'))
{
Write-Warning 'Parameter issue: productId is ignored when installPath is specified.'
}

if ($baseArgumentSet.ContainsKey('channelId'))
{
Write-Warning 'Parameter issue: channelId is ignored when installPath is specified.'
}

$installedProducts = Resolve-VSProductInstance -AnyProductAndChannel -PackageParameters $PackageParameters
if (($installedProducts | Measure-Object).Count -gt 0)
{
Expand All @@ -96,19 +96,6 @@
Write-Warning "Did not detect any installed Visual Studio products at path $($baseArgumentSet['installPath'])."
}
}
elseif ($baseArgumentSet.ContainsKey('productId'))
{
if (-not $baseArgumentSet.ContainsKey('channelId'))
{
throw "Parameter error: when productId is specified, channelId must be specified, too."
}

$baseArgumentSet['__internal_productReference'] = New-VSProductReference -ChannelId $baseArgumentSet['channelId'] -ProductId $baseArgumentSet['productId']
}
elseif ($baseArgumentSet.ContainsKey('channelId'))
{
throw "Parameter error: when channelId is specified, productId must be specified, too."
}
else
{
if (($ProductInstance | Measure-Object).Count -ne 0)
Expand All @@ -117,7 +104,15 @@
}
else
{
$installedProducts = Resolve-VSProductInstance -ChannelReference $ChannelReference -PackageParameters $PackageParameters
if ($null -ne $ProductReference)
{
$installedProducts = Resolve-VSProductInstance -ProductReference $ProductReference -PackageParameters $PackageParameters
}
else
{
$installedProducts = Resolve-VSProductInstance -ChannelReference $ChannelReference -PackageParameters $PackageParameters
}

if (($installedProducts | Measure-Object).Count -eq 0)
{
throw "Unable to detect any supported Visual Studio product. You may try passing --installPath or both --productId and --channelId parameters."
Expand Down Expand Up @@ -289,15 +284,8 @@
$overallExitCode = 0
foreach ($argumentSet in $argumentSets)
{
if ($argumentSet.ContainsKey('installPath'))
{
$productDescription = "Visual Studio product: [installPath = '$($argumentSet.installPath)']"
}
else
{
$productDescription = "Visual Studio product: [productId = '$($argumentSet.productId)' channelId = '$($argumentSet.channelId)']"
}

# installPath should always be present
$productDescription = "Visual Studio product: [installPath = '$($argumentSet.installPath)']"
Write-Debug "Modifying $productDescription"

$thisProductReference = $ProductReference
Expand Down

0 comments on commit 599da8b

Please sign in to comment.