How to add a Rake task to the default Rake task? - ruby

Somehow Rspec and Cucumber are making it into my default rake task (which is fine because I want them there). But I have tried adding additional tasks to the default task and it has no effect.
What is the proper way to add tasks to the default rake task?

Typically your Rakefile will have something like this:
task :default => [:spec]
You just need to add more tasks into this list.

The answer from davogenes is correct for non-namespaced rake tasks.
If you want to have a default task in namespaces you need to do the following
namespace :mynamespace do
desc "This should be the default"
task :mydefault do
Do.something_by_default
end
desc "Another task in this namespace"
task :other do
Do.something
end
end
desc "This is the default task of mynamespace"
task mynamespace: 'mynamespace:mydefault'
Then you can run rake mynamespace as well as rake mynamespace:other or rake mynamespace:mydefault.

Related

execute task in .rb file

I'm working on a project that's been in development for quite a while now. I'm trying to figure out how everything works and my next step was how to update/fill the 'library' as it's called in the project.
I found several .rake files hidden away somewhere, but they are used in another .rb file. In said .rb file the entire logic behind the several tasks is set up, so everything happens in the right order.
Now, here's my problem: I need to use the tasks in the .rb file in order to generate the library. They're nested in namespaces and I can't figure out how to reach the tasks.
Here's a shortened version of the structure in the library.rb file:
namespace :library do
task :something do
...
end
...
namespace :local do
...
namespace :generate do
task :default do
...
end
end
end
end
I want to reach the task :default.
Thanks in advance for any help or advice.
EDIT:
The command rake library:local:generate:default gives an error:
rake aborted!
Don't know how to build task 'library:local:generate:default'
EDIT: I can't change the file's names, extentions or locations. Library.rb is currently located in config/deploy/
I'm assuming you would like to run the rake task from the command line, in which case you would enter rake library:local:generate:default.
You also need to require your library.rb file to make the task available. You can do that with the -f flag on the rake command. So, for example: rake -f path/to/library.rb library:local:generate:default.
A fast solution could be rename the file with .rake extension and put the file under lib/task so you can call it with rake namespace:task
namespace :library do
task :something do
...
end
...
namespace :local do
...
namespace :generate do
task default: :environment do
...
end
end
end
end
You forgot to add environment to it. Try the same rake task command again:
rake library:local:generate:default
This might help you

How can I run features within a rake task in parallel?

Normally I run my tests with parallel_cucumber which runs different features in parallel using parallel_test gem. I want to setup a rake task that will run using different profiles and run the features within each task in parallel.
I have setup my Rakefile this way:
namespace :features do
Cucumber::Rake::Task.new(:basket) do |t|
t.profile = "basket"
end
Cucumber::Rake::Task.new(:fruits) do |t|
t.profile = "fruits"
end
Cucumber::Rake::Task.new(:veggies) do |t|
t.profile = "veggies"
end
task :all => [:basket, :fruits, :veggies]
end
When I run "rake features:all" it will run each task in sequence(as expected/desired) but will run the features within each task one at a time(not desired). I would like to keep each task running in sequence but would like the features within each task to run in parallel. Is this possible? If not is there a way this can be done?
As always your help is much appreciated.
Rake offers multitask. You could either
Changing task to multitask in the rakefile:
multitask :all => [:basket, :fruits, :veggies]
use -m option in the command line:
rake -m features:all
Have a look at the parallel_tests gem. It allows you to run your features in parallel. While the typical use case is to run all of your features, you are able to run specific rake tasks in parallel. See the documentation for more details.

How can you pass in arguments to rake tasks called using Heroku Scheduler?

Rake tasks scheduled in Heroku found online are in the "rake task-name" format. Heroku passes in an :environment argument to the rake task, but there is no documentation about extra args being passed to a task listed in scheduler.rake
Heroku Scheduler Doc
You may try this:
task :my_task, [:arg1, :arg2] => :environment do |t, args|
puts "Args were: #{args}"
end
and then run manually to see the output:
heroku run rake my_task
UPDATE:
this answer can be an old one already
Instead of rake task_name input rake task_name[arg1, arg2] to the string input accepted by the Task column in the Heroku Scheduler main screen.
As mentioned by CodeGroover, your rake task defined in lib/scheduler.rake should define the parameters it is expecting.

Access default_url_options from rake task

In my application.rb file I have a few options to modify my apps URLs like so:
config.action_controller.default_url_options = { :trailing_slash => true }
But these do not seem to take effect in my Rake tasks, despite the fact that I am running them in the Rails environment via the :environment dependency.
I know that I can get this to work simply by calling the following in my rake task:
default_url_options[:trailing_slash] = true
... but I'd like to DRY this up. Is there a clean way to make a rake task use Rails' default_url_options from application.rb?
You can use the following in a rake task (and in Rails console):
Rails.application.config.action_controller.default_url_options

CruiseControl.rb with RSpec: Rake task 'spec' not found

I'm trying to use CruiseControl.rb (ver. 2.0.0pre1) with RSpec for my Ruby on Rails 3 app. The cruise_config.rb for my project looks like this:
Project.configure do |project|
project.rake_task = 'db:migrate db:test:prepare spec'
project.scheduler.polling_interval = 1.hour
project.scheduler.always_build = false
end
But when I try to run a build with CruiseControl, it says:
rake aborted!
Custom rake task(s) 'spec' not defined
Tasks: TOP => cc:build
(See full trace by running task with --trace)
It can't find the spec rake task to run the RSpec tests. I also tried to define a custom rake task inside my Rakefile and removed the project.rake_task = 'db:migrate db:test:prepare spec' line inside the cruise_config.rb:
desc "Custom Task for CruiseControl.rb"
task :cruise do
puts "Custom Rake task"
Rake::Task['db:migrate'].execute
Rake::Task['db:test:prepare'].execute
Rake::Task['spec'].execute
end
If i do so, CruiseControl says
rake aborted!
ActiveRecord::ConnectionNotEstablished
Tasks: TOP => cruise
(See full trace by running task with --trace)
[CruiseControl] Invoking Rake task "cruise"
Custom Rake task
Does anybody have CruiseControl.rb working with RSpec?
Ensure that you have the :spec task defined in your Rakefile, for RSpec 2, it looks like this:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

Resources