require 'erb' require 'ostruct' # ==================== # 0. Helper # ==================== def render_file(text, variables) struct = OpenStruct.new(variables) rendered_file = ERB.new(text).result(struct.instance_eval { binding }) end # ==================== # 1. Gem File # ==================== run 'rm Gemfile && touch Gemfile' add_source 'https://rubygems.org/' gem 'puma', '~> 3.0' gem 'mysql2', '>= 0.3.18', '< 0.5' # Use mysql as the database for Active Record gem 'rails', '~> 5.0.0' gem 'lograge' gem 'wisper', '2.0.0.rc1' gem 'active_model_serializers' gem 'rack-attack' # Rack middleware for blocking & throttling abusive requests gem 'rails-i18n', '~> 5.0.0' gem 'devise' gem 'devise-i18n' gem 'doorkeeper' gem 'doorkeeper-i18n' gem_group :development do gem 'thin' gem 'capistrano', require: false gem 'capistrano-bundler', require: false gem 'capistrano-chruby', require: false gem 'capistrano-rails', require: false gem 'capistrano3-puma', require: false gem 'capistrano-sidekiq', require: false gem 'better_errors' gem 'listen', '~> 3.0.5' end gem_group :test do gem 'rspec' gem 'factory_girl_rails' gem 'database_cleaner' end gem_group :development, :test do gem 'rubocop', require: false gem 'rspec-rails' gem 'pry-rails' gem 'pry-byebug' gem 'awesome_print' gem 'faker' gem 'binding_of_caller' end run 'bundle install' after_bundle do setup_application setup_database # set_routes # prepare_files # setup_i18n # setup_cable # setup_vendor # setup_git end # ==================== # 2. application.rb # ==================== def setup_application generators_config = <<-EOS config.generators do |g| g.test_framework :rspec g.fixture_replacement :factory_girl, dir: 'spec/factories' end EOS environment "config.time_zone = 'Beijing'" environment "config.i18n.available_locales = ['zh-CN', :en]" environment "config.i18n.default_locale = 'zh-CN'" environment 'config.i18n.fallbacks = true' environment 'config.middleware.use Rack::Attack' environment generators_config end # ==================== # 3. database.yml # ==================== def setup_database database_temp = <<-EOS default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root password: <%= mysql_passwd %> host: localhost development: <<: *default database: <%= app_name %>_development test: <<: *default database: <%= app_name %>_test EOS run 'rm config/database.yml' file 'config/database.yml', render_file(database_temp, app_name: app_name.downcase, mysql_passwd: ENV['LOCAL_MYSQL_PASSWD']) # drop_db = yes?('Drop DB for initialize? (Y/n)') # rails_command 'db:drop' if drop_db # rails_command 'db:setup' if drop_db # rails_command 'db:migrate' end # ==================== # 4. routes.rb # ==================== def set_routes api_temp = <<-EOS namespace :api do namespace :v1 do match 'public', via: :get, to: 'root#public' match '*path', via: :all, to: 'root#not_found' end end EOS route "root to: 'home#index'" route api_temp end # ==================== # 5. Files Ready # ==================== def prepare_files home_ctrl_temp = < 'Page not found' }, 404) end end end end EOS api_root_temp = <::Application.configure do config.lograge.enabled = true end EOS initializer 'lograge.rb', render_file(lograge_temp, app_name: app_name) # Devise rails_command 'g devise:install' environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }", env: 'development' rails_command 'g devise User' rails_command 'db:migrate' # Doorkeeper rails_command 'g doorkeeper:install' rails_command 'g doorkeeper:migration' rails_command 'db:migrate' end # ==================== # 8. Git Config # ==================== def setup_git file '.gitignore', <<-EOS .bundle tmp/ db/*.sqlite3 log/ .sass-cache/ config/*.yml .DS_Store *.swp *.swo .rvmrc .idea EOS git :init git add: '.' git commit: "-a -m 'Initial commit'" end