static void ProcessElement(Counter counter, XElement element, Node rootNode) { foreach (XElement e in element.Elements("File")) { XAttribute attribute = e.Attribute("RelativePath"); if (attribute == null) continue; FileNode file = new FileNode(); file.Path = Path.Combine(Path.GetDirectoryName(projectPath), attribute.Value); counter.CountFile(file); if (file.Valid) rootNode.ChildNodes.Add(file); } foreach (XElement e in element.Elements("Filter")) { XAttribute attribute = e.Attribute("Name"); if (attribute == null) continue; Node node = new Node(); node.Name = attribute.Value; ProcessElement(counter, e, node); rootNode.ChildNodes.Add(node); } }
public static Node Process(Counter counter, string fileName) { projectPath = fileName; Node rootNode = new Node(); rootNode.Name = Path.GetFileNameWithoutExtension(fileName); XDocument project = XDocument.Load(fileName); XElement rootFile = project.Element("VisualStudioProject").Element("Files"); ProcessElement(counter, rootFile, rootNode); return rootNode; }
public void Output(Node rootNode) { string directory = outputDirectory; Directory.CreateDirectory(directory); File.WriteAllText(Path.Combine(directory, "default.css"), Resources._default); directory = Path.Combine(directory, "img"); Directory.CreateDirectory(directory); Resources.bg.Save(Path.Combine(directory, "bg.gif")); Resources.bgcontainer.Save(Path.Combine(directory, "bgcontainer.gif")); Resources.bgfooter.Save(Path.Combine(directory, "bgfooter.gif")); Resources.bgheader.Save(Path.Combine(directory, "bgheader.gif")); Resources.bgnavigation.Save(Path.Combine(directory, "bgnavigation.gif")); Resources.bgul.Save(Path.Combine(directory, "bgul.gif")); Resources.li.Save(Path.Combine(directory, "li.gif")); Resources.quote.Save(Path.Combine(directory, "quote.gif")); Resources.sidenavh1.Save(Path.Combine(directory, "sidenavh1.gif")); OutputHtml(rootNode); }
void OutputHtml(Node rootNode) { FileNode results = Counter.Count(rootNode); string indexPage = Resources.index.Replace("$Title", rootNode.Name); StringBuilder content = new StringBuilder(); content.AppendLine("<h1>Solution Overview</h1>"); content.Append("<p>Project Count: "); content.Append(rootNode.ChildNodes.Count); content.AppendLine("<br>"); content.Append("Folder Count: "); content.Append(Counter.CountFolders(rootNode, false)); content.AppendLine("<br>"); content.Append("Total Line Count: "); content.Append(results.TotalLines); content.AppendLine("<br>"); content.AppendLine("<ul>"); content.Append("<li>Code Lines: "); content.Append(results.CodeLines); content.AppendLine("</li>"); content.Append("<li>Blank Lines: "); content.Append(results.BlankLines); content.AppendLine("</li>"); content.Append("<li>Comments: "); content.Append(results.Comments); content.AppendLine("</li>"); content.AppendLine("</ul></p>"); indexPage = indexPage.Replace("$Content", content.ToString()); File.WriteAllText(Path.Combine(outputDirectory, "index.html"), indexPage); }
public static Node Process(Counter counter, string fileName) { Node rootNode = new Node(); rootNode.Name = Path.GetFileNameWithoutExtension(fileName); XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; XDocument document; try { document = XDocument.Load(fileName); } catch (XmlException) { return null; } XElement projectElement = document.Element(ns + "Project"); if (projectElement == null) return null; foreach (XElement e in projectElement.Elements(ns + "ItemGroup")) { foreach (XElement i in e.Elements(ns + "Compile")) { string file = i.Attribute("Include").Value; string path = Path.Combine(Path.GetDirectoryName(fileName), file); Node currentNode = rootNode; while (file.Contains('\\')) { int index = file.IndexOf('\\'); string directory = file.Substring(0, index); file = file.Substring(index + 1); bool found = false; foreach (Node node in currentNode.ChildNodes) { if (node.Name == directory) { found = true; currentNode = node; break; } } if (!found) { Node node = new Node(); node.Name = directory; currentNode.ChildNodes.Add(node); currentNode = node; } } FileNode fileNode = new FileNode(); fileNode.Path = path; counter.CountFile(fileNode); if (fileNode.Valid) currentNode.ChildNodes.Add(fileNode); } } return rootNode; }
internal static FileNode Count(Node node) { FileNode results = new FileNode(); foreach (Node n in node.ChildNodes) { FileNode fn = n as FileNode; if (fn == null) fn = Count(n); results.BlankLines += fn.BlankLines; results.CodeLines += fn.CodeLines; results.Comments += fn.Comments; results.TotalLines += fn.TotalLines; } return results; }
internal static int CountFiles(Node node) { int count = 0; foreach (Node n in node.ChildNodes) { if (n is FileNode) count++; count += CountFiles(n); } return count; }
internal static int CountFolders(Node node, bool countTopLevel) { int count = 0; foreach (Node n in node.ChildNodes) { if (!(n is FileNode)) { if (countTopLevel) count++; count += CountFolders(n, true); } } return count; }
public void Calculate() { Node rootNode = new Node(); rootNode.Name = Path.GetFileNameWithoutExtension(inputFile); foreach (string line in File.ReadAllLines(inputFile).Where(s => s.StartsWith("Project", false, CultureInfo.InvariantCulture))) { string project = line.Extract('"', 5, 6); if (!project.Contains("proj")) continue; project = Path.Combine(Path.GetDirectoryName(inputFile), project); if (project.Contains(".vcproj")) rootNode.ChildNodes.Add(VCProject.Process(this, project)); else { Node node = CSProject.Process(this, project); if( node != null ) rootNode.ChildNodes.Add(node); else Warnings.Add("Invalid project type: " + Path.GetExtension(project)); } } if (rootNode.ChildNodes.Count == 0) throw new InvalidOperationException("No valid projects found."); FileNode results = Count(rootNode); CodeLines = results.CodeLines; Comments = results.Comments; BlankLines = results.BlankLines; TotalLines = results.TotalLines; Directories = CountFolders(rootNode, false); Projects = rootNode.ChildNodes.Count; Files = CountFiles(rootNode); }