如何在Rake中调用另一个任务

时间:2022-08-03 02:32:27

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.

虽然这可能与您在此处找到的其他问题类似,但有一点点扭曲。我有两个目录,比如/ home / rails / Rake和/ home / rails / test_app。 rails目录是我放置所有rails项目的地方。

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

在Rake中,我有一个Rakefile和一个create.rake文件。

This is what my rakefile look's like

这就是我的rakefile看起来像

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

并且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.

它的作用是显而易见的。我想将Rake目录的内容复制到/ test_app / lib / tasks。然后将目录更改为test_app并运行setup:create task,该文件现在位于test_app / lib / tasks中的install.rake文件中。这有效,但这是这样做的耙方式吗?任何人都可以给我一点点暗示它是如何完成的,Rake方式。

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

这是我使用invoke方法时得到的错误:

$ 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)

2 个解决方案

#1


36  

This is more rake-ish :)

这是更rake-ish :)

 Rake::Task["setup:create"].invoke

#2


4  

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,

虽然@apneadiving的答案有所帮助,但从字面上讲,我试图从另一个Rakefile调用Rakefile只是让我感到震惊。无论如何,要做到这一点,我必须先加载rake文件,

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

(requiring it will also do the trick)

(要求它也会做到这一点)

and then invoke it.

然后调用它。

Rake::Task["setup:create"].invoke

#1


36  

This is more rake-ish :)

这是更rake-ish :)

 Rake::Task["setup:create"].invoke

#2


4  

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,

虽然@apneadiving的答案有所帮助,但从字面上讲,我试图从另一个Rakefile调用Rakefile只是让我感到震惊。无论如何,要做到这一点,我必须先加载rake文件,

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

(requiring it will also do the trick)

(要求它也会做到这一点)

and then invoke it.

然后调用它。

Rake::Task["setup:create"].invoke