Skip to content

Instantly share code, notes, and snippets.

@mikew
Created October 2, 2009 00:58
Show Gist options
  • Save mikew/199358 to your computer and use it in GitHub Desktop.
Save mikew/199358 to your computer and use it in GitHub Desktop.
Helper script to open rails projects
# ...
workon () {
OLDIFS=$IFS
IFS=$'\n'
output=$(workon.rb $1)
for line in $output; do
echo $line
done
eval $line
IFS=$OLDIFS
}
#!/usr/bin/env ruby
# Set to the root of your work directories
WORK_DIR = "/Users/mike/Work"
# Array of relevant sub directories ("languages")
WORK_DIRS = %w(build html php rails)
Dir.chdir WORK_DIR
@potentials = []
WORK_DIRS.each do |language|
Dir.foreach(language) do |dir|
path = File.join WORK_DIR, language, dir
if dir == ARGV[0]
next if ARGV[1] && ARGV[1] != language
@potentials << { :language => language, :path => path }
end
end
end
def run(command, *arguments)
callback = arguments.last.is_a?(Proc) ? arguments.pop : lambda {}
puts "Running #{command}"
Kernel.system "#{command}", *arguments
callback.call
end
def commit(to)
destination = to[:path]
host = "#{ARGV[0]}.local"
puts "# Using #{to[:path]}"
run "open #{destination}"
run "mate #{destination}"
puts
puts "# Ping to #{host}"
run "ping -c 1 #{host} &> /dev/null", lambda { run "open http://#{host}" if $? == 0 }
puts
puts "# Next Step ..."
puts "cd #{destination}"
end
def determine_language
range = 2..@potentials.length
case @potentials.length
when range
puts "Which language did you mean?"
@potentials.each_index do |i|
key = i == 0 ? "#{i.next}*:" : "#{i.next}:"
puts " #{key} #{@potentials[i][:language]}"
end
index = $stdin.gets.to_i
commit @potentials[index == 0 ? 0 : index - 1]
when 1
commit @potentials.first
else
puts "you fool!"
end
end
determine_language
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment