IntelliJ IDEA 2019.2 Help

Rake

IntelliJ IDEA provides a convenient way to run, reload, and create Rake tasks. The IDE automatically detects the following tasks:

  • Predefined tasks that come with Rails

  • Tasks defined in Rakefile placed under the project root

  • Tasks placed in the lib/tasks project directory

Run Rake task

IntelliJ IDEA automatically creates run configurations for Rake tasks used to launch the Test::Unit and RSpec tests in Rails applications - test and spec. You can run these tasks by running a corresponding run configuration.

You can also run a specific Rake task. For example, let’s see on how to run the db:migrate task required for migrating a database in the Rails application. Perform the following steps:

  1. Do one of the following:

    • Press Ctrl twice and start typing db:migrate in the invoked popup. Select rake db:migrate from the list and press Enter.

      Run Anything / rake db:migrate

    • From the main menu, choose Tools | Run Rake Task N/A. In the invoked popup, start typing db:migrate, select db:migrate and press Enter.

  2. In the invoked Execute 'db:migrate' dialog, select the required migration version and environment. Click OK.

    Execute db:migrate

IntelliJ IDEA will execute this task and create a corresponding run configuration. You can modify this configuration to provide additional options for running the task.

Reload Rake tasks

Sometimes it is necessary to reload Rake tasks. For example, this can be useful if you created a custom task and need to run it. To reload Rake tasks, do one of the following:

  • Press Ctrl+Shift+A and start typing Reload Rake Tasks. Select this item from the list and press Enter.

  • From the main menu, select Tools | Run Rake Task N/A. In the invoked popup, select Reload rake tasks list.

Create Rake task

To create a new Rake task in the lib/tasks directory, do the following:

  1. Right-click the lib/tasks directory in the Project view and select New | File from the context menu. You can also invoke this menu by pressing Alt+Insert.

  2. In the New File dialog, specify the name of the file, followed by the .rake extension (for example, sample_task.rake), and click OK.

  3. In the editor, specify code for your task. For example, sample_task below launches the script.rb file.

    task :sample_task do ruby "script.rb" end
  4. Reload Rake tasks as described above. You can now run your task using Run Anything.

    Run custom Rake task

Collect code coverage with Rake task

If you run a test using the Rake task, you need to provide code coverage measurement. For example, you already have the special Rake task that collects code coverage information using rcov.

  1. Open your .rake file in the editor, and add the following code:

    require 'rcov/rcovtask' Rcov::RcovTask.new do |t| t.libs << "test" t.test_files = FileList ['test/**/*_test.rb'] t.verbose = true end

  2. Create the run/debug configuration for the rcov task and run it with coverage.

Last modified: 19 August 2019