As I write this the tenth week in GSoC 2016 is over and it’s the end of the month now. I am a little late with my timeline mostly because I am doing work on the web interface for some pages and things need to be changed over and over to fit the design choices. But in the mean time I got to learn something new. We had to run two separate rake tasks for running rails tests and JavaScript tests. So there was an issue to run all tasks with one rake task for ease. This is where I learnt about creating custom Rake tasks.
Rake is a Make-like program implemented in Ruby. It comes as a Rubygem and has to be included in your Gemfile during development. Custom Rake tasks start with a namespace followed by the task name separated by a colon. They reside in the lib/tasks
directory of a rails app and they go in a file with a .rake extension.
Here is the railsguides documentation on How to create Custom Rake tasks. Here is this good blog post by ANDREY KOLESHKO while I learnt about writing rake tasks. As you can see there you can either directly write the task in the namespace.rake
(remeber to name the file same as the namespace ) file in the lib/tasks
directory or you can use the rails generator to create the task file
$ rails g task my_namespace my_task
This will create a lib/tasks/my_namespace.rake
file where you can write your task.
I needed to run the two tasks rake test
and rake spec:javascript
from this task. So basically I had to run rake tasks from within a rake task. Here is a stackoverflow answer that answers it perfectly! Know the difference between execute
, invoke
and reenable
.
Here is a custom task I wrote. It can run using rake test:all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rake test:all | |
namespace :test do | |
desc "Run rails and jasmine tests" | |
task :all do | |
puts "Running Rails tests" | |
Rake::Task["test"].execute | |
puts "Running jasmine tests headlessly" | |
Rake::Task["spec:javascript"].execute | |
end | |
end |
You can find this in the commit e90fe of plots2. I would be probably posting about the work on design changes in my next post.