Skip to content

Instantly share code, notes, and snippets.

@andrewle
Created September 20, 2012 10:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewle/3755035 to your computer and use it in GitHub Desktop.
Save andrewle/3755035 to your computer and use it in GitHub Desktop.
A Rails app generator tuned my common needs
#!/usr/bin/env ruby
require 'rails/generators'
require 'rails/generators/rails/app/app_generator'
require "active_support/core_ext/string/strip"
class CleverAppGenerator < Rails::Generators::AppGenerator
class_option :skip_bundle, default: true
class_option :database, default: "postgresql"
class_option :skip_test_unit, default: true, hide: true
def self.source_root(path=nil)
Rails::Generators::AppGenerator.source_root(path)
end
protected
def self.banner
"./clever-rails #{self.arguments.map(&:usage).join(' ')} [options]"
end
def app_secret
"APP_SECRET"
end
end
class AppBuilder < Rails::AppBuilder
def readme
copy_file "README", "README.md"
end
def gitignore
super
@generator.append_file '.gitignore', <<-DOC.strip_heredoc
/.env
/config/database.yml
DOC
end
def database_yml
template "config/databases/#{options[:database]}.yml", "config/database.example.yml"
template "config/databases/#{options[:database]}.yml", "config/database.yml"
end
def lib
empty_directory "lib"
empty_directory_with_gitkeep "lib/tasks"
empty_directory_with_gitkeep "lib/assets"
end
def public_directory
super
remove_file "public/index.html"
remove_file 'app/assets/images/rails.png'
empty_directory_with_gitkeep "app/assets/images"
end
def gemfile
super
@generator.append_file 'Gemfile', <<-DOC.strip_heredoc
gem "bootstrap-sass"
gem "pjax_rails"
gem "thin"
gem "foreman"
group :development, :test do
gem "rspec-rails"
end
DOC
end
def rspec
say("Installing rspec from generator", :yellow)
system('rails generate rspec:install')
end
def rake
say("Creating and migrating initial database", :yellow)
system "rake db:create db:migrate"
end
def leftovers
foreman_setup
app_secret_from_env
swap_pg_username
update_application_config
update_boot_rb
rspec
bootstrap_sass
rake
end
def foreman_setup
create_file 'Procfile', <<-DOC.strip_heredoc
web: bundle exec rails server thin -p $PORT
DOC
create_file '.env', <<-DOC.strip_heredoc
PORT=3000
RAILS_SECRET_TOKEN=#{SecureRandom.hex(64)}
DOC
end
def app_secret_from_env
gsub_file("config/initializers/secret_token.rb", "'APP_SECRET'", "ENV['RAILS_SECRET_TOKEN']")
end
def swap_pg_username
%w[config/database.yml config/database.example.yml].each do |file|
gsub_file(file, /^ username: .*$/, ' username: postgres')
end
end
def update_application_config
inject_into_class("config/application.rb", "Application") do
<<-DOC
config.assets.initialize_on_precompile = false
config.autoload_paths += %W(\#{config.root}/lib)
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.generators do |g|
g.view_specs false
g.routing_specs false
g.stylesheets false
g.javascripts false
g.helper false
end
DOC
end
end
def update_boot_rb
prepend_to_file("config/boot.rb") do
<<-DOC.strip_heredoc
# Don't buffer stdout
$stdout.sync = true
DOC
end
end
def bootstrap_sass
gsub_file("app/assets/javascripts/application.js", "//= require_tree .") do
<<-DOC.strip_heredoc
//= require bootstrap
//= require_tree .
DOC
end
remove_file("app/assets/stylesheets/application.css")
create_file("app/assets/stylesheets/application.sass") do
'@import "bootstrap";'
end
end
end
CleverAppGenerator.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment