(For more resources related to this topic, see here.)
As Rake is a Ruby library, you should first install Ruby on the system if you don’t have it installed already. The installation process is different for each operating system. However, we will see the installation example only for the Debian operating system family.
Just open the terminal and write the following installation command:
$ sudo apt-get install ruby
If you have an operating system that doesn’t contain the apt-get utility and if you have problems with the Ruby installation, please refer to the official instructions at https://www.ruby-lang.org/en/installation. There are a lot of ways to install Ruby, so please choose your operating system from the list on this page and select your desired installation method.
Rake is included in the Ruby core as Ruby 1.9, so you don’t have to install it as a separate gem. However, if you still use Ruby 1.8 or an older version, you will have to install Rake as a gem. Use the following command to install the gem:
$ gem install rake
The Ruby release cycle is slower than that of Rake and sometimes, you need to install it as a gem to work around some special issues. So you can still install Rake as a gem and in some cases, this is a requirement even for Ruby Version 1.9 and higher.
To check if you have installed it correctly, open your terminal and type the following command:
$ rake --version
This should return the installed Rake version.
The next sign that Rake is installed and is working correctly is an error that you see after typing the rake command in the terminal:
$ mkdir ~/test-rake $ cd ~/test-rake $ rake rake aborted! No Rakefile found (looking for: rakefile,
Rakefile, rakefile.rb, Rakefile.rb) (See full trace by running task with --trace)
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Introducing rake tasks
From the previous error message, it’s clear that first you need to have Rakefile. As you can see, there are four variants of its name: rakefile, Rakefile, rakefile.rb, and Rakefile.rb. The most popularly used variant is Rakefile. Rails also uses it. However, you can choose any variant for your project. There is no convention that prohibits the user from using any of the four suggested variants.
Rakefile is a file that is required for any Rake-based project. Apart from the fact that its content usually contains DSL, it’s also a general Ruby file. Also, you can write any Ruby code in it. Perform the following steps to get started:
- Let’s create a Rakefile in the current folder, which will just say Hello Rake, using the following commands:
$ echo "puts 'Hello Rake'" > Rakefile $ cat Rakefile puts 'Hello Rake'
Here, the first line creates a Rakefile with the content, puts ‘Hello Rake’, and the second line just shows us its content to make sure that we’ve done everything correctly.
- Now, run rake as we tried it before, using the following command:
$ rake Hello Rake rake aborted! Don't know how to build task 'default' (See full trace by running task with --trace)
The message has changed and it says Hello Rake. Then, it gets aborted because of another error message. At this moment, we have made the first step in learning Rake.
- Now, we have to define a default rake task that will be executed when you try to start Rake without any arguments. To do so, open your editor and change the created Rakefile with the following content:
task :default do puts 'Hello Rake' end
- Now, run rake again:
$ rake Hello, Rake
The output that says Hello, Rake demonstrates that the task works correctly.
The command-line arguments
The most commonly used rake command-line argument is -T. It shows us a list of available rake tasks that you have already defined.
We have defined the default rake task, and if we try to show the list of all rake tasks, it should be there. However, take a look at what happens in real life using the following command:
$ rake -T
The list is empty. Why? The answer lies within Rake. Run the rake command with the -h option to get the whole list of arguments. Pay attention to the description of the -T option, as shown in the following command-line output:
-T, --tasks [PATTERN] Display the tasks
(matching optional PATTERN) with descriptions, then exit.
You can get more information on Rake in the repository at the following GitHub link at https://github.com/jimweirich/rake.
The word description is the cornerstone here. It’s a new term that we should know. Additionally, there is also an optional description to name a rake task. However, it’s recommended that you define it because you won’t see the list of all the defined rake tasks that we’ve already seen. It will be inconvenient for you to read your Rakefile every time you try to run some rake task. Just accept it as a rule: always leave a description for the defined rake tasks.
Now, add a description to your rake tasks with the desc method call, as shown in the following lines of code:
desc "Says 'Hello, Rake'" task :default do puts 'Hello, Rake.' end
As you see, it’s rather easy. Run the rake -T command again and you will see an output as shown:
$ rake -T rake default # Says 'Hello, Rake'
If you want to list all the tasks even if they don’t have descriptions, you can pass an -A option with the -T option to the rake command. The resulting command will look like this: rake -T -A.