How do I get a custom Rake task to run in Sinatra? - ruby-on-rails

*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)

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.

Related

Rake -T does not identify tasks

I am trying to setup a Rake Task (without rails), and thus created a demo Hello World Task. However, upon running rake -T or rake --tasks nothing is returned, as if the task hasn't been identified. What am I doing wrong here?
Also When I run rake hello_world I get the following error:
➜ ~/GitHub/Cerner/test git:(master) ✗ rake hello_world
rake aborted!
Don't know how to build task 'hello_world' (See the list of available tasks with `rake --tasks`)
/usr/share/rvm/gems/ruby-2.6.6/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/hsbagga28/gems/bin/ruby_executable_hooks:22:in `eval'
/home/hsbagga28/gems/bin/ruby_executable_hooks:22:in `<main>'
(See full trace by running task with --trace)
[Update] The rakefile:
# frozen_string_literal: true
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
$LOAD_PATH.push File.expand_path('lib', __dir__)

Rake doesn't know how to build task 'db:migrate'

I've created an app using Ruby on Rails and Spree.
After changing my SQlite database to PostgreSQL I needed to migrate my database.
But when I try to run rake db:migrate, rake db:migrate RAILS_ENV=development, bin/rake db:migrate RAILS_ENV=development I get the same error.
rake aborted!
Don't know how to build task 'db:migrate:up' (see --tasks)
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
When I run --trace I get the following response
** Invoke default (first_time)
** Invoke spec (first_time)
** Execute spec
/usr/local/rvm/rubies/ruby-2.3.0/bin/ruby -I/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.7.0/lib:/usr/local/rvm/gems/ruby-2.3.0/gems/rspec-support-3.7.0/lib /usr/local/rvm/gems/ruby-2.3.0/gems/rspec-core-3.7.0/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
Could not load dummy application. Please ensure you have run `bundle exec rake test_app`
** Execute default
Does any of you know what the cause of the problem is and what I should do?
Rakefile
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rspec/core/rake_task'
require 'spree/testing_support/common_rake'
RSpec::Core::RakeTask.new
task default: :spec
desc "Generates a dummy app for testing"
task :test_app do
ENV['LIB_NAME'] = 'spree/frontend'
Rake::Task['common:test_app'].invoke
end
I added,
require File.expand_path('../config/application', __FILE__)
and
YouApp::Application.load_tasks
to my rake file.
Credits go to #nattfodd

rails aborted! Don't know how to build task 'task_name'

I was creating a quick one-off task:
require 'yaml'
task generate_permissions_yaml: :environment do
permissions = []
Permission.order(:title).each do |permission|
permissions << {
title: permission.title,
code: permission.code,
description: permission.description
}
end
puts permissions.to_yaml
end
When I ran it with rails generate_permissions_yaml, I got this error:
rails aborted!
Don't know how to build task 'generate_permissions_yaml' (see --tasks)
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/bundle/gems/railties-5.0.2/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/bundle/gems/railties-5.0.2/lib/rails/commands.rb:18:in `<top (required)>'
As it turns out, it was due to the way that I named the file. It should have a .rake extension, not .rb.
I renamed this:
lib/tasks/generate_permissions_yaml.rb
To this:
lib/tasks/generate_permissions_yaml.rake
I uncovered this after trying to run rails g task generate_permissions_yaml and seeing that it generated a file with a .rake extension.
For newbies - this is a common error if you use a space instead of a colon in a rake command. For example if you enter:
rake db migrate
instead of:
rake db:migrate
The correct format has a colon, not a space

How do I call another task in Rake

Although this might sound similar to the other questions you find here, there is a slight twist. I have two directories, say /home/rails/Rake and /home/rails/test_app. The rails directory is where I place all my rails projects.
Inside Rake, I have a Rakefile and a create.rake file.
This is what my rakefile look's like
namespace :setup do
desc "something"
task :init do
print "Name of the destination directory: "
name = STDIN.gets.strip
cp_r '.', "../#{name}/lib/tasks"
cd "../#{name}"
sh "rake setup:create"
end
end
And create.rake
namespace :setup do
desc "Install"
task :create do
sh 'git init'
#some other code
end
end
What it does is obvious. I want to copy the contents of the Rake directory to /test_app/lib/tasks. Then change directory to test_app and run setup:create task defined in the install.rake file now placed in test_app/lib/tasks. This works, but is this the rake way of doing it? Can anyone give me a slight hint of how it's done, the Rake way.
Here is the error which I get when I used invoke method:
$ rake setup:init
Name of the destination directory:
testapp
cp -r . ../testapp/lib/tasks
cd ../testapp
rake aborted!
Don't know how to build task 'setup:create'
/home/TradeRaider/rails/Rake/Rakefile:8:in `block (2 levels) in <top (required)>'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/TradeRaider/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => setup:init
(See full trace by running task with --trace)
This is more rake-ish :)
Rake::Task["setup:create"].invoke
Although #apneadiving answer helped, it just struck me that I was trying to call a Rakefile from another Rakefile, literally speaking. Anyways, to do so, I had to first load the rake file,
load "../#{name}/lib/tasks/create.rake"
(requiring it will also do the trick)
and then invoke it.
Rake::Task["setup:create"].invoke

