Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Assists in updating an existing Sitecore Visual Studio solution to the new nuget structure for 9.1, when using packages.config style Nuget. This code is provided AS-IS, and is not supported by Sitecore.
## Nuget v2 URL for the target platform, and the target marketing version
$SitecoreNuget = "https://sitecore.myget.org/F/sc-platform-9-1/api/v2"
$TargetVersion = "9.1.0"
function Add-XMLAttribute([System.Xml.XmlNode] $Node, $Name, $Value)
{
$attrib = $Node.OwnerDocument.CreateAttribute($Name)
$attrib.Value = $Value
$node.Attributes.Append($attrib)
}
## Nuget URL to retrieve package metadata, including dependencies
$MetaPackage = "$SitecoreNuget/Packages(Id='Sitecore.Experience.Platform',Version='$TargetVersion')"
## Retrieve the package metadata from nuget (no nice way of doing this via CLI)
$PackageMetadata = [xml](Invoke-WebRequest $MetaPackage).Content
## Parse the package dependencies so we know the appropriate package versions for this release
$versions = $PackageMetadata.entry.properties.Dependencies.Split('|') | % {
$dependencySplit = $_.Split(':')
New-Object PSObject -Property @{
Name = $dependencySplit[0]
Version = $dependencySplit[1]
}
}
## Package folder may be at a different depth but should always contain this
$packageFolder = "..\packages\"
Get-ChildItem *.csproj -recurse | % {
New-Object psobject -Property @{
File = $_
ProjectXml = [xml](gc $_ -Raw)
}
} | % {
$fileName = $_.File.Name
## Find the packages.config for the project
$packagesConfig = $_.File.Directory | Get-ChildItem | ? { $_.Name -eq "packages.config" } | select -first 1
if (-not $packagesConfig) {
Write-Host "WARNING: Could not find packages.config for $fileName" -ForegroundColor yellow
return
}
$packagesConfigXml = [xml]($packagesConfig | gc -Raw)
## Use of packages.config means you'll have a regular Reference
$_.ProjectXml.GetElementsByTagName("Reference") | ? {
## Ensure it's a Sitecore nuget reference, that's not FakeDb
$_.HintPath -and $_.HintPath.Contains($packageFolder) -and $_.Include.StartsWith("Sitecore") -and -not $_.Include.Contains("FakeDb")
} | % {
## Resolve the full path to the packages folder
$packagesPath = $_.HintPath.Substring(0, $_.HintPath.IndexOf($packageFolder) + $packageFolder.Length)
## For Sitecore nuget, package name will be assembly name
$packageName = $_.Include.Split(",")[0]
## Look up the appropriate version
$newPackage = $versions | ? { $_.Name -eq $packageName } | select -first 1
if (-not $newPackage) {
Write-Host "WARNING: Could not find new version for package $packageName in $fileName" -ForegroundColor yellow
return
}
## Version will include pin markers, not needed here
$numberVersion = $newPackage.Version.Replace("[", "").Replace("]", "")
## Update reference and path based on metadata lookup
$_.Include = "$packageName" ## Would be difficult to determine assembly version, might differ from package
$_.HintPath = "$($packagesPath)$($packageName).$numberVersion\lib\net471\$($packageName).dll"
## Find the package in packages.config, maybe with .NoReferences suffix
$packageReference = $packagesConfigXml.packages.package | ? { $_.id -eq $packageName } | select -first 1
if (-not $packageReference) {
$packageReference = $packagesConfigXml.packages.package | ? { $_.id -eq "$packageName.NoReferences" } | select -first 1
}
if (-not $packageReference) {
Write-Host "WARNING: Could not find $packageName in packages.config for $fileName" -ForegroundColor yellow
return
}
## Ensure package no longer uses .NoReferences
$packageReference.id = $packageName
$packageReference.version = $numberVersion
if ($packageReference.allowedVersions) {
$packageReference.allowedVersions = $newPackage.Version
} else {
Add-XMLAttribute -Node $packageReference -Name allowedVersions -Value $newPackage.Version | out-null
}
$packageReference.targetFramework = "net471"
}
## Save the project file and packages.config
$_.ProjectXml.Save($_.File)
$packagesConfigXml.Save($packagesConfig.FullName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.