in

CodePrairie .NET

South Dakota .NET User Group

chrisortman

Rake task to update version information

I maintain assembly version numbers in my msbuild project files. It is (unfortunately) spread across 3 files for the time being, a .targets file, the master .proj file and a wix (.wxs) file.

So I thought I would hack a quick rake task that will update all these version numbers for me when it comes time to update the application. I ran into a snag though because REXML will by default output attributes like this

<PropertyGroup Condition='$(Configuration) == &apos;&apos;'>

which is not what I want in my msbuild files. I instead want it to look like

<PropertyGroup Condition="$(Configuration) == '' ">

What REXML outputs might still work correctly in MSBUILD, I never tested it, but it will be a lot harder to look at the file and maintain it by hand, so a solution is needed.

Thanks to the flexibility of ruby a solution is not that difficult. I found this post which got me started, then I just need to make a small addition (the Text::unnormalize call)

 

   REXML::Attribute.class_eval( %q^
def to_string
%Q[#@expanded_name="#{Text::unnormalize(to_s()).gsub(/"/,'&quot;')}"]
end
^)

My complete task looks like this:

task :bump_version do |t|
include REXML
file_names = ["build.proj","project.targets","Install/program/install.wxs"]

build_file, targets_file, install_file = file_names.collect do |file_name|
f = File.new(file_name,"r")
Document.new(f)
end

major,minor = build_file.root.elements["//ApplicationMajorVersion"].text, build_file.root.elements["//ApplicationMinorVersion"].text

if minor.to_i == 9
major = (major.to_i + 1).to_s
minor = "0"
else
minor = (minor.to_i + 1).to_s
end

puts major + "." + minor

build_file.root.elements["//ApplicationMajorVersion"].text = major
build_file.root.elements["//ApplicationMinorVersion"].text = minor

product_el = install_file.root.elements["Product"]
product_el.attributes["Version"] = major + "." + minor + "." + "0.0"
product_el.attributes["Id"] = new_guid[1..-2]
product_el.elements["Upgrade"].each_element do |el|
el.attributes["Minimum"] = product_el.attributes["Version"] unless el.attributes["Minimum"].nil?
el.attributes["Maximum"] = product_el.attributes["Version"] unless el.attributes["Maximum"].nil?
end

targets_file.root.elements["//AssemblyMajor"].text = major
targets_file.root.elements["//AssemblyMinor"].text = minor

# see: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/74224 ... slightly modified
REXML::Attribute.class_eval( %q^
def to_string
%Q[#@expanded_name="#{Text::unnormalize(to_s()).gsub(/"/,'&quot;')}"]
end
^)


[build_file, targets_file, install_file].each_with_index do |file, index|
File.open(file_names[index],'w') { |writeable| file.write(writeable,0) }
end
end
Published May 25 2007, 02:09 PM by chrisortman
Filed under:

Comments

No Comments

Leave a Comment

(required)
(optional)
(required)
Add
Powered by Community Server (Commercial Edition), by Telligent Systems