re-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/CONTRIBUTING.md +38 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +133 -0
  6. data/LICENSE +21 -0
  7. data/README.md +181 -0
  8. data/Rakefile +13 -0
  9. data/bin/suspenders +18 -0
  10. data/lib/suspenders.rb +3 -0
  11. data/lib/suspenders/actions.rb +35 -0
  12. data/lib/suspenders/app_builder.rb +323 -0
  13. data/lib/suspenders/generators/app_generator.rb +199 -0
  14. data/lib/suspenders/version.rb +3 -0
  15. data/re-rails.gemspec +32 -0
  16. data/spec/fakes/bin/heroku +5 -0
  17. data/spec/fakes/bin/hub +5 -0
  18. data/spec/features/github_spec.rb +10 -0
  19. data/spec/features/heroku_spec.rb +12 -0
  20. data/spec/features/new_project_spec.rb +23 -0
  21. data/spec/spec_helper.rb +24 -0
  22. data/spec/support/fake_github.rb +21 -0
  23. data/spec/support/fake_heroku.rb +30 -0
  24. data/spec/support/suspenders.rb +50 -0
  25. data/templates/Gemfile_clean +49 -0
  26. data/templates/Procfile +2 -0
  27. data/templates/README.md.erb +25 -0
  28. data/templates/_flashes.html.erb +5 -0
  29. data/templates/_javascript.html.erb +10 -0
  30. data/templates/application.css.scss +8 -0
  31. data/templates/background_jobs_rspec.rb +19 -0
  32. data/templates/bin_setup +29 -0
  33. data/templates/config_locales_en.yml +11 -0
  34. data/templates/database_cleaner_rspec.rb +21 -0
  35. data/templates/disable_xml_params.rb +3 -0
  36. data/templates/errors.rb +28 -0
  37. data/templates/factories_spec.rb +13 -0
  38. data/templates/factories_spec_rake_task.rb +9 -0
  39. data/templates/factory_girl_syntax_rspec.rb +3 -0
  40. data/templates/postgresql_database.yml.erb +11 -0
  41. data/templates/rack_timeout.rb +1 -0
  42. data/templates/sample.env +3 -0
  43. data/templates/secret_token.rb +10 -0
  44. data/templates/smtp.rb +10 -0
  45. data/templates/spec_helper.rb +30 -0
  46. data/templates/staging.rb +3 -0
  47. data/templates/suspenders_gitignore +13 -0
  48. data/templates/suspenders_layout.html.erb.erb +15 -0
  49. data/templates/unicorn.rb +29 -0
  50. metadata +190 -0
