Skip to content

Instantly share code, notes, and snippets.

@aolszowka
Last active October 13, 2020 21:06
Show Gist options
  • Save aolszowka/15b92e364279cc2a96f05b372fd5e6d5 to your computer and use it in GitHub Desktop.
Save aolszowka/15b92e364279cc2a96f05b372fd5e6d5 to your computer and use it in GitHub Desktop.
PowerShell Snippet To Find CSPROJ Files with Particular Package Reference
[string[]]$ReferenceAssemblies =
"System.Collections.Concurrent",
"System.IO.FileSystem",
"System.Linq",
"System.Linq.Parallel",
"System.Runtime.Extensions",
"System.Xml",
"System.Xml.Linq",
"System.Xml.ReaderWriter",
"System.Xml.XDocument"
Add-Type @'
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
public static class MsBuildProjectEvaluator
{
public static string[] GetProjectsWithPackageReferenceTo(string targetDirectory, string packageReference)
{
return
Directory
.EnumerateFiles(targetDirectory, "*.csproj", SearchOption.AllDirectories)
.AsParallel()
.Where(projectFile => ProjectContainsPackageReference(projectFile, packageReference))
.ToArray();
}
public static bool ProjectContainsPackageReference(string projectFile, string packageName)
{
XNamespace msbuildNS = @"http://schemas.microsoft.com/developer/msbuild/2003";
var projXml = XDocument.Load(projectFile);
bool containsPackageReference =
projXml
.Descendants(msbuildNS + "PackageReference")
.Any(packageReference =>
packageReference
.Attribute("Include")
.Value
.Equals(packageName, StringComparison.InvariantCultureIgnoreCase)
);
return containsPackageReference;
}
}
'@ -ReferencedAssemblies $ReferenceAssemblies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment