Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake vs. Thor for automation scripts?

Tags:

ruby

rake

thor

I want to automate things like:

  • Creating a new Ruby on Rails application with pre-selected database, Git initialize it, create a Heroku project, commit all files, etc.
  • Upload all files in folder to another computer through SSH, but do not overwrite files.
  • Upgrade Ubuntu, install all basic packages through apt-get.

From what I understand, tools for this are Rake and Thor, however, which one should I use?

Rake seems to me more de-facto and popular. I have heard people recommending Thor.

How do these stand to each other in a rundown?

like image 226
never_had_a_name Avatar asked Aug 19 '10 17:08

never_had_a_name


People also ask

When would you use a rake task?

Rake allows you to define a list of other tasks that must run before the current task.

Why using Rake?

Rake utility allows you to create a job/task which uses rails environment. So say, you want to count the votes a user has given to an article and save it somewhere. You write a rake job, in which you can use Rails models and other helpers and get it done without going away from Rails.

What is rake file in Ruby?

Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax. Rake has the following features: Rakefiles (rake's version of Makefiles) are completely defined in standard Ruby syntax.


2 Answers

For setting up Ubuntu chores, Chef might be a better option.

From their web site:

Chef is an open source systems integration framework, built to bring the benefits of server configuration management to your entire infrastructure.

It's written in Ruby and there are tons of Chef recipes/cookbooks. Chef will handle setting up Ubuntu and installing packages, servers, etc.

I don't know if you are working with virtual machines, but Vagrant will set up a virtual machine and then use Chef to configure it.

like image 23
Tom Corbin Avatar answered Sep 23 '22 23:09

Tom Corbin


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.

like image 53
Derick Bailey Avatar answered Sep 23 '22 23:09

Derick Bailey