Tutorial on task build tool rake in Ruby, rubyrake

Source: Internet
Author: User

Tutorial on task build tool rake in Ruby, rubyrake

Rake Introduction

Rake indicates Ruby Make, a code building tool developed by ruby.

But why does Ruby need Rake?

It is reasonable to say that Ruby Code does not need to be compiled and Rake is not required? It turns out that Rake has another clever use, that is, using Rake as a task management tool... There are two advantages to doing so:

1. Create and run scripts as tasks

Of course, you can use scripts to create each task that you want to run automatically. however, for large applications, you almost always need to write scripts for database migration (such as db: migrate task in Rails), clearing the cache, or code maintenance. for each task, you may need to write several scripts, which will complicate your management. therefore, it will be much easier to organize them together by means of tasks.

2. Trace and manage dependencies between tasks

Rake also allows you to easily manage dependencies between tasks. for example, the "migrate" task and "schema: dump" task depend on the "connect_to_database" task. Before the "migrate" task is called, the "connect_to_database" task will be executed.

Next let's get into the question, that is, how to use Rake to write a task script ..

Sequential execution

After defining a task in Rake, you can specify the execution sequence of the task, for example, routine tasks after getting up every morning:
1. Turn off the alarm
2. Dress up
3. Coffee Cup
4. Dog walking
The preceding items 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


Run the task through rake ready_for_the_day. Then you can see that all tasks are executed in the order you have specified.

 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 values to variables in the command.

Namespace

There is no problem in defining tasks as above, but if you need to define other things, such as work-related and traffic-related, it is obviously not appropriate to mix all tasks together, after all, the above tasks are just routine things we get up and have nothing to do with others.

Namespace can help us define a task similar to rake db: migrate in Rails, draw clear boundaries between things, and include the above tasks in a code block in namespace, as shown below:

 namespace :morning do  task :turn_of_alarm  .... end


This time, we need to make some changes to the call command. rake COFFEE_CUPS = 3 morning: ready_for_the_day. Is it similar to calling a rake task in rails?

Default Task

With those settings above, if we forget or do not want to write a detailed task name, what will happen if we directly execute rake? The result is rake aborted !, An error is reported when the task is interrupted. In this case, we need to eliminate these risks by setting the default task as follows:

 task :default => 'morning:turn_off_alarm'


When you directly execute the rake command, the default operation will be executed to help us disable the alarm.

Describe your Task

As the number of tasks increases, management problems are exposed. In addition to namespaces, we also need support for document classes, to help us sort out the tasks and display the purpose and functions of each task, you can try desc to describe 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 can be viewed in not only the document, but also the rake-T can be used to clean and understand what each task is doing. The output result of rake-T is in alphabetical order.

 rake morning:make_coffee    # Make coffee


Retrieve Task

Rake can also be called between different tasks. For example, in the following code, if you want to have a cup of coffee in the afternoon, you don't need to repeat the definition. Just use the morning Bubble Method to have a cup of coffee.

namespace :afternoon do  task :make_coffee do   Rake::Task['morning:make_coffee'].invoke   puts "Ready for the rest of the day!"  end end


Rake script writing

Here is a simple example:

