Last active
January 4, 2016 06:08
-
-
Save TomDeMille/8579611 to your computer and use it in GitHub Desktop.
Linqpad script to parse a directory of .csproj files and dump information about references, both dll and project references, ordered by name and count. Check the to variables at the top, you can display more detail and change from dll to project references by flipping the bit.
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
| //iterate a folder and subfolders, find all .csproj files, find all references, count references (by version) | |
| //requires System.Collections.Generic and System.Linq.Queryable | |
| //see each proj or just the statistics? | |
| var showDetail = false; | |
| var projRefs = false; | |
| System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\Code\Boomtown"); | |
| IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.csproj", System.IO.SearchOption.AllDirectories); | |
| IEnumerable<System.IO.FileInfo> fileQuery = from file in fileList | |
| orderby file.Name | |
| select file; | |
| var sortedListOfRefs = new System.Collections.Generic.SortedList<string,int>(); | |
| if (showDetail) | |
| ("Iterating all .csproj file in " + dir.FullName).Dump("Root"); | |
| foreach(var f in fileQuery) { | |
| if (showDetail) | |
| f.FullName.Dump("Project"); | |
| XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003"; | |
| XDocument projDefinition = XDocument.Load(f.FullName); | |
| IEnumerable<XElement> references = projDefinition | |
| .Element(msbuild + "Project") | |
| .Elements(msbuild + "ItemGroup") | |
| .Elements(msbuild + ((projRefs)? "ProjectReference" : "Reference").ToString()) | |
| .Select(refElem => refElem); | |
| foreach (var r in references) | |
| { | |
| var refName = string.Empty; | |
| var refVer = string.Empty; | |
| var attributes= r.Attribute("Include").Value.Split(','); | |
| refName=attributes[0]; | |
| if(attributes.Length > 1){ | |
| var verAttr = attributes[1]; | |
| var x =verAttr.Split('='); | |
| refVer=x[1]; | |
| } | |
| if (showDetail){ | |
| if(refVer != string.Empty) | |
| (refName + " " + refVer).Dump(); | |
| else | |
| refName.Dump(); | |
| } | |
| var fN = (refName+ ((refVer==string.Empty)? "" : " : " +refVer)).ToLower(); | |
| //stash some simple stats | |
| if (sortedListOfRefs.ContainsKey(fN)) | |
| sortedListOfRefs[fN] = int.Parse(sortedListOfRefs[fN].ToString()) + 1; | |
| else | |
| sortedListOfRefs.Add(fN,1); | |
| } | |
| if (showDetail) | |
| "============================================".Dump(); | |
| } | |
| List<System.Collections.Generic.KeyValuePair<string,int>> sortMe = sortedListOfRefs.OrderByDescending(o=>o.Value).ToList(); | |
| sortMe.Dump("By Count"); | |
| sortedListOfRefs.Dump("by name"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment