Wednesday, February 27, 2013

Windows Dev and Dashboard Prompt

First set up a cmd file (a batch file with a *.cmd extension) whose sole purpose is to set environment variables.

Mine, called DashboardEnv.cmd, looks like this on one of the machines where I have Visual Studio 2010 Express Edition installed:

    @call "%VS100COMNTOOLS%\vsvars32.bat"
    @set PATH=C:\Program Files\Git\bin;%PATH%
    @set PATH=%USERPROFILE%\DevBox\cmake-2.8.10.2\bin;%PATH%
    @set PATH=C:\Python27;%PATH%
    @set PATH=C:\Qt\4.8.4-vs10\bin;%PATH%
    @set PATH=C:\dev\tools\bin;%PATH%


As you can see by inspecting that file, it sets up the environment just like a Visual Studio command prompt, and then adds a bunch of useful stuff to the PATH environment variable: git, cmake, python, qmake, ninja and jom, to name a few. I have ninja and jom in the C:\dev\tools\bin directory -- they could be anywhere, you just have to add the right directory to the PATH here in this script.

Next, set up a cmd file in the same directory as the environment batch file to display a "developer cmd prompt."

Mine, called DashboardPrompt.cmd, looks like this:

    @call "%~dp0DashboardEnv.cmd"

    @title Dashboard Prompt

    @echo.
    @echo Environment set by "%~f0"
    @echo.

    @call "%COMSPEC%"


If you prefer the "git bash" prompt to the raw Windows cmd prompt, you can change the call COMSPEC line to:

    @call "C:\Program Files\Git\bin\sh.exe" --login -i
After you have both of those setup, double click the prompt cmd file to test it out. Then you can create a shortcut to the prompt cmd file, put it on your desktop, or whereever you like, and then just double-click the shortcut to get a new instance of your customized developer prompt.

Some things I do to make the command prompt itself slightly less intolerable:
  • edit the command prompt window properties: with the window open, click on the icon in the top left corner, and choose "Properties" from the menu
  • modify the properties of the window to allow "select-and-Enter-key to copy, right-click to paste" behavior by choosing the "QuickEdit Mode" checkbox
  • set the screen buffer height (number of scroll back lines) to 9999, the max allowed
  • set the window size to something larger so you can see more text at once (120 by 40-50 is nice depending on your usual screen)
  • set the font to Lucida Console, and choose a font size large enough to read
  • if prompted, check "modify the shortcut that started this prompt" on the way out
Now that you're all set up for an interactive prompt with the right environment... here's the reason why separating it into two scripts is good for you. You can easily run any other script with the very same environment by adding one line at the top of it:

    @call "%~dp0DashboardEnv.cmd"

The %~dp0 there means "drive letter (d) and full path of containing directory (p) without any double quotes (~) of this script file (arg 0) including the trailing '\' character (implicit in p)" -- so if you write a script that references another script in the same directory, using %~dp0 is a reliable way to reference it, regardless of how the batch file was invoked. See the output of "help for" in a Windows command prompt for all the gory details about possible letter codes you can use in such constructs.

So: to run dashboards or other automated builds with the same environment that you use for interactive development, you can write a script that uses your Env.cmd file.

Mine, called RunDashboards.cmd, looks like this:

    @call "%~dp0DashboardEnv.cmd"

    @title Run Dashboards

    @echo.
    @echo Running script "%~f0"
    @echo.  started on %DATE% at %TIME%
    @echo.

    @echo.
    @echo Updating VTKLargeData...
    @cd "C:\dev\My Tests\VTKLargeData"
    @git pull

    @echo.
    @echo Updating VTKData...
    @cd "C:\dev\My Tests\VTKData"
    @git pull

    @echo.
    @echo Running VTK Release dashboard...
    @cd "C:\dev\My Tests\VTK"
    @ctest -S C:\dev\EasyDashboardScripts\EasyDashboard.cmake,ninja-Nightly-Release

    @echo.
    @echo Running VTK Debug dashboard...
    @cd "C:\dev\My Tests\VTK"
    @ctest -S C:\dev\EasyDashboardScripts\EasyDashboard.cmake,ninja-Nightly-Debug


One nice thing about guaranteeing the right environment is set for running a script like this is being able to just use "git" and "ctest" in the script itself.

Obviously, you'll need to adjust path values according to tool installations on different machines.

For more details on setting up to run dashboards on Windows, see this page over on the CMake blog. (Also published on the Kitware blog.)

Good luck -- tweet me @DLRdave or ping me on G+ if you use this technique.