+1.647.833.3852 [email protected]

In Rails 2.3, templates are simple ruby files containing DSL for adding plugins, gems, initializers, etc. to your freshly created Rails project. To apply the template, you need to provide the rails generator with location of the template you wish to apply, using -m option.

Reading up on the commands in the template system and watching the railscasts.com episode, I got fired up on the possibilities of such a simple yet amazing system. So I dove in and started creating my base template. I was so excited that I write the whole template out in one go and than ran it. The result: FAIL!

After including some gems, I ran a rake "gems:install" and ended up with an Exec format error. I googled the problem and found that mostly Windows users were getting this error and the recommend solution was to use a rake.bat (or rake.cmd) instead of the rake command from ruby. So I wrote my own function:

#HELPERS
def win_env?
RUBY_PLATFORM=~ /win32/
end

def rake(command, options = {})
env = options[:env] || 'development'
log 'rake', "#{command} in #{env} "
sudo = win_env? ? '' : (options[:sudo] ? 'sudo' : '')
rake_cmd = win_env? ? "rake.bat" : 'rake'
in_root { run("#{sudo} #{rake_cmd} #{command} RAILS_ENV=#{env}", false) }
end

Another thing that I was able to fix is the “Sudo” option, works great on UNIX systems but not so great in Windows. I haven’t been able to test it but I’m pretty sure this will work on a UNIX system without any issues.

All worked out great till I ran run "cucumber features -n" and got the exact same error! Lucky there was a .bat version of cucmber command so I was able to easily fix it by calling run "cucumber#{win_env? ? '.bat': ''} features -n"

One more small issue I found in Windows when making a template was in the git command. When doing a commit you need to use double quotes inside the commit message otherwise you get an error: pathspec 'commit'' did not match any file(s) known to git.

If you’re interested in downloading my template, you can download it from here

If you really want to see the power of template system I recommend you read the template_runner.rb in the rails folder. Enjoy and have fun with it!