Skip to content

Instantly share code, notes, and snippets.

@dflynn15
Created March 13, 2013 19:50
Show Gist options
  • Save dflynn15/5155505 to your computer and use it in GitHub Desktop.
Save dflynn15/5155505 to your computer and use it in GitHub Desktop.
This is a Rails Application Generator that I commonly use. There are a lot of questions that prompt the user for feedback, and as I continue to develop more gems or Rails generators may be changed. For now it depends on the gem hub (install by running gem install hub).
class AppBuilder < Rails::AppBuilder
# Checks to make sure that hub is installed before continuing
def rakefile
if `which hub`.strip == ""
say "hub", "Please install hub. https://github.com/defunkt/hub"
exit 1
end
end
# Makes the readme use Markdown and pre-populates it with basic information
def readme
@app_name = File.basename(File.expand_path("."))
create_file "README.md", "#{app_name} App\n======================\nDo this first\n-------\n\t$ bundle exec rake db:migrate"
end
# Asks the user basic questions and adds commonly used gems
def gemfile
template "Gemfile"
@generator.gem 'unicorn'
@generator.gem 'newrelic_rpm'
@generator.gem 'foreman', group: [:development, :test]
@generator.gem 'sqlite3', group: [:development, :test]
@generator.gem 'pg', group: [:production]
if yes? "Do you want to include the better_errors gem for debugging?"
@generator.gem 'better_errors'
@generator.gem 'binding_of_caller'
end
end
# Sets up testing based on RSpec and Capybara
def test
if yes? "Do you want to use RSpec to testing?"
rspec = true
@generator.gem 'rspec-rails', group: [:test, :development]
end
if yes? "Do you want to include Capybara for testing?"
capy = true
@generator.gem 'capybara', group: [:test, :development]
@generator.gem 'launchy', group: [:test, :development]
end
if yes? "Do you want to use Guard to monitor and proivde live changes?"
guard = true
@generator.gem 'rb-fsevent', :require => false if RUBY_PLATFORM =~ /darwin/i
@generator.gem 'guard-rspec'
if yes? "Use livereload to refresh CSS live with Guard?"
reload = true
@generator.gem 'guard-livereload'
end
end
run "bundle install"
generate 'rspec:install' if rspec
generate 'guard init livereload' if reload
generate 'guard init rspec' if guard
end
# This is where all the extraneous information goes
def leftovers
# Generate a root controller
if yes? "Do you want to generate a root controller?"
name = ask("What should it be called?").underscore
generate :controller, "#{name} index"
route "root to: '#{name}\#index'"
remove_file "public/index.html"
end
# Initialize Git and ignore files
git :init
append_file ".gitignore", "public/assets/\n*~\n.project\n.DS_Store\n.idea\n*.sublime-project\n*.sublime-workspace"
# Add line into config/application.rb
insert_into_file "config/application.rb", "config.assets.initialize_on_precompile = false", :after => "config.assets.version = '1.0'\n"
# Set config assets compile to true
gsub_file "config/environments/production.rb", "config.assets.compile = false", "config.assets.compile = true"
# Add the rack deflater for Gzip and compression
insert_into_file 'config.ru', "use Rack::Deflater\n", :before => "run"
# Remove the public index.html
remove_file 'public/index.html'
# Create the procfile
create_file "Procfile", "web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb\n# db: postgres -D /usr/local/var/postgres"
# Create the unicorn.rb file used specifically for Heroku apps and the Unicorn gem
create_file "config/unicorn.rb", "worker_processes 3\ntimeout 30\npreload_app true\nafter_fork do |server, worker|\ndefined?(ActiveRecord::Base) and\n\tActiveRecord::Base.establish_connection\nend"
# Curl in the newrelic yaml file (there is a bug with Ruby's open url so curl was used instead)
run 'curl -O https://raw.github.com/newrelic/rpm/master/newrelic.yml'
run 'cp newrelic.yml config/newrelic.yml && rm newrelic.yml'
# Add inital commit
git add: ".", commit: "-m 'initial commit'"
# Add a Git repository if it exists
if yes? "Do you want to create a GitHub Repository?"
github_private = yes? "Create a private repository?"
tried_create_already = false
while (@git_uri = `git config remote.origin.url`.strip) && @git_uri.size == 0
if tried_create_already
@repo_name = ask_wizard "Repository #{@repo_name} already exists. What project name?"
else
@repo_name = File.basename(File.expand_path("."))
end
if github_private
run "hub create #{@repo_name} -p"
else
run "hub create #{@repo_name}"
end
tried_create_already = true
end
# Add Heroku app
if yes? "Add Heroku sites?"
@repo_name = File.basename(File.expand_path("."))
run "heroku apps:create #{@repo_name}"
run "heroku apps:create #{@repo_name}-staging"
run "git remote add staging git@heroku.com:#{@repo_name}-staging"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment