Skip to content

Instantly share code, notes, and snippets.

@NatMarchand
Created March 27, 2024 13:51
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 NatMarchand/c1f0db53b3d05928246442aea731028a to your computer and use it in GitHub Desktop.
Save NatMarchand/c1f0db53b3d05928246442aea731028a to your computer and use it in GitHub Desktop.
Global
var path = @"";
var allVersions = new Dictionary<string, HashSet<string>>();
foreach (var file in Directory.EnumerateFiles(path, "*.*proj", SearchOption.AllDirectories))
{
var doc = XDocument.Load(file);
bool hasChanges = false;
foreach (var reference in doc.XPathSelectElements("//PackageReference"))
{
hasChanges = true;
var package = reference.Attribute("Include").Value;
var versions = allVersions.GetValueOrDefault(package, new HashSet<string>());
var versionAttribute = reference.Attribute("Version");
versions.Add(versionAttribute.Value);
allVersions[package] = versions;
versionAttribute.Remove();
}
if (hasChanges)
{
File.WriteAllText(file, doc.ToString());
}
}
Console.WriteLine(
string.Join(Environment.NewLine,
allVersions.ToDictionary(v => v.Key, v => v.Value.OrderDescending().First())
.OrderBy(v => v.Key)
.Select(v => $"<PackageVersion Include=\"{v.Key}\" Version=\"{v.Value}\" />")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment