-
Notifications
You must be signed in to change notification settings - Fork 64
Extending & Custom Tasks
It's now easier than ever to create your own tasks that take advantage of the Albacore infrastructure.
Start with the following skeleton to build a task that executes any arbitrary Ruby code (in or out of a module, we don't follow good module guidelines here). I'll explain what each part is for and show you where to put your custom code.
require "albacore"
class Foo
include Albacore::Task
attr_accessor :bar, :baz
def initialize()
@bar = "whatever"
end
def execute()
@logger.debug "tell me something interesting"
# ... do something! ...
end
end
Now, for the breakdown...
-
require "albacore"
- load the entire Albacore library -
class Foo
- this will become the name of your custom task (down-cased) -
include Albacore::Task
- magic Albacore infrastructure stuff -
attr_accessor :bar, :baz
- user configurable properties -
def initialize()
- a parameterless constructor is required, default values can be assigned in the body of the constructor -
def execute()
- a parameterlessexecute
method is required and will be run by Albacore -
@logger
- comes along with theinclude
Tasks that execute a command line application need a little bit more (exe
, bat
, or whatever else is executable in your shell).
require "albacore"
class Foo
include Albacore::Task
include Albacore::RunCommand
attr_accessor :bar, :baz
def initialize()
@bar = "whatever"
end
def execute()
@logger.debug "tell me something interesting"
# ... setup the cli properties! ...
p = []
p << @bar if @bar
p << "/baz \"#{@baz}\"" if @baz
run_command("foo", p)
end
end
And, the statements of interest here are...
-
include Albacore::RunCommand
- provides access to therun_command
and other methods of interest -
p = []; p << @bar
- you need to build up the parameters to your command, make the strings work and quote and escape whatever is necessary, we'll take care of@command
,@working_directory
, and@parameters
for you -
result = run_command(...)
- actually run the command!
You can make some properties required by using a guard-clause in the execute
method, like
raise "foo requires #bar" unless @bar
You can pick the name of your custom task method, instead of having it assigned from the class
name, provide this line inside your class
TaskName = :fizz
You should now be able to write a task like
require "albacore"
require "albacore-foo"
foo :whatever do |foo|
foo.bar = "bar"
foo.baz = "baz"
end
To have your custom task participate in Albacore's global configuration block or YAML configuration, you need to jump through a few hoops (the API is a little wonky and, honestly, I'm not sure how much of this is necessary, but I know this style "just works")...
require "albacore"
module FooConfig
include Albacore::Configuration
def self.fooconfig()
@config ||= OpenStruct.new.extend(OpenStructToHash).extend(Foo)
end
def foo()
config || = FooConfig.fooconfig()
yield(config) if block_given?
config
end
end
# in addition to all the other parts (above)
class Foo
include FooConfig
def initialize()
super()
update_attributes(foo.to_hash)
end
end