Rails 3 rake task can't find model in production

My simple rake task, stored in lib/tasks/items_spider.rake runs just fine in development. All it does is call spider! on the Item model.
namespace :items do
desc "Spider the web for data, hoorah"
task :spider => :environment do
Item.spider!
end
end
I have the :environment task as a dependency, so everything works just fine. However, when I add RAILS_ENV=production, I hit errors, both on my local server and the production server:
$ rake items:spider RAILS_ENV=production --trace
(in /home/matchu/Websites/my-rails-app)
** Invoke items:spider (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute items:spider
rake aborted!
uninitialized constant Object::Item
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rspec-core-2.0.0.beta.22/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rspec-expectations-2.0.0.beta.22/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
/home/matchu/Websites/openneo-impress-items/lib/tasks/items_spider.rake:4:in `block (2 levels) in <top (required)>'
/home/matchu/.rvm/gems/ruby-1.9.2-preview3#rails3/gems/rake-0.8.7/lib/rake.rb:636:in `call'
[...trace of how rake gets to my task...]
This just seems odd to me. Apparently the models have not been loaded correctly. I'm on Rails 3.0.3, though development on this app started back when Rails 3 was in beta. How can I go about debugging this issue? Thanks!
Contrary to running your application in production, a Rake task does not eager load your entire code base. You can see it in the source:
module Rails
class Application
module Finisher
# ...
initializer :eager_load! do
if config.cache_classes && !$rails_rake_task
ActiveSupport.run_load_hooks(:before_eager_load, self)
eager_load!
end
end
# ...
end
end
end
So only if $rails_rake_task is false, will the application be eager-loaded in production. And $rails_rake_task is set to true in the :environment Rake task.
The easiest workaround is to simply require the model that you need. However, if you really need all of your application to be loaded in the Rake task, it is quite simple to load it:
Rails.application.eager_load!
The reason all of this work in development is because Rails autoloads your models in development mode. This also works from within a Rake task.
In your environment/production.rb, you should add the following:
config.dependency_loading = true if $rails_rake_task
It solved the problem for me.
(Note: this should be added AFTER the config.threadsafe! call)
The same thing happens to me in Rails 6.0.3.2.
I was trying to configure the rake file task :foo do ... end. Instead it should've been task foo: :environment do ... end.
Just found another one: I was developing on windows, deploying to Heroku. The web app and rails console worked fine, but rake tasks and even direct require could not load the model. Turned out I had absentmindedly created the model file as Model.rb instead of model.rb - system dependent case sensitivity.

Resources