Archive

Archive for July, 2009

Updating Version Numbers

July 10th, 2009 Russell No comments

I have a problem every build, where I have to update the assembly version numbers for all of my projects in my solution.

This is a hassle as I have to find them, check them out, update the version and check them in.

Ok doesnt sound like much work but all time must be accountable for minimising build times! (Especially for large projects)

I decided a python script would be a good idea to iterate through each file given a pattern and path, make a system call to TFS to check out files, update them and check them back in again.

If this script is successful, it could be used for other purposes later as well (e.g. mass replaces).

The TFS part is important for me because when files are not checked out they are in read-only mode and cant be modified using my script. Checking in is less important, however I thought it would be interesting to try to automate the whole process.

The first part is working out how to find a list of files that meet my requirements (in this case “AssemblyInfo.cs”). The os.walk() method is handy for returning a recursive list of files, and allow us to retrieve the full file path of each file.

Now we are in the context of each file:

We want to check it out. Use subprocess.call which passes a list of arguments (the first being the application) to run.

Now we read the file into a list to so we can update lines.

I decided to use regular expressions to find lines that contain [assembly: Assembly(File)Version("...")]
If it does, we update the line to (”X.Y.Z”) our new version number.

Then after that we re-write each line back to the file.

Once each file is updated, we call the same application again, passing it all the files we updated, and provides a comment.

Wallah! Easy :)

Source:

#
# Finds all AssemblyInfo.cs files and updates version numbers.
# Includes Checkout and Checkins.
#

import os
import subprocess
from os.path import join
import re

tfexec = 'C:/Program Files/Microsoft Visual Studio 9.0/Common7/IDE/tf.exe'

file_pattern = 'AssemblyInfo.cs'
asm_version = '1.0.2.9'

basepath = 'C:\\projects\\ProjectName.ProjectDir'
paths = ['Source\\Project1',
         'Source\\Project2']

file_list = []

for path in paths:
    path = basepath + '\\' + path
    for root, dirs, files in os.walk(path):
        for f in files:
            if f == file_pattern:
                filename = join(root, f)

                # TFS Checkout
                subprocess.call([tfexec, 'checkout', filename])

                # Update Value
                fin = open(filename)
                filedata = fin.readlines()
                fin.close()

                new_filedata = []

                # Go over each line in the file
                for file_line in filedata:

                    # Find/update line
                    match = re.compile('^\[assembly: AssemblyVersion\(\".*\"\)').search(file_line)
                    if (match):
                        file_line = '[assembly: AssemblyVersion(\"' + asm_version + '\")]\n'
                        file_list.append(filename + ' ')

                    # Find/update line
                    match = re.compile('^\[assembly: AssemblyFileVersion\(\".*\"\)').search(file_line)
                    if (match):
                        file_line = '[assembly: AssemblyFileVersion(\"' + asm_version + '\")]\n'
                        file_list.append(filename + ' ')                        

                    # Add line to new array.
                    new_filedata.append(file_line)

                # Write file.
                fout = open(filename, 'w')

                for line in new_filedata:
                    fout.write( line )

                fout.close()

# TFS Checkin

args = [tfexec,
        'checkin',
        '/recursive',
        '/noprompt',
        '/comment:Update Assembly Version Numbers ' + asm_version]

args.append(file_list)

subprocess.call(args)
Categories: Uncategorized Tags: