Package Manager Console For More Than Managing Packages

Like most developers, I prefer to not have to pick up the mouse if I don't have to. I use the Executor launcher for almost everything so it's extremely rare for me to ever click the "Start" button in Windows. I also use shortcuts keys when I can so I don't have to pick up the mouse. By now most people know that the Package Manager Console that comes with NuGet is PowerShell embedded inside of Visual Studio. It is based on its PowerConsole predecessor which was the first (that I'm aware of) to embed PowerShell inside of Visual Studio and give access to the Visual Studio automation DTE object. It does this through an inherent $dte variable that is automatically available and ready for use. This variable is also available inside of the NuGet Package Manager console.

Adding a new class file to a Visual Studio project is one of those mundane tasks that should be easier. First I have to pick up the mouse. Then I have to right-click where I want it file to go and select "Add –> New Item…" or "Add –> Class…"

If you know the Ctrl+Shift+A shortcut, then you can avoid the mouse for adding a new item but you have to manually assign a shortcut for adding a new class. At this point it pops up a dialog just so I can enter the name of the class I want. Since this is one of the most common tasks developers do, I figure there has to be an easier way and a way that avoids picking up the mouse and popping up dialogs. This is where your embedded PowerShell prompt in Visual Studio comes in. The first thing you should do is to assign a keyboard shortcut so that you can get a PowerShell prompt (i.e., the Package Manager console) quickly without ever picking up the mouse. I assign "Ctrl+P, Ctrl+M" because "P + M" stands for "**P**ackage **M**anager" so it is easy to remember:

At this point I can type this command to add a new class:

PM> $dte.ItemOperations.AddNewItem("CodeClass", "Foo.cs")

dteaddnew

which will result in the class being added:

pm-solexpl

At this point I've satisfied my original goal of not having to pick up a mouse and not having the "Add New Item" dialog pop up. However, having to remember that $dte method call is not very user-friendly at all. The best thing to do is to make this a re-usable function that always loads when Visual Studio starts up. There is a $profile variable that you can use to figure out where that location is for your machine:

PM> $profile C:\Users\steve.michelotti\Documents\WindowsPowerShell\NuGet_profile.ps1

If the NuGet_profile.ps1 file does not already exist, you can just create it yourself and place it in the directory. Now you can put a function inside like this:

function addClass($className)
{
    if ($className.EndsWith(".cs") -eq $false) {
        $className = $className + ".cs"
    }

    $dte.ItemOperations.AddNewItem("CodeClass", $className)
}

Since it's in the NuGet_profile.ps1 file, this function will automatically always be available for me after starting Visual Studio. Now I can simply do this:

PM> addClass Foo

At this point, we have a very nice developer experience. All I did to add a new class was: "Ctrl-P, Ctrl-M", then "addClass Foo". No mouse, no pop up dialogs, no complex commands to remember. In fact, PowerShell gives you auto-completion as well. If I type "addc" followed by [TAB], then intellisense pops up:

pm-psintelli

You can see my custom function appear in intellisense above. Now I can type the next letter "c" and [TAB] to auto-complete the command. And if that's still too many key strokes for you, then you can create your own PowerShell custom alias for your function like this:

PM> Set-Alias addc addClass
PM> addc Foo

While all this is very useful, I did run into some issues which prompted me to make even further customization. This command will add the new class file to the current active directory. Depending on your context, this may not be what you want. For example, by convention all view model objects go in the "Models" folder in an MVC project. So if the current document is in the Controllers folder, it will add your class to that folder which is not what you want. You want it to always add it to the "Models" folder if you are adding a new model in an MVC project. For this situation, I added a new function called "addModel" which looks like this:

function addModel($className)
{
    if ($className.EndsWith(".cs") -eq $false) {
        $className = $className + ".cs"
    }

    $modelsDir = $dte.ActiveSolutionProjects[0].UniqueName.Replace(".csproj", "") + "Models"
    $dte.Windows.Item([EnvDTE.Constants]::vsWindowKindSolutionExplorer).Activate()
    $dte.ActiveWindow.Object.GetItem($modelsDir).Select([EnvDTE.vsUISelectionType]::vsUISelectionTypeSelect)
    $dte.ItemOperations.AddNewItem("CodeClass", $className)
}

First I figure out the path to the Models directory on line #7. Then I activate the Solution Explorer window on line #8. Then I make sure the Models directory is selected so that my context is correct when I add the new class and it will be added to the Models directory as desired.

These are just a couple of examples for things you can do with the PowerShell prompt that you have available in the Package Manager console. As developers we spend so much time in Visual Studio, why would you not customize it so that you can work in whatever way you want to work?! The next time you're not happy about the way Visual Studio makes you do a particular task – automate it! The sky is the limit.

Tweet Post Share Update Email RSS