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

2

7
# 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.

Triangulate answered 10/5, 2012 at 11:21 Comment(0)
P
9

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.

Pol answered 28/7, 2016 at 12:15 Comment(1)
You need to add :environment to your task: task your_task: :environment doChem
U
4

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]
Udella answered 5/10, 2012 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.