Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a rake task for a Rails engine which is not exposed to the host application?

# lib/tasks/test.rake
task :hello do
  puts 'hello'
end

$ rake app:hello

To run the task I need to prefix it with "app:" and it runs in the context of the dummy app. It is also exposed to the host application (i.e when used as a plugin in a parent Rails app) as rake hello.

I want to run a rake task which does not require a Rails environment and runs some command, but it is run from the engine root, not the dummy app root.

like image 409
Kris Avatar asked May 10 '12 11:05

Kris


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

Where do I put rake tasks?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.

How do you define a rake task?

Rake enables you to define a set of tasks and the dependencies between them in a file, and then have the right thing happen when you run any given task. The combination of convenience and flexibility that Rake provides has made it the standard method of job automation for Ruby projects.


2 Answers

I know that's a bit late, but for others here searching for the correct answer, do the following :

Create your task :

# lib/tasks/your_engine_tasks.rake
desc "Explaining what the task does"
task :your_task do
  # Task goes here
end

Then go to your engine ./Rakefile and add

load 'lib/tasks/your_engine_tasks.rake'

Here we go, now:

$ rake -T

gives you your task.

Hope I helped.

like image 83
Donovan BENFOUZARI Avatar answered Sep 17 '22 23:09

Donovan BENFOUZARI


I want a better answer to this question however I did figure out that you can add tasks in the Rakefile for the engine (so ./Rakefile not in the spec/dummy application) like so:

task :my_task do
  puts "hi!"
end

task :default => [:spec, :my_task]

I would prefer to have my task in another file but at least this provides a way to go forward. In my case, I want to run Konacha javascript tests in the dummy application so my Rakefile looks like this:

task :spec_javascript do
  exec 'cd spec/dummy && rake konacha:run' # exec passes command return value up stack!
end

task :default => [:spec, :spec_javascript]
like image 36
Cymen Avatar answered Sep 21 '22 23:09

Cymen