Posts Tagged ‘script’

Creating a new rails app shouldn’t be boring

Monday, March 24th, 2008

For the impatient: grab the script here

You know, rails rocks for quickly prototyping an idea. And I like to think in code, so I tend to start a lot of apps to see if they lead somewhere (which they usually don’t, but that’s not why we are here). The problem with rails is that, even if it makes great points about don’t repeating yourself, and code generation, starting a new app is a tedious and boring process.

  • Update my rails svn checkout
  • Create the app from rails edge
  • Check it into source control
  • Emptying our routes file to get rid of the ugly defaults
  • Apply Dr. Nic’s GemsOnRails to freeze gems into the app.
  • Install rspec
  • Install haml
  • Any other gems or plugins you tend to use always (make-resourceful, sexy-scaffolds, etc)

So, a long while ago I hacked a quick shell script to do some of this for me, using svn and whatnot. I was, of course, not alone. We programmers are a bunch of lazy bastards, for sure :)

Yet times have changed. My script was getting old (I didn’t use haml or make_resourceful back in those days), and I’m using git now. So it was time for a rewrite.

Enter railify

It basically looks like this:

announcing "Fetching EDGE rails" do
  Dir.chdir(RAILS_SVN_CHECKOUT) { silent "svn update" }
end
 
announcing "Creating application layout" do
  silent "ruby #{RAILS_SVN_CHECKOUT}/railties/bin/rails #{app_name}"
end
 
Dir.chdir(app_name) do
  announcing "Setting up rails app" do
    silent "rm README"
    silent "rm public/index.html"
    silent "rm log/*.log"
    silent "rm public/images/rails.png"
    silent "cp config/database.{,sample.}yml"
    silent "rm -r test"
    File.open("config/routes.rb", "w") {|f| f << templates[:routes] }
  end
 
  announcing "Creating databases" do
    rake "db:create"
    rake "db:create", :rails_env => "test"
  end
 
  announcing "Configuring git repo" do
    silent "git init"
    File.open(".gitignore", "w") {|f| f << templates[:gitignore] }
    silent "touch {tmp,log}/.gitignore"
    git "Basic rails app structure"
  end
 
  announcing "Freezing rails" do
    braid "http://dev.rubyonrails.org/svn/rails/trunk", "vendor/rails"
  end
 
  announcing "Installing GemsOnrails" do
    silent "gemsonrails"
    git "Froze GemsOnRails plugin"
  end
 
  announcing "Installing haml" do
    silent "haml --rails ."
    rake "gems:freeze", :gem => "haml"
    git "Froze haml gem and plugin"
  end
 
  announcing "Installing RSpec" do
    braid "http://rspec.rubyforge.org/svn/trunk/rspec", "vendor/plugins/rspec"
    braid "http://rspec.rubyforge.org/svn/trunk/rspec_on_rails", "vendor/plugins/rspec_on_rails"
  end
 
  announcing "Generating RSpec base files" do
    silent "script/generate rspec"
    git "Added RSpec base files"
  end
 
  announcing "Installing make_resourceful" do
    braid "http://svn.hamptoncatlin.com/make_resourceful/trunk", "vendor/plugins/make_resourceful"
  end
end

Doesn’t it look pretty? :) You can check the full source at github.

So how do I make it work?

You need a few things in order for railify to run:

  • First and foremost, git and subversion
  • A checkout of the rails svn trunk (or a stable branch / tag, if you don’t want to run on the edge). The script’ll update this
    and generate the app from there.
  • The rails stack working (ruby, rubygems, a database of choice – I’m using sqlite) DUH!
  • Dr Nic’s GemsOnRails (sudo gem install gemsonrails)
  • haml (sudo gem install haml) *
  • braid, which is like piston for git, but much much more. There’re instructions on
    how to get it working here.
  • An operating system with a real user interface (aka, terminal). Linux, Mac or even CygWin should do, normal Win won’t.

It sounds like a lot, but probably you already have most of that working already.

But it doesn’t do…

The script is pretty simple to hack, so please do it! If there’s this other plugin you absolutely need to install, just add a block at the bottom and use the braid method to install it.

If you don’t want to update a local rails repo and instead just use the rails command, then you can just delete the ‘Fetching EDGE rails’ block, change the next one to run ‘rails #{app_name}’, and then use rake rails:freeze:gems in the ‘Freezing rails’ block.

Etc. You can fork the project at github and do whatever you want with it.

There’s still room for improvement

I have a few things in mind, like adding a reset.css and a barebones application.sass. A little error handling won’t hurt, either (for now it’s rm -rf !$ && !!). Any ideas?

Oh, and last, but not least, the script is released under a WTFP license


Nicolas works at CitrusByte, lives in Montevideo, Uruguay, uses vim and mate randomly and drinks too much caffeine. The stuff he does in mate usually ends up filled with :w.