Introduction to Task Building tool rake in Ruby Tutorial _ruby topics

Source: Internet
Author: User

Rake Introduction

Rake means Ruby make, a code-building tool developed with Ruby.

But why does Ruby need rake?

It's supposed to be that Ruby code doesn't need to be compiled, so it doesn't have to be rake. It turns out, rake has another magical effect, that is, rake as a task management tool to use ... There are two advantages to doing this:

1. Create and run scripts in the form of tasks

Of course, you can use your script to create every task that you want to run automatically. However, for large applications, you almost always need to write scripts for database migrations (such as db:migrate tasks in rails), empty caching, or code maintenance. For each task, You may all need to write a few scripts that will complicate your management. Then, putting them together in a task style makes management a lot easier.

2. Tracing and managing the dependencies between tasks

Rake also provides a way to easily manage dependencies between tasks. For example, the "Migrate" task and the "Schema:dump" task depend on the "connect_to_database" task, before the "migrate" task is invoked, "connect_to _database "task will be executed.

Here's how to write a task script with rake.

Sequential execution

After you define tasks in rake, you can specify the order in which tasks are performed, such as the routine of waking up every morning:
1. Turn off the alarm
2. Grooming and dressing
3. Cup OF coffee
4. Walking the Dog
A few of the above things that are described in Rakefile

 Task:turn_off_alarm do
  puts ' turned off alarm. Would have liked 5 more minutes, though. "
 End

 task:groom_myself do
  puts "brushed teeth."
  Puts "showered."
  Puts "shaved."
 End

 Task:make_coffee do
  cups = env["coffee_cups"] | | 2
  puts "Made #{cups} cups of coffee. Shakes are gone. "
 End

 Task:walk_dog do
  puts "dog walked."
 End

 Task:ready_for_the_day => [: Turn_off_alarm,: groom_myself,: Make_coffee,: Walk_dog] do
  puts "ready For the day! "
 End


Use Rake ready_for_the_day to perform tasks, and then you can see that all tasks are executing in the order you intended.

 Turned off alarm. Would have liked 5 more minutes, though.
 Brushed teeth.
 Showered.
 Shaved.
 Made 5 cups of coffee. Shakes are gone.
 Dog walked.
 Ready for the day!


You can also use rake make_coffee coffee_cups=5 to assign a value to a variable in a command.

Name space

It's fine to define a task like that, but if you need to define something else, such as work-related, traffic-related, it's obviously not appropriate to have all the tasks mixed up at this point, after all, the tasks above are just routine things that we get up on.

The namespace can help us define a sort of rake db:migrate task in rails, draw clear boundaries between things, and include the above tasks in a block of namespace code, as follows

 namespace:morning do
  task:turn_of_alarm ...
  .
 End


This time our call command will need to make a slight change, rake coffee_cups=3 morning:ready_for_the_day. Is it similar to the way the rake task is invoked in rails?

Default task

With the above settings, if we forget or do not want to write a detailed task name, direct execution rake will have any effect, the result is rake aborted!, the task was interrupted error. Since there is such a possibility, we need to eliminate these pitfalls by setting default defaults tasks, as follows:

 Task:d efault => ' Morning:turn_off_alarm '


When you execute the rake command directly, the default action is performed to help us turn off the alarm.

Describe your task.

