private void FindUnreferencedFilesInDirectory(DirectoryInfo directory, ReferencedProjectFiles referencedProjectFiles, List <string> unreferencedFiles) { try { foreach (FileInfo file in directory.GetFiles()) { if (FileRequiresChecking(file.FullName)) { string relativeFileName = GetFileNameRelativeToProject(file.FullName); if (!referencedProjectFiles.IsFileReferenced(relativeFileName)) { unreferencedFiles.Add(file.FullName); } } } foreach (DirectoryInfo subDirectory in directory.GetDirectories()) { if (DirectoryRequiresChecking(subDirectory.FullName)) { FindUnreferencedFilesInDirectory(subDirectory, referencedProjectFiles, unreferencedFiles); } } } catch (Exception e) { string msg = String.Format("Error while finding unreferenced files in the '{0}' directory.", directory.FullName); throw new Exception(msg, e); } }
/// <summary> /// Adds all of the files in the item group into the ReferencedProjectFiles object. /// </summary> /// <param name="itemGroupElement">An ItemGroup element from a MSBuild project.</param> /// <param name="referencedProjectFiles">The object to add all referenced files into.</param> private void AddProjectFilesFromItemGroup(XElement itemGroupElement, ReferencedProjectFiles referencedProjectFiles) { var itemElements = from element in itemGroupElement.Elements() select element; foreach (XElement itemElement in itemElements) { if (itemElement.Name == MSBUILD_NAMESPACE + ELEMENT_REFERENCE) { // Ignore Reference elements. continue; } XAttribute includeAttribute = itemElement.Attribute(ATTRIBUTE_INCLUDE); if (includeAttribute == null) { // Include is a required attribute on an item element. See http://msdn.microsoft.com/en-us/library/ms164283.aspx string msg = String.Format("The '{0}' attribute was missing from the '{1}' element.", ATTRIBUTE_INCLUDE, itemElement.Name); throw new Exception(msg); } string filePath = includeAttribute.Value; referencedProjectFiles.AddFile(filePath); } }
/// <summary> /// Parses the given project file to build a ReferencedProjectFiles object containing all the files referenced by the project. /// </summary> /// <param name="projectElement">The project element from a MSBuild project.</param> /// <returns>A ReferencedProjectFiles containing all the files referenced by the project.</returns> public ReferencedProjectFiles GetReferencedFilesForProject(XElement projectElement) { ReferencedProjectFiles referencedProjectFiles = new ReferencedProjectFiles(); AddProjectFiles(projectElement, referencedProjectFiles); return(referencedProjectFiles); }
private ReferencedProjectFiles GetReferencedProjectFiles() { XElement projectElement = XElement.Load(ProjectFile); ProjectParser projectParser = new ProjectParser(); ReferencedProjectFiles referencedProjectFiles = projectParser.GetReferencedFilesForProject(projectElement); return(referencedProjectFiles); }
/// <summary> /// This method assumes that all referenced files are children of an ItemGroup element. /// It finds all the project's item groups and then adds the files from each item group to the ReferencedProjectFiles object. /// </summary> /// <param name="projectElement">The project element from a MSBuild project.</param> /// <param name="referencedProjectFiles">The object to add all referenced files into.</param> private void AddProjectFiles(XElement projectElement, ReferencedProjectFiles referencedProjectFiles) { var itemGroupElements = from element in projectElement.Descendants(MSBUILD_NAMESPACE + ELEMENT_ITEM_GROUP) select element; foreach (XElement itemGroupElement in itemGroupElements) { AddProjectFilesFromItemGroup(itemGroupElement, referencedProjectFiles); } }
public List <string> FindUnreferencedProjectFiles() { try { DirectoryInfo projectDirectory = new DirectoryInfo(Path.GetDirectoryName(ProjectFile)); ReferencedProjectFiles referencedProjectFiles = GetReferencedProjectFiles(); List <string> unreferencedFiles = new List <string>(); FindUnreferencedFilesInDirectory(projectDirectory, referencedProjectFiles, unreferencedFiles); return(unreferencedFiles); } catch (Exception e) { string msg = String.Format("Error while finding unreferenced files for the '{0}' project.", ProjectFile); throw new Exception(msg, e); } }