Extend Visual Studio with NuGet PowerShell commands basis

There are many ways to extend Visual Studio. The most used is to vsix but today, I want to write about NuGet.

In most cases, NuGet is used to add a dll reference or update a config file or things like this.

But today, I’m writing about a way to extend Visual Studio using PowerShell in a NuGet package that WAQS uses.

 

When you want to create a NuGet package, the easiest way is to download, install and use the NuGet Package Explorer.

When you run it, you have this window:

image

So as you can see a cool feature if you want to learn how NuGet works: you can open a package from online feed (So you can open WAQS packages). // when you do it, take care about licensing…

When you create a new package, you have two parts: Package metadata and Package contents.

image

After editing your package metadata (you can name your package “HelloWorld”, you can focus on Package contents.

Using right click or content menu, you can add some folders. In our case, we are interested in Tools folder.

image

In this folder, there are three important files you can have:

  • init.ps1
  • install.ps1
  • uninstall.ps1

image

As you can see you can use init and install. As their names suggest, install script is only run when you install the package and init is used at initialization which means when you install the package but also when you open a solution with such Nuget package is installed.

Note that there is a strange behavior with install.ps1: it only runs if you have a lib folder in your NuGet. So WAQS creates an empty lib folder for it.

If you look at one of WAQS packages, install.ps1 only open WAQS documentation in Visual Studio:

param($installPath, $toolsPath, $package, $project)
$DTE.ItemOperations.Navigate(‘https://waqs.codeplex.com/documentation’)

 

When you want to add PowerShell commands that can be run in VS Package Manager Console, you need to add them in the init.ps1 (in order to have them anytime you open your solution).

 

So first, you will add a new file in your NuGet package: “MyNuGetCommands.psm1”

In this file, you can define some functions that can be exposed in Package Manager Console.

For example imagine the following function:

function Hello($name, $city)
{
    Write-Host (‘Hello ‘ + $name + ‘. See you soon in ‘ + $city + ‘.’)
}

 

To be able to use it in the Package Manager Console, you have to export this function:

Export-ModuleMember Hello

 

Then, you can add intellisense in the Console using Register-TabExpansion command:

Register-TabExpansion ‘Hello’ @{ 
‘name’ = { "MSFTees", "MVPs", "My friends" };
‘city’ = { "Redmond", "Seattle", "Bellevue", "Duvall" };
}  

MyNuGetCommands.psm1 is done.

 

You now have to import your module in the init.ps1 file:

param($installPath, $toolsPath, $package)
Import-Module (Join-Path $toolsPath MyNuGetCommands.psm1)

 

Now you can save your NuGet on a specified folder (ex: C:\NuGet).

 

Then, in Visual Studio, you can add this folder as a NuGet package Source:

image

image

image

Then, you can install the package in your solution.

To do it you can use the wizard or use the Package Manager Console with install-package command:

image 

Then we can use Hello command (with intellisense on tab):

image

image

Here we are. In the next post, we will see a concrete sample of WAQS NuGet PowerShell commands usage.

 

See also: How does WAQS code generation work?

This entry was posted in 17894, 18440, 8606. Bookmark the permalink.

One Response to Extend Visual Studio with NuGet PowerShell commands basis

  1. Shiwani says:

    Thanks for sharing useful post. Appreciate your work. For more details, Please visit our website @ http://englishspeakingclassesfaridabad.in

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>