Skip to content

Instantly share code, notes, and snippets.

@tarikguney
Last active December 9, 2016 05:32
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 tarikguney/0fb8da4441d00857b46ed4528576c351 to your computer and use it in GitHub Desktop.
Save tarikguney/0fb8da4441d00857b46ed4528576c351 to your computer and use it in GitHub Desktop.
Finding all references in a solution
/*
Author: Tarik Guney <atarikguney@gmail.com>
GitHub: https://github.com/atarikguney
This class will read a given .sln file and find all the .csproj it has,
and will find the assemblies referenced from within those .csproj files,
and save them to the given text file path.
*/
public class AssemblyNameLister
{
private string SaveTextFilePath { get; set; }
private string SolutionFilePath { get; set;}
public ProjectLister(string solutionFilePath, string pathToSaveReferenceNames)
{
SolutionFilePath = solutionFilePath;
SaveTextFilePath = pathToSaveReferenceNames;
}
public void SaveAllReferencesFromSolution()
{
// The reason why we are opening the file at the beginning is that we give some time
// for the file to be ready for being writablle.
File.OpenWrite(SaveTextFilePath);
var solutionContent = GetAllLinesFromSolutionFile(SolutionFilePath);
// The project path is relative to the project, so setting the working directory
// will help to skip adding absolute path.
Directory.SetCurrentDirectory(Path.GetDirectoryName(SolutionFilePath));
var projects = ExtractProjectsFromSolution(solutionContent);
var assemblies = GetProjectNamesSet(projects);
File.WriteAllLines(SaveTextFilePath, assemblies.ToArray());
}
private ISet<string> GetProjectNamesSet(IReadOnlyList<ProjectDetail> projectDetails)
{
var referenceSet = new SortedSet<string>();
foreach (var element in projectDetails)
{
var document = XDocument.Load(element.Path);
// This is important since .csproj files have the following namespace applied to them.
XNamespace nspace = "http://schemas.microsoft.com/developer/msbuild/2003";
var includeAttributeValues = document.Root.Descendants(nspace + "Reference").Attributes("Include");
foreach (var el in includeAttributeValues)
{
if (!referenceSet.Contains(el.Value))
{
referenceSet.Add(el.Value);
}
}
}
return referenceSet;
}
private string[] GetAllLinesFromSolutionFile(string path)
{
if (File.Exists(path))
{
return File.ReadAllLines(path);
}
throw new FileNotFoundException($"The given path: {path} cannot be found");
}
private IReadOnlyList<ProjectDetail> ExtractProjectsFromSolution(string[] solutionContent)
{
var list = new List<ProjectDetail>();
foreach (var item in solutionContent)
{
var result = Regex.Match(item, @"(?<=Project\(\""\{.*\}\""\)\s\=\s)(.*\.csproj.*)");
if (result.Success)
{
var projectDetail = new ProjectDetail();
var items = result.Value.ToString().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
list.Add(new ProjectDetail()
{
Name = Regex.Match(items[0], "(?<=\").*(?=\")").Value,
Path = Regex.Match(items[1], "(?<=\").*(?=\")").Value
});
}
}
return list;
}
private class ProjectDetail
{
public string Name { get; set; }
public string Path { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment