Class: Suspenders::AppBuilder

Inherits:
Rails::AppBuilder
  • Object
show all
Extended by:
Forwardable
Includes:
Actions
Defined in:
lib/suspenders/app_builder.rb

Instance Method Summary collapse

Methods included from Actions

#action_mailer_asset_host, #action_mailer_host, #configure_environment, #expand_json, #replace_in_file

Instance Method Details

#configure_action_mailerObject



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/suspenders/app_builder.rb', line 187

def configure_action_mailer
  action_mailer_host "development", %{"localhost:3000"}
  action_mailer_asset_host "development", %{"http://localhost:3000"}
  action_mailer_host "test", %{"www.example.com"}
  action_mailer_asset_host "test", %{"http://www.example.com"}
  action_mailer_host "production", %{ENV.fetch('APPLICATION_HOST')}
  action_mailer_asset_host(
    "production",
    %{ENV.fetch('ASSET_HOST', ENV.fetch('APPLICATION_HOST'))},
  )
end

#configure_action_mailer_in_specsObject



170
171
172
# File 'lib/suspenders/app_builder.rb', line 170

def configure_action_mailer_in_specs
  copy_file 'action_mailer.rb', 'spec/support/action_mailer.rb'
end

#configure_automatic_deploymentObject



242
243
244
245
246
# File 'lib/suspenders/app_builder.rb', line 242

def configure_automatic_deployment
  append_file "Procfile", "release: bin/auto_migrate\n"
  copy_file "bin_auto_migrate", "bin/auto_migrate"
  chmod "bin/auto_migrate", 0o755
end

#configure_generatorsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/suspenders/app_builder.rb', line 80

def configure_generators
  config = <<-RUBY

config.generators do |generate|
  generate.helper false
  generate.javascripts false
  generate.request_specs false
  generate.routing_specs false
  generate.stylesheets false
  generate.test_framework :rspec
  generate.view_specs false
end

  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_i18n_for_missing_translationsObject



165
166
167
168
# File 'lib/suspenders/app_builder.rb', line 165

def configure_i18n_for_missing_translations
  raise_on_missing_translations_in("development")
  raise_on_missing_translations_in("test")
end

#configure_local_mailObject



98
99
100
# File 'lib/suspenders/app_builder.rb', line 98

def configure_local_mail
  copy_file "email.rb", "config/initializers/email.rb"
end

#configure_locales_and_time_zoneObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/suspenders/app_builder.rb', line 174

def configure_locales_and_time_zone
  remove_file "config/locales/en.yml"
  template "config_locales_it.yml.erb", "config/locales/it.yml"

  config = <<-RUBY
config.i18n.available_locales = %i[en it]
config.i18n.default_locale = :it
config.time_zone = 'Rome'
  RUBY

  inject_into_class 'config/application.rb', 'Application', config
end

#configure_quiet_assetsObject



67
68
69
70
71
72
73
# File 'lib/suspenders/app_builder.rb', line 67

def configure_quiet_assets
  config = <<-RUBY
config.assets.quiet = true
  RUBY

  inject_into_class "config/application.rb", "Application", config
end

#configure_sourcemaps_for_sassObject



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/suspenders/app_builder.rb', line 306

def configure_sourcemaps_for_sass
  config = [
    "  config.sass.inline_source_maps = true\n",
    "  config.sass.line_comments = true\n"
  ].join

  inject_into_file(
    "config/environments/development.rb",
    config,
    after: "  config.assets.debug = true\n"
  )
end

#copy_dotfilesObject



233
234
235
# File 'lib/suspenders/app_builder.rb', line 233

def copy_dotfiles
  directory("dotfiles", ".")
end

#copy_miscellaneous_filesObject



253
254
255
256
# File 'lib/suspenders/app_builder.rb', line 253

def copy_miscellaneous_files
  copy_file "errors.rb", "config/initializers/errors.rb"
  copy_file "json_encoding.rb", "config/initializers/json_encoding.rb"
end

#create_databaseObject



147
148
149
# File 'lib/suspenders/app_builder.rb', line 147

