Clean & Simple: The Lost Art of Coding
William Drol
William Drol
Home  |  Blog

One Simple Thing to Help Visual Studio 2015 Become a Better Team Player

April 7th 2016  |  LinkedIn

Gulp Starting with Visual Studio 2015, Microsoft has officially recognized the popular Gulp.js task runner and now provides a visual interface in their flagship IDE. What we've been doing for years at the command line is now available in a more intuitive way, which is great news for team members not too familiar with Gulp. Check out Microsoft's official documentation, because they have some cool features you can’t do from the command line. Things like binding Gulp tasks to Visual Studio IDE events (Project Open, Post Build, etc.) and more. Nice!

Command Prompt

Did they Really Forget About This?

Even so, their current implementation is missing something important for teams. From the command line, we often pass custom parameters to our Gulp tasks. This is useful in team environments where a Gulp task might need to perform differently for each team member.

As of this writing, this feature is missing in Visual Studio 2015 and it seems to be a major oversight. Suppose we have a Gulp task named cleanup. We can run it any of the following ways from the command line:

gulp cleanup  ( no parameters )

gulp cleanup --LocalUserName=bill

gulp cleanup --LocalUserName=mary

gulp cleanup --LocalUserName=jenny

Internally, the Gulp cleanup task can see if the LocalUserName parameter is set and respond appropriately. As of this writing, this only works from the command line. Is there anything we do to duplicate this behavior in the IDE?

System Variables to the Rescue

Behind the scenes, Gulp runs under the control of Node.js and inherits a built-in variable named process. Inside Gulpfile.js, we can use it to inspect system variables:

var name = process.env.LocalUserName;

Once we have a system variable in Gulp, we can treat it just like a command line parameter. The drawback is that users must create system variables on their computers in order to support this behavior.

To create a new system variable, click the Environment Variables button in System Properties:


Next, in the System Variables section, click the New button to create a new system variable. I've entered LocalUserName with a value of bill (as shown below). Click OK to save the variable (also click OK on System Properties to close it).



So restart Windows. When you're back up, launch Visual Studio 2015 and enter the following code into your Gulpfile.js. As of this writing, Gulpfile.js must be in the root of your website and spelled with a capital G because the IDE doesn’t recognize gulpfile.js — seriously?

gulp.task( 'MyTest' , function()
    var name = process.env.LocalUserName;
    console.log( name );

Save your changes to see MyTest in the Task Runner Explorer Window (double-click on Tasks > MyTest to run it). The correct output on my computer is bill:


Now you can use this value exactly like a command line parameter. Hopefully Microsoft will include support for this missing feature soon, but until then, this is a reasonable, slightly inconvenient workaround.