Wednesday, August 20, 2014

Creating Visual Studio item templates to increase productivity

Goal

In this post we'll look at extending Visual Studio by creating an Item Template to help with code generation.  In the past I've relied heavily upon code snippets for generating chunks of code but item templates help more so when I need to generate an entire code file or several code files at once.  Also, parts of the file will need to be dynamic in that I want to give the file a custom class name.

These instructions are for use with Visual Studio 2013.


Prerequisites

Make sure you have VS 2013 SDK installed.
Install the SideWaffle template pack extension.


Steps

1. Create a new VSIX project.

2. Open the Package Manager Console in Visual Studio

3. Execute Install-Package TemplateBuilder –pre

4. Create a folder named ItemTemplates at the root of the project

5. Under that folder create a folder with the name you want to show up in the Add Item dialog box (a good choice would be your company name).

6. Right-click the new Company folder (or whatever you named it) and click Add -> New Item.  Select the SideWaffle Item Template and give it an appropriate name.

7. Add the actual file that you want to be created by your template into this new template folder (same level as the readme.txt file that was added).  Optionally, if you want more than 1 file to be added just add those also.  If you want them to be added to subfolders then create those subfolders here to match.

8. Inside the Definitions folder that it creates will be 4 files with a .vstemplat- extension.  Rename all applicable files for your template to have a .vstemplate extension.

9. Edit the .vstemplate file(s) and set DefaultName to the actual filename that you want shown in the dialog window when it asks for a filename to save to.  Also, change the Name and Description fields accordingly.

10. In the same file edit the ProjectItem element by removing readme.txt from it's contents.  Replace it with the name of the file you added in step 7.  If you're adding more than 1 file then just copy and paste this line for every file needed.  If the file is in a subfolder then make sure to put the folder name first in the content like this:

<ProjectItem ReplaceParameters="true">css\site.css</ProjectItem>

(For info on replacing parameters, skip down to the last section)

11. Edit the source.extension.vsixmanifest file at the project root.  Make sure to edit the Install Targets to the correct version(s) of Visual Studio that you want.  For me I edited the version range to be [11.0, 12.0].

12. Edit the Metadata and put values for Product Name, Author, and Description.

13. Now build your project and go to the Debug folder in File Explorer and you should see a .vsix file there that you can deploy.  If you just want to test the extension then run it in Debug mode and it will launch a 2nd VS instance that will have the Item Template available.


Replacing parameters in template files

Let's say that you're adding a new CSharp class from this template and you need to know the name that the user entered.  You can put tokens in your source file that will get substituted.  FYI, the documentation that I found on MSDN didn't work for me for some reason.

I ended up using these two tokens:  $fileinputname$  and  $rootnamespace$

Intellisense will complain but you should still be able to build the solution.  Here is what my sourcefile.cs looks like:
namespace $rootnamespace$
{
    public class $fileinputname$
    {
        public $fileinputname$()
        {
        }
    }
}

Wednesday, August 6, 2014

Addressing the problem with npm install jest-cli on Windows 8

This post is specifically about trying to get Facebook's Jest testing library working on a machine running Windows 8 (or 8.1).  I also happen to have Visual Studio 2013 installed (and just this version).


Install Python

I will assume that node and npm are already up and running on your machine.  You need to have Python 2.7.x installed.  Version 3 is out already but we need 2.7.  During the install you have to manually enable the option that puts Python in your Path environment variable.


Install jest-cli

Open a command prompt in your project root and type:
npm install jest-cli --save-dev --msvs-version=2013

The reason you need the --msvs-version option is that the contextify node package will need to be built using VS build tools and it will look for 2010 by default.  Be sure to specify the correct version on your machine.  Credit goes to Kevin Griffin for his article on this.


If all goes well you should see no errors.  Here is what my package.json file looks like afterwards:

{
  "name": "reapcast",
  "version": "1.0.0",
  "description": "smart podcasting",
  "devDependencies": {
    "jest-cli": "^0.1.18"
  },
  "scripts": {
    "test": "jest"
  }
}


Friday, August 1, 2014

Getting the windows sidebar working on Windows 8.1

There are a few desktop gadgets that I like having: a clock, calendar, weather, twitter feed, and magic folder launcher.  Since Windows 8 has turned off windows sidebar and gadgets due to security issues, you have to do a little work to get them back.

Step 1

Install 8gadgetpack (free)

Step 2

Run a registry command to enable explained here

In case that site gets lost, here are the contents of the .reg file

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Windows\Sidebar]
"TurnOffSidebar"=-

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Windows\Sidebar]
"TurnOffSidebar"=-

Step 3

Right-click on the windows desktop and select Gadgets.  The window should pop up and allow you to start putting widgets on your desktop again.  7 Sidebar is useful for grouping widgets together and docking them to a single side of your monitor.