forked from dao42/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer.rb
298 lines (260 loc) · 8.32 KB
/
composer.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
def remove_gem(*names)
names.each do |name|
gsub_file 'Gemfile', /gem '#{name}'.*\n/, ''
end
end
def remove_comment_of_gem
gsub_file('Gemfile', /^\s*#.*$\n/, '')
end
def replace_myapp(file)
gsub_file file, /myapp/, app_name, verbose: false
end
def get_remote(src, dest = nil)
dest ||= src
if ENV['RAILS_TEMPLATE_DEBUG'].present?
repo = File.join(File.dirname(__FILE__), 'files/')
else
repo = 'https://raw.githubusercontent.com/dao42/rails-template/master/files/'
end
remote_file = repo + src
get(remote_file, dest, force: true)
replace_myapp(dest)
end
def get_remote_dir(names, dir)
names.each do |name|
src = File.join(dir, name)
get_remote(src)
end
end
def yarn(lib)
run("yarn add #{lib}")
end
def remove_dir(dir)
run("rm -rf #{dir}")
end
remove_comment_of_gem
# gitignore
get_remote('gitignore', '.gitignore')
# lock sprockets to 3.7 ignore this issue: https://github.com/rails/sprockets-rails/issues/444
gem 'sprockets-rails', '~> 3.2.1'
gem 'sprockets', '~> 3.7.2'
gem 'sassc-rails'
# postgresql
say 'Applying postgresql...'
remove_gem('sqlite3')
gem 'pg', '>= 1.1'
get_remote('config/database.yml.example')
get_remote('config/database.yml.example', 'config/database.yml')
# environment variables set
say 'Applying figaro...'
gem 'figaro'
get_remote('config/application.yml.example')
get_remote('config/application.yml.example', 'config/application.yml')
get_remote('config/spring.rb')
after_bundle do
say "Stop spring if exists"
run "spring stop"
end
say 'Applying jquery & font-awesome & bootstrap4...'
after_bundle do
yarn 'webpack@^4.0.0'
yarn 'jquery@^3.3.1'
yarn 'expose-loader'
inject_into_file 'config/webpack/environment.js', after: "const { environment } = require('@rails/webpacker')\n" do <<~EOF
const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
}))
environment.loaders.append('expose', {
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: '$'
}, {
loader: 'expose-loader',
options: 'jQuery',
}]
})
EOF
end
yarn '@fortawesome/fontawesome-free@^5.9.0'
yarn 'popper.js@^1.14.7'
yarn 'bootstrap@^4.3.1'
end
remove_dir 'app/assets'
jss = [ 'base.js' ]
get_remote_dir(jss, 'app/javascript/js')
images = [ 'favicon.ico' ]
get_remote_dir(images, 'app/javascript/images')
styles = [ 'application.scss', 'bootstrap_custom.scss', 'home.scss' ]
get_remote_dir(styles, 'app/javascript/styles')
packs = [ 'application.js' ]
get_remote_dir(packs, 'app/javascript/packs')
say 'Applying google anlytics...'
packs = [ 'ga.js.erb' ]
get_remote_dir(packs, 'app/javascript/packs')
after_bundle do
rails_command 'webpacker:install:erb'
remove_file('app/javascript/packs/hello_erb.js.erb')
end
say 'Applying simple_form...'
gem 'simple_form', '~> 4.1'
after_bundle do
generate 'simple_form:install', '--bootstrap'
end
say 'Applying slim...'
gem 'slim-rails'
say 'Applying high_voltage...'
gem 'high_voltage', '~> 3.1'
get_remote('app/controllers/home_controller.rb')
get_remote('app/helpers/application_helper.rb')
get_remote('app/views/home/index.html.slim')
get_remote('app/views/pages/about.html.slim')
remove_file('app/views/layouts/application.html.erb')
get_remote('app/views/layouts/application.html.slim')
say 'Applying action cable config...'
inject_into_file 'config/environments/production.rb', after: "# Mount Action Cable outside main process or domain.\n" do <<-EOF
config.action_cable.allowed_request_origins = [ "\#{ENV['PROTOCOL']}://\#{ENV['DOMAIN']}" ]
EOF
end
# active_storage
say 'Applying active_storage...'
after_bundle do
rails_command 'active_storage:install'
end
say "Applying browser_warrior..."
gem 'browser_warrior', '>= 0.11.0'
after_bundle do
generate 'browser_warrior:install'
end
say 'Applying redis & sidekiq...'
gem 'sidekiq', '~> 5'
get_remote('config/initializers/sidekiq.rb')
get_remote('config/sidekiq.yml')
get_remote('config/routes.rb')
get_remote('config/secret.yml')
say 'Applying adminlte 3...'
after_bundle do
yarn 'admin-lte@^3.0.0-beta.1'
yarn 'daterangepicker@^3.0.5'
yarn 'moment-timezone'
yarn 'tempusdominus-core'
end
styles = [ 'admin.scss' ]
get_remote_dir(styles, 'app/javascript/styles')
images = [ 'admin-user.jpg', 'logo.png' ]
get_remote_dir(images, 'app/javascript/images')
packs = [ 'admin.js' ]
get_remote_dir(packs, 'app/javascript/packs')
admin_jss = [ 'sidebar.js' ]
get_remote_dir(admin_jss, 'app/javascript/js/admin')
controllers = [ 'accounts_controller.rb', 'base_controller.rb', 'dashboard_controller.rb', 'sessions_controller.rb' ]
get_remote_dir(controllers, 'app/controllers/admin')
accounts_views = [ 'edit.html.slim' ]
get_remote_dir(accounts_views, 'app/views/admin/accounts')
dashboard_views = [ 'index.html.slim' ]
get_remote_dir(dashboard_views, 'app/views/admin/dashboard')
sessions_views = [ 'new.html.slim' ]
get_remote_dir(sessions_views, 'app/views/admin/sessions')
shared_admin_layouts = [ '_flash_messages.html.slim', '_header.html.slim', '_sidebar.html.slim' ]
get_remote_dir(shared_admin_layouts, 'app/views/shared/admin')
admin_layouts = [ 'admin.html.slim' ]
get_remote_dir(admin_layouts, 'app/views/layouts')
inject_into_file 'config/routes.rb', after: "Rails.application.routes.draw do\n" do <<-EOF
namespace :admin do
get 'login', to: 'sessions#new', as: :login
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy', as: :logout
resource :account, only: [:edit, :update]
root to: 'dashboard#index'
end
EOF
end
# secure access for sidekiq
inject_into_file 'config/routes.rb', after: "Sidekiq::Web.set :session_secret, Rails.application.secrets[:secret_key_base]\n" do <<-EOF
class AdminConstraint
def matches?(request)
return false unless request.session[:current_admin_id].present?
admin = Administrator.find_by(id: request.session[:current_admin_id])
admin.present?
end
end
EOF
end
inject_into_file 'config/routes.rb', after: "mount Sidekiq::Web => '/sidekiq'" do
', constraints: AdminConstraint.new'
end
# add db seeds
get_remote('db/seeds.rb')
gem 'bcrypt'
after_bundle do
generate(:model, 'administrator', 'name:string:uniq:index', 'password:digest')
inject_into_file 'app/models/administrator.rb', after: "class Administrator < ApplicationRecord\n" do <<-EOF
validates :name, presence: true, uniqueness: true
EOF
end
end
say 'Applying kaminari & rails-i18n...'
gem 'kaminari', '~> 1.1.1'
gem 'rails-i18n', '~> 6.0.0'
after_bundle do
generate 'kaminari:config'
generate 'kaminari:views', 'bootstrap4'
end
say 'Applying mina & its plugins...'
gem 'mina', '~> 1.2.2', require: false
gem 'mina-ng-puma', '>= 1.4.0', require: false
gem 'mina-multistage', require: false
gem 'mina-sidekiq', require: false
gem 'mina-logs', require: false
get_remote('config/deploy.rb')
get_remote('config/puma.rb')
get_remote('config/deploy/production.rb')
get_remote('config/nginx.conf.example')
get_remote('config/nginx.ssl.conf.example')
get_remote('config/logrotate.conf.example')
get_remote('config/monit.conf.example')
get_remote('config/backup.rb.example')
say 'Applying application config...'
inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<-EOF
config.generators.assets = false
config.generators.helper = false
config.time_zone = 'Beijing'
config.i18n.available_locales = [:en, :'zh-CN']
config.i18n.default_locale = :'zh-CN'
EOF
end
say 'Applying rspec test framework...'
gem_group :development do
gem 'rails_apps_testing'
end
gem_group :development, :test do
gem 'rspec-rails'
gem 'factory_bot_rails'
end
gem_group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'launchy'
gem 'selenium-webdriver'
end
after_bundle do
generate 'testing:configure', 'rspec --force'
end
get_remote 'README.md'
get_remote 'ackrc', '.ackrc'
get_remote 'bin/setup'
after_bundle do
say 'Almost done! Now init `git` and `database`...'
rails_command 'db:drop'
rails_command 'db:create'
rails_command 'db:migrate'
rails_command 'db:seed'
git :init
git add: '.'
git commit: '-m "init rails with dao42/rails-template"'
say "Build successfully! `cd #{app_name}` First, then start `./bin/webpack-dev-server` first, input `rails s` to start your rails app..."
end