Rake I will not introduce it anymore. Ruby's Make is easier to use than Make in many aspects. Unlike Makefile, Rakefile itself is actually a piece of Ruby code, which has many advantages. On the one hand, in Rake, You Can Do Anything Ruby can do directly, on the other hand, because Ruby supports DSL well, Rakefile generally does not look as "code ".
However, the Code is always code, and Makefile can still be written in disorder. It is easier to write Rakefile in disorder. Fortunately, rake provides some functions that allow us to organize Rakefile.
One of them is the import function, which writes tasks of different functions to different files, for example, like this:
Copy codeThe Code is as follows:
Rakefile
Task/
+ -- Doc. rake
+ -- Compile. rake
'-- Deploy. rake
In this way, write in Rakefile
Copy codeThe Code is as follows:
Import ("task/doc. rake ")
Such a statement can be imported into each sub-task, and it will not be a mess if different tasks are written into different files. In addition, the import is different from Ruby's own require. The import is not imported immediately, but is imported only after the entire Rakefile is executed. Therefore, you can write the import in any place, instead of worrying about dependencies, you only need to define shared variables in the main Rakefile.
The import module organizes different functional modules. In addition, Rake also allows us to abstract repetitive tasks. Specifically, it is a custom task. In general, we use the general tasks and file tasks provided by Rake to construct the work we need to complete. In addition, Rake also comes with some task types for special tasks, for example, building rdoc or running test. In fact, a Task is a common Ruby class. We can inherit the task class in Rake and redefine related functions to implement custom Task types. However, this is somewhat troublesome. In fact, most of the tasks we want to define can be broken down into small tasks implemented using built-in general tasks and file tasks, in this case, Tasklib can be used to easily define custom tasks.
Specifically, it is to write a class that inherits from Tasklib (although it is only a Convention but not a necessity ), then, use the task or file in the initialization function of this class to define the subtasks that actually complete the task. For example, we can define an ErlcTask, which can be used to compile some Erlang files into a directory and automatically compile the files in the clean directory. beam file cleanup:
Copy codeThe Code is as follows:
Require 'rake'
Require 'rake/clean'
Require 'rake/tasklib'
Class ErlcTask <Rake: TaskLib
Attr_accessor: name
Attr_accessor: sources
Attr_accessor: dest_dir
Attr_accessor: include_path
Attr_accessor: flags
Attr_accessor: extra_dep
Def initialize (name =: erlc)
# Default values
If name. is_a? Hash
@ Name = name. keys. first
@ Extra_dep = name. values. first
Else
@ Name = name
@ Extra_dep = []
End
@ Sources = FileList []
@ Dest_dir = '.'
@ Include_path = []
@ Flags = "-W + warn_unused_vars + warn_unused_import"
Yield self if block_given?
Define
End
Def define
Beams = @ sources. pathmap (File. join (@ dest_dir, '% n. beam '))
Include_path = Array (@ include_path). map {| incl | "-I" + incl}. join ("")
Directory @ dest_dir
Beams.zip (@ sources). each do | beam, source |
File beam => source do
Sh "erlc-pa # {@ dest_dir} # {@ flags} # {include_path}-o # {@ dest_dir} # {source }"
End
End
Task @ name => beams + Array (@ extra_dep)
CLEAN. include (beams)
End
End
First, define some Task-related attributes, set the initial values in the initialization function, call block to fill in the actual values, and finally call the define function, the define function uses directory, file, and task to define the tasks for directory Creation, compilation, and cleaning. It should be easy to understand the basic syntax of Ruby and Rake.
Next, save the file to a. rb file, and then in Rakefile require, you can write it like this:
Copy codeThe Code is as follows:
ErlcTask. new: compile do | t |
T. sources = FileList ['src/*. erl']
T. dest_dir = '../ebin'
T. include_path = '../include'
T. extra_dep =: library
End
It looks much refreshed! And can be reused. Finally, by the way, I would like to lament that although Python has been used more recently, it can be quite comfortable to write Ruby every time. This is basically impossible to find it in Python!