-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails3_template.rb
260 lines (191 loc) · 6.72 KB
/
rails3_template.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
# >----------------------------[ Initial Setup ]------------------------------<
initializer 'generators.rb', <<-RUBY
Rails.application.config.generators do |g|
end
RUBY
@recipes = [ "slim", "rspec", "cleanup", "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
say_wizard "Checking configuration. Please confirm your preferences."
# >------------------------[ Get default Gemfile ]---------------------------------<
gem 'rails', :version => '3.2.1'
gem 'mysql2'
gem 'json'
gem 'authlogic'
gem 'acl9'
gem 'will_paginate'
gem 'slim-rails'
gem 'haml'
gem 'carrierwave'
gem 'jquery-rails'
gem 'capistrano'
gem 'twitter-bootstrap-rails'
gem_group :development, :test do
gem 'rb-fsevent'
gem 'growl'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'guard-rspec'
gem 'spork'
end
# >---------- set up databases
after_bundler do
say_wizard "Create database"
run "bundle exec rake db:create"
end
# >---------- template files
%w(controllers/user_sessions_controller.rb controllers/users_controller.rb controllers/application_controller.rb
models/user.rb models/user_session.rb
views/user_sessions/new.html.slim views/user_sessions/new.html.slim views/users/new.html.slim
).each do |file|
get "https://raw.github.com/sylow/Rails-3-App-Templates/master/vendors/app/#{file}", "app/#{file}"
end
%w(db/migrate/20100414115528_create_users.rb).each do |file|
get "https://raw.github.com/sylow/Rails-3-App-Templates/master/vendors/#{file}", "#{file}"
end
# >---------------------------------[ RSpec ]---------------------------------<
after_bundler do
say_wizard "RSpec recipe running 'after bundler'"
generate 'rspec:install'
say_wizard "Removing test folder (not needed for RSpec)"
run 'rm -rf test/'
inject_into_file 'config/application.rb', :after => "Rails::Application\n" do
<<-RUBY
# don't generate RSpec tests for views and helpers
config.generators do |g|
g.view_specs false
g.helper_specs false
end
RUBY
end
end
# >---------------------------[ ApplicationLayout ]---------------------------<
@current_recipe = "application_layout"
@before_configs["application_layout"].call if @before_configs["application_layout"]
say_recipe 'ApplicationLayout'
@configs[@current_recipe] = config
# Application template recipe for the rails3_devise_wizard. Check for a newer version here:
# https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/application_layout.rb
after_bundler do
say_wizard "ApplicationLayout recipe running 'after bundler'"
#install bootstrap
run 'bundle exec rails g bootstrap:install'
#adding layout
run 'bundle exec rails generate bootstrap:layout application fixed --slim'
#
run 'bundle exec rake db:migrate'
end
# >------Routes
route("resources :users")
route("resources :user_sessions")
# >--------------------------------[ Cleanup ]--------------------------------<
@current_recipe = "cleanup"
@before_configs["cleanup"].call if @before_configs["cleanup"]
say_recipe 'Cleanup'
@configs[@current_recipe] = config
# Application template recipe for the rails3_devise_wizard. Check for a newer version here:
# https://github.com/fortuity/rails3_devise_wizard/blob/master/recipes/cleanup.rb
after_bundler do
say_wizard "Cleanup recipe running 'after bundler'"
# remove unnecessary files
%w{
README
doc/README_FOR_APP
public/index.html
public/images/rails.png
app/views/layout/application.html.erb
}.each { |file| remove_file file }
# add placeholder READMEs
get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.txt", "README"
get "https://github.com/fortuity/rails-template-recipes/raw/master/sample_readme.textile", "README.textile"
gsub_file "README", /App_Name/, "#{app_name.humanize.titleize}"
gsub_file "README.textile", /App_Name/, "#{app_name.humanize.titleize}"
# remove commented lines from Gemfile
# thanks to https://github.com/perfectline/template-bucket/blob/master/cleanup.rb
gsub_file "Gemfile", /#.*\n/, "\n"
gsub_file "Gemfile", /\n+/, "\n"
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 rails3_devise_wizard. Check for a newer version here:
# https://github.com/fortuity/rails3_devise_wizard/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://github.com/fortuity/rails3-gitignore/raw/master/gitignore.txt", ".gitignore"
# Initialize new Git repo
git :init
git :add => '.'
git :commit => "-aqm 'new Rails app initialized'"
end
@current_recipe = nil
# >-----------------------------[ Run Bundler ]-------------------------------<
say_wizard "Running 'bundle install'. This will take a while."
run 'bundle install'
say_wizard "Running 'after bundler' callbacks."
@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 app template."
say_wizard "Your new Rails app is ready. Any problems?"