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:
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.
From the main menu, choose db:migrate, select db:migrate and press Enter.N/A. In the invoked popup, start typing
In the invoked Execute 'db:migrate' dialog, select the required migration version and environment. Click OK.
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 Reload rake tasks list.N/A. In the invoked popup, select
Create Rake task
To create a new Rake task in the lib/tasks directory, do the following:
Right-click the lib/tasks directory in the Project view and select from the context menu. You can also invoke this menu by pressing Alt+Insert.
In the New File dialog, specify the name of the file, followed by the .rake extension (for example, sample_task.rake), and click OK.
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
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.
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
Create the run/debug configuration for the rcov task and run it with coverage.