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
Related
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__)
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
*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.
I have defined a rake task as follows in a file called file_locker_task.rake
namespace :myspace do
task :process => :environment do
FileLocker.lock_files
end
end
How do I execute this rake task from the command line?
I tried:
rake myspace:process and rake process but both are throwing an error like this:
rake aborted!
Don't know how to build task 'process'
Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
According to docs
Any ruby file (including other rakefiles) can be included with a standard Ruby require command.
-
Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.
I've written a rake task to run a few other rake tasks via system (so as to bind ActiveRecord to different databases, among other things). It works fine on my OS X box, but fails on our production Linux boxes with a load error. The tasks trivially boil down to:
namespace :jobs do
task :foo => :environment do
system "rake jobs:bar"
end
task :bar => :environment do
puts "foobar"
end
and the traced output is:
-bash-3.2$ rake jobs:foo --trace
(in /the/path)
** Invoke jobs:foo (first_time)
** Invoke environment (first_time)
** Execute environment
** Erubis 2.6.6
** Execute jobs:foo
/usr/bin/rake:19:in `load': no such file to load -- rake (LoadError)
from /usr/bin/rake:19
I dumped a puts $: into /usr/bin/rake and have discovered something interesting. The primary job has a load path containing both of these paths:
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib
while the secondary job has a load path containing only:
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib
which probably explains the load error, but not the reason for it. Any ideas?
Check to make sure your install has set up the required environment variables correctly.
http://docs.rubygems.org/read/chapter/3
The same problem occurred for me when using "export RUBYOPT=RUBYGEM" instead of "export RUBYOPT=RUBYGEMS". Ahh the difference a single character can make.
If you are really trying to invoke a rake task from another rake task. Why not do this? "Rake::Task['jobs:bar'].invoke". You can even do it in a loop, for instance an Array#each that changes ENV vars, etc. I've done this in tasks before.
Though, if your example was contrived and your not really calling one task but just asking why the sub shell has different PATH setting, that I do not know. Perhaps if it's hard, then it's a hint that it should be done another way.