def create_database
  bundle_command "exec rails db:create db:migrate"
end

#create_heroku_apps(flags) ⇒ Object



237
238
239
240
# File 'lib/suspenders/app_builder.rb', line 237

def create_heroku_apps(flags)
  create_staging_heroku_app(flags)
  create_production_heroku_app(flags)
end

#customize_error_pagesObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/suspenders/app_builder.rb', line 258

def customize_error_pages
  meta_tags =<<-EOS
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1" />
  EOS

  %w(500 404 422).each do |page|
    path = "public/#{page}.html"
    if File.exist?(path)
      inject_into_file path, meta_tags, after: "<head>\n"
      replace_in_file path, /<!--.+-->\n/, ''
    end
  end
end

#disallow_wrapping_parametersObject



138
139
140
# File 'lib/suspenders/app_builder.rb', line 138

def disallow_wrapping_parameters
  remove_file "config/initializers/wrap_parameters.rb"
end

#enable_rack_canonical_hostObject



102
103
104
105
106
107
108
# File 'lib/suspenders/app_builder.rb', line 102

def enable_rack_canonical_host
  config = <<-RUBY
config.middleware.use Rack::CanonicalHost, ENV.fetch('APPLICATION_HOST')
  RUBY

  configure_environment "production", config
end

#enable_rack_deflaterObject



110
111
112
# File 'lib/suspenders/app_builder.rb', line 110

def enable_rack_deflater
  configure_environment "production", "config.middleware.use Rack::Deflater"
end

#gemfileObject



31
32
33
# File 'lib/suspenders/app_builder.rb', line 31

def gemfile
  template "Gemfile.erb", "Gemfile"
end

#gitignoreObject



27
28
29
# File 'lib/suspenders/app_builder.rb', line 27

def gitignore
  copy_file "suspenders_gitignore", ".gitignore"
end

#provide_setup_scriptObject



75
76
77
78
# File 'lib/suspenders/app_builder.rb', line 75

def provide_setup_script
  template "bin_setup", "bin/setup", force: true
  run "chmod a+x bin/setup"
end

#raise_on_delivery_errorsObject



46
47
48
49
# File 'lib/suspenders/app_builder.rb', line 46

def raise_on_delivery_errors
  replace_in_file 'config/environments/development.rb',
    'raise_delivery_errors = false', 'raise_delivery_errors = true'
end

#raise_on_missing_assets_in_testObject



42
43
44
# File 'lib/suspenders/app_builder.rb', line 42

def raise_on_missing_assets_in_test
  configure_environment "test", "config.assets.raise_runtime_errors = true"
end

#raise_on_unpermitted_parametersObject



59
60
61
62
63
64
65
# File 'lib/suspenders/app_builder.rb', line 59

def raise_on_unpermitted_parameters
  config = <<-RUBY
config.action_controller.action_on_unpermitted_parameters = :raise
  RUBY

  inject_into_class "config/application.rb", "Application", config
end

#readmeObject



23
24
25
# File 'lib/suspenders/app_builder.rb', line 23

def readme
  template 'README.md.erb', 'README.md'
end

#remove_config_comment_linesObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/suspenders/app_builder.rb', line 273

def remove_config_comment_lines
  config_files = [
    "application.rb",
    "environment.rb",
    "environments/development.rb",
    "environments/production.rb",
    "environments/test.rb",
  ]

  config_files.each do |config_file|
    path = File.join(destination_root, "config/#{config_file}")

    accepted_content = File.readlines(path).reject do |line|
      line =~ /^.*#.*$/ || line =~ /^$\n/
    end

    File.open(path, "w") do |file|
      accepted_content.each { |line| file.puts line }
    end
  end
end

#replace_default_puma_configurationObject



199
200
201
# File 'lib/suspenders/app_builder.rb', line 199

def replace_default_puma_configuration
  copy_file "puma.rb", "config/puma.rb", force: true
end

#replace_gemfile(path) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/suspenders/app_builder.rb', line 151

