Use Visual Studio extension to distribute your Code Snippets

Introduction

In a previous post we saw how you can build code snippets to write tests faster empowering your team to follow good practices. https://www.albertocorrales.com/2018/10/writing-unit-tests-faster.html

In this post, we will see how we can distribute our snippets with a Visual Studio extension, which offers different advantages, for example:
  • You encapsulate your snippets in a VS extension, you you don't need to distribute them independently, and developers don't need to add them to VS one by one. 
  • The extension can be updated improving he current snippets or including new snippets.
  • You can add more functionality in your Visual Studio extension. 

Creating a new Visual Studio extension

First of all, if you have never developed a Visual Studio extension, you might need to install Visual Studio extension development kit. To do it, install this option in your Visual Studio Installer



Create a new Visual Studio extensions project. For this example, I will name it "SnippetsExtension":


In the root of the project, create a new folder named "snippets". Here you can store all the snippets you wan to distribute with your Visual Studio Extension. For this example, we will used the unittest.snippet example that we saw in the previous post. You can download it here: https://github.com/albertocorrales/unittestsnippet/blob/master/unittest.snippet

For file unittest.snippet, it is important to select the following properties:
  • Build Action: Content
  • Copy to Output Directory: Copy always
  • Include in VSIX


In folder snippets, we will create a new text file and we will name it snippets.pkgdef. This file is used to add certain keys to the registry. As for this example, we are using CSharp, write the following:
-----------------------------------------------------------------
// Csharp
[$RootKey$\Languages\CodeExpansions\Csharp\Paths]
"snippets"="$PackageFolder$"

-----------------------------------------------------------------

For file snippets.pkgdef select the following properties:
  • Build Action: Content
  • Copy to Output Directory: Copy always
  • Include in VSIX

If you followed the previos steps, you should see something like this:



Now add your package definition in your VSIX manifest. To do it, double click on source.extensionvsxmanifest. Go to tab "Assets", and then, select the following options to distribute your snippets with your extension:


Finally, test your extension. If you press F5, an experimental instance of Visual Studio will raise, the changes you make in this instance will not affect your development environment. To test your extension is working fine, create a new test project and type "utest" to see the new snippet you added.


You can download this example from my GitHub repository. https://github.com/albertocorrales/SnippetsExtension

If you want more information about how to develop your own Visual Studio extensions, you can go here: https://docs.microsoft.com/en-us/visualstudio/extensibility/starting-to-develop-visual-studio-extensions?view=vs-2017

I hope you found this post useful,

Happy coding!

Comments

Popular posts from this blog

Building Micro-Frontends with Single-Spa

AWS assuming role + MFA with pulumi (and other applications)

Managing snapshots for Amazon ElasticSearch with Dotnet Core Lambdas