Ruby: run rake task to execute ruby scripts - ruby

I have a bunch of Ruby scripts and I'd like to start them with a Rake task.
A simplified version to illustrate my issue:
export_stats.rake:
desc 'Export statistics'
task :export_stats do
puts "executing: export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
ruby "export_stats.rb #{START_MONTH} #{END_MONTH} #{OUTPUT} #{ENVIRONMENT}"
end
rake aborted! elk-stack/export_stats.rake Don't know how to build task
'export_stats'
the export_stats.rb file is in same directory with export_stats.rake
the rake gem is installed and if I run
rake export_stats
I get an error:
rake aborted!
Don't know how to build task 'export_stats'
What am I missing?

If I understand you correctly you have a folder with some ruby scripts and you are trying to run a rake task that is located in the same folder. I assume you are not using any application framework like Rails (because you did tag the question only with "Ruby").
Do you have a Rakefile in same directory? If so does it contain a statement to load the specific files to run?
# Rakefile
#!/usr/bin/env rake
load 'export_stats.rake'

You have a typo.
The code says export_stats, and the error says exports_stats.
There's an extra s.
Read the error message carefully! ;)

Related

How do I run Rails/Rake from another directory?

Without cd-ing into the root directory of my Rails application, how can I execute a Rails or Rake command for that application.
I tried:
bundle exec rake my_tasks:do_stuff BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile
among other combinations, to no avail.
[Update]
The issue is actually two-fold, bundle doesn't know where the gemfile is and rake doesn't know what to run.
To use Bundler:
BUNDLE_GEMFILE=/PATH/TO/RAILS_APP/Gemfile bundle exec ...
Note that BUNDLE_GEMFILE has to go before 'bundle exec'.
To use Rake:
rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
To use Rails console:
????
Have yet to figure out how to enter the Rails console from another directory. Looking at the source, I think it may not be possible, because it eventually does File.join('script','rails') to kick off the rails process.
Without you showing the error message you are getting, I'm assuming it has less to do with Bundler than it does Rake. When the rake command is run, it searches for a Rakefile starting in the current directory and traversing up the tree until it finds one. You can override this behavior by explicitly specifying a Rakefile in the options to the rake command. This is done using the -f <RAKEFILE> option.
eg.
bundle exec rake -f /PATH/TO/RAILS_APP/Rakefile my_task:do_stuff
Bear in mind that your Rake tasks have to be "CWD agnostic". Most tasks and scripts are as they tend to get the project directory based on a path relative to a known file in the directory tree. You probably already understand that, but it's worth mentioning in case the tasks are expecting the current working directory to actually be the rails root. That would be a case where running them from outside the project could potentially be dangerous.

travis.ci keeps on failing when running "bundle exec rake"

I am trying to get automated testing sorted out with Travis.ci. However, at the moment the build keeps on failing when trying to execute bundle exec rake.
This is what I see...
$ bundle exec rake
rake aborted!
Don't know how to build task 'default'
/home/travis/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/home/travis/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
The command "bundle exec rake" exited with 1.
Done. Your build exited with 1.
My unit tests are in the test folder in the main directory and is named test_np_search.rb. I understand that I am somehow supposed to point travis to this location in order to run the unit tests but I have no idea how to do this.
I have read the ruby related documentation on travis.ci a number of times and have looked online for tutorials, however I have been unable to get this to work.
The whole github repository in question is here: https://github.com/IsmailM/NeuroPeptideSearch
The Travis.CI link is here: https://travis-ci.org/IsmailM/NeuroPeptideSearch
I have been trying to get this sorted for over a week now with any success and so would be highly grateful if anyone could help me.
Many Thank
if you want to execute bundle exec rake on travis, you will have to make sure, that it runs on your local machine!
if you call rake without providing a task-name, it will assume that you want to run the default-task.
if you want to run your minitest testsuite as a default-task, you have to do this:
require "rake/testtask"
Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
end
task default: :test

How can I speed up Ruby/Rake task

rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks
Try spring
Command line will look like:
spring rake -T
It will take more time running the first time, but subsequent runs will be very fast.
This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a lib/tasks/no_rails directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.
I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the -f rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.
For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like require File.expand_path('../config/application', __FILE__), you can do:
$ rake -f Rakefile spec
and it should run your spec task much faster. Try $ time rake -f Rakefile -T; I did this with a rails-independent Rakefile of mine and got:
real 0m1.543s
user 0m1.308s
sys 0m0.201s
The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like rake db:migrate.
The entire rails environment has to be loaded, therefore even simple rake tasks such as rake --tasks take a while. Opening a console with rails console or script/console takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.

Aptana 3 executing a rake task from a ruble command

Im all new to this so I have no idea if its possible but all I want to do is execute a rake task from a ruble command in Aptana Studio 3.
The rakefile is located in the project folder and all it does in convert and minimize some sass code.
Anyone got any ideas?
Thanks
I would first look at http://wiki.appcelerator.org/display/tis/A+Simple+Command to get familiar with creating a command in a ruble.
If you wanted to add a rake task to a command in a ruble, I would then look at http://wiki.appcelerator.org/display/tis/Executing+an+External+Command
Inside the invoke block for the command, you can invoke an external rake task like:
rake your_rake_task

How do I create a global rakefile on Windows 7?

I want to be able to create a rakefile on Windows 7 that I can call with the rake -g <taskname> or rake --system <taskname> command. Where do I save the rakefile? I've tried creating a Rake directory under my user directory (c:\users\me\rake), but when I call rake -g hello_world, rake errors out saying it doesn't know how to build the :hello_world task, which makes me think it just can't see the rakefile. Here's what my global rakefile looks like:
require 'rake'
desc "a hello world task"
task :hello_world do
puts "hello from your global rakefile"
end
Figured it out - turns out Rake expects global rakefiles to end in a ".rake" extension. I changed the example above to this:
require 'rake'
desc "a hello world task"
task :hello_world do
puts "hello from {__FILE__}"
end
And saved it as "testing.rake" in my c:\users\me\rake\ directory. I opened up a command prompt to a random directory and ran "rake -g hello_world" and got this output:
C:\Windows\system32>rake -g hello_world
(in C:/Windows/system32)
hello from C:/Users/me/Rake/testing.rake
Rake looks for Rakefile in the current directory unless specified explicitly as in:
rake --rakefile C:\users\me\rake\[rakefile]
Rake on Windows looks for $HOME/Rake/*.rake if the target cannot be found locally. If HOME is not set, then it will use c:\Users\me\Rake as the other responders have indicated.
But, if you happen to have HOME set (as I do, operating in a mixed Cygwin/Windows 7 world), now you know where rake will be expecting to find your global rake files.
Btw, I don't find it necessary to pass the -g flag; Rake seems happy to find the global definitions on its own. I think the only case in which -g is warranted is if you need to be absolutely sure there is no rake task of the same name locally.

Resources