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; }
static void Main(string[] args) { if (args.Length != 1 && (args.Length != 2 || args[1] != "/s")) { Console.WriteLine("Reports on line statistics for Visual Studio solutions."); Console.WriteLine(); Console.WriteLine("Usage: LineCounter.exe filename [/s]"); } else { bool silent = args.Length == 2; if (!File.Exists("config.txt")) CreateConfig(); string[] configuration = File.ReadAllLines("config.txt"); #if !DEBUG try { #endif Counter counter = new Counter(args[0], configuration); counter.Calculate(); if (!silent) { foreach (string s in counter.Warnings) Console.WriteLine("Warning: " + s); Console.WriteLine("Projects : " + counter.Projects); Console.WriteLine("Files : " + counter.Files); Console.WriteLine("Directories : " + counter.Directories); Console.WriteLine("Total Lines : " + counter.TotalLines); Console.WriteLine("Code Lines : " + counter.CodeLines); Console.WriteLine("Comments : " + counter.Comments); Console.WriteLine("Blank Lines : " + counter.BlankLines); } #if !DEBUG } catch (Exception e) { if (!silent) Console.WriteLine("Error: " + e.Message); } #endif } #if DEBUG Console.ReadLine(); #endif }
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; }