Suppose you are a member of Mars, old versions, such as yaner and tiger. On the weekend, you plan to go to the car to make a hot pot and then go to the PC group. in this case, you need to develop three tasks for yourself: finding a car, roasting fish, and Internet cafe PC. use vim to create a file named rakefile. (Note: Rake searches for files named Rakefile, rakefile, and RakeFile in the current path. rb and rakefile. and enter the following code:

Desc "task 1 -- take a ride to the car" # This is said to be a hard job, because it is too far away task: busboy do puts "finds male" end desc "task 2 -- grilled fish" task: bitchfish do puts "boss, "end desc" task 3 -- Internet cafe PC "task: pc do puts" I selected "end

Open the command line tool, enter the directory where the file is located, and then run the following command. The result is similar to the following:

D: \ work> rake busboy (in D:/work) found male D: \ work> rake bitchfish (in D:/work) Boss, first bake nine catty fish D: \ work \ ruby_works \ ruby_book> rake laundry (in D:/work)


(Note: There is no logic in the text part. It is purely entertaining ...)

Analysis:

I believe that after reading the above section, you already know how to do it... now we will introduce some basic knowledge to help you better understand it. as you can see from the code above, this file defines a total of three tasks. desc is the method defined by Rake, indicating the description of the following defined tasks. this description will be output to the screen when the Rake -- tasks (or Rake-T) command is used.

D: \ work> rake -- tasks (in D:/work) rake bitchfish # Task 2 -- grilled fish rake busboy # Task 1 -- take a ride to the car (this is said to be a bitter problem, because it's too far.) rake pc Task 3 -- Internet cafe PC


Task is the most important method for Rake. its method definition is: task (args, & block ). the task body is a block. In this example, the task is simply output. note that the code

Puts "male found"


It is a general Ruby statement, and puts is a general method of Ruby output. It can be seen that the Rake task can fully use Ruby's capabilities, which makes it very powerful.

Go ..

Next, add the dependency:

Apparently, in our defined task, "Grilled Fish" relies on "riding a taxi to a car" (I don't know if there are roast fish in other places, the location will be there ). then, we need to add this dependency in our task definition. The modified file is as follows:

Desc "task 1 -- take a ride to the car" task: busboy do puts "find male" end desc "task 2 -- grilled fish" task: bitchfish =>: busboy do puts "boss, "end desc" task 3 -- Internet cafe PC "task: pc do puts" I selected "end

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

D: \ work> rake bitchfish (in D:/work) found a male boss, first roast nine catty fish

Add a namespace:

Similar to any programming language, when you have many rake files and many tasks, you need to pay attention to their naming conflicts. namespace) is a natural solution. you can define a namespace named dan for the preceding three tasks.

Namespace: dan do desc "task 1 -- take a ride to the car" task: busboy do puts "find male" end ...... End

Run rake -- tasks again and you will get the following results:

D: \ work> rake -- tasks (in D:/work) rake dan: bitchfish # Task 2 -- grilled fish rake dan: pc # Task 3 -- Internet cafe PC rake dan: busboy # Task 1 -- take a ride to the car


Now you need to use rake dan: bitchfish to start the roast fish task.
(BTW, you can use multiple namespaces in your rakefile to classify tasks .)

After learning about the above two knowledge points, let's take a look at two specific instances:

1. Call another task in one task

When there are many tasks, you may need to call another task in one task. Suppose we define all the work to be done today as a task: today. in this task, two tasks need to be called, one is roast fish and the other is the internet cafe PC. of course, because the grilled fish relies on a ride 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: dan do ...... End

It can be seen that the method to call other tasks is very simple, you only need to call

Rake::Task["task_name"].invoke 

You can run the following command to start rake today:

D: \ work> rake today

2. Default task:

You can add a default task for Rake, so that you can simply use the Rake command to trigger this default task. In the preceding rakefile, we can use the "today" task as the default task as follows.

task :default => [:today] 


Call rake directly in the command line to get the same output result as calling rake today.

This is a simple definition of a Rake task. The modified rakefile is as follows:

Task: default => [: today] desc "today's task" Task: today do Rake: task ["dan: bitchfish"]. invoke Rake: Task ["dan: pc"]. invoke end namesoace: dan do desc "task 1 -- take a ride to the car (this is said to be a hard time, because it is too far away)" task: busboy do puts "finds male" end desc "task 2 -- grilled fish" task: bitchfish do puts "boss, "end desc" task 3 -- Internet cafe PC "task: pc do puts" I selected "end

After reading the above two examples, it is estimated that the rake task will be fully understood... the other is code ..

Articles you may be interested in:
  • Knowledge about Ruby (rvm, gem, bundle, rake, rails, etc)

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.