Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call another task in Rake

Although this might sound similar to the other questions you find here, there is a slight twist. I have two directories, say /home/rails/Rake and /home/rails/test_app. The rails directory is where I place all my rails projects.

Inside Rake, I have a Rakefile and a create.rake file.

This is what my rakefile look's like

namespace :setup do 
    desc "something"
    task :init do
        print "Name of the destination directory: "
        name = STDIN.gets.strip
        cp_r '.', "../#{name}/lib/tasks"
        cd  "../#{name}"
        sh "rake setup:create"

    end
end

And create.rake

namespace :setup do 
    desc "Install"
    task :create do
        sh 'git init'
        #some other code
    end
end

What it does is obvious. I want to copy the contents of the Rake directory to /test_app/lib/tasks. Then change directory to test_app and run setup:create task defined in the install.rake file now placed in test_app/lib/tasks. This works, but is this the rake way of doing it? Can anyone give me a slight hint of how it's done, the Rake way.

Here is the error which I get when I used invoke method:

$ rake setup:init
Name of the destination directory: 
testapp
cp -r . ../testapp/lib/tasks
cd ../testapp
rake aborted!
Don't know how to build task 'setup:create'
/home/TradeRaider/rails/Rake/Rakefile:8:in `block (2 levels) in <top (required)>'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => setup:init
(See full trace by running task with --trace)
like image 785
TradeRaider Avatar asked Jul 22 '13 06:07

TradeRaider


People also ask

How do you call a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

How does rake task work?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


2 Answers

Although @apneadiving answer helped, it just struck me that I was trying to call a Rakefile from another Rakefile, literally speaking. Anyways, to do so, I had to first load the rake file,

load "../#{name}/lib/tasks/create.rake"

(requiring it will also do the trick)

and then invoke it.

Rake::Task["setup:create"].invoke
like image 94
TradeRaider Avatar answered Sep 17 '22 06:09

TradeRaider


This is more rake-ish :)

 Rake::Task["setup:create"].invoke
like image 25
apneadiving Avatar answered Sep 18 '22 06:09

apneadiving