Change to Solution Folder in Package Manager Console
When you install nupack (which seems likely to be renamed nuget in the near future), you get a new dockable Visual Studio 2010 window called the Package Manager Console which allows you to run nupack commands right from within VS2010.
The great thing about this window is that it can be used for more than just nupack commands. It is a fully working PowerShell window and all the commands on your path are also available. For example, I use Mercurial on a number of my applications, so it allows me to input commands such as hg add or hg commit directly within the console window.
There was just one slight snag, and that is that the current working directory of the Package Manager Console seems to default to your user account:
PM> pwd Path ---- C:\Users\Mark
So, despite knowing virtually nothing about PowerShell, I set about working out how I could automate this process. The first thing I discovered was that you can query PowerShell for all the variables that are available using the Get-Variable command. This will show the names and current values of all variables.
Sadly, there seemed to be none containing the path of the loaded solution, but asking a question on StackOverflow pointed me in the right direction. There is a variable called
$dte which is a COM object allowing automation of the VS development environment. We can ask this for the path of the loaded solution:
PM> $dte.Solution.FileName C:\Users\Mark\Code\TestApp\TestApp.sln
We now need to find a way to strip off the filename to get the folder. You can list all available PowerShell commands with by typing Get-Command. Eventually after some searching on Google I found a way to strip the filename off this path:
PM> Split-Path -parent $dte.Solution.FileName C:\Users\Mark\Code\TestApp
To change to this folder you take this string and pipe it into the cd command as follows:
PM> Split-Path -parent $dte.Solution.FileName | cd PM> pwd Path ---- C:\Users\Mark\Code\TestApp
Mission almost accomplished, but that is a rather cumbersome command to remember. I wanted to make a PowerShell command immediately available to me. This requires editing my PowerShell user profile. The path to this is found in the
PM> $profile C:\Users\Mark\Documents\WindowsPowerShell\NuPack_profile.ps1
This file didn’t actually exist (nor did the WindowsPowerShell folder), so I had to create a blank one. Then, I added my snippet of script into a function:
Having done that, you need to reload Visual Studio, and after loading a solution, you can type solutionFolder to navigate to the solution folder:
PM> solutionFolder PM> pwd Path ---- C:\Users\Mark\Code\TestApp
And that's it. Now I can run my Mercurial commands from within the Package Manager Console:
PM> hg status M UnitTests\UnitTests.csproj ? UnitTests\Thread.cs