-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.rb
624 lines (487 loc) · 20.9 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# >---------------------------------------------------------------------------<
#
# _____ _ _ __ ___ _
# | __ \ (_) | \ \ / (_) | |
# | |__) |__ _ _| |___\ \ /\ / / _ ______ _ _ __ __| |
# | _ // _` | | / __|\ \/ \/ / | |_ / _` | '__/ _` |
# | | \ \ (_| | | \__ \ \ /\ / | |/ / (_| | | | (_| |
# |_| \_\__,_|_|_|___/ \/ \/ |_/___\__,_|_| \__,_|
#
# This template was generated by rails_apps_composer, a custom version of
# RailsWizard, the application template builder. For more information, see:
# https://github.com/RailsApps/rails_apps_composer/
#
# >---------------------------------------------------------------------------<
# >----------------------------[ Initial Setup ]------------------------------<
initializer 'generators.rb', <<-RUBY
Rails.application.config.generators do |g|
end
RUBY
@recipes = ["activerecord", "haml", "foundation", "paperclip", "carrierwave", "extras", "devise", "develon", "airbrake", "git"]
def recipes; @recipes end
def recipe?(name); @recipes.include?(name) end
def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
def say_wizard(text); say_custom(@current_recipe || 'wizard', text) end
def ask_wizard(question)
ask "\033[1m\033[30m\033[46m" + (@current_recipe || "prompt").rjust(10) + "\033[0m\033[36m" + " #{question}\033[0m"
end
def yes_wizard?(question)
answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
case answer.downcase
when "yes", "y"
true
when "no", "n"
false
else
yes_wizard?(question)
end
end
def no_wizard?(question); !yes_wizard?(question) end
def multiple_choice(question, choices)
say_custom('question', question)
values = {}
choices.each_with_index do |choice,i|
values[(i + 1).to_s] = choice[1]
say_custom (i + 1).to_s + ')', choice[0]
end
answer = ask_wizard("Enter your selection:") while !values.keys.include?(answer)
values[answer]
end
@current_recipe = nil
@configs = {}
@after_blocks = []
def after_bundler(&block); @after_blocks << [@current_recipe, block]; end
@after_everything_blocks = []
def after_everything(&block); @after_everything_blocks << [@current_recipe, block]; end
@before_configs = {}
def before_config(&block); @before_configs[@current_recipe] = block; end
# this application template only supports Rails version 3.1 and newer
case Rails::VERSION::MAJOR.to_s
when "3"
case Rails::VERSION::MINOR.to_s
when "2"
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
when "1"
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
when "0"
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported. Try 3.1 or newer."
raise StandardError.new "Rails #{Rails::VERSION::STRING} is not supported. Try 3.1 or newer."
else
say_wizard "You are using Rails version #{Rails::VERSION::STRING}."
end
else
say_wizard "You are using Rails version #{Rails::VERSION::STRING} which is not supported. Try 3.1 or newer."
raise StandardError.new "Rails #{Rails::VERSION::STRING} is not supported. Try 3.1 or newer."
end
say_wizard "Checking configuration. Please confirm your preferences."
# >---------------------------[ Autoload Modules/Classes ]-----------------------------<
inject_into_file 'config/application.rb', :after => 'config.autoload_paths += %W(#{config.root}/extras)' do <<-'RUBY'
config.autoload_paths += %W(#{config.root}/lib)
RUBY
end
# >---------------------------------[ Recipes ]----------------------------------<
# >-----------------------------[ ActiveRecord ]------------------------------<
@current_recipe = "activerecord"
@before_configs["activerecord"].call if @before_configs["activerecord"]
say_recipe 'ActiveRecord'
config = {"auto_create"=>false}
config['database'] = multiple_choice("Which database are you using?", [["MySQL", "mysql"], ["Oracle", "oracle"], ["PostgreSQL", "postgresql"], ["SQLite", "sqlite3"], ["Frontbase", "frontbase"], ["IBM DB", "ibm_db"]]) if true && true unless config.key?('database')
config['auto_create'] = yes_wizard?("Automatically create database with default configuration?") if true && true unless config.key?('auto_create')
@configs[@current_recipe] = config
if config['database']
say_wizard "Configuring '#{config['database']}' database settings..."
old_gem = gem_for_database
@options = @options.dup.merge(:database => config['database'])
gsub_file 'Gemfile', "gem '#{old_gem}'", "gem '#{gem_for_database}'"
template "config/databases/#{@options[:database]}.yml", "config/database.yml.new"
run 'mv config/database.yml.new config/database.yml'
end
after_bundler do
rake "db:create:all" if config['auto_create']
end
# >---------------------------------[ HAML ]----------------------------------<
@current_recipe = "haml"
@before_configs["haml"].call if @before_configs["haml"]
say_recipe 'HAML'
config = {"haml"=>true}
config['haml'] = yes_wizard?("Would you like to use Haml instead of ERB?") if true && true unless config.key?('haml')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/haml.rb
if config['haml']
gem 'haml', '>= 3.1.6'
gem 'haml-rails', '>= 0.3.4', :group => :development
else
recipes.delete('haml')
end
# >------------------------------[ Foundation ]-------------------------------<
@current_recipe = "foundation"
@before_configs["foundation"].call if @before_configs["foundation"]
say_recipe 'Foundation'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/foundation.rb
gem 'zurb-foundation', '>= 2.2.1.2', :group => :assets
after_bundler do
generate "foundation:install"
if recipes.include? 'haml'
generate "foundation:layout application --haml"
else
generate "foundation:layout"
end
remove_file 'app/views/layouts/application.html.erb'
end
# >-------------------------------[ Paperclip ]-------------------------------<
@current_recipe = "paperclip"
@before_configs["paperclip"].call if @before_configs["paperclip"]
say_recipe 'Paperclip'
config = {}
config['paperclip'] = yes_wizard?("Would you like to use Paperclip to store your files?") if true && true unless config.key?('paperclip')
@configs[@current_recipe] = config
if config['paperclip']
if recipes.include? 'mongoid'
gem 'mongoid-paperclip'
else
gem 'paperclip'
end
else
recipes.delete('paperclip')
end
if config['paperclip']
after_bundler do
# Code here is run after Bundler installs all the gems for the project.
# You can run generators and rake tasks in this section.
if recipes.include? 'mongoid'
create_file 'config/initializers/mongoid-paperclip.rb' do
<<-RUBY
require 'mongoid_paperclip'
RUBY
end
end
if recipes.include? 'cloudfiles'
# Add Storage module file for paperclip in lib
get 'https://raw.github.com/gist/2476222/986bf7a49556ac549b75768af5dce2e6e4c67b61/Cloudfilesstorage.rb', 'lib/cloud_files_storage.rb'
# Add config initialize file
create_file 'config/initializers/cloudfiles.rb' do
<<-RUBY
require 'cloudfiles'
require 'cloud_files_storage'
RUBY
end
create_file 'config/rackspace_cloudfiles.yml' do
<<-YAML
DEFAULTS: &DEFAULTS
username: [username]
api_key: [api_key]
development:
<<: *DEFAULTS
container: [container name]
test:
<<: *DEFAULTS
container: [container name]
production:
<<: *DEFAULTS
container: [container name]
YAML
end
end
end
end
if config['paperclip']
after_everything do
# These blocks are run after the bundler blocks and are reserved for
# special cases like committing the files to a git repository (something
# that depends on everything having been generated).
end
end
# A recipe is two parts: the Ruby code and YAML back-matter that comes
# after a blank line with the __END__ keyword.
# >------------------------------[ Carrierwave ]------------------------------<
@current_recipe = "carrierwave"
@before_configs["carrierwave"].call if @before_configs["carrierwave"]
say_recipe 'Carrierwave'
config = {}
config['carrierwave'] = yes_wizard?("Would you like to use Carrierwave to store your files?") if true && true unless config.key?('carrierwave')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/carrierwave.rb
if config['carrierwave']
gem "carrierwave", ">= 0.6.2"
end
# >--------------------------------[ Extras ]---------------------------------<
@current_recipe = "extras"
@before_configs["extras"].call if @before_configs["extras"]
say_recipe 'Extras'
config = {"footnotes"=>false, "ban_spiders"=>false, "paginate"=>false, "kaminari"=>true, "jsruntime"=>true}
config['footnotes'] = yes_wizard?("Would you like to use 'rails-footnotes' (it's SLOW!)?") if true && true unless config.key?('footnotes')
config['ban_spiders'] = yes_wizard?("Would you like to set a robots.txt file to ban spiders?") if true && true unless config.key?('ban_spiders')
config['paginate'] = yes_wizard?("Would you like to add 'will_paginate' for pagination?") if true && true unless config.key?('paginate')
config['kaminari'] = yes_wizard?("Would you like to add 'kaminari' for pagination?") if true && true unless config.key?('kaminari')
config['jsruntime'] = yes_wizard?("Add 'therubyracer' JavaScript runtime (for Linux users without node.js)?") if true && true unless config.key?('jsruntime')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/extras.rb
if config['footnotes']
say_wizard "Adding 'rails-footnotes'"
gem 'rails-footnotes', '>= 3.7', :group => :development
after_bundler do
generate 'rails_footnotes:install'
end
end
if config['ban_spiders']
say_wizard "Banning spiders by modifying 'public/robots.txt'"
after_bundler do
# ban spiders from your site by changing robots.txt
gsub_file 'public/robots.txt', /# User-Agent/, 'User-Agent'
gsub_file 'public/robots.txt', /# Disallow/, 'Disallow'
end
end
if config['paginate']
say_wizard "Adding 'will_paginate'"
if recipes.include? 'mongoid'
gem 'will_paginate_mongoid'
else
gem 'will_paginate', '>= 3.0.3'
end
recipes << 'paginate'
end
if config['kaminari']
say_wizard "Adding 'kaminari'"
gem 'kaminari', '>= 0.13'
after_bundler do
generate 'kaminari:config'
if recipes.include? 'haml'
generate 'kaminari:views default -e haml'
else
generate 'kaminari:views default'
end
end
recipes << 'kaminari'
end
if config['jsruntime']
say_wizard "Adding 'therubyracer' JavaScript runtime gem"
# maybe it was already added by the html5 recipe for bootstrap_less?
unless recipes.include? 'jsruntime'
gem 'therubyracer', :group => :assets, :platform => :ruby
end
end
# >--------------------------------[ Devise ]---------------------------------<
@current_recipe = "devise"
@before_configs["devise"].call if @before_configs["devise"]
say_recipe 'Devise'
config = {}
config['devise'] = multiple_choice("Would you like to use Devise for authentication?", [["No", false], ["Devise with default modules", "standard"], ["Devise with Confirmable module", "confirmable"], ["Devise with Confirmable and Invitable modules", "invitable"]]) if true && true unless config.key?('devise')
config['authorization'] = yes_wizard?("Would you like to manage authorization with CanCan & Rolify?") if true && true unless config.key?('authorization')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/devise.rb
case config['devise']
when 'no'
recipes.delete('devise')
say_wizard "Devise recipe skipped."
when 'standard'
gem 'devise', '>= 2.1.0'
when 'confirmable'
gem 'devise', '>= 2.1.0'
recipes << 'devise-confirmable'
when 'invitable'
gem 'devise', '>= 2.1.0'
gem 'devise_invitable', '>= 1.0.2'
recipes << 'devise-confirmable'
recipes << 'devise-invitable'
else
recipes.delete('devise')
say_wizard "Devise recipe skipped."
end
if config['authorization']
gem 'cancan', '>= 1.6.7'
gem 'rolify', '>= 3.1.0'
recipes << 'authorization'
end
if recipes.include? 'devise'
after_bundler do
say_wizard "Devise recipe running 'after bundler'"
# Run the Devise generator
generate 'devise:install' unless recipes.include? 'datamapper'
generate 'devise_invitable:install' if recipes.include? 'devise-invitable'
if recipes.include? 'mongo_mapper'
gem 'mm-devise'
gsub_file 'config/initializers/devise.rb', 'devise/orm/', 'devise/orm/mongo_mapper_active_model'
generate 'mongo_mapper:devise User'
elsif recipes.include? 'mongoid'
# Nothing to do (Devise changes its initializer automatically when Mongoid is detected)
# gsub_file 'config/initializers/devise.rb', 'devise/orm/active_record', 'devise/orm/mongoid'
end
# Prevent logging of password_confirmation
gsub_file 'config/application.rb', /:password/, ':password, :password_confirmation'
if recipes.include? 'cucumber'
# Cucumber wants to test GET requests not DELETE requests for destroy_user_session_path
# (see https://github.com/RailsApps/rails3-devise-rspec-cucumber/issues/3)
gsub_file 'config/initializers/devise.rb', 'config.sign_out_via = :delete', 'config.sign_out_via = Rails.env.test? ? :get : :delete'
end
if config['authorization']
inject_into_file 'app/controllers/application_controller.rb', :before => 'end' do <<-RUBY
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
RUBY
end
end
end
after_everything do
say_wizard "Devise recipe running 'after everything'"
if recipes.include? 'rspec'
say_wizard "Copying RSpec files from the rails3-devise-rspec-cucumber examples"
begin
# copy all the RSpec specs files from the rails3-devise-rspec-cucumber example app
remove_file 'spec/factories/users.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/factories/users.rb', 'spec/factories/users.rb'
gsub_file 'spec/factories/users.rb', /# confirmed_at/, "confirmed_at" if recipes.include? 'devise-confirmable'
remove_file 'spec/controllers/home_controller_spec.rb'
remove_file 'spec/controllers/users_controller_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/home_controller_spec.rb', 'spec/controllers/home_controller_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/controllers/users_controller_spec.rb', 'spec/controllers/users_controller_spec.rb'
remove_file 'spec/models/user_spec.rb'
get 'https://raw.github.com/RailsApps/rails3-devise-rspec-cucumber/master/spec/models/user_spec.rb', 'spec/models/user_spec.rb'
rescue OpenURI::HTTPError
say_wizard "Unable to obtain RSpec example files from the repo"
end
remove_file 'spec/views/home/index.html.erb_spec.rb'
remove_file 'spec/views/home/index.html.haml_spec.rb'
remove_file 'spec/views/users/show.html.erb_spec.rb'
remove_file 'spec/views/users/show.html.haml_spec.rb'
remove_file 'spec/helpers/home_helper_spec.rb'
remove_file 'spec/helpers/users_helper_spec.rb'
end
end
end
# >--------------------------------[ Develon ]--------------------------------<
@current_recipe = "develon"
@before_configs["develon"].call if @before_configs["develon"]
say_recipe 'Develon'
config = {}
config['capistrano'] = yes_wizard?("Setup Capistrano for remote deployment?") if true && true unless config.key?('capistrano')
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/develon.rb
gem "wirble", :group => :development
gem "hirb", :group => :development
if config['capistrano']
gem 'capistrano', :git => "git://github.com/develonlab/capistrano.git", :group => :development
gem 'rvm-capistrano', '>= 1.1.0', :group => :development
gem 'auth-deploy', :git => "git://github.com/develonlab/auth-deploy.git", :branch => 'noscript', :group => :development
run 'cp config/environments/production.rb config/environments/staging.rb'
after_everything do
say_wizard 'Configuring Capistrano for remote deployment'
run 'capify .'
prepend_to_file 'Capfile' do
<<-RUBY
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "rvm/capistrano" # Load RVM's capistrano plugin.
RUBY
end
remove_file 'config/deploy.rb'
create_file 'config/deploy.rb' do
<<-RUBY
require 'bundler/capistrano'
require 'capistrano/ext/multistage'
require 'auth-deploy/capistrano'
set :application, 'application_name'
set :stages, %w(staging production)
set :default_stage, 'staging'
set :scm, 'git'
set :repository, 'git_url'
set :deploy_via, :remote_cache
set :scm_verbose, true
set :branch, 'master'
# set :user, 'user_name_sulla_macchina' # da utilizzare se il vostro utente locale è diverso da quello sul server
set :deploy_to, "/space/apache/htdocs/#{application}"
RUBY
end
create_file 'config/deploy/staging.rb' do
<<-RUBY
puts '*** Deploying to: STAGING environment ***'
server '', :app, :web, :db, :primary => true
set :rails_env, "staging"
RUBY
end
create_file 'config/deploy/production.rb' do
<<-RUBY
puts '*** Deploying to: PRODUCTION environment ***'
server '', :app, :web, :db, :primary => true
set :rails_env, "production"
RUBY
end
end
end
# >-------------------------------[ Airbrake ]--------------------------------<
@current_recipe = "airbrake"
@before_configs["airbrake"].call if @before_configs["airbrake"]
say_recipe 'Airbrake'
config = {"use_heroku"=>false}
config['use_heroku'] = yes_wizard?("Use the Airbrake Heroku addon?") if true && recipe?('heroku') unless config.key?('use_heroku')
config['api_key'] = ask_wizard("Enter Airbrake API Key:") if !config['use_heroku'] && true unless config.key?('api_key')
@configs[@current_recipe] = config
gem 'airbrake'
if config['use_heroku']
after_everything do
say_wizard "Adding airbrake:developer Heroku addon (you can always upgrade later)"
run "heroku addons:add airbrake:developer"
generate "airbrake --heroku"
end
else
after_bundler do
generate "airbrake --api-key #{config['api_key']}"
inject_into_file 'config/initializers/airbrake.rb', :before => 'end' do
<<-RUBY
config.host = 'errors.lab.develon.com'
config.port = 80
config.secure = config.port == 443
RUBY
end
end
end
# >----------------------------------[ Git ]----------------------------------<
@current_recipe = "git"
@before_configs["git"].call if @before_configs["git"]
say_recipe 'Git'
@configs[@current_recipe] = config
# Application template recipe for the rails_apps_composer. Check for a newer version here:
# https://github.com/RailsApps/rails_apps_composer/blob/master/recipes/git.rb
after_everything do
say_wizard "Git recipe running 'after everything'"
# Git should ignore some files
remove_file '.gitignore'
get "https://raw.github.com/RailsApps/rails3-application-templates/master/files/gitignore.txt", ".gitignore"
if recipes.include? 'omniauth'
append_file '.gitignore' do <<-TXT
# keep OmniAuth service provider secrets out of the Git repo
config/initializers/omniauth.rb
TXT
end
end
# Initialize new Git repo
git :init
git :add => '.'
git :commit => "-aqm 'new Rails app generated by Rails Apps Composer gem'"
# Create a git branch
git :checkout => ' -b working_branch'
git :add => '.'
git :commit => "-m 'Initial commit of working_branch'"
git :checkout => 'master'
end
@current_recipe = nil
# >-----------------------------[ Run Bundler ]-------------------------------<
say_wizard "Running 'bundle install'. This will take a while."
run 'bundle install'
run 'bundle update'
say_wizard "Running 'after bundler' callbacks."
require 'bundler/setup'
@after_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
@current_recipe = nil
say_wizard "Running 'after everything' callbacks."
@after_everything_blocks.each{|b| config = @configs[b[0]] || {}; @current_recipe = b[0]; b[1].call}
@current_recipe = nil
say_wizard "Finished running the rails_apps_composer app template."
say_wizard "Your new Rails app is ready."