Last active
February 18, 2021 07:00
-
-
Save a4099181/074a6c3dd524ea0d343382137492399c to your computer and use it in GitHub Desktop.
Converts packages.config into PackageReference at *.csproj project file. Requires XSLT stylesheet available as second file in the gist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Convert-ToPackageReference | |
{ | |
Param ( [Parameter( Mandatory, ValueFromPipeline )][String] $inputUri, | |
[String] $stylesheetUri = "https://gist.githubusercontent.com/a4099181/074a6c3dd524ea0d343382137492399c/raw/cdd0fb31efd70c4c0f8c86ddb314de86ab8972e8/Convert-ToPackageReference.xsl", | |
[String] $resultsFile = [System.IO.Path]::GetTempFileName() ) | |
Process { | |
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform | |
$xslt.Load( $stylesheetUri ) | |
$xslt.Transform( $inputUri, $resultsFile ) | |
Get-Content $resultsFile | |
} | |
} | |
Function Update-ProjectFile | |
{ | |
Param ( [Parameter( Mandatory )][String] $projectFile, | |
[Parameter( Mandatory, ValueFromPipeline )][String] $content ) | |
Begin { $packageReference = '' } | |
Process { $packageReference += $content } | |
End { | |
$projXml = New-Object System.Xml.XmlDocument | |
$projXml.Load( $projectFile ) | |
$nameSpc = New-Object System.Xml.XmlNamespaceManager $projXml.NameTable | |
$nameSpc.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); | |
$prefXml = New-Object System.Xml.XmlDocument | |
$prefXml.LoadXml( $packageReference ) | |
$prefXml.SelectNodes( "/x:Project/x:ItemGroup/x:PackageReference", $nameSpc ) | | |
% { | |
$pkgNode = $projXml.ImportNode( $_, $true ) | |
$pkgName = $_.Include.Split('-')[0] | |
$version = $_.Version | |
$nodes = $projXml.SelectNodes( "/x:Project/x:ItemGroup/x:Reference", $nameSpc ) | | |
? Include -Like "$pkgName*" | |
if ( $nodes.Count -eq 0 ) | |
{ | |
Write-Information "$projectFile $pkgName package outside the project file." | |
$itemGroup = $projXml.CreateElement( "ItemGroup", $nameSpc.LookupNamespace( "x" ) ) | |
$itemGroup.AppendChild( $pkgNode ) | |
$projXml.DocumentElement.AppendChild( $itemGroup ) | |
} | |
else { | |
$nodes | % { | |
if ( $_.Include -Match "$version" ) | |
{ | |
Write-Information "$projectFile $pkgName valid reference." | |
$_.ParentNode.AppendChild( $pkgNode ) | |
$_.ParentNode.RemoveChild( $_ ) | |
} | |
else | |
{ | |
Write-Warning "$projectFile $pkgName version mismatched." | |
$_.ParentNode.InsertBefore( $pkgNode, $_ ) | |
} | |
} | |
} | |
} | |
$projXml.SelectNodes( "/x:Project/x:ItemGroup/x:*[@Include='packages.config']", $nameSpc ) | | |
% { $_.ParentNode.RemoveChild( $_ ) } | |
$projXml.Save( $projectFile ) | |
} | |
} | |
Get-ChildItem . -Recurse -File -Filter packages.config | | |
select @{ Name="ProjectFile"; Expression={ "$($_.Directory)\$($_.Directory.BaseName).csproj" } }, | |
@{ Name="PackagesConfig"; Expression={ $_.FullName } } | | |
? { Test-Path $_.ProjectFile } | | |
% { | |
$_.PackagesConfig | Convert-ToPackageReference | Update-ProjectFile $_.ProjectFile -InformationAction Continue | |
Remove-Item $_.PackagesConfig | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<xsl:output omit-xml-declaration="yes" method="xml" indent="no"/> | |
<xsl:template match="/"> | |
<xsl:element name="Project"> | |
<xsl:element name="ItemGroup"> | |
<xsl:apply-templates/> | |
</xsl:element> | |
</xsl:element> | |
</xsl:template> | |
<xsl:template match="package"> | |
<xsl:element name="PackageReference"> | |
<xsl:attribute name="Include"> | |
<xsl:value-of select="@id" /> | |
</xsl:attribute> | |
<xsl:attribute name="Version"> | |
<xsl:value-of select="@version" /> | |
</xsl:attribute> | |
</xsl:element> | |
</xsl:template> | |
</xsl:stylesheet> |
The script saved my night! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aienabled Fine, well done. I'm very glad that you've got it worked out and my script is useful anyway. Yes, "usage tip" does not include downloading
xslt
file. It is not required whileis able to handle
xslt
file from web.