Thursday, 12 September 2013

Windows PowerShell for Developers #6: the Package Manager Console

I've been playing about with the PMC that comes with NuGet.  This is quite a handy tool as it exposes the root design time environment object to PowerShell commands as $dte.  I'm currently adapting the substantial script I wrote to prepare a solution for NuGet packaging so it runs directly from this console. One hope is that I can cut the code down by manipulating the solution and projects directly instead of hacking the underlying files. 

This has proved to be more difficult that I thought.  $dte doesn't support all the interfaces you'd expect.  You have to derive a Solution2 COM interface before you can manipulate the solution directly. This, for example is how you add a file at solution level:

$vsSolution = Get-Interface $dte.Solution ([EnvDTE80.Solution2])
$vsProject = $vsSolution.AddSolutionFolder("newFolder")
$projectItems = Get-Interface $vsProject.ProjectItems ([EnvDTE.ProjectItems])
$projectItems.AddFromFile("greenery.txt")

It's still nicer than having to parse the solution file all the same. More on this as the project develops.

No comments:

Post a Comment