Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Last active August 29, 2015 14:04
Show Gist options
  • Save AlexArchive/b8c887bf0180da109af9 to your computer and use it in GitHub Desktop.
Save AlexArchive/b8c887bf0180da109af9 to your computer and use it in GitHub Desktop.
public class Counter
{
public static int CountLines(string path)
{
return
File.ReadLines(path)
.Count(Predicate);
}
private static bool Predicate(string line)
{
// do not match empty lines
// do not match comments
// do not match braces
// ... do all of this without violating the open/closed principle
return true;
}
}
public class Parser
{
public static IEnumerable<string> Parse(string path)
{
var document = XDocument.Load(path);
return
document.Descendants()
.Where(descendant => descendant.Name.LocalName == "Compile")
.Select(descendant => descendant.FirstAttribute.Value);
}
}
public static class Processor
{
public static int Process(string path)
{
var paths = Parser.Parse(path);
var directoryPath = Path.GetDirectoryName(path);
// convert relative path to an absoloute path
paths = paths.Select(p => Path.Combine(directoryPath, p));
return paths.Sum(p => Counter.CountLines(p));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment