views:

72

answers:

2

I have been playing around with Rake and Albacore, to see if I can replace our existing MSBuild script that deploys software with something that isn't XML. I have a task that will change the debug value inside a web.config to false. The task takes the directory of the web.config as an argument, but I can't quite figure out the syntax needed to supply this argument in the default task.

require 'albacore'
require 'nokogiri'

deployment_path = "c:\\test-mars-deploy"

task :default => [ :buildMARS, :publishMARS, :updatewebconfig ['c:\\test-mars-deploy']]

msbuild :buildMARS do |msb|
  msb.solution = "MARS/MARS.sln"
  msb.targets :clean, :build
  msb.properties :configuration => :release
end

task :updatewebconfig, :deploypath do |t, args|  
  deployment_path = #{args[:deploypath]}
  webconfig_path = deployment_path + "/Web.config"

  f = File.open(webconfig_path, "r+")
  doc = Nokogiri::XML(f)
  puts "finding attribute"
  attribute = doc.xpath("/configuration/system.web/compilation")
  attribute.attr("debug","false")
  puts attribute.to_xml

  f.close()

  File.delete(webconfig_path)

  fi = File.new(webconfig_path, "w")
  fi.write(doc.to_s)
  fi.close()
end

msbuild :publishMARS do |msb|
  msb.targets :ResolveReferences, :_CopyWebApplication
  msb.properties(
    :configuration => :release, 
    :webprojectoutputdir => deployment_path,
    :outdir => deployment_path + "/bin/"
  )
  msb.solution = "MARS/MARS.vbproj"
end

I'm pretty sure there's an easy solution to this, but I can't seem to figure it out!

A: 

basically, you name your args as extra symbols after the name of the task. an args param will get passed into the block that responds to the name of your args, and you can invoke the task passing the args in square brackets ([])

ree-1.8.7-2010.02@rails3 matt@Zion:~/setup$ cat lib/tasks/blah.rake 
task :blah, :n do |t, args|
  puts args.n
end
ree-1.8.7-2010.02@rails3 matt@Zion:~/setup$ rake blah[20]
(in /home/matt/setup)
20
Matt Briggs
Yes, it runs correctly from the command-line using the above, but what I am looking for is how to pass the argument using the default task so that I can just type 'rake'.
Dan Scott
sorry, really didn't read the question well enough
Matt Briggs
A: 

I think you might have to use the old style parameter passing, eg:

nicholas@hal:/tmp$ cat Rakefile
task :default => :all

deploy_path = ENV['deploy_path'] || "c:/some_path"

task :all do |t, args|
    puts deploy_path.inspect
end

And invoke with:

nicholas@hal:/tmp$ rake
(in /tmp)
"c:/some_path"

Or, to override the path:

nicholas@hal:/tmp$ rake deploy_path=c:/other_path
(in /tmp)
"c:/other_path"
ngoozeff
I managed to refactor my code so it is run through a batch file using this method.
Dan Scott
@Dan: can you comment on why you need a batch file?
ngoozeff
The snippet I posted doesn't really need one, but in my actual code I have tasks that build a couple of different projects inside the one file. I could probably separate them out into separate files, but I haven't got that far in my ruby learning.
Dan Scott