When the task becomes more and more, the management problem is exposed, in addition to the namespace, we also need the support of the document class, help us to comb the task and show us the purpose and function of each task, then you can try a trial desc description of the task.

 ...
 Desc ' Make coffee '
 task:make_coffee do
  cups = env["Coffee_cups" | | | 2
  puts "Made #{cups} cups of coffee. Shakes are gone. "
 End
 ...


The above description not only can be viewed in the document, but also can be cleaned by using rake-t to understand what each task is. The output of the rake-t is sorted alphabetically.

 Rake Morning:make_coffee    # Make Coffee


To fetch a task

Rake also allows you to call each other in different tasks, such as the following code, you want to have a cup of coffee in the afternoon, do not repeat the definition, directly using the morning bubble, to a cup.

Namespace:afternoon do
  Task:make_coffee does
   rake::task[' Morning:make_coffee '].invoke
   puts ' Ready for The rest of the day! "
  End End
 


Rake Script Writing

Let's start with a simple example, as follows:

Suppose you are a member of Mars, the old version of those, such as the Yan ER Tiger running, and so on the weekend you plan to go to the car to play hot pot, and then the collective PC go. In response to this situation, you need to set three tasks for yourself: hitchhiking, grilled fish and Internet Café PCs. Create a file called Rakefile with vim (note Rake will look for a rake file named Rakefile, Rakefile, RAKEFILE.RB, and rakefile.rb under the current path, and type the following code:

DESC "Mission 1--hitch a ride to the car" #这据说是个苦差, because it's too far 
task:busboy do 
puts "found down man" End 
 
desc "Mission 2--Grilled fish" 
task:bitchfish Do 
puts "Boss, first bake nine pounds fish" 
End 
 
desc "Task 3--Internet Café pc" task 
:p C do 
puts "I select Road" 
end 

Open the Command line tool, enter the directory where the file is located, and then run the following command, which should roughly resemble the following results:

D:\work>rake busboy 
(in d:/work) 
found 
the boss of D:\work>rake Bitchfish (in d:/work) 
to bake nine pounds of fish first. 
 
D:\work\ruby_works\ruby_book>rake Laundry 
(in D:/work) 
I picked the way. 


(Remark: The text part is illogical, pure entertainment ...)

Analysis:

Believe that after reading the above section, you already know how to do ... Now introduce some basic knowledge to facilitate deeper understanding. From the above code, you can tell that this file defines 3 tasks altogether, DESC is a rake-defined method that represents a description of the tasks defined below. This description is output on the screen when using Rake--tasks (or rake-t) commands.

D:\work>rake--tasks 
(in d:/work) 
rake bitchfish #任务2-Grilled fish 
rake busboy #任务1-Hitch a ride to the car (which is said to be a chore because it's too far away) 
   rake PC Task 3--Internet café pc 


Task is the most important method of rake. Its method definition is: Task (args, &block). The task body is a block, in this case it simply prints out the work you want to do. The code that needs to be noted

Puts "found a man with a crush" 


A completely ordinary Ruby statement, puts is the general method of exporting in Ruby, and it is clear that the rake task can fully use Ruby's ability, which makes it very powerful.

Go.. Go.. Go.. Go..

Next, add dependencies:

Obviously, in the task we define, "grilled fish" is dependent on "hitchhiking to the car there" (other places have no grilled fish to eat do not know, anyway, the location is set in that). Then we need to include this dependency in our task definition, and the modified file is as follows:

DESC "Mission 1--hitch a ride to the car," 
Task:busboy do 
puts "find the loser" end 
 
desc "Task 2--Grilled fish" 
task:bitchfish =>: Busb Oy do 
puts "Boss, first bake nine Jin fish" 
End 
 
desc "Task 3--Internet Café pc" task 
:p C do 
puts "I select Road" 
end 

Run the grilled fish task again and you will get the following results:

D:\work>rake bitchfish 
(in d:/work) 
found 
the boss, first roast nine kg of fish 

To join the namespace:

Like any programming language, when you have a lot of rake files, when you have a lot of tasks, you need to pay attention to their naming conflict problem, namespace (namespace) is a natural solution. You can define a namespace called Dan for the three tasks above.

Namespace:d a 
do desc "Mission 1--hitch a ride to the car" 
Task:busboy do 
puts "find the man 
" 
end ... End 

Run the rake--tasks again, and you'll get the following results:

D:\work >rake--tasks 
(in d:/work) 
Rake Dan:bitchfish # Task 2-Grilled Fish 
Rake dan:pc # Task 3--Internet café pc 
rake da N:busboy # mission 1--hitch a ride to the car. 


You need to use rake dan:bitchfish to start the grilled fish task now.
(BTW, you can use multiple namespaces in your rakefile to categorize tasks.)

Once you've learned the above two points, let's take a look at the next two specific examples:

1. Invoke another task in one task

When there are many tasks, you probably need to invoke another task in one task. Let's say we define all the work we have to do today as a task: now. In this task, there are two tasks to be called, one is grilled fish, the other is the Internet Café pc. Of course, as the grilled fish depended on a hitch to the car, We still need a ride to the car. Define a today task at the top of the file:

Desc "Today's task" 
task:today do 
rake::task["Dan:bitchfish"].invoke rake::task[ 
"dan:pc"].invoke 
End 
 
namespace:d A Do 
... 
.. End 

As you can see, the way to invoke other tasks is simple, and you just need to call

rake::task["Task_name"].invoke 

method on it. To start rake on the command line today, you can get:

D:\work >rake today 
(in d:/work) 
found 
the boss, first roast nine kg fish 
I selected road 

2. Default tasks:

You can add a default task for rake so that you can simply trigger the default task with the Rake command, and in the above rakefile we can use the "Today" task as the default task in the following way.

Task:d efault => [: Today] 


Then call rake directly in the command line to get the same output as the call rake today.

This is our simple rake task definition, and the following is the complete modified rakefile:

Task:d efault => [: Today] 
 
desc "Tasks of the day" 
task:today do 
 rake::task["Dan:bitchfish"].invoke 
 Rake:: task["dan:pc"].invoke 
end 
 
namesoace:d 
 a Do desc "task 1--a hitch to the car (which is said to be a chore because it's too far)" Task:busboy do 
  Puts "found down man" 
 end 
 
 desc Task 2-grilled fish "task:bitchfish do 
  puts" boss, first roast nine kg fish "end 
 
 desc" Task 3--Internet café pc 
 task:p C do 
  puts "I select Road" 
 End 
 

After reading the two examples above, it is estimated that the rake task is fully understood ... The others are just some code gun flowers. The more you play, the more you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.