官术网_书友最值得收藏!

Using global Rakefiles to run tasks anywhere

By default, Rake is looking for tasks that are placed in the current folder (that is, the folder where you run the rake command) in the Rakefile. Assume that we need to have a rake task that can be executed in any folder. For example, say that we have a rake task that cleans the Linux files ending with ~. The following Rakefile defines the rake task to remove them:

desc 'Cleans backup files *~'
task :default do
  files = Dir['*~']
  rm(files)
end

Here, we get temporary files in the current folder and remove them with the rm method. This method is defined in the FileUtils module, which is included in Rake as well. So, we will discuss it in the next chapters.

When you are in the current folder, check this rake task using the Rakefile:

$ rake
rm

Here, we see that the rm command was executed and Rake explicitly said this in the second line. If you don't want to see this verbose message, pass the -q option to the command.

However, what would happen if we go to the folder one level up? When you try to type the rake command, you will have an error message that says that no Rakefile was found. We can get rid of this problem by passing the -f option with the path to the Rakefile as shown in the following lines of code:

$ rake -f ~/my-rake-task/Rakefile
rm

This works well, but you may agree with me that it's too unhandy. Rake produces one useful feature to make this situation work the way we want. It's based on the method of finding the Rakefile. First, Rake tries to find the Rakefile in the current folder. If Rake can't find it there, the search continues till it reaches the user's home folder. If there is no Rakefile there, it finally raises an exception saying that the Rakefile was not found. We can apply this behavior to our issue. Just move the Rakefile to your home folder and mark the rake tasks defined in it as available for the current user everywhere. Open the terminal and type the following commands to achieve the expected output:

$ mv ~/my-rake-task/Rakefile ~/
$ cd ~/my-rake-task
$ rake
(in /Users/andrey)
rm

As you can see, this works as expected, and there is one more new line, as follows:

(in /Users/andrey)

This command says that the Rakefile was found at the user home folder. You can disable showing this information by passing the -s option.

There is another way to define global Rakefiles. You have an option to define them in the ~/.rake folder, and they can be executed from any folder with the help of the -g option. The following is the Rake output of the help command:

-g, --system Using system wide (global) rakefiles (usually '~/.rake/*.rake').

So, let's define a global Rakefile in this way and check it in action. The following is an example of how to do it through the terminal:

$ mkdir ~/.rake
$ touch ~/.rake/hello.rake
$ echo -e 'task "hello" do\n  puts "Hello, Rake"\nend' > ~/.rake/hello.rake
$ rake -g hello
Hello, Rake
主站蜘蛛池模板: 垫江县| 崇义县| 辉南县| 永寿县| 宜川县| 郯城县| 杭锦后旗| 西平县| 承德县| 镇雄县| 克东县| 拉孜县| 竹山县| 连山| 汉寿县| 图片| 思南县| 平乡县| 孝义市| 宁乡县| 日喀则市| 清水河县| 永登县| 鄂伦春自治旗| 莫力| 宁武县| 乌拉特前旗| 依安县| 抚宁县| 尼勒克县| 梅河口市| 射阳县| 沛县| 大田县| 麻城市| 崇仁县| 东乡县| 马龙县| 阳春市| 嵩明县| 新干县|