About the author

J Sawyer is a developer based in Houston, TX who absolutely loves to write code. After spending 9 years at Microsoft, he moved on to other things and is currently the Lead Developer for the RealTime Data Management team at Logica US. He spends his days building Really Cool Things around StreamInsight and having a blast doing it.

He has been involved with HDNUG, one of the oldest and largest .NET-focused user groups in the US, since its inception in 2001 and has watched it grow from 5-10 technologists meeting around a conference table to a thriving community of over 5000 with regular meeting attendance averaging 100 attendees. He currently serves as the Vice President. You can join him at HDNUG on the second Thursday of every month at the Houston Microsoft office.

He also loves to ride his Yamaha FZ1. And sometimes his Ninja 650. And also his Honday XR-400 dirt bike. But he doesn't code and ride at the same time. That would be bad.

Microsoft.TeamFoundation.Build.Workflow.Activities.ExpandEnvironmentVariables

September 15, 2010 12:01 PM

Since Microsoft doesn’t seem to be interested in publishing docs on built-in TF Build 2010 Activities, I’ll be publishing blog posts about activities that I’ve worked and/or experimented with. The titles will be the full name of the activity class with the hope that it’ll come up near the top on searches. The information here is a result of analysis of the activity from .NET Reflector as well as testing and experimentation in the context of a TFS 2010 Build Server but should not be construed as “official documentation”.

Description

This activity will expand Environment Variables specified in the standard MSBuild property syntax. For example, $(WinDir) will be expanded to the Windows directory on the current machine. Note that this may be different for the controller and the agent and will reflect the current machine context based on where it is executed in the build process. Because of that, be careful if you evaluate/cache results between these contexts.

The syntax is similar to an MSBuild property. For example, to expand the %WINDIR% environment variable, use $(WINDIR).

Note that this method only includes standard environment variables by default and does not include any default MSBuild or TFBuild (<2010) variables. Internally, it uses a regular expression to identify properties identified as $([VarName]). It first tries to resolve the matches using System.Environment::GetEnvironmentVariable. If that fails, it then looks in the dictionary supplied in the AdditionalVariables property.

Comments

Like MSBuild properties, this will use the $(PropName) syntax. For example, to expand %WinDir%, you will use $(WinDir).

While this activity does have its uses, there are some key variables missed. For example, any and all variables that were available in TF Build < 2010. This makes things very difficult to have a single template that can be used across many projects. To help with this, you can use the AdditionalVariables property and add any (custom) expansion variables that you want to, making this activity far more useful.

I will be creating activities to do this that will be available as a part of the Community TFS Build Extensions and may also be available separately. I’m planning a single, code-based activity to do one variable at a time as well as a composite activity that will do the standard TFS Build 2008 properties. In the meantime, you can download an example build process template here. It adds a couple of those TF Build <= 2008 standard properties as AdditionalVariables and also shows how to call a standard environment variable.

Once you figure this activity out, it’s pretty simple and can be highly useful. However, the lack of documentation (are you listening Microsoft DevDiv?) makes it far more complicated than it really needs to be. One should not need to use Reflector to understand how a Microsoft-supplied component works.

Tags:

TFS 2010 | TFS Build