DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Quick-Start Rails Server Wherever You Are

08.11.2008
| 5545 views |
  • submit to reddit
        Set a workspace directory, and you can use this script to quickly launch any Rails application on port 80 by only typing the first few letters, wherever you are.

Eg. "rlaunch r" will find a project in your workspace directory that starts with "r", in this case "rails_is_awesome".

~$ rlaunch r
Loading rails_is_awesome...
** Starting Mongrel listening at 0.0.0.0:80
** Starting Rails with development environment...
** Rails loaded.

If you have more than one that starts with r, it'll show you all available projects that start with that pattern.

~$ rlaunch r
rails_is_awesome
rad_site 

Save as rlaunch, then "chmod u+x rlaunch" and put it somewhere in your path.

#!/usr/bin/env ruby
require 'abbrev'

WORKSPACE = "/Users/#{ENV['USER']}/Workspace"

begin
  entries = []
  Dir.entries(WORKSPACE).each do |f|
    entries << f if File.directory?("#{WORKSPACE}/#{f}")
  end
  entries.slice!(0, 2)
  entries= entries.abbrev
  if entries[ARGV[0]].nil?
    alternatives = []
    entries.each do |k, v|
      alternatives << v if k =~ /^#{ARGV[0]}/
    end
    alternatives.uniq!
    puts alternatives.empty? ? "No projects start with #{ARGV[0]}." : alternatives
    exit
  end
  puts "Loading #{entries[ARGV[0]]}..."
  Dir.chdir("#{WORKSPACE}/#{entries[ARGV[0]]}")
  `sudo script/server -p 80`
rescue
  puts "An error occurred."
end