Skip to content

Instantly share code, notes, and snippets.

@peplau
Last active November 2, 2021 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peplau/450a72767b8a15a61db552560bc8bf9f to your computer and use it in GitHub Desktop.
Save peplau/450a72767b8a15a61db552560bc8bf9f to your computer and use it in GitHub Desktop.
Updates a packages.config file into your desired version of Sitecore to help automatize upgrades with the aid of the CSV provided by Jan Bluemink: http://www.stockpick.nl/english/sitecore-nuget-dependencies-in-sitecore-101/
<#
.Synopsis
Provides NuGet commands to updates a packages.config file into your desired version of Sitecore to help automatize upgrades
with the aid of the CSV provided by Jan Bluemink: http://www.stockpick.nl/english/sitecore-nuget-dependencies-in-sitecore-101/
.Description
The script uses a CSV file to scan the existing packages.config file and generate NuGet commands to update the project
.Example
.\Update-PackagesFile.ps1 -Csv "C:\temp\NuGet Sitecore.Platform.Assemblies 10.1.0 rev. 005207.csv" -Packages "D:\Git\myproject\packages.config" -ProjectName "Altudo.SitecoreMarketingAutomation.Connector.Common"
#>
[CmdletBinding(DefaultParameterSetName = "None", SupportsShouldProcess = $true)]
param(
# Path to the CSV file
[parameter(mandatory=$True)]
[string]
$Csv,
# Path to the packages.config file
[parameter(mandatory=$True)]
[string]
$Packages,
# Project Name
[parameter(mandatory=$True)]
[string]
$ProjectName,
# Target Sitecore version
[parameter()]
[string]
$TargetSitecore,
# If $true => Will dump the Update-Package with -reinstall commands for libraries not used by Sitecore
[parameter()]
[bool]
$DumpUnknownReinstall,
# If $true => Will dump the Update-Package commands for known libraries when versions doesn't match
[parameter()]
[bool]
$DumpKnownUpdates,
# If $true => Will dump the Update-Package with -reinstall when versions are the same
[parameter()]
[bool]
$DumpKnownReinstall,
# If $true => Will dump the Update-Package for Sitecore.* libraries
[parameter()]
[bool]
$DumpSitecoreUpdates,
# If $true => then the Sitecore entries will NOT be uninstalled (Update)
# If $false => then the Sitecore entries will be uninstalled and re-installed (useful when migrating from earlier versions into SC10)
[parameter()]
[bool]
$KeepSitecoreLibs
)
Begin {
# Load packages.config file into array of lines
$packagesArray = Select-Xml -Path $Packages -XPath "/packages/package" | Select-Object -ExpandProperty Node
Write-Host 'Lines in the packages.config: ' -NoNewline
Write-Host $packagesArray.Count
# Load the CSV file into memory
$csvArray = Import-Csv $Csv
Write-Host 'Lines in the CSV: ' -NoNewline
Write-Host $csvArray.Length
# Add column "NuGetName" and "NuGetVersion" to the array
foreach($csvEntry in $csvArray){
$NuGetVersion = $csvEntry.'NUGet version'
$parts = $NuGetVersion.Split('.')
$name = ""
$version = ""
foreach($part in $parts){
if ([int]::TryParse($part,[ref]$null)){
$version += "$($part)."
continue;
}
$name += "$($part)."
}
if ($name.Length -gt 0){
$name = $name.Substring(0,$name.Length-1)
}
if ($version.Length -gt 0){
$version = $version.Substring(0,$version.Length-1)
}
Add-Member -InputObject $csvEntry -NotePropertyName "NuGetName" -NotePropertyValue $name
Add-Member -InputObject $csvEntry -NotePropertyName "NuGetVersion" -NotePropertyValue $version
}
# For each entry in the packages.config file
foreach($package in $packagesArray)
{
$packageId = $package.id
$packageVersion = $package.version
$isSitecore = $packageId.StartsWith("Sitecore.")
# Check if the packages entry exists in the CSV
if ($isSitecore){
if ($DumpSitecoreUpdates -and $packageVersion -ne $TargetSitecore){
# Uninstall and re-install Sitecore libraries
if ($KeepSitecoreLibs -eq $false){
# Uninstall
Write-Host "Uninstall-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' "
# Re-install
Write-Host "Install-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' " -NoNewline
Write-Host "-Version '$($TargetSitecore)' -IgnoreDependencies "
}
else {
Write-Host "Update-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' " -NoNewline
Write-Host "-Version '$($TargetSitecore)' "
}
}
} else {
$packFromCsv = $csvArray | Where-Object { $_.NuGetName -eq $packageId }
if ($packFromCsv){
$ver = $packFromCsv.NuGetVersion
if ($DumpKnownUpdates){
if ($ver -ne $packageVersion){
Write-Host "Update-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' " -NoNewline
Write-Host "-Version '$($ver)' "
}
}
if ($DumpKnownReinstall){
if ($ver -eq $packageVersion){
Write-Host "Update-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' -reinstall"
}
}
}
else {
if ($DumpUnknownReinstall){
Write-Host "Update-Package '" -NoNewline
Write-Host $packageId -NoNewline
Write-Host "' -ProjectName '$($ProjectName)' -reinstall"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment