Capistrano 2 - Pass array arguments to a Rake task - ruby

I am trying to write a Rake task in my deploy.rb that gets passed an array of arguments. I will run it through capistrano.
Assuming a task like the following:
desc "my task"
task :mytask, [:arguments] => :environment do |t, args|
puts 'hello'
end
When I try running cap mytask['arg1 arg2 arg3'], I get the following:
the task `mytask["arg1 arg2 arg3"]' does not exist
Any ideas? I have a task defined like this that I run locally with no problems, but defining tasks like this be run using cap does not work.
NOTE: running cap mytask without the arguments part works. This is obviously not what I want.

Rake tasks are defined by a string and called via Rake::Task["name"] internally . If cap treats your array as part of the task name string it will not work.
As 'mytask' != 'mytask["arg1 arg2 arg3"]'

Related

Confused with the second variable of task : some_random_name1, [:some_random_name2] => :environment in rake?

I am new to ruby and rake, and currently confused with some ruby syntax.
task :some_random_name1, [:some_random_name2] => :environment do |task, args|
end
What does [:some_random_name2] here mean? I know some_random_name1 is a task that depends on another task :environment, and task and args in |task, args| are arguments from command line. Thanks in advance.
[:some_random_name2] is referring to arguments that you can pass into your rake task. May I suggest checking out this article which can explain in more depth passing arguments to a rake task.
When you call rake -T from the command line, you should see:
rake example: some_random_name1[some_random_name2]
(assuming your namespace is 'example')

Rake task return value for Ruby system() call

I have a Rake task which looks something like the following. What I’m trying to do run a system command, and return its error-value. Before returning I’d like to display a message saying something like “[OK]” or “[FAILED]".
With this code, Rake returns success every time.
How do I get the Rake task to return the correct error value?
task :build do
status = system BUILD_SHELL_COMMAND
puts status ? "[OK]" : "[FAILED]"
status
end
It appears there isn’t a way to specify a “return value” from a rake task. The task should fail if the system() method fails.
The standard way to do this would be to use Rake’s sh utility method:
task :build do
sh BUILD_SHELL_COMMAND
end
To display an error/success message however, for the case in question, the following would not work:
task :build do
sh BUILD_SHELL_COMMAND or fail “[FAILED]”
puts “[OK]"
end
because as soon as the shell command fails, it would not display the failure message (which in reality would be a longer non-trivial message :), which is what we want.
This works:
task :build do
system BUILD_SHELL_COMMAND or fail “[FAILED]”
puts “[OK]"
end

Invoking the same rake task twice in RSpec

I am trying to test a rake task with rspec, and for that purpose I need to invoke it twice but
it is only being invoked once.
it 'first test' do
Rake::Task['my_rake_task'].invoke
# rake task was processed
end
it 'second test' do
Rake::Task['my_rake_task'].invoke
# rake task was NOT processed
end
if a rake task has already been invoked once it won't run again unless you call:
#rake[#task_name].reenable
or invoke it with
#rake[#task_name].execute
To adding to Guy Segev answer I prefer adding this to your spec file
after(:each) do
Rake::Task["task:name"].reenable
end

Namespaces in Ruby Rake Tasks

Are the following equivalent?
namespace :resque do
task setup: :environment do
end
end
task "resque:setup" => :environment do
end
In short: yes. When running rake resque:setup both of these tasks will be invoked.
Rake will merge these tasks. You can test this by doing the following:
p Rake.application.tasks
Which in this case would return something like
[<Rake::Task resque:setup => [environment]>]
Which is simply an Array holding a single Rake::Task object. You can also check the scope or list of namespaces for a task by doing:
p Rake.application.tasks.first.scope
#=> ["resque"]
If you want to learn a little more on how the internals of Rake work, check out Rake::Task and Rake::TaskManager

Run rake tasks in sequence

I have rake tasks which i want to run in proper sequence.
I want to run one rake task which run other rake tasks in proper sequence.
How may i do that?
you should consider defining dependencies between your tasks like this
task :primary => [:secondary]
task :secondary do
puts "Doing Secondary Task"
end
But if you really, really need to call the tasks directly you can use invoke to call another task
task :primary do
Rake::Task[:secondary].invoke
end
task :secondary do
puts "Doing Secondary Task"
end
see also here

Resources