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) { 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 void CountFile(FileNode file) { string extension = Path.GetExtension(file.Path).ToLower(); if (!fileTypes.Contains(extension)) { file.Valid = false; return; } if (!File.Exists(file.Path)) { throw new InvalidOperationException("Could not open file " + file.Path); } file.Valid = true; file.Name = Path.GetFileName(file.Path); string[] lines = File.ReadAllLines(file.Path); bool inComment = false; CommentProfile profile = CommentProfile.Empty; if (profiles.ContainsKey(extension)) { profile = profiles[extension]; } foreach (string line in lines) { file.TotalLines++; string temp = line.Trim(); if (string.IsNullOrEmpty(temp)) { file.BlankLines++; } else if (inComment) { file.Comments++; if (temp.Contains(profile.EndComment)) { inComment = false; } } else { if (profile.LineComment != null && temp.StartsWith(profile.LineComment)) { file.Comments++; } else if (profile.StartComment != null && temp.Contains(profile.StartComment)) { file.Comments++; inComment = true; } else { file.CodeLines++; } } } }
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 void CountFile(FileNode file) { string extension = Path.GetExtension(file.Path).ToLower(); if (!fileTypes.Contains(extension)) { file.Valid = false; return; } if (!File.Exists(file.Path)) throw new InvalidOperationException("Could not open file " + file.Path); file.Valid = true; file.Name = Path.GetFileName(file.Path); string[] lines = File.ReadAllLines(file.Path); bool inComment = false; CommentProfile profile = CommentProfile.Empty; if (profiles.ContainsKey(extension)) profile = profiles[extension]; foreach (string line in lines) { file.TotalLines++; string temp = line.Trim(); if (string.IsNullOrEmpty(temp)) file.BlankLines++; else if (inComment) { file.Comments++; if (temp.Contains(profile.EndComment)) inComment = false; } else { if (profile.LineComment != null && temp.StartsWith(profile.LineComment)) file.Comments++; else if (profile.StartComment != null && temp.Contains(profile.StartComment)) { file.Comments++; inComment = true; } else file.CodeLines++; } } }