@@ -0,0 +1,323 @@
1
+ module Suspenders
2
+ class AppBuilder < Rails::AppBuilder
3
+ include Suspenders::Actions
4
+
5
+ def readme
6
+ template 'README.md.erb', 'README.md'
7
+ end
8
+
9
+ def raise_on_delivery_errors
10
+ replace_in_file 'config/environments/development.rb',
11
+ 'raise_delivery_errors = false', 'raise_delivery_errors = true'
12
+ end
13
+
14
+ def raise_on_unpermitted_parameters
15
+ action_on_unpermitted_parameters = <<-RUBY
16
+
17
+ # Raise an ActionController::UnpermittedParameters exception when
18
+ # a parameter is not explcitly permitted but is passed anyway.
19
+ config.action_controller.action_on_unpermitted_parameters = :raise
20
+ RUBY
21
+ inject_into_file(
22
+ "config/environments/development.rb",
23
+ action_on_unpermitted_parameters,
24
+ before: "\nend"
25
+ )
26
+ end
27
+
28
+ def setup_devise
29
+ run 'bundle exec rails g devise:install'
30
+ run 'bundle exec rails g devise:views'
31
+ end
32
+
33
+ def generate_cancan_ability_class
34
+ run 'bundle exec rails g cancan:ability'
35
+ end
36
+
37
+ def provide_setup_script
38
+ copy_file 'bin_setup', 'bin/setup'
39
+ run 'chmod a+x bin/setup'
40
+ end
41
+
42
+ def configure_generators
43
+ config = <<-RUBY
44
+ config.generators do |generate|
45
+ generate.helper false
46
+ generate.javascript_engine false
47
+ generate.request_specs false
48
+ generate.routing_specs false
49
+ generate.stylesheets false
50
+ generate.test_framework :rspec
51
+ generate.view_specs false
52
+ end
53
+
54
+ RUBY
55
+
56
+ inject_into_class 'config/application.rb', 'Application', config
57
+ end
58
+
59
+ def enable_factory_girl_syntax
60
+ copy_file 'factory_girl_syntax_rspec.rb', 'spec/support/factory_girl.rb'
61
+ end
62
+
63
+ def test_factories_first
64
+ copy_file 'factories_spec.rb', 'spec/models/factories_spec.rb'
65
+ append_file 'Rakefile', factories_spec_rake_task
66
+ end
67
+
68
+ def configure_smtp
69
+ copy_file 'smtp.rb', 'config/initializers/smtp.rb'
70
+
71
+ prepend_file 'config/environments/production.rb',
72
+ "require Rails.root.join('config/initializers/smtp')\n"
73
+
74
+ config = <<-RUBY
75
+
76
+ config.action_mailer.delivery_method = :smtp
77
+ config.action_mailer.smtp_settings = SMTP_SETTINGS
78
+ RUBY
79
+
80
+ inject_into_file 'config/environments/production.rb', config,
81
+ :after => 'config.action_mailer.raise_delivery_errors = false'
82
+ end
83
+
84
+ def enable_rack_deflater
85
+ config = <<-RUBY
86
+
87
+ # Enable deflate / gzip compression of controller-generated responses
88
+ config.middleware.use Rack::Deflater
89
+ RUBY
90
+
91
+ inject_into_file 'config/environments/production.rb', config,
92
+ :after => "config.serve_static_assets = false\n"
93
+ end
94
+
95
+ def setup_staging_environment
96
+ staging_file = 'config/environments/staging.rb'
97
+ copy_file 'staging.rb', staging_file
98
+
99
+ config = <<-RUBY
100
+
101
+ #{app_name.classify}::Application.configure do
102
+ # ...
103
+ end
104
+ RUBY
105
+
106
+ append_file staging_file, config
107
+ end
108
+
109
+ def setup_secret_token
110
+ template 'secret_token.rb',
111
+ 'config/initializers/secret_token.rb',
112
+ :force => true
113
+ end
114
+
115
+ def create_partials_directory
116
+ empty_directory 'app/views/application'
117
+ end
118
+
119
+ def create_shared_flashes
120
+ copy_file '_flashes.html.erb', 'app/views/application/_flashes.html.erb'
121
+ end
122
+
123
+ def create_shared_javascripts
124
+ copy_file '_javascript.html.erb', 'app/views/application/_javascript.html.erb'
125
+ end
126
+
127
+ def create_application_layout
128
+ template 'suspenders_layout.html.erb.erb',
129
+ 'app/views/layouts/application.html.erb',
130
+ :force => true
131
+ end
132
+
133
+ def remove_turbolinks
134
+ replace_in_file 'app/assets/javascripts/application.js',
135
+ /\/\/= require turbolinks\n/,
136
+ ''
137
+ end
138
+
139
+ def use_postgres_config_template
140
+ template 'postgresql_database.yml.erb', 'config/database.yml',
141
+ :force => true
142
+ end
143
+
144
+ def create_database
145
+ bundle_command 'exec rake db:create db:migrate'
146
+ end
147
+
148
+ def replace_gemfile
149
+ remove_file 'Gemfile'
150
+ copy_file 'Gemfile_clean', 'Gemfile'
151
+ end
152
+
153
+ def set_ruby_to_version_being_used
154
+ inject_into_file 'Gemfile', "\n\nruby '#{RUBY_VERSION}'",
155
+ after: /source 'https:\/\/rubygems.org'/
156
+ create_file '.ruby-version', "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}\n"
157
+ end
158
+
159
+ def enable_database_cleaner
160
+ copy_file 'database_cleaner_rspec.rb', 'spec/support/database_cleaner.rb'
161
+ end
162
+
163
+ def configure_spec_support_features
164
+ empty_directory_with_keep_file 'spec/features'
165
+ empty_directory_with_keep_file 'spec/support/features'
166
+ end
167
+
168
+ def configure_rspec
169
+ remove_file 'spec/spec_helper.rb'
170
+ copy_file 'spec_helper.rb', 'spec/spec_helper.rb'
171
+ end
172
+
173
+ def use_spring_binstubs
174
+ run 'bundle exec spring binstub --all'
175
+ end
176
+ def configure_background_jobs_for_rspec
177
+ copy_file 'background_jobs_rspec.rb', 'spec/support/background_jobs.rb'
178
+ run 'rails g delayed_job:active_record'
179
+ end
180
+
181
+ def configure_time_zone
182
+ config = <<-RUBY
183
+ config.active_record.default_timezone = :utc
184
+
185
+ RUBY
186
+ inject_into_class 'config/application.rb', 'Application', config
187
+ end
188
+
189
+ def configure_time_formats
190
+ remove_file 'config/locales/en.yml'
191
+ copy_file 'config_locales_en.yml', 'config/locales/en.yml'
192
+ end
193
+
194
+ def configure_rack_timeout
195
+ copy_file 'rack_timeout.rb', 'config/initializers/rack_timeout.rb'
196
+ end
197
+
198
+ def configure_action_mailer
199
+ action_mailer_host 'development', "#{app_name}.local"
200
+ action_mailer_host 'test', 'www.example.com'
201
+ action_mailer_host 'staging', "staging.#{app_name}.com"
202
+ action_mailer_host 'production', "#{app_name}.com"
203
+ end
204
+
205
+ def generate_rspec
206
+ generate 'rspec:install'
207
+ end
208
+
209
+ def configure_unicorn
210
+ copy_file 'unicorn.rb', 'config/unicorn.rb'
211
+ end
212
+
213
+ def setup_foreman
214
+ copy_file 'sample.env', '.sample.env'
215
+ copy_file 'Procfile', 'Procfile'
216
+ end
217
+
218
+ def setup_stylesheets
219
+ remove_file 'app/assets/stylesheets/application.css'
220
+ copy_file 'application.css.scss',
221
+ 'app/assets/stylesheets/application.css.scss'
222
+ end
223
+
224
+ def gitignore_files
225
+ remove_file '.gitignore'
226
+ copy_file 'suspenders_gitignore', '.gitignore'
227
+ [
228
+ 'app/views/pages',
229
+ 'spec/lib',
230
+ 'spec/controllers',
231
+ 'spec/helpers',
232
+ 'spec/support/matchers',
233
+ 'spec/support/mixins',
234
+ 'spec/support/shared_examples'
235
+ ].each do |dir|
236
+ run "mkdir #{dir}"
237
+ run "touch #{dir}/.keep"
238
+ end
239
+ end
240
+
241
+ def init_git
242
+ run 'git init'
243
+ end
244
+
245
+ def create_heroku_apps
246
+ path_addition = override_path_for_tests
247
+ run "#{path_addition} heroku create #{app_name}-production --remote=production"
248
+ run "#{path_addition} heroku create #{app_name}-staging --remote=staging"
249
+ run "#{path_addition} heroku config:add RACK_ENV=staging RAILS_ENV=staging --remote=staging"
250
+ end
251
+
252
+ def set_heroku_remotes
253
+ remotes = <<-RUBY
254
+
255
+ # Set up staging and production git remotes
256
+ git remote add staging git@heroku.com:#{app_name}-staging.git
257
+ git remote add production git@heroku.com:#{app_name}-production.git
258
+ RUBY
259
+
260
+ append_file 'bin/setup', remotes
261
+ end
262
+
263
+ def set_heroku_rails_secrets
264
+ path_addition = override_path_for_tests
265
+ run "#{path_addition} heroku config:add SECRET_KEY_BASE=#{generate_secret} --remote=staging"
266
+ run "#{path_addition} heroku config:add SECRET_KEY_BASE=#{generate_secret} --remote=production"
267
+ end
268
+
269
+ def create_github_repo(repo_name)
270
+ path_addition = override_path_for_tests
271
+ run "#{path_addition} hub create #{repo_name}"
272
+ end
273
+
274
+ def copy_miscellaneous_files
275
+ copy_file 'errors.rb', 'config/initializers/errors.rb'
276
+ end
277
+
278
+ def customize_error_pages
279
+ meta_tags =<<-EOS
280
+ <meta charset='utf-8' />
281
+ <meta name='ROBOTS' content='NOODP' />
282
+ EOS
283
+
284
+ %w(500 404 422).each do |page|
285
+ inject_into_file "public/#{page}.html", meta_tags, :after => "<head>\n"
286
+ replace_in_file "public/#{page}.html", /<!--.+-->\n/, ''
287
+ end
288
+ end
289
+
290
+ def remove_routes_comment_lines
291
+ replace_in_file 'config/routes.rb',
292
+ /Application\.routes\.draw do.*end/m,
293
+ "Application.routes.draw do\nend"
294
+ end
295
+
296
+ def disable_xml_params
297
+ copy_file 'disable_xml_params.rb', 'config/initializers/disable_xml_params.rb'
298
+ end
299
+
300
+ def setup_default_rake_task
301
+ append_file 'Rakefile' do
302
+ "task(:default).clear\ntask :default => [:spec]\n"
303
+ end
304
+ end
305
+
306
+ private
307
+
308
+ def override_path_for_tests
309
+ if ENV['TESTING']
310
+ support_bin = File.expand_path(File.join('..', '..', 'spec', 'fakes', 'bin'))
311
+ "PATH=#{support_bin}:$PATH"
312
+ end
313
+ end
314
+
315
+ def factories_spec_rake_task
316
+ IO.read find_in_source_paths('factories_spec_rake_task.rb')
317
+ end
318
+
319
+ def generate_secret
320
+ SecureRandom.hex(64)
321
+ end
322
+ end
323
+ end
@@ -0,0 +1,199 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/rails/app/app_generator'
3
+
4
+ module Suspenders
5
+ class AppGenerator < Rails::Generators::AppGenerator
6
+ class_option :database, :type => :string, :aliases => '-d', :default => 'postgresql',
7
+ :desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"
8
+
9
+ class_option :heroku, :type => :boolean, :aliases => '-H', :default => false,
10
+ :desc => 'Create staging and production Heroku apps'
11
+
12
+ class_option :github, :type => :string, :aliases => '-G', :default => nil,
13
+ :desc => 'Create Github repository and add remote origin pointed to repo'
14
+
15
+ class_option :skip_test_unit, :type => :boolean, :aliases => '-T', :default => true,
16
+ :desc => 'Skip Test::Unit files'
17
+
18
+ def make_gemset
19
+ gemset_name = ask "Name your gemset"
20
+ run "rvm gemset use #{gemset_name} --create"
21
+ run "touch .ruby-gemset"
22
+ run "echo #{gemset_name} >> .ruby-gemset"
23
+ end
24
+
25
+ def finish_template
26
+ invoke :suspenders_customization
27
+ super
28
+ end
29
+
30
+ def suspenders_customization
31
+ invoke :customize_gemfile
32
+ invoke :setup_development_environment
33
+ invoke :setup_test_environment
34
+ invoke :setup_production_environment
35
+ invoke :setup_staging_environment
36
+ invoke :setup_secret_token
37
+ invoke :create_suspenders_views
38
+ invoke :setup_coffeescript
39
+ invoke :configure_app
40
+ invoke :setup_stylesheets
41
+ invoke :copy_miscellaneous_files
42
+ invoke :customize_error_pages
43
+ invoke :remove_routes_comment_lines
44
+ invoke :setup_git
45
+ invoke :setup_database
46
+ invoke :create_heroku_apps
47
+ invoke :create_github_repo
48
+ invoke :outro
49
+ end
50
+
51
+ def customize_gemfile
52
+ build :replace_gemfile
53
+ build :set_ruby_to_version_being_used
54
+ bundle_command 'install'
55
+ end
56
+
57
+ def setup_database
58
+ say 'Setting up database'
59
+
60
+ if 'postgresql' == options[:database]
61
+ build :use_postgres_config_template
62
+ end
63
+
64
+ build :create_database
65
+ end
66
+
67
+ def setup_development_environment
68
+ say 'Setting up the development environment'
69
+ build :raise_on_delivery_errors
70
+ build :raise_on_unpermitted_parameters
71
+ build :provide_setup_script
72
+ build :configure_generators
73
+ end
74
+
75
+ def setup_test_environment
76
+ say 'Setting up the test environment'
77
+ build :enable_factory_girl_syntax
78
+ build :test_factories_first
79
+ build :generate_rspec
80
+ build :configure_rspec
81
+ build :use_spring_binstubs
82
+ build :configure_background_jobs_for_rspec
83
+ build :enable_database_cleaner
84
+ build :configure_spec_support_features
85
+ end
86
+
87
+ def setup_production_environment
88
+ say 'Setting up the production environment'
89
+ build :configure_smtp
90
+ build :enable_rack_deflater
91
+ end
92
+
93
+ def setup_staging_environment
94
+ say 'Setting up the staging environment'
95
+ build :setup_staging_environment
96
+ end
97
+
98
+ def setup_secret_token
99
+ say 'Moving secret token out of version control'
100
+ build :setup_secret_token
101
+ end
102
+
103
+ def create_suspenders_views
104
+ say 'Creating suspenders views'
105
+ build :create_partials_directory
106
+ build :create_shared_flashes
107
+ build :create_shared_javascripts
108
+ build :create_application_layout
109
+ end
110
+
111
+ def setup_coffeescript
112
+ say 'Setting up CoffeeScript defaults'
113
+ build :remove_turbolinks
114
+ end
115
+
116
+ def configure_app
117
+ say 'Configuring app'
118
+ build :configure_action_mailer
119
+ build :configure_time_zone
120
+ build :configure_time_formats
121
+ build :configure_rack_timeout
122
+ build :setup_devise
123
+ build :generate_cancan_ability_class
124
+ build :disable_xml_params
125
+ build :setup_default_rake_task
126
+ build :configure_unicorn
127
+ build :setup_foreman
128
+ end
129
+
130
+ def setup_stylesheets
131
+ say 'Set up stylesheets'
132
+ build :setup_stylesheets
133
+ end
134
+
135
+ def setup_git
136
+ if !options[:skip_git]
137
+ say 'Initializing git'
138
+ invoke :setup_gitignore
139
+ invoke :init_git
140
+ end
141
+ end
142
+
143
+ def create_heroku_apps
144
+ if options[:heroku]
145
+ say 'Creating Heroku apps'
146
+ build :create_heroku_apps
147
+ build :set_heroku_remotes
148
+ build :set_heroku_rails_secrets
149
+ end
150
+ end
151
+
152
+ def create_github_repo
153
+ if !options[:skip_git] && options[:github]
154
+ say 'Creating Github repo'
155
+ build :create_github_repo, options[:github]
156
+ end
157
+ end
158
+
159
+ def setup_gitignore
160
+ build :gitignore_files
161
+ end
162
+
163
+ def init_git
164
+ build :init_git
165
+ end
166
+
167
+ def copy_miscellaneous_files
168
+ say 'Copying miscellaneous support files'
169
+ build :copy_miscellaneous_files
170
+ end
171
+
172
+ def customize_error_pages
173
+ say 'Customizing the 500/404/422 pages'
174
+ build :customize_error_pages
175
+ end
176
+
177
+ def remove_routes_comment_lines
178
+ build :remove_routes_comment_lines
179
+ end
180
+
181
+ def outro
182
+ say 'Congratulations! Your app is now generated'
183
+ end
184
+
185
+ def run_bundle
186
+ # Let's not: We'll bundle manually at the right spot
187
+ end
188
+
189
+ protected
190
+
191
+ def get_builder_class
192
+ Suspenders::AppBuilder
193
+ end
194
+
195
+ def using_active_record?
196
+ !options[:skip_active_record]
197
+ end
198
+ end
199
+ end