def replace_gemfile(path)
  template 'Gemfile.erb', 'Gemfile', force: true do |content|
    if path
      content.gsub(%r{gem ['"]welaika-suspenders['"].*}) { |s| %{#{s}, path: "#{path}"} }
    else
      content
    end
  end
end

#ruby_versionObject



161
162
163
# File 'lib/suspenders/app_builder.rb', line 161

def ruby_version
  create_file '.ruby-version', "#{Suspenders::RUBY_VERSION}\n"
end

#run_rubocop_autocorrectObject



295
296
297
# File 'lib/suspenders/app_builder.rb', line 295

def run_rubocop_autocorrect
  Bundler.with_clean_env { run "bundle exec rubocop -a" }
end

#set_test_delivery_methodObject



51
52
53
54
55
56
57
# File 'lib/suspenders/app_builder.rb', line 51

def set_test_delivery_method
  inject_into_file(
    "config/environments/development.rb",
    "\n  config.action_mailer.delivery_method = :letter_opener",
    after: "config.action_mailer.raise_delivery_errors = true",
  )
end

#set_up_foregoObject



203
204
205
206
207
# File 'lib/suspenders/app_builder.rb', line 203

def set_up_forego
  # NOTE: we want to use ruby foreman!
  copy_file "Procfile", "Procfile"
  Bundler.with_clean_env { run "gem install foreman" }
end

#setup_asset_hostObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/suspenders/app_builder.rb', line 114

def setup_asset_host
  replace_in_file 'config/environments/production.rb',
    "# config.action_controller.asset_host = 'http://assets.example.com'",
    "config.action_controller.asset_host = ENV.fetch('ASSET_HOST', ENV.fetch('APPLICATION_HOST'))"

  if File.exist?("config/initializers/assets.rb")
    replace_in_file 'config/initializers/assets.rb',
      "config.assets.version = '1.0'",
      'config.assets.version = (ENV["ASSETS_VERSION"] || "1.0")'
  end

  config = <<-EOD
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=31557600',
  }
  EOD

  configure_environment("production", config)
end

#setup_bundler_auditObject



248
249
250
251
# File 'lib/suspenders/app_builder.rb', line 248

def setup_bundler_audit
  copy_file "bundler_audit.rake", "lib/tasks/bundler_audit.rake"
  append_file "Rakefile", %{\ntask default: "bundle:audit"\n}
end

#setup_default_directoriesObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/suspenders/app_builder.rb', line 209

def setup_default_directories
  [
    'app/decorators',
    'app/forms',
    'app/queries',
    'app/interactions',
    'app/views/pages',
    'spec/lib',
    'spec/controllers',
    'spec/helpers',
    'spec/decorators',
    'spec/forms',
    'spec/queries',
    'spec/interactions',
    'spec/fixtures',
    'spec/factories',
    'spec/support/matchers',
    'spec/support/mixins',
    'spec/support/shared_examples'
  ].each do |dir|
    empty_directory_with_keep_file dir
  end
end

#setup_rack_mini_profilerObject



35
36
37
38
39
40
# File 'lib/suspenders/app_builder.rb', line 35

def setup_rack_mini_profiler
  copy_file(
    "rack_mini_profiler.rb",
    "config/initializers/rack_mini_profiler.rb",
  )
end

#setup_rakefileObject



299
300
301
302
303
304
# File 'lib/suspenders/app_builder.rb', line 299

def setup_rakefile
  # NOTE: we named our file `rakefile_template` and not
  # `Rakefile` because otherwise `copy_file` would copy the
  # rails `Rakefile` into the project folder.
  copy_file 'rakefile_template.rb', 'Rakefile', force: true
end

#setup_secret_tokenObject



134
135
136
# File 'lib/suspenders/app_builder.rb', line 134

def setup_secret_token
  template 'secrets.yml', 'config/secrets.yml', force: true
end

#use_postgres_config_templateObject



142
143
144
145
# File 'lib/suspenders/app_builder.rb', line 142

def use_postgres_config_template
  template 'postgresql_database.yml.erb', 'config/database.yml',
    force: true
end