-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails-wxmp-setup.rb
350 lines (294 loc) · 11.7 KB
/
rails-wxmp-setup.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
run "if uname | grep -q 'Darwin'; then pgrep spring | xargs kill -9; fi"
# GEMFILE
########################################
gsub_file('Gemfile', 'source \'https://rubygems.org\'', '')
add_source 'https://gems.ruby-china.com'
inject_into_file 'Gemfile', before: 'group :development, :test do' do
<<~RUBY
gem 'devise'
gem 'autoprefixer-rails', '10.2.5'
gem 'font-awesome-sass'
gem 'simple_form'
# For API calls
gem 'rest-client'
# Use Active Storage variant
gem 'image_processing', '~> 1.2'
# Alicloud storage
gem 'activestorage-aliyun'
# background jobs processing
gem 'sidekiq'
gem 'sidekiq-failures', '~> 1.0'
# authentication
gem 'jwt'
# handle money
gem 'money-rails', '~>1.12'
# models translation
gem 'json_translate'
# serializer
gem 'active_model_serializers', '~> 0.10.0'
RUBY
end
inject_into_file 'Gemfile', after: 'group :development, :test do' do
<<-RUBY
gem 'pry-byebug'
gem 'pry-rails'
RUBY
end
inject_into_file 'Gemfile', after: 'group :development do' do
<<-RUBY
# kill n+1 queries
gem 'bullet'
RUBY
end
gsub_file('Gemfile', /# gem 'redis'/, "gem 'redis'")
# Assets
########################################
run 'rm -rf app/assets/stylesheets'
run 'rm -rf vendor'
run 'curl -L https://github.com/lewagon/rails-stylesheets/archive/master.zip > stylesheets.zip'
run 'unzip stylesheets.zip -d app/assets && rm stylesheets.zip && mv app/assets/rails-stylesheets-master app/assets/stylesheets'
# Dev environment
########################################
gsub_file('config/environments/development.rb', /config\.assets\.debug.*/, 'config.assets.debug = false')
# Layout
########################################
if Rails.version < "6"
scripts = <<~HTML
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload', defer: true %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
HTML
gsub_file('app/views/layouts/application.html.erb', "<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>", scripts)
end
gsub_file('app/views/layouts/application.html.erb', "<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>", "<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload', defer: true %>")
style = <<~HTML
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
HTML
gsub_file('app/views/layouts/application.html.erb', "<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>", style)
# Flashes
########################################
file 'app/views/shared/_flashes.html.erb', <<~HTML
<% if notice %>
<div class="alert alert-info alert-dismissible fade show m-1" role="alert">
<%= notice %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
<% if alert %>
<div class="alert alert-warning alert-dismissible fade show m-1" role="alert">
<%= alert %>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<% end %>
HTML
run 'curl -L https://github.com/lewagon/awesome-navbars/raw/master/templates/_navbar_wagon.html.erb > app/views/shared/_navbar.html.erb'
inject_into_file 'app/views/layouts/application.html.erb', after: '<body>' do
<<-HTML
<%= render 'shared/navbar' %>
<%= render 'shared/flashes' %>
HTML
end
# README
########################################
markdown_file_content = <<-MARKDOWN
##Gems Setup:
- Authentication with **devise**
- Authentication with **jwt**
- Background jobs with **sidekiq**
- API calls with **rest-client**
- Handle money and currencies with **money-rails**. Default currency: ***CNY***
- Model translations with **json_translate**. Locales: :en, :cn
- Active Storage with **activestorage-aliyun**
## Credentials set-up
#### In order to use this template set up the following values in config/credentials.yml.enc
```yaml
wx_mp:
app_id: [YOUR-Mini-Program-APP-ID (provided by WECHAT)]
app_secret: [YOUR-Mini-Program-APP-SECRET (provided by WECHAT)]
aliyun_oss:
access_key_id: [YOUR-ALIYUN-KEY-ID]
access_key_secret: [YOUR-ALIYUN-KEY-SECRET]
bucket_name: [YOUR-ALIYUN-BUCKET-NAME (set by you when creating a bucket)]
endpoint: https://oss-cn-shanghai.aliyuncs.com (depends on your bucket location)
api_key: [YOUR-API-KEY]
jwt:
token_secret_key: [YOUR-JWT-TOKEN]
expiration: [TOKEN-EXPIRATION-IN-SECONDS (Recommended: 900)]
```
MARKDOWN
file 'README.md', markdown_file_content, force: true
# Generators
########################################
generators = <<~RUBY
config.generators do |generate|
generate.assets false
generate.helper false
generate.test_framework :test_unit, fixture: false
end
RUBY
environment generators
########################################
# AFTER BUNDLE
########################################
after_bundle do
# Get resources
########################################
run 'curl -L https://github.com/filser89/rails-wxmp-setup-resourses/archive/master.zip > resources.zip'
run 'unzip resources.zip && rm resources.zip && mv rails-wxmp-setup-resourses-master resources'
# Add services
########################################
run 'mv resources/services app/services'
# Generators: db + simple form + pages controller
########################################
rails_command 'db:drop db:create db:migrate'
generate('simple_form:install', '--bootstrap')
generate(:controller, 'pages', 'home', '--skip-routes', '--no-test-framework')
# Rails money config
########################################
generate('money_rails:initializer')
gsub_file('config/initializers/money.rb', /# config.default_currency = :usd/, "config.default_currency = :cny")
gsub_file('config/initializers/money.rb', /# config.locale_backend = :i18n/, "config.locale_backend = :i18n")
gsub_file('config/initializers/money.rb', /# config.rounding_mode = BigDecimal::ROUND_HALF_UP/, "config.rounding_mode = BigDecimal::ROUND_HALF_UP")
# locales config
########################################
file 'config/initializers/locale.rb', <<~RUBY
I18n.default_locale = :en
I18n.available_locales = [:en, 'zh-CN']
RUBY
run 'rm config/locales/en.yml'
run 'mv resources/locales/en.yml config/locales/en.yml'
run 'mv resources/locales/cn.yml config/locales/cn.yml'
environment 'config.i18n.fallbacks = true', env: 'development'
# sidekiq as job adapter
########################################
environment 'config.active_job.queue_adapter = :sidekiq', env: 'development'
run 'mv resources/workers app/workers'
# Aliyun
########################################
rails_command 'active_storage:install'
run 'rm config/storage.yml'
if Rails.version < "6.1"
settings = <<~YAML
test:
service: Disk
\s\sroot: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
\s\sroot: <%= Rails.root.join("storage") %>
aliyun:
service: Aliyun
\s\saccess_key_id: <%= Rails.application.credentials.dig(:aliyun_oss, :access_key_id) %>
\s\saccess_key_secret: <%= Rails.application.credentials.dig(:aliyun_oss, :access_key_secret) %>
\s\sbucket: <%= Rails.application.credentials.dig(:aliyun_oss, :bucket) %>
\s\sendpoint: <%= Rails.application.credentials.dig(:aliyun_oss, :endpoint) %>
\s\s# Bucket mode: [public, private], default: public
\s\smode: "private"
YAML
else
settings = <<~YAML
test:
service: Disk
\s\sroot: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
\s\sroot: <%= Rails.root.join("storage") %>
aliyun:
service: Aliyun
\s\saccess_key_id: <%= Rails.application.credentials.dig(:aliyun_oss, :access_key_id) %>
\s\saccess_key_secret: <%= Rails.application.credentials.dig(:aliyun_oss, :access_key_secret) %>
\s\sbucket: <%= Rails.application.credentials.dig(:aliyun_oss, :bucket) %>
\s\sendpoint: <%= Rails.application.credentials.dig(:aliyun_oss, :endpoint) %>
\s\spublic: false
YAML
end
file 'config/storage.yml', settings
gsub_file('config/environments/development.rb', /config\.active_storage\.service.*/, 'config.active_storage.service = :aliyun')
gsub_file('config/environments/production.rb', /config\.active_storage\.service.*/, 'config.active_storage.service = :aliyun')
# Serializer
########################################
generate('serializer', 'Application')
generate('serializer', 'User', 'mp_session_key', 'mp_openid', 'unionid')
# Routes
########################################
route "root to: 'pages#home'"
route <<~RUBY
namespace :api, defaults: { format: :json } do
namespace :v1 do
post 'users/wx_login', to: 'users#wx_login'
end
end
RUBY
# Git ignore
########################################
append_file '.gitignore', <<~TXT
# Ignore Mac and Linux file system files
*.swp
.DS_Store
TXT
# Devise install + user
########################################
generate('devise:install')
generate('devise', 'User mp_openid:string mp_session_key:string unionid:string')
#Controlles
########################################
run 'rm -rf app/controllers'
run 'mv resources/controllers app/controllers'
# migrate + devise views
########################################
rails_command 'db:migrate'
generate('devise:views')
# Environments
########################################
environment 'config.action_mailer.default_url_options = { host: "http://localhost:3000" }', env: 'development'
environment 'config.action_mailer.default_url_options = { host: "http://TODO_PUT_YOUR_DOMAIN_HERE" }', env: 'production'
environment "config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]"
environment "config.time_zone = 'Beijing'"
# Webpacker / Yarn
########################################
run 'yarn add popper.js jquery bootstrap@4.6'
append_file 'app/javascript/packs/application.js', <<~JS
// ----------------------------------------------------
// Note(lewagon): ABOVE IS RAILS DEFAULT CONFIGURATION
// WRITE YOUR OWN JS STARTING FROM HERE 👇
// ----------------------------------------------------
// External imports
import "bootstrap";
// Internal imports, e.g:
// import { initSelect2 } from '../components/init_select2';
document.addEventListener('turbolinks:load', () => {
// Call your functions here, e.g:
// initSelect2();
});
JS
inject_into_file 'config/webpack/environment.js', before: 'module.exports' do
<<~JS
const webpack = require('webpack');
// Preventing Babel from transpiling NodeModules packages
environment.loaders.delete('nodeModules');
// Bootstrap 4 has a dependency over jQuery & Popper.js:
environment.plugins.prepend('Provide',
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
Popper: ['popper.js', 'default']
})
);
JS
end
# Rubocop
########################################
run 'curl -L https://raw.githubusercontent.com/lewagon/rails-templates/master/.rubocop.yml > .rubocop.yml'
# Remove resources folder
########################################
run 'rm -rf resources'
# Git
########################################
git add: '.'
git commit: "-m 'Initial commit with rails-wxmp-setup config'"
# Fix puma config
gsub_file('config/puma.rb', 'pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }', '# pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }')
end