Skip to content

Instantly share code, notes, and snippets.

@benpickles
Created December 22, 2009 18:55
Show Gist options
  • Save benpickles/261940 to your computer and use it in GitHub Desktop.
Save benpickles/261940 to your computer and use it in GitHub Desktop.
Framework-aware console/server/database scripts.
#!/usr/bin/env ruby
require 'pathname'
require 'yaml'
conf = Pathname.new(Dir.pwd).join('config', 'database.yml')
if conf.exist?
env = ARGV.first || ENV['RAILS_ENV'] || 'development'
yaml = YAML.load(conf.read)
config = yaml[env] || yaml[env.to_sym]
database = config['database'] || config[:database]
username = config['username'] || config[:username]
password = config['password'] || config[:password]
cmd = "mysql -u#{username} "
cmd << "-p#{password} " if password
cmd << database
puts "Opening database #{database}"
exec cmd
end
puts "Couldn't detect database"
exit 1
#!/usr/bin/env ruby
require 'pathname'
pwd = Pathname.new(Dir.pwd)
bin = if pwd.join('script', 'rails').exist?
'./script/rails console'
elsif pwd.join('script', 'console').exist?
'./script/console'
elsif pwd.join('schema', 'schema.rb').exist?
'merb -i'
elsif pwd.join('config.ru').exist?
if `which racksh`.chomp == ''
puts 'Use racksh to a console to any Rack app:'
puts ' gem install racksh --source http://gemcutter.org'
exit
else
'racksh'
end
else
puts "Couldn't detect framework"
exit 1
end
bin = "bundle exec #{bin}" if pwd.join('Gemfile').exist?
command = "#{bin} #{ARGV.join(' ')}"
puts command
exec command
#!/usr/bin/env ruby
require 'pathname'
pwd = Pathname.new(Dir.pwd)
bin = if pwd.join('script', 'rails').exist?
'./script/rails server'
elsif pwd.join('script', 'server').exist?
'./script/server'
elsif pwd.join('config.ru').exist?
'rackup -p 3000'
elsif pwd.join('schema', 'schema.rb').exist?
'merb'
else
if `which thin`.chomp == ''
puts 'Use Thin to quickly serve static files:'
puts ' gem install thin'
exit 1
else
puts 'No framework detected, using Thin to quickly serve static files.'
'thin start -A file -p 8080'
end
end
bin = "bundle exec #{bin}" if pwd.join('Gemfile').exist?
command = "#{bin} #{ARGV.join(' ')}"
puts command
exec command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment