PS for .NET devs part 4: Source digging

The kind of adhoc scripting you can do with PowerShell lends it's self very well for getting an overview of the code-base you find yourself in. When you find yourself in a project or you're just interested in some basic statistics/metrics about your code-base you could follow something like the path I'm about to uncover. For brevity and a real life flavor I'm using the short form aliases of the Cmdlets look them up with Get-Alias if you find any that are unfamiliar.

How many files are actually in the archive?

How many files of which type are there in the archive?

What are the most popular file types?

Ignoring directories the top 10 becomes:

C# is the main language here.

How many classes do we have?

Explanation

Consider all C# files search for the 'whole word' class and count all the matches.

Do we have any tests?

Let's look at the first one to see which testing framework were using:

Ah, NUnit so,

How many test fixtures do we have:

Lets check some basic line counts

Explanation

Going through all files in the archive exclude some known binary types, count the number of lines excluding lines containing only whitespace.

How are these lines distributed across the different file-types?

Explanation
  • retrieve all files excluding a large number of binary file-types specific to this project
  • filter out the directories
  • group the files by their extension
  • select the Name of the group which will be the extension, calculate the total lines of code for all files in the group by
    • retrieving the contents of each file using gc an alias for Get-Content
    • counting the number of lines
    • extracting the Lines property for the Measure-Object return value as a value, rather than an object containing only a Lines property
  • sort in descending order by the number of lines
  • select the top 10
  • format them in a table with auto-sized columns

What about the ratio of test vs. production code?

Explanation

The code follows the same pattern as above except that the grouping is done on the fact whether the file is a [TestFixture] or not.

Epilogue

I believe that this shows what kind of awesome stuff you can do with adhoc scripting using PowerShell. Until next time.

Sun 04 Dec 2011
References