Monday, February 05, 2007

Generating LOC from NAnt, Part D'oh!

At some point I'd like to make all the code available, and perhaps even contribute it NAntContrib, but at the moment it's really butt-ugly. I'm glad that C# 2.0 has better support for sort-of-functional programming constructs, but this is unpleasantly verbose:

public static TreeSorter Sort(string[] paths)
{
if (paths.Length == 0)
return new TreeSorter("");
List<string> pathsList = new List<string>(paths);
pathsList.Sort();
List<List<string>> splitPaths = pathsList.ConvertAll<List<string>>(delegate(string path)
{
return
new List<string>(
path.Split(
System.IO.Path.DirectorySeparatorChar));
});

int index = 0;
for (; index < splitPaths[0].Count; ++index)
{
if (!splitPaths.TrueForAll(delegate(List<string> input)
{
return index < input.Count && input[index] == splitPaths[0][index];
}))
break;
}

IEnumerable<string> commonPath = splitPaths[0].GetRange(0, index);
IList<IEnumerable<string>> truncatedPaths = splitPaths.ConvertAll<IEnumerable<string>>(delegate(List<string> input)
{
return
input.GetRange(index,
input.Count -
index);
});
return new TreeSorter(commonPath, truncatedPaths);
}

No comments: