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
Nicolás 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.



Why not just put the barebones Rails app into a git repository then clone it for each project?
You might be better off creating a "clean app" to clone off of.
It's less error prone in the long run.
Sean, engtech, those solutions miss his first step: build a brand new app with all the edge defaults.
yes, we talked about creating a "base" app that has our own flavor stuff but it wouldn't work out. you'd have to always update it and manage your base app.
Writing a script that just puts the pieces together solves the problem in a much more sexy manner.
That's the idea w/ the base app in a git repo. You update it in one spot, and all your child apps can pull from it.
Also, do we really need another rake?
Meh. Why do people have to criticize? He's just sharing a good little script he uses that makes his life easier!
I love the idea. Good job - thanks for sharing sir.
Say Hi to Daniel and Will for me will you.
Christian
Nice work, much more pleasant than my kind of rubbish list of bash functions. I think HAML is better to fetch from its git repo though and isn't db/schema.rb now recommended to be versioned?
Btw if you are on Mac OS X and don't like seeing the ~/bin directory in your Finder you can run this command
/Developer/Tools/SetFile -a V ~/bin
and Finder won't show it anymore. Does require XCode but then so does MacPorts so you are probably likely to have it already.
Wow, very nice and clean script!
I couldn't get braid to work with git repositories though.
end
Now, I'm very new to git, so I don't know if what braid added to .git/config is actually right.
Anybody got this script to work with plugins in git repos?
Thanks
Mathijs: Does using braid manually work for you? The way braid works is by creating its own branch of what its tracking. You then merge the braid/track branch into your master branch.
I've admittedly not had a chance to actually test out this script, works been keeping me too busy to run a simple script. How bad is that...
I have now tried it and yes it works fine for me with git repositories
Great script!
I'm a little confused about a couple of things -
I don't quite understand the need (or use) of braid. If I want to freeze edge rails why don't I just do rake rails:freeze:edge ?
Also, is braid just another way of doing a script/plugin install ? If so, could I use script/plugin install git://git_project (since this is enabled in edge rails)
Chris
Mathijs: I've started noticing braid hanging on some git repositories while doing a git-index-pack. I can't quite work out why as it works fine on the command line.
Chris: rake rails:freeze:edge would do roughly the same thing if you kept updating it and committing all the differences between your last freeze. Again yes its a bit like plugin install but you keep the links to the plugins in your repository and your braid/track branch. So you can have a bit more control over what version of rails or a plugin you've frozen to. Makes working with remote repositories less of a moving target.
Thanks Geoff, I'm starting to see its value now :) However, I'm having a problem getting it to work ->
braid add git://github.com/technoweenie/permalinkfu.git --type git vendor/plugins/permalinkfu
gives me:
braid: Checking out work branch 'braid/track'. braid: Adding git mirror from 'git://github.com/technoweenie/permalinkfu.git', branch 'master' into 'vendor/plugins/permalinkfu' using local branch 'braid/git/vendor/plugins/permalink-fu/master'. braid: Setting up remote branch and fetching data. braid: Error occured: fatal: 'braid/git/vendor/plugins/permalink-fu/master': unable to chdir or not a git archive fatal: The remote end hung up unexpectedly Cannot get the repository state from braid/git/vendor/plugins/permalink-fu/master fetch braid/git/vendor/plugins/permalink-fu/master: command returned error: 1 braid: Resetting braid/track to 26218373c99ccf12c5dd1779d5ccab74156a4b70. braid: Checking out branch 'master'. braid: An exception has occured: fatal: 'braid/git/vendor/plugins/permalink-fu/master': unable to chdir or not a git archive fatal: The remote end hung up unexpectedly Cannot get the repository state from braid/git/vendor/plugins/permalink-fu/master fetch braid/git/vendor/plugins/permalink-fu/master: command returned error: 1 (fatal: 'braid/git/vendor/plugins/permalink-fu/master': unable to chdir or not a git archive fatal: The remote end hung up unexpectedly Cannot get the repository state from braid/git/vendor/plugins/permalink-fu/master fetch braid/git/vendor/plugins/permalink-fu/master: command returned error: 1)
sorry, ignore that last post - I forgot to put quotes around git.
Really Really newbie question here but I've been teaching myself all of this. I understand exactly what this does and how it does it. Very clean/nifty. But what steps do you do to use the script. Not how do you get it, you download it with git. But where do you put it if anywhere to run on your os. Or should we just symlink it. And also is it just railify your_app? I know, its really sad that I don't know this.
Well I just copied and pasted, added a couple extra bits I normally want, saved the file to ~/bin/railify, set ~/bin/railify executable and all was dandy.
Basically it just needs to be somewhere in your shells $PATH, you could put it in /usr/bin if you felt like it.
Then just use like
prompt> railify app_name
and leave it to finish. If it hangs for a while on a git repo check to see if git-index-pack is running. For some reason braid has issues with that, I've poked around but not found a fix for it.
I've seen the braid git-index-pack hang on the rspec, rspec-rails and rails git repositories.
I might try taking a look at git submodule but I don't know if I can use this to track svn repositories in git as well.
Hi Geoff,
Yes, I have the same problem with braid - when trying to install rspec/rspec-on-rails it's hanging on git-index-pack.
Sorry, comments are closed for this article.