Question

I have a solution with multiple projects in it. One request is to have different versions for each project. I thought I had a solution, but not knowing much about ruby I'm not sure how to fix it or why it is happening. Here are the pertinent parts of my rakefile:

projects = ["ProjectOne", "ProjectTwo"]

assemblyinfo :assemblyinfo do |asm|
   projects.each do | proj |
      bumper_file proj + "/VERSION"
      asm.version = bumper_version.to_s
      asm.file_version = bumper_version.to_s

      asm.product_name = proj
      asm.output_file = proj + "/Properties/AssemblyInfo.cs"
   end
end

Everything builds fine, but each project gets the version of the first project in the list. Interestingly, if I change it slightly to this:

projects.each do | proj |
   bumper_file proj + "/VERSION"
   assemblyinfo :assemblyinfo do |asm|
      asm.version = bumper_version.to_s
      asm.file_version = bumper_version.to_s

      asm.product_name = proj
      asm.output_file = proj + "/Properties/AssemblyInfo.cs"
   end
end

Each project then gets the version of the last file in the list. How do I set the version to what is found in the VERSION file in each project?

Was it helpful?

Solution

So, if I read your question correctly, you have two projects each with a separate VERSION file in some project-specific sub-folder. Something like this...

my-project/
  source/
    projectA/
      Properties/
        AssemblyInfo.cs
      VERSION
    projectB/
      Properties/
        AssemblyInfo.cs
      VERSION

And you want to a Rake/Albacore task that will apply the correct version to each project. The way you have your first task structured, you're only creating one task and you're overwriting the properties assigned to it with that loop!

assemblyinfo :assemblyinfo do |asm|  #=> one :version task
  projects.each do |proj|            #=> many iterations
    # ... assignment ...             #=> overwriting properties
  end
end

The second task definition, the "slight tweak", is actually kind-of accidentally working. You're defining the entire task each time through the projects loop, but with the same name. This is where you're hitting a little-known Rake feature that you might not have been aware of... tasks with the same name are appended to one another. So, you're actually making 2 tasks with 1 name with different bodies.

projects.each do |proj|                #=> many iterations
  assemblyinfo :assemblyinfo do |asm|  #=> many tasks mapped to the same name
    # ... assignment ...               #=> separate properties
  end
end

To make things more clear, I recommend a task definition where every assemblyinfo task gets it's own unique name, then you construct a version task that's dependent on all of them.

projects.each do |proj|
  name = File.join(proj, "Properties/AssemblyInfo.cs")
  bumper_file = File.join(proj, "VERSION")

  assemblyinfo name do |asm|
    asm.output_file = asm
    asm.version = asm.file_version = bumper_version.to_s
    asm.product_name = proj
  end
end

desc "Version every project in the solution"
task :version => projects.map { |proj| File.join(proj, "Properties/AssemblyInfo.cs") }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top