Listing the namespaces in your project using Windows Powershell

Today I was in a chat with a couple of the guys who are busy with the Sdl DXA framework.  The subject turned to namespaces, and I was asking if it was possible to tell from the namespace which classes belong to the DXA framework and which belong to the Sdl Web product per se. In the end the answer was a pragmatic one: “Just look in the code of the framework”. So rather than spend the rest of my morning reading through the code… why not throw together a quick one-liner to take the pain out of it? So I reached for my trusty Powershell, and the select-string cmdlet.

If you want to play along at home, you can pull the code of the framework via git.

git clone git@github.com:sdl/dxa-web-application-dotnet.git

(or you can just use this technique to analyze a code base of your own…. that’s kind of the point, eh?)

So I opened the Powershell in this project directory and, after a couple of false starts, was able to get a reasonable result with the following:

gci -R *.cs | select-string "namespace (.*)" | %{$_.Matches.Groups[1].Value} | select -Unique

So as you can see, I’m using a recursive Get-ChildItem to get all the C# source files, and piping the result into Select-String. Here, I’m using a RegEx to pick out all the lines which contain the text “namespace ” (putting the space in helps to eliminate some false positives – I’m looking for where the “namespace” keyword is being used).

At this point, Select-String shows its superiority over tools like grep: instead of just handing back a string, we get a RegEx Matches collection, which makes it very easy to pull out the actual namespace from the rest of the string. In practice, I still had a couple of false positives, and as I could see that all my namespaces began with “Sdl.”, I managed to clean up the results with a minor tweak like so:

gci -R *.cs | select-string "namespace (Sdl.*)" | %{$_.Matches.Groups[1].Value} | select -unique

And the end result was this:

Sdl.Web.Common.Configuration
Sdl.Web.Common.Extensions
Sdl.Web.Common.Interfaces
Sdl.Web.Common.Logging
Sdl.Web.Common.Mapping
Sdl.Web.Common.Models
Sdl.Web.Common
Sdl.Web.Common.Models.Data
Sdl.Web.Mvc.Configuration
Sdl.Web.Mvc.Context
Sdl.Web.Mvc.Controllers
Sdl.Web.Mvc
Sdl.Web.Mvc.Formats
Sdl.Web.Mvc.Html
Sdl.Web.Mvc.Statics
Sdl.Web.Tridion.Configuration
Sdl.Web.Tridion.ContentManager
Sdl.Web.Tridion.Context
Sdl.Web.Tridion.Extensions
Sdl.Web.Tridion.Linking
Sdl.Web.Tridion.Mapping
Sdl.Web.Tridion.Query
Sdl.Web.Tridion.Statics
Sdl.Web.Tridion
Sdl.Web.Tridion.Tests
Sdl.Web.Site.Areas.Core.Controllers
Sdl.Web.Site.Areas.Core
Sdl.Web.Site

It’s a quick-and-dirty approach, but this enabled me very easily to enumerate the namespaces that are used by this particular library. Without the tweak, it’s generically useful for scanning any similar codebase. I’d argue not only that you should know what namespaces you are using, but that it’s very useful to be able to extract a list of them from your code, if only as a double-check.