How to get my custom Rake task *args to work like built-in Rake task *args? - ruby

I have a custom rake task, implemented as a method, which sits "above" the task method. It works with a custom configuration object to give the whole thing a more declarative feel. Here it is.
def robocopy(*args, &block)
config = RobocopyConfiguration.new
block.call config
body = proc {
system "robocopy #{config.make_parameters.join ' '}"
}
Rake::Task.define_task *args, &body
end
class RobocopyConfiguration
attr_accessor :source, :destination, :files
attr_reader :mirror
def mirror
#mirror = true
end
def make_parameters
parameters = [ #source, #destination ]
parameters << #files.flatten if #files
parameters << '/MIR' if #mirror
parameters
end
end
And you can declare it like a normal-ish rake task with a name and dependencies.
robocopy :copy => [ :build ] do |cmd|
cmd.source = File.expand_path File.dirname __FILE__
cmd.destination = '//some/default/network/share'
cmd.mirror
end
However, as soon as you add arguments, things start to blow up.
robocopy :copy, [ :destination ] => [ :build ] do |cmd, args|
args.with_defaults(:destination => '//some/default/network/share')
cmd.source = File.expand_path File.dirname __FILE__
cmd.destination = args[:destination]
cmd.mirror
end
cmd> rake copy['//some/network/share']
rake aborted!
undefined method `with_defaults' for nil:NilClass
I suspect the *args aren't getting turned into TaskArguments, with all the special methods, they're are being used immediately in the custom block.call. And I can't quite figure out what call to make to turn them into the right kind of arguments.

Related

How to reference a function in a different rake file

I want to call a function that is in another rake file.
Rake File 1:
task :build => [:some_other_tasks] do
foo
end
def foo(type = :debug)
# ...
end
Rake File 2:
require_relative 'path_to_rake_file_1'
task :foo2 => [:some_other_tasks] do
foo
end
I am currently getting a no such file to load error despite confirming the path is absolutely correct.
Instead of defining methods inside rake files and sharing them among rake tasks, it is best practice to create a RakeHelper module and include it in your rake file. So, you could have something like:
rake_helper.rb
module RakeHelper
def self.foo
end
end
task1.rake
include RakeHelper
task :build => [:some_other_tasks] do
RakeHelper.foo
end
task2.rake
include RakeHelper
task :foo2 => [:some_other_tasks] do
RakeHelper.foo
end

How to extract tasks and variables from a Rakefile?

I need to:
Open a Rakefile
Find if a certain task is defined
Find if a certain variable is defined
This works to find tasks defined inside a Rakefile, but it pollutes the global namespace (i.e. if you run it twice, all tasks defined in first one will show up in the second one):
sub_rake = Rake::DefaultLoader.new
sub_rake.load("Rakefile")
puts Rake.application.tasks
In Rake, here is where it loads the Makefile:
https://github.com/ruby/rake/blob/master/lib/rake/rake_module.rb#L28
How do I get access to the variables that are loaded there?
Here is an example Rakefile I am parsing:
load '../common.rake'
#source_dir = 'source'
desc "Run all build and deployment tasks, for continuous delivery"
task :deliver => ['git:pull', 'jekyll:build', 'rsync:push']
Here's some things I tried that didn't work. Using eval on the Rakefile:
safe_object = Object.new
safe_object.instance_eval("Dir.chdir('" + f + "')\n" + File.read(folder_rakefile))
if safe_object.instance_variable_defined?("#staging_dir")
puts " Staging directory is " + f.yellow + safe_object.instance_variable_get("#staging_dir").yellow
else
puts " Staging directory is not specified".red
end
This failed when parsing desc parts of the Rakefile. I also tried things like
puts Rake.instance_variables
puts Rake.class_variables
But these are not getting the #source_dir that I am looking for.
rakefile_body = <<-RUBY
load '../common.rake'
#source_dir = 'some/source/dir'
desc "Run all build and deployment tasks, for continuous delivery"
task :deliver => ['git:pull', 'jekyll:build', 'rsync:push']
RUBY
def source_dir(ast)
return nil unless ast.kind_of? AST::Node
if ast.type == :ivasgn && ast.children[0] == :#source_dir
rhs = ast.children[1]
if rhs.type != :str
raise "#source_dir is not a string literal! #{rhs.inspect}"
else
return rhs.children[0]
end
end
ast.children.each do |child|
value = source_dir(child)
return value if value
end
nil
end
require 'parser/ruby22'
body = Parser::Ruby22.parse(rakefile_body)
source_dir body # => "some/source/dir"
Rake runs load() on the Rakefile inside load_rakefile in the Rake module. And you can easily get the tasks with the public API.
Rake.load_rakefile("Rakefile")
puts Rake.application.tasks
Apparently that load() invocation causes the loaded variables to be captured into the main Object. This is the top-level Object of Ruby. (I expected it to be captured into Rake since the load call is made in the context of the Rake module.)
Therefore, it is possible to access instance variables from the main object using this ugly code:
main = eval 'self', TOPLEVEL_BINDING
puts main.instance_variable_get('#staging_dir')
Here is a way to encapsulate the parsing of the Rakefile so that opening two files will not have all the things from the first one show up when you are analyzing the second one:
class RakeBrowser
attr_reader :tasks
attr_reader :variables
include Rake::DSL
def task(*args, &block)
if args.first.respond_to?(:id2name)
#tasks << args.first.id2name
elsif args.first.keys.first.respond_to?(:id2name)
#tasks << args.first.keys.first.id2name
end
end
def initialize(file)
#tasks = []
Dir.chdir(File.dirname(file)) do
eval(File.read(File.basename(file)))
end
#variables = Hash.new
instance_variables.each do |name|
#variables[name] = instance_variable_get(name)
end
end
end
browser = RakeBrowser.new(f + "Rakefile")
puts browser.tasks
puts browser.variables[:#staging_dir]

How to know which task is being executed with rake

I'd like to know from within a rake ask what is the name of the task that is being executed? How to do this? For example, in the code bellow, when I run rake my_incredible_task, it should print "my_incredible_task":
task :boot do
task_name = <what comes here?>
puts task_name
end
task :my_incredible_task => [:boot] do
#do some stuff
end
I'm not sure there is a way out of the box, however you can do something like this:
require 'rake'
module Rake
class Application
attr_accessor :current_task_name
end
class Task
alias :old_execute :execute
def execute(args=nil)
Rake.application.current_task_name = #name
old_execute(args)
end
end
end
namespace :so do
task :my_task do
puts Rake.application.current_task_name
end
end
Not sure how this will work out with tasks that run in parallel...
You can use a parameter in the block :
task :my_incredible_task do |t|
puts t.name
end
EDIT
You could use methods instead :
task :boot do |t|
boot(t.name)
end
task :my_incredible_task do |t|
boot(t.name)
#do some stuff
end
def boot task_name
# do stuff
end

How do I use a Ruby script to add method to delayed job

I have a rubyscript in /myapp/scripts/myscript.rb
when i run ruby script/myscript.rb it executes fine, but how do I run this method with delayed job from within a ruby script? Hope this makes sense
---myscript.rb---
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
def populate
#do some stuff here
end
handle_asynchronously :populate
I get a undefined method 'handle_asynchronously' error
I think handle_asynchronously works only on methods and not on top-level functions.
Maybe you can declare a module like this:
# scripts/myscript.rb
class Tasks
def populate
puts "Populating..."
end
handle_asynchronously :populate
end
# call it in some function
def my_user_task
Tasks.new.populate
end

Alias of task name in Rake

When I need to alias some task's name, how should I do it?
For example, how do I turn the task name:
rake db:table
rake db:create
rake db:schema
rake db:migration
to:
rake db:t
rake db:c
rake db:s
rake db:m
Editing after getting the answer:
def alias_task(tasks)
tasks.each do |new_name, old_name|
task new_name, [*Rake.application[old_name].arg_names] => [old_name]
end
end
alias_task [
[:ds, :db_schema],
[:dc, :db_create],
[:dr, :db_remove]
]
Why do you need an alias? You may introduce a new task without any code, but with a prerequisite to the original task.
namespace :db do
task :table do
puts "table"
end
#kind of alias
task :t => :table
end
This can be combined with parameters:
require 'rake'
desc 'My original task'
task :original_task, [:par1, :par2] do |t, args|
puts "#{t}: #{args.inspect}"
end
#Alias task.
#Parameters are send to prerequisites, if the keys are identic.
task :alias_task, [:par1, :par2] => :original_task
To avoid to search for the parameters names you may read the parameters with arg_names:
#You can get the parameters of the original
task :alias_task2, *Rake.application[:original_task].arg_names, :needs => :original_task
Combine it to a define_alias_task-method:
def define_alias_task(alias_task, original)
desc "Alias #{original}"
task alias_task, *Rake.application[original].arg_names, :needs => original
end
define_alias_task(:alias_task3, :original_task)
Tested with ruby 1.9.1 and rake-0.8.7.
Hmmm, well, I see that's more or less exactly the same solution RyanTM already posted some hours ago.
Here is some code someone wrote to do it: https://gist.github.com/232966
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end

Resources