Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a custom Rake task to run in Sinatra?

*I want to get a custom Rake task to run in my Sinatra app but I keep getting rake aborted! Don't know how to build task 'greet'.

Here's the custom Rake task (greet.rake) for testing purpose:

task :greet do
  puts "Hello!"
end

I've put the greet.rake in ./lib/tasks (Rails). I'm guessing Rake can't find the correct directory for the file.

How do I get a custom Rake task to run in Sinatra?

I'm using Ruby 2.0.0 and Sinatra 1.4.4.

UPDATE

The Rakefile now looks like this:

require "./app"
require "sinatra/activerecord/rake"
require "./lib/tasks"

When using:

rake greet

I get:

rake aborted!
cannot load such file -- ./lib/tasks
/Users/*/.rvm/gems/ruby-2.0.0-p247@global/gems/activesupport-        4.0.1/lib/active_support/dependencies.rb:229:in `block in require'
/Users/*/.rvm/gems/ruby-2.0.0-p247@global/gems/activesupport-    4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/*/.rvm/gems/ruby-2.0.0-p247@global/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/Users/*/Dropbox/Development/Sinatra/sinatra-mp-experiment/Rakefile:3:in `<top    (required)>'
(See full trace by running task with --trace)
like image 418
narzero Avatar asked Nov 19 '13 20:11

narzero


1 Answers

Create a Rakefile at your Sinatra app's top directory, require the file that contains this task you want to use and you should be good to go.

Edit:

A simple solution is changing your Rakefile to:

require "./app"
require "sinatra/activerecord/rake"
Dir.glob('lib/tasks/*.rake').each { |r| load r}

Now any .rake file under lib/tasks will be loaded.

like image 84
Maurício Linhares Avatar answered Oct 11 '22 06:10

Maurício Linhares