I'm using a rake script to tag the AssemblyInfo.cs files in my .NET projects, but there's a phantom carriage return (or newline, etc.) being introduced that's breaking the compilation. The method that gets the version number is:
def get_version()
version = ''
IO.popen("#{SVNVERSION_APPLICATION} #{build_info.root_dir}") do |output|
output.readlines.each do |line|
version << line.gsub(/\:.*/, '')
end
end
result = version
end
If there's a better way to do that, please let me know. I'm mostly tinkering here (and this is actually the first Ruby I've ever worked on, so bear with me). As you can see (and my regex skills have never really been ninja-level) I'm just trying to get rid of the first colon and everything after it. Svnversion returns things like "64:67M
" and I'm interested only in the first number.
Then I tag the files as such:
contents = contents.gsub(/AssemblyVersion\(\"([\d.*]*)\"\)/, "AssemblyVersion(\"#{helper.build_info.build_number_template}\")")
Naturally, there's more code surrounding these bits. This is just the meat of what's going on. Basically, in the task I call get_version to store the version number on a helper object and that object is later used in a method that reads AssemblyInfo.cs files into "contents" and does the substitution and writes it back out.
Everything is working fine except for a phantom newline in the output:
[assembly: AssemblyVersion("1.1.0.62
")]
[assembly: AssemblyFileVersion("1.1.0.62
")]
I've tried adding another .gsub to try to filter out \n and \r with no luck, I've tried .chomp, etc. It always seems to put that newline in the resulting file.
Am I missing something obvious?
[Edit in response to first answer:]
Here's the method that edits the AssemblyInfo.cs file:
def replace_assembly_strings(file_path, helper)
if not (File.exists?(file_path) || File.writable?(file_path))
raise "the file_path \"#{file_path}\" can not be written to. Does it exist?"
end
path = Pathname.new(file_path)
contents = path.read
puts "AssemblyVersion(\"#{helper.build_info.build_number_template}\")"
contents = contents.gsub(/AssemblyVersion\(\"([\d.*]*)\"\)/, "AssemblyVersion(\"#{helper.build_info.build_number_template}\")")
contents = contents.gsub(/AssemblyFileVersion\(\"([\d.*]*)\"\)/, "AssemblyFileVersion(\"#{helper.build_info.build_number_template}\")")
contents = contents.gsub(/AssemblyCompany\(\"(.*)\"\)/, "AssemblyCompany(\"#{helper.build_info.company}\")")
contents = contents.gsub(/AssemblyCopyright\(\"(.*)\"\)/, "AssemblyCopyright(\"#{helper.build_info.copyright}\")")
File.open(file_path, 'w') {|f| f.write(contents)}
end
That puts
line outputs AssemblyVersion("1.1.0.62")
but the resulting file shows AssemblyVersion("1.1.0.>")