Why is this Albacore task, defined in a class, not working?
Question
I put this class together, but it is not working to actually run a build. I inserted some other logging and no errors are being raised. I declare myself as a noob on Ruby so hoping some lovely Ruby Expert can spot any idiotic error I have!
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
msb.execute
end
end
end
I have also tried using :build rather than :compile.
I have seen albacore working and realise the power of it, just need to get my skills a little honed hopefully
Solution
the msbuild
method that you are calling is never executing because Albacore is a suite of rake tasks, and msbuild
is one of those tasks that wants to be executed by rake, not directly in another method, though it can be done.
Calling msb.execute
inside of the do |msb| ... end
block won't execute the task, because this block is not evaluated until the rake task itself is executed.
you have a few of options for getting this to work. option #1 is the recommended and intended use of Albacore. I strongly recommend using rake and Albacore as they were meant to be used, so that you don't run into problems in the future. option #2 and #3 will work right now, but changes to the API of rake or Albacore can break these without notice. of course, you can use them however you want or need.
1. turn this into a rake script instead of a class and method
# rakefile.rb
require 'albacore'
task :default => [:compile]
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
end
and then run this via a rake, by calling rake
from the command line, in the same folder as the rakefile.rb
2. use Task[:compile].execute
to execute the task within your method.
since the msbuild
call is a rake task and not a standard method that executes it's code immediately, you have to manually execute the task that is created behind the scenes. this will make your existing code work, but it's not really the recommended way of working with rake tasks.
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
end
Task[:compile].execute
end
end
3. use the MSBuild
class directly, instead of the msbuild
rake task
if you really do need to call msbuild from within a method, then you should avoid using the msbuild task and call the MSBuild class directly. this let's you work directly with the code that you want... no more going through rake to call something that you can call directly
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msb = MSBuild.new
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
msb.execute
end
end
this is the only scenario in which you need to call .execute, manually.