Skip to content

Working with TFS using Ruby

September 28, 2012
Official Ruby logo


We needed to migrate legacy Java + jBehave code to Ruby + Cucumber but there was the only problem how to deal with TFS. TFS was used only for check out some files, edit them and getting check in files change set. Originally it was used Java TFS SDK that make TFS operations more simple. In this post I describe how to access TFS using Ruby on Windows based machine.


First of all we need to install TFS or just command line utility on target Windows PC (actually CI server where we are going to run tests) and add path to tf.exe into environment variables PATH (to access tfs console using tf shortcut in command line).

Then add Team Foundation Server operations into *.tfs script files. In our case check out script  looks like (checkout.tfs):

workspace -new -server:%1/tfs -login:%3\%2,%4 Workspace -noprompt
workfold -map $/ProjectName Temp -workspace:Workspace/Temp
get $/ProjectName/PathTo/File/toGet.ext
checkout $/ProjectName/PathTo/File/toGet.ext

Check in script (checkin.tfs):

checkin $/ProjectName/PathTo/File/toGet.ext -comment:"Ruby TFS test checkin" -noprompt
workfold -unmap -workspace:Workspace $/ProjectName
workspace /delete Workspace -noprompt

And one can call these scripts within Ruby code as:

def check_out()
output = `tf @./tfs_scripts/checkout.tfs #{@tfs_path} #{@user} #{@domen} #{@password}`

def check_in()
output = `tf @./tfs_scripts/checkin.tfs`
return output[/#(\d+)/].delete("#") # removing hash and return change set number

Some useful links:
Ruby shell commands
Team foundation version control command reference

From → Automation

  1. If to integrate with Jenkins one should remember about the bug

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: