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


165
166
167
168
169
170
171
172
173
174
175
# File 'lib/suspenders/app_builder.rb', line 165

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


156
157
158
# File 'lib/suspenders/app_builder.rb', line 156

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

#configure_automatic_deploymentObject


182
183
184
185
# File 'lib/suspenders/app_builder.rb', line 182

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

#configure_generatorsObject


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

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


151
152
153
154
# File 'lib/suspenders/app_builder.rb', line 151

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

#configure_local_mailObject


96
97
98
# File 'lib/suspenders/app_builder.rb', line 96

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

#configure_quiet_assetsObject


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

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

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

#configure_time_formatsObject


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

def configure_time_formats
  remove_file "config/locales/en.yml"
  template "config_locales_en.yml.erb", "config/locales/en.yml"
end

#copy_miscellaneous_filesObject


191
192
193
194
# File 'lib/suspenders/app_builder.rb', line 191

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


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

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

#create_github_repo(repo_name) ⇒ Object


187
188
189
# File 'lib/suspenders/app_builder.rb', line 187

def create_github_repo(repo_name)
  run "hub create #{repo_name}"
end

#create_heroku_apps(flags) ⇒ Object


177
178
179
180
# File 'lib/suspenders/app_builder.rb', line 177

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

#disallow_wrapping_parametersObject


124
125
126
# File 'lib/suspenders/app_builder.rb', line 124

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

#gemfileObject


29
30
31
# File 'lib/suspenders/app_builder.rb', line 29

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

#gitignoreObject


25
26
27
# File 'lib/suspenders/app_builder.rb', line 25

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

#provide_setup_scriptObject


73
74
75
76
# File 'lib/suspenders/app_builder.rb', line 73

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

#raise_on_delivery_errorsObject


44
45
46
47
# File 'lib/suspenders/app_builder.rb', line 44

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


40
41
42
# File 'lib/suspenders/app_builder.rb', line 40

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

#raise_on_unpermitted_parametersObject


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

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


21
22
23
# File 'lib/suspenders/app_builder.rb', line 21

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

#remove_config_comment_linesObject


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/suspenders/app_builder.rb', line 196

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 { |line|
      line =~ /^.*#.*$/ || line =~ /^$\n/
    }

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

#remove_routes_comment_linesObject


218
219
220
221
222
# File 'lib/suspenders/app_builder.rb', line 218

def remove_routes_comment_lines
  replace_in_file "config/routes.rb",
    /Rails\.application\.routes\.draw do.*end/m,
    "Rails.application.routes.draw do\nend"
end

#replace_gemfile(path) ⇒ Object


137
138
139
140
141
142
143
144
145
# File 'lib/suspenders/app_builder.rb', line 137

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

#ruby_versionObject


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

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

#set_test_delivery_methodObject


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

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

#setup_asset_hostObject


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/suspenders/app_builder.rb', line 100

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_default_rake_taskObject


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/suspenders/app_builder.rb', line 224

def setup_default_rake_task
  append_file "Rakefile" do
    <<~EOS
      task(:default).clear
      task default: [:spec]
      
      if defined? RSpec
        task(:spec).clear
        RSpec::Core::RakeTask.new(:spec) do |t|
          t.verbose = false
        end
      end
    EOS
  end
end

#setup_rack_mini_profilerObject


33
34
35
36
37
38
# File 'lib/suspenders/app_builder.rb', line 33

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

#setup_secret_tokenObject


120
121
122
# File 'lib/suspenders/app_builder.rb', line 120

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

#use_postgres_config_templateObject


128
129
130
131
# File 'lib/suspenders/app_builder.rb', line 128

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