How to execute arbitrary line of code in TFS build process?

553 Views Asked by At

I am creating a custom process template in our TFS server.

I would like to execute the following line of during the process, to automatically modify the assembly's revision number in AssemblyInfo.cs based on the current MMDD:

File.WriteAllText(file, Regex.Replace(File.ReadAllText(file), "(?<=\[assembly: AssemblyFileVersion\(""[0-9]*.[0-9]*.[0-9]*.)[0-9]*(?=""\)\])", Function(m) DateTime.Now.ToString("MMdd")))

Unfortunately I cannot find out how to execute an arbitrary line of VB.NET code in the build process. There are no code activities called "execute", "run" etc.

I tried to hack it by inserting a lambda function into a WriteBuildMessage() call, but found that lambdas are disallowed:

enter image description here

(EDIT: after other problems with that expression were fixed, it now simply gives the error: "Statement lambdas cannot be converted to expression trees" - ergo I can't do what I'm trying to do here. If "File.WriteAllLines" was string instead of void then I could return it and be laughing.)

Is there a way I can execute an arbitrary line of VB.NET code as part of the build process?

3

There are 3 best solutions below

1
On BEST ANSWER

As you mentioned yourself it is possible to use the "InvokeMethod" in order to somewhat execute a line of code.

What I would suggest is to create a custom activity where you can provide it with the parameters, such as the file name and file path.

In order to achieve this two main tasks are needed:

  1. Include a custom build process template
  2. Creating a custom activity

You either do this in your current solution or you create a whole new solution to handle your custom activities and process template.

Include Custom Build Process Template

Whether you choose to work on your current solution or a new one, it is important to have the Process Build Template included in a separate project.

The project needs to be an "Activity Library" which is found under installed (VS 2013):

Right-click solution -> Add.. -> New Project... -> Visual C# -> Workflow

In this project you add your process template that you have been working on, by adding it as an existing item and making it a link. This can be done by pressing the small arrow at the "add" button

When this is included you will need to include several references, which can be found here [1]

Creating Custom Activities

As for the Build Process Template all the custom activities needs a project to be build in. This is to ensure that when they are to be used it is simply referencing the project or including the dll.

This project also needs to be an Activity Library, where this time the activities are to be created.

An activity is a Code Activity and can be created by:

Right-click Project -> Add -> New Item... -> Visual C# Items -> Workflow -> Code Activity

And again this project needs several references in order to build, which can be found here[1]

References:

For more information and ideas take a look at this:

[1]: Ewald Hofman - Customize Team Build

0
On

Note: I got around it by adding a node to InvokeMethod directly on System.IO.File.WriteAllText, passing the Regex.Replace call as one of the parameters.

I'll leave the question open in case anyone can provide a direct answer to the original question.

0
On

You should not create this yourself. You should use the precreate TfsVersion activity built into the TFS Community Build Tools.

https://tfsbuildextensions.codeplex.com/wikipage?title=How%20to%20integrate%20the%20TfsVersion%20build%20activity&referringTitle=Documentation

This tool will do all of the heavy lifting and is supported by the Visual Studio ALM Ranger and MVP's.