IntelliJ IDEA 2019.3 Help

Rake

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

IntelliJ IDEA provides a convenient way to run, debug, and reload Rake tasks. Moreover, you can use run/debug configurations to run tasks with specific parameters: you can pass task arguments, specify environment variables, and so on.

Before running a Rake task

  1. Make sure the 'rake' gem is installed to the project SDK.

  2. Check that the Rakefile is located in the project's root.

Run a Rake task

IntelliJ IDEA allows you to run an arbitrary Rake task. For example, let’s see how to run the db:migrate task required for migrating a database in the Rails application:

  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

After you've run a Rake task, IntelliJ IDEA automatically creates a special profile - a temporary run/debug configuration. You can customize settings of this configuration, for example, you can pass task arguments, specify environment variables, and so on. Then, you can save the customized configuration to quickly run this configuration in the future.

Run a task using a run/debug configuration

IntelliJ IDEA automatically creates the Rake run configurations for running the Minitest and RSpec tests in Rails applications - test and spec. You can run these tasks or any other task with the existing run/debug configurations in one of the following ways:

  • Press Ctrl twice to invoke the Run Anything popup. Start typing the required configuration name, select it from the list, and press Enter.

  • Click the Run button on the toolbar to run the currently selected run/debug configuration.

  • Select the Run | Run Shift+F10 command from the main menu and select the required configuration.

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 rake --tasks and press Enter.

Configure parameters for running a task

When you run a Rake task for the first time, IntelliJ IDEA automatically creates a corresponding Rake temporary configuration, which can be saved. If necessary, you can create the Rake run/debug configuration manually from the predefined template.

To customize the run/debug configuration, do the following:

  1. Open the Run/Debug Configuration dialog:

    • Select Run | Edit Configurations from the main menu.

    • With the Navigation bar visible (View | Appearance | Navigation Bar), choose Edit Configurations from the run/debug configuration selector.

    • Press Shift+Alt+F10, then press 0 or select the configuration from the popup and press F4.

  2. In the opened Run/Debug Configurations dialog, select the required configuration in the Rake group, and specify its settings.

    Run/Debug Configurations: Rake

    For example, you can specify the following settings in the Configuration tab:

    Option

    Description

    Task name

    Specify the name of the Rake task to be executed. Note that you can use autocompletion (Ctrl+Space) to see the available tasks.

    Arguments

    Specify arguments to be passed to the Rake task. These arguments should be separated with commas. Let's see how it works using the following example:

    • Create the sample Rake task as shown below:

      task :sample, [:first, :last] do |t, args| puts "First name is #{args.first}" puts "Last name is #{args.last}" end
    • Create the Rake run configuration and specify its settings in the following way:

      • Task name: sample

      • Arguments: "Andrew", "Fuller"

    • Run the created configuration. A program will return:

      First name is Andrew Last name is Fuller
    Turn on invoke/execute tracing, enable full backtrace (--trace)

    Enable the --trace Rake command-line option.

    Do a dry run without executing actions (--dry-run)

    Enable the --dry-run Rake command-line option.

    Display the tasks and dependencies, then exit (--prereqs)

    Enable the --prereqs Rake command-line option.

    Attach test runner UI for frameworks

    Depending on the used testing framework, enable the required test runner UI for performing tests.

    Working directory

    Specify the working directory used by the running task. Note that the working directory should contain the Rakefile.

    Environment variables

    Specify the list of environment variables as the name-value pairs, separated with semi-colons. Alternatively, click the ellipsis button to create variables and specify their values in the Environment Variables dialog. For example, you can set the Rails environment to test for running tests:

    RAILS_ENV=test
    Ruby arguments

    Specify the command-line arguments to be passed to the Ruby interpreter.

    Ruby SDK

    Specify the Ruby interpreter used to run a task. You can choose the project default Ruby SDK, or select a different one from the list of configured Ruby SDKs.

Last modified: 9 December 2019