Rake VS. Thor for Automation Scripts

Rake vs. Thor for automation scripts?

Rake and Thor serve different purposes.

Rake is a general build script tool that is project-specific. In other words, you put your rakefile into your project folder and in your project's source control, and you can create, build and do other automation tasks that are specific to your project in that rakefile. Rake requires a rakefile to run.

Thor is a general purpose command line scripting tool that makes it very easy to re-use scripts across many projects and to do project setup, etc., like you are suggesting. Thor allows you to "install" an executable script that you can call from anywhere on your system, similar to calling "ruby", "gem" or "rake" command lines. However, Thor's scripts are more suited to general purpose, cross-application automation because the Thor script does not rely on a file sitting in your project-specific folder. A Thor script is the entire script, packed and installed for re-use anywhere.

Based on your stated needs, you are better off using Thor because you will be able to install your script in one location and have it work anywhere on your system. You will not be bound to where a Rake file is sitting or anything like that.

By the way, Rails 3 uses Thor for pretty much everything that is not project specific. You still have a Rake file and you still run things like "rake db:migrate" or "rake test:units". Thor is used for things like "rails new ...", "rails server" and "rails generate ..." The use of Thor AND Rake in Rails 3 is the perfect illustration of where each of these tools is best suited.

Rake task in Thor script ruby

The rails generator api actually provides a rake method, and is very easy to use. So for example your generator file could look like:

class RakeTestGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

def rake_db
rake("db:migrate")
end
end

You could then execute this within your rails app by running the following.

rails g rake_test

Which would be the equivalent of running "rake db:migrate" in the command line. Note that all publicly defined methods in a rails generator are executed when the command is run.

Additional info: The rake method is provided by Rails::Generators::Actions module and is available by the Rails::Generators::Base class. See the Official Documentation for more information.

Boson vs Thor for console applications

Disclaimer: I'm the author of boson.

I've used both and thor was what inspired me to write boson. While the two have overlapping functionality, I see them as having different goals.

Thor is a scripting framework which quickly and beautifully gives applications a commandline interface. The 116 gems (including rails) that depend on it are good evidence of that. Initially I tried using thor to manage and use snippets but after awhile, the forced namespacing, the lack of aliasing, writing redundant usage lines, and poor searching, made me realize thor wasn't optimized to manage snippets.

So I wrote boson to manage the endless number of ruby snippets I used to put in ~/bin with this philosophy in mind. At 400+ commands, I'm able to instantly find and use any ruby snippet as a full-blown executable. There are too many features to go over here, though you seem to know some of boson's strengths. As for being the sole contributor, I welcome anyone to contribute their ideas.

If there was one simple comparison to make between the two, I'd say thor is centered around creating executables for projects and apps while boson is centered around creating them for users.

Using Rake or Gem for building scripts?

Going with rake or a gem is fine. If you want to centralize your rake tasks (assuming you are on OSX or Some Linux/*nix variant) you can create them in your home directory:

~/.rake/*.rake

Rake will look there for tasks to run if in a directory w/ no Rakefile.

Also, consider (again, if you are on some sort of *nix platform) just creating shell aliases to your commands.

Edit:

Another consideration specific to your Rails work is to leverage Application Templates. Here is a link to a good screencast.

Use Capistrano for Rake tasks?

A few attempts have been made at making capistrano more universal - capistrano-boss and my own capistrano-provisioning, for example - it may be that somebody has already done it. Certainly, in terms of running commands remotely and organising tasks, namespaces, hooks etc, capistrano is second-to-none. But I've yet to see a comprehensive solution that allows you to really administer remote systems using it.

Have you investigated chef? It seems that this is frequently used in conjunction with cap. 37signals, for example, have extensive chef recipes available - and they were also the source of capistrano.

Can someone provide an example of Thor::HiddenTask usage?

In thor/spec/fixtures/script.thor you can find such usage:

desc "hidden TYPE", "this is hidden", :hide => true
def hidden(type)
[type]
end

Building applications with multiple Rake files

In WebYaST, the main Rakefile runs tasks from all subprojects, but in a separate rake process, via system.



Related Topics



Leave a reply



Submit