|
| 1 | +require_relative 'utils' |
| 2 | + |
| 3 | +class Pathfinder |
| 4 | + |
| 5 | + attr_reader :template, :utils |
| 6 | + |
| 7 | + def initialize(template) |
| 8 | + @template = template |
| 9 | + @utils = Utils.new(template) |
| 10 | + end |
| 11 | + |
| 12 | + def call |
| 13 | + @template.instance_exec(self) do |pathfinder| |
| 14 | + @bower_packages = [['select2', '4.0.3'], ['lodash', '4.16.6']] |
| 15 | + @monitoring_enabled = false |
| 16 | + @carrierwave_enabled = false |
| 17 | + |
| 18 | + def configure_rollbar |
| 19 | + initializer 'rollbar.rb', <<-CODE |
| 20 | + Rollbar.configure do |config| |
| 21 | + config.access_token = ENV['ROLLBAR_ACCESS_TOKEN'] |
| 22 | + config.environment = ENV['ROLLBAR_ENV'] || Rails.env |
| 23 | + config.exception_level_filters.merge!( |
| 24 | + 'ActionController::RoutingError': 'ignore' |
| 25 | + ) |
| 26 | +
|
| 27 | + if Rails.env.test? || Rails.env.development? |
| 28 | + config.enabled = false |
| 29 | + end |
| 30 | + end |
| 31 | + CODE |
| 32 | + |
| 33 | + inside 'config' do |
| 34 | + append_file 'application.yml.example', "\nROLLBAR_ACCESS_TOKEN: ''" |
| 35 | + append_file 'application.yml', "\nROLLBAR_ACCESS_TOKEN: ''" |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + def configure_airbrake |
| 40 | + initializer 'airbrake.rb', <<-CODE |
| 41 | + Airbrake.configure do |config| |
| 42 | + config.api_key = ENV['AIRBRAKE_API_KEY'] |
| 43 | + end |
| 44 | + CODE |
| 45 | + |
| 46 | + inside 'config' do |
| 47 | + append_file 'application.yml.example', "\nAIRBRAKE_API_KEY: ''" |
| 48 | + append_file 'application.yml', "\nAIRBRAKE_API_KEY: ''" |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def configure_database |
| 53 | + inside 'config' do |
| 54 | + remove_file 'database.yml' |
| 55 | + create_file 'database.yml' do <<-EOF |
| 56 | + default: &default |
| 57 | + adapter: postgresql |
| 58 | + encoding: unicode |
| 59 | + pool: 5 |
| 60 | +
|
| 61 | + development: |
| 62 | + <<: *default |
| 63 | + database: #{app_name}_development |
| 64 | +
|
| 65 | + staging: |
| 66 | + <<: *default |
| 67 | + database: #{app_name}_staging |
| 68 | + username: #{app_name} |
| 69 | + password: <%= ENV['#{app_name.upcase}_DATABASE_PASSWORD'] %> |
| 70 | +
|
| 71 | + test: |
| 72 | + <<: *default |
| 73 | + database: #{app_name}_test |
| 74 | +
|
| 75 | + production: |
| 76 | + <<: *default |
| 77 | + database: #{app_name}_production |
| 78 | + username: #{app_name} |
| 79 | + password: <%= ENV['#{app_name.upcase}_DATABASE_PASSWORD'] %> |
| 80 | +
|
| 81 | + EOF |
| 82 | + end |
| 83 | + append_file 'application.yml.example', "\n#{app_name.upcase}_DATABASE_PASSWORD: ''" |
| 84 | + append_file 'application.yml', "\n#{app_name.upcase}_DATABASE_PASSWORD: ''" |
| 85 | + end |
| 86 | + rake 'db:create' |
| 87 | + end |
| 88 | + |
| 89 | + def configure_redis |
| 90 | + initializer 'redis.rb', <<-CODE |
| 91 | + Redis.current = Redis.new(Rails.application.config_for(:redis)) |
| 92 | + CODE |
| 93 | + |
| 94 | + inside 'config' do |
| 95 | + create_file 'redis.yml' do <<-EOF |
| 96 | + default: &default |
| 97 | + host: localhost |
| 98 | + port: 6379 |
| 99 | +
|
| 100 | + development: |
| 101 | + <<: *default |
| 102 | + test: |
| 103 | + <<: *default |
| 104 | + EOF |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def configure_sidekiq |
| 110 | + initializer 'sidekiq.rb', <<-CODE |
| 111 | + redis_host = Redis.current.client.host |
| 112 | + redis_port = Redis.current.client.port |
| 113 | +
|
| 114 | + Sidekiq.configure_server do |config| |
| 115 | + config.redis = { url: "redis://\#{redis_host}:\#{redis_port}" } |
| 116 | + end |
| 117 | +
|
| 118 | + Sidekiq.configure_client do |config| |
| 119 | + config.redis = { url: "redis://\#{redis_host}:\#{redis_port}" } |
| 120 | + end |
| 121 | + CODE |
| 122 | + |
| 123 | + inside 'config' do |
| 124 | + create_file 'sidekiq.yml' do <<-EOF |
| 125 | + --- |
| 126 | + :concurrency: 5 |
| 127 | + :pidfile: tmp/pids/sidekiq.pid |
| 128 | + staging: |
| 129 | + :concurrency: 10 |
| 130 | + production: |
| 131 | + :concurrency: 20 |
| 132 | + :queues: |
| 133 | + - [default, 10] |
| 134 | + - [mailers, 10] |
| 135 | + EOF |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + insert_into_file 'config/routes.rb', before: "Rails.application.routes.draw do\n" do <<-RUBY |
| 140 | + require 'sidekiq/web' |
| 141 | + RUBY |
| 142 | + end |
| 143 | + |
| 144 | + insert_into_file 'config/routes.rb', after: "Rails.application.routes.draw do\n" do <<-RUBY |
| 145 | + namespace :admin do |
| 146 | + authenticate :user, lambda { |u| u.admin? } do |
| 147 | + mount Sidekiq::Web => '/sidekiq' |
| 148 | + end |
| 149 | + end |
| 150 | + get '/404', to: 'errors#not_found' |
| 151 | + get '/500', to: 'errors#internal_error' |
| 152 | + RUBY |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + def configure_gitignore |
| 157 | + remove_file '.gitignore' |
| 158 | + create_file '.gitignore' do <<-EOF |
| 159 | + !/log/.keep |
| 160 | + *.rdb |
| 161 | + .bundle |
| 162 | + config/application.yml |
| 163 | + config/database.yml |
| 164 | + config/secrets.yml |
| 165 | + config/secrets.*.yml |
| 166 | + log/* |
| 167 | + public/assets |
| 168 | + public/uploads |
| 169 | + tmp |
| 170 | + .DS_Store |
| 171 | + *.sublime-* |
| 172 | + .rvmrc |
| 173 | + stellar.yml |
| 174 | + .rubocop.yml |
| 175 | +
|
| 176 | + # Ignore generated coverage |
| 177 | + /coverage |
| 178 | +
|
| 179 | + # Bower stuff |
| 180 | + vendor/assets/.bowerrc |
| 181 | + vendor/assets/bower.json |
| 182 | + vendor/assets/bower_components |
| 183 | + EOF |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + def configure_bower_resources(bower_resources = []) |
| 188 | + remove_file 'bower.json' |
| 189 | + create_file 'bower.json' do <<-TEXT |
| 190 | + { |
| 191 | + "lib": { |
| 192 | + "name": "bower-rails generated lib assets", |
| 193 | + "dependencies": { } |
| 194 | + }, |
| 195 | + "vendor": { |
| 196 | + "name": "bower-rails generated vendor assets", |
| 197 | + "dependencies": { |
| 198 | + TEXT |
| 199 | + end |
| 200 | + |
| 201 | + |
| 202 | + packages = bower_resources.map { |package, version| "\"#{package}\": \"#{version}\"" } |
| 203 | + .join(",\n") |
| 204 | + |
| 205 | + append_file 'bower.json', "#{packages}\n" unless packages.empty? |
| 206 | + append_file 'bower.json' do <<-TEXT |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + TEXT |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + def configure_carrierwave |
| 215 | + initializer 'carrierwave.rb', <<-CODE |
| 216 | + require 'carrierwave/storage/fog' |
| 217 | + CarrierWave.configure do |config| |
| 218 | + config.fog_provider = 'fog/aws' |
| 219 | + config.fog_directory = ENV['AWS_S3_BUCKET'] |
| 220 | + config.fog_public = true |
| 221 | + config.storage = :fog |
| 222 | + config.cache_dir = Rails.root.join('tmp/cache') |
| 223 | +
|
| 224 | + config.fog_credentials = { |
| 225 | + provider: 'AWS', |
| 226 | + aws_access_key_id: ENV['AWS_ACCESS_KEY'], |
| 227 | + aws_secret_access_key: ENV['AWS_SECRET_KEY'], |
| 228 | + region: 'eu-west-1' |
| 229 | + } |
| 230 | + end |
| 231 | + CODE |
| 232 | + |
| 233 | + inside 'config' do |
| 234 | + append_file 'application.yml.example', "\nAWS_ACCESS_KEY: ''" |
| 235 | + append_file 'application.yml', "\nAWS_ACCESS_KEY: ''" |
| 236 | + append_file 'application.yml.example', "\nAWS_SECRET_KEY: ''" |
| 237 | + append_file 'application.yml', "\nAWS_SECRET_KEY: ''" |
| 238 | + append_file 'application.yml.example', "\nAWS_S3_BUCKET: ''" |
| 239 | + append_file 'application.yml', "\nAWS_S3_BUCKET: ''" |
| 240 | + end |
| 241 | + end |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + remove_file 'Gemfile' |
| 246 | + run 'touch Gemfile' |
| 247 | + add_source 'https://rubygems.org' |
| 248 | + |
| 249 | + append_file 'Gemfile', "ruby \'#{pathfinder.utils.ask_with_default('Which version of ruby do you want to use?', default: RUBY_VERSION)}\'" |
| 250 | + |
| 251 | + gem 'rails', pathfinder.utils.ask_with_default('Which version of rails do you want to use?', default: '4.2.5') |
| 252 | + |
| 253 | + # DB |
| 254 | + gem 'pg' |
| 255 | + |
| 256 | + # User Management |
| 257 | + gem 'devise' |
| 258 | + gem 'pundit' |
| 259 | + |
| 260 | + # Model |
| 261 | + gem 'aasm' |
| 262 | + gem 'keynote' |
| 263 | + gem 'paranoia' |
| 264 | + |
| 265 | + # Forms |
| 266 | + gem 'simple_form' |
| 267 | + |
| 268 | + # Searchs |
| 269 | + gem 'ransack' if yes?("Do you want to use Ransack?") |
| 270 | + gem 'kaminari' |
| 271 | + gem 'searchkick' if yes?("Are you going to use ElasticSearch?") |
| 272 | + |
| 273 | + # Assets |
| 274 | + gem 'bootstrap-sass', '~> 3.3.3' |
| 275 | + gem 'bootstrap-datepicker-rails', '~> 1.6.0' if yes?("Do you want to use Bootstrap datepicker?") |
| 276 | + gem 'font-awesome-sass', '~> 4.3.0' |
| 277 | + gem 'sass-rails', '~> 5.0' |
| 278 | + gem 'modernizr-rails' |
| 279 | + gem 'autoprefixer-rails' |
| 280 | + gem 'compass-rails', '~> 3.0.1' |
| 281 | + gem 'magnific-popup-rails' |
| 282 | + gem 'jquery-rails' |
| 283 | + gem 'uglifier', '>= 1.3.0' |
| 284 | + gem 'sdoc', '~> 0.4.0', group: :doc |
| 285 | + # Assets |
| 286 | + gem 'bower-rails' |
| 287 | + |
| 288 | + # Jobs |
| 289 | + gem 'redis' |
| 290 | + gem 'sidekiq' |
| 291 | + gem 'sinatra', require: nil |
| 292 | + |
| 293 | + # File uploads |
| 294 | + if yes?("Do you want to use Carrierwave?") |
| 295 | + @carrierwave_enabled = true |
| 296 | + gem 'carrierwave' |
| 297 | + gem 'fog-aws' |
| 298 | + gem 'mini_magick' if yes?("Are you going to handle images?") |
| 299 | + end |
| 300 | + |
| 301 | + |
| 302 | + # Monitoring |
| 303 | + case ask('Choose Monitoring Engine:', limited_to: %w(rollbar airbrake none)) |
| 304 | + when 'rollbar' |
| 305 | + gem 'rollbar' |
| 306 | + @monitoring_enabled = :rollbar |
| 307 | + when 'airbrake' |
| 308 | + gem 'airbrake' |
| 309 | + @monitoring_enabled = :airbrake |
| 310 | + else |
| 311 | + end |
| 312 | + |
| 313 | + # Emails |
| 314 | + gem 'premailer-rails' |
| 315 | + |
| 316 | + gem_group :development, :test do |
| 317 | + gem 'bundler-audit', require: false |
| 318 | + gem 'byebug' |
| 319 | + gem 'rspec-rails' |
| 320 | + gem 'spring' |
| 321 | + gem 'quiet_assets' |
| 322 | + gem 'figaro' |
| 323 | + gem 'mailcatcher' |
| 324 | + gem 'factory_girl_rails' |
| 325 | + gem 'faker' |
| 326 | + gem 'pry-rails' |
| 327 | + gem 'pry-coolline' |
| 328 | + gem 'pry-byebug' |
| 329 | + gem 'rubocop', require: false |
| 330 | + end |
| 331 | + |
| 332 | + gem_group :development do |
| 333 | + gem 'spring-commands-rspec', require: false |
| 334 | + gem 'better_errors' |
| 335 | + end |
| 336 | + |
| 337 | + gem_group :test do |
| 338 | + gem 'simplecov', require: false |
| 339 | + gem 'capybara', require: false |
| 340 | + gem 'capybara-webkit', require: false |
| 341 | + gem 'database_cleaner', require: false |
| 342 | + gem 'fakeredis', require: false |
| 343 | + gem 'poltergeist', require: false |
| 344 | + gem 'shoulda-matchers', require: false |
| 345 | + end |
| 346 | + |
| 347 | + after_bundle do |
| 348 | + run 'spring stop' |
| 349 | + |
| 350 | + inside 'config' do |
| 351 | + create_file 'application.yml' |
| 352 | + create_file 'application.yml.example' |
| 353 | + remove_file 'routes.rb' |
| 354 | + create_file 'routes.rb' do <<-RUBY |
| 355 | + Rails.application.routes.draw do |
| 356 | + end |
| 357 | + RUBY |
| 358 | + end |
| 359 | + end |
| 360 | + |
| 361 | + configure_database |
| 362 | + configure_carrierwave if @carrierwave_enabled |
| 363 | + |
| 364 | + generate 'rspec:install' |
| 365 | + |
| 366 | + generate 'simple_form:install' |
| 367 | + generate 'pundit:install' |
| 368 | + insert_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do <<-RUBY |
| 369 | + include Pundit |
| 370 | + RUBY |
| 371 | + end |
| 372 | + |
| 373 | + generate 'devise:install' |
| 374 | + insert_into_file 'config/environments/development.rb', after: "Rails.application.configure do\n" do <<-RUBY |
| 375 | + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } |
| 376 | + config.action_controller.asset_host = 'http://localhost:3000' |
| 377 | + config.action_mailer.asset_host = 'http://localhost:3000' |
| 378 | + config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } |
| 379 | + config.action_mailer.delivery_method = :smtp |
| 380 | + config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 } |
| 381 | + RUBY |
| 382 | + end |
| 383 | + |
| 384 | + configure_rollbar if @monitoring_enabled == :rollbar |
| 385 | + configure_airbrake if @monitoring_enabled == :airbrake |
| 386 | + configure_redis |
| 387 | + configure_sidekiq |
| 388 | + configure_gitignore |
| 389 | + |
| 390 | + |
| 391 | + run 'rails g bower_rails:initialize json' |
| 392 | + configure_bower_resources @bower_packages |
| 393 | + rake 'bower:install' |
| 394 | + rake 'bower:resolve' |
| 395 | + end |
| 396 | + end |
| 397 | + end |
| 398 | + |
| 399 | +end |
0 commit comments