Created
March 3, 2022 13:00
-
-
Save nimatt/ccb34bcaea70d8eab0da948c833c2258 to your computer and use it in GitHub Desktop.
Sync NuGet package versions among projects
This file contains hidden or 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
## WARNING: This is a one of hack that is saved for further refinement if needed in future | |
$Projects = Get-ChildItem . -Recurse -Filter *.csproj | |
$Packages = @{} | |
foreach ($Project in $Projects) { | |
[Regex]::Matches((Get-Content -Raw $Project.FullName), '<PackageReference\s+Include="(?<name>[^"]+)"\s+Version="(?<version>[^"]+)"') | | |
ForEach-Object { | |
if (-not $Packages.ContainsKey($_.Groups['name'].Value)) { | |
$Packages.Add($_.Groups['name'].Value, (New-Object System.Collections.Generic.HashSet[string])) | |
} | |
$Packages[$_.Groups['name'].Value].Add($_.Groups['version'].Value) | Out-Null | |
} | |
} | |
$Highest = @{} | |
$Packages.Keys | | |
Where-Object { $Packages[$_].Count -gt 1} | | |
ForEach-Object { | |
Write-Host "${_}: $($Packages[$_])" | |
$version = $Packages[$_] | | |
ForEach-Object { | |
[string]::Join('.', ($_.Split('.') | ForEach-Object {$_.PadLeft(3,'0')})) | |
} | | |
Sort-Object -Descending | | |
Select -First 1 | | |
Foreach-Object { | |
[string]::Join('.', ($_.Split('.') | ForEach-Object {$_.TrimStart('0').PadLeft(1, '0')})) | |
} | |
$Highest.Add($_, $version) | |
} | |
foreach ($Project in $Projects) { | |
$content = Get-Content -Raw $Project.FullName | |
foreach ($ref in $Highest.Keys) { | |
[Regex]::Matches((Get-Content -Raw $Project.FullName), "<PackageReference\s+Include=`"$ref`"\s+Version=`"[^`"]+`"") | | |
ForEach-Object { | |
Write-Host "<PackageReference Include=`"$ref`" Version=`"$($Highest[$ref])`"" | |
$content = $content.Replace($_.Value, "<PackageReference Include=`"$ref`" Version=`"$($Highest[$ref])`"") | |
} | |
} | |
$content.Trim() | Out-File -FilePath $Project.FullName -Encoding utf8NoBOM | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment