bankai 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class Base < Rails::Generators::Base
9
+ def self.default_source_root
10
+ File.expand_path(File.join('..', '..', '..', 'templates'), __dir__)
11
+ end
12
+
13
+ private
14
+
15
+ def app_name
16
+ Rails.app_class.parent_name.demodulize.underscore.dasherize
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class CiGenerator < Base
9
+ def configure_ci
10
+ template 'gitlab-ci.yml.erb', '.gitlab-ci.yml'
11
+ end
12
+
13
+ protected
14
+
15
+ def pg?
16
+ gemfile.match?(/gem .pg./)
17
+ end
18
+
19
+ def mysql?
20
+ gemfile.match?(/gem .mysql2./)
21
+ end
22
+
23
+ private
24
+
25
+ def gemfile
26
+ @gemfile ||= File.read(destination_root + '/Gemfile')
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class DbOptimizationsGenerator < Base
9
+ def configure_bullet
10
+ inject_into_file(
11
+ 'config/environments/development.rb',
12
+ configuration,
13
+ after: 'config.file_watcher = ' \
14
+ "ActiveSupport::EventedFileUpdateChecker\n"
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ def configuration
21
+ <<-RUBY
22
+
23
+ config.after_initialize do
24
+ Bullet.enable = true
25
+ Bullet.bullet_logger = true
26
+ Bullet.rails_logger = true
27
+ Bullet.add_footer = true
28
+ end
29
+ RUBY
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class DeployGenerator < Base
9
+ def install_capistrano
10
+ Bundler.with_clean_env { run 'bundle exec cap install' }
11
+ end
12
+
13
+ def configure_capfile
14
+ inject_into_file(
15
+ 'Capfile',
16
+ capistrano_plugins,
17
+ after: "# require \"capistrano/passenger\"\n"
18
+ )
19
+ end
20
+
21
+ def replace_deploy_config
22
+ template 'deploy.rb.erb', 'config/deploy.rb', force: true
23
+ end
24
+
25
+ protected
26
+
27
+ def repo_url
28
+ res = `git remote get-url origin`
29
+ return 'git@example.com:me/my_repo.git' if res.blank?
30
+
31
+ res
32
+ end
33
+
34
+ private
35
+
36
+ def capistrano_plugins
37
+ <<-RUBY
38
+ require "capistrano/bundler"
39
+ require "capistrano/rails/assets"
40
+ require "capistrano/rails/migrations"
41
+ RUBY
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class JsonGenerator < Base
9
+ def add_oj
10
+ gem 'oj'
11
+ Bundler.with_clean_env { run 'bundle install' }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class LintGenerator < Base
9
+ def configure_overcommit
10
+ template 'overcommit.yml.erb', '.overcommit.yml'
11
+ end
12
+
13
+ def configure_rubocop
14
+ template 'rubocop.yml.erb', '.rubocop.yml'
15
+ end
16
+
17
+ def install_overcommit
18
+ Bundler.with_clean_env { run 'bundle exec overcommit --install' }
19
+ end
20
+
21
+ def rubocop_autocorrect
22
+ Bundler.with_clean_env { run 'bundle exec rubocop --auto-correct' }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class MailerGenerator < Base
9
+ def configure_letter_opener
10
+ inject_into_file(
11
+ 'config/environments/development.rb',
12
+ configuration,
13
+ after: 'config.file_watcher = ' \
14
+ "ActiveSupport::EventedFileUpdateChecker\n"
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ def configuration
21
+ <<-RUBY
22
+
23
+ config.action_mailer.delivery_method = :letter_opener
24
+ config.action_mailer.perform_deliveries = true
25
+ RUBY
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Generators
7
+ # :nodoc:
8
+ class TestingGenerator < Base
9
+ def generate_rspec
10
+ generate 'rspec:install'
11
+ end
12
+
13
+ def configure_rspec
14
+ remove_file 'spec/rails_helper.rb'
15
+ remove_file 'spec/spec_helper.rb'
16
+ copy_file 'rails_helper.rb', 'spec/rails_helper.rb'
17
+ copy_file 'spec_helper.rb', 'spec/spec_helper.rb'
18
+ end
19
+
20
+ def provide_shoulda_matchers_config
21
+ copy_file(
22
+ 'spec/shoulda_matchers.rb',
23
+ 'spec/support/shoulda_matchers.rb'
24
+ )
25
+ end
26
+
27
+ def provide_database_rewinder_config
28
+ copy_file(
29
+ 'spec/database_rewinder.rb',
30
+ 'spec/support/database_rewinder.rb'
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module Bankai
2
+ VERSION = '0.1.0'.freeze
3
+ RUBY_VERSION = '2.5.0'.freeze
4
+ RAILS_VERSION = '5.2.2'.freeze
5
+ RUBOCOP_VERSION = '0.60.0'.freeze
6
+ CAPISTRANO_VERSION = '3.11.0'.freeze
7
+ end
data/lib/bankai.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'bankai/version'
2
+ require 'bankai/generator'
3
+ require 'bankai/generators/testing_generator'
4
+ require 'bankai/generators/ci_generator'
5
+ require 'bankai/generators/json_generator'
6
+ require 'bankai/generators/db_optimizations_generator'
7
+ require 'bankai/generators/mailer_generator'
8
+ require 'bankai/generators/deploy_generator'
9
+ require 'bankai/generators/lint_generator'
10
+ require 'bankai/builder'
@@ -0,0 +1,110 @@
1
+ source 'https://rubygems.org'
2
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
+
4
+ ruby '<%= RUBY_VERSION -%>'
5
+ <% gemfile_entries.each do |gem| -%>
6
+ <% if gem.comment -%>
7
+
8
+ # <%= gem.comment %>
9
+ <% end -%>
10
+ <%= gem.commented_out ? '# ' : '' %>gem '<%= gem.name %>'<%= %(, '#{gem.version}') if gem.version -%>
11
+ <% if gem.options.any? -%>
12
+ , <%= gem.options.map { |k,v|
13
+ "#{k}: #{v.inspect}" }.join(', ') %>
14
+ <% end -%>
15
+ <% end -%>
16
+
17
+ # Use ActiveModel has_secure_password
18
+ # gem 'bcrypt', '~> 3.1.7'
19
+ <% unless skip_active_storage? -%>
20
+
21
+ # Use ActiveStorage variant
22
+ # gem 'mini_magick', '~> 4.8'
23
+ <% end -%>
24
+
25
+ <% if depend_on_bootsnap? -%>
26
+ # Reduces boot times through caching; required in config/boot.rb
27
+ gem 'bootsnap', '>= 1.1.0', require: false
28
+
29
+ <%- end -%>
30
+ <%- if options.api? -%>
31
+ # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
32
+ # gem 'rack-cors'
33
+
34
+ <%- end -%>
35
+ <% if RUBY_ENGINE == 'ruby' -%>
36
+ group :development, :test do
37
+ # Call 'byebug' anywhere in the code to
38
+ # stop execution and get a debugger console
39
+ gem 'byebug', platforms: %i[mri mingw x64_mingw]
40
+
41
+ gem 'brakeman', require: false
42
+ gem 'bundler-audit', '~> 0.6.0', require: false
43
+ gem 'rubocop', '~> <%= Bankai::RUBOCOP_VERSION %>', require: false
44
+
45
+ <%- unless options[:skip_rspec] -%>
46
+ gem 'database_rewinder'
47
+ gem 'factory_bot_rails'
48
+ gem 'faker'
49
+ gem 'rspec', require: false
50
+ gem 'rspec-rails', require: false
51
+ gem 'shoulda', require: false
52
+ gem 'shoulda-matchers', require: false
53
+ gem 'simplecov', require: false
54
+ <%- end -%>
55
+
56
+ gem 'bankai'
57
+ end
58
+
59
+ group :development do
60
+ <% if depend_on_listen? -%>
61
+ gem 'listen', '>= 3.0.5', '< 3.2'
62
+ <% end -%>
63
+ <%- unless options.api? -%>
64
+ # Access an interactive console on exception pages or
65
+ # by calling 'console' anywhere in the code.
66
+ <%- if options.dev? || options.edge? -%>
67
+ gem 'web-console', github: 'rails/web-console'
68
+ <%- else -%>
69
+ gem 'web-console', '>= 3.3.0'
70
+ <%- end -%>
71
+ <%- end -%>
72
+ <% if spring_install? -%>
73
+ # Spring speeds up development by keeping your application running in
74
+ # the background. Read more: https://github.com/rails/spring
75
+ gem 'spring'
76
+ <% if depend_on_listen? -%>
77
+ gem 'spring-watcher-listen', '~> 2.0.0'
78
+ <% end -%>
79
+ <% end -%>
80
+
81
+ <% unless options[:skip_capistrano] -%>
82
+ gem 'capistrano', '<%= Bankai::CAPISTRANO_VERSION %>'
83
+ gem 'capistrano-bundler'
84
+ gem 'capistrano-rails'
85
+ # gem 'capistrano-sidekiq'
86
+ # gem 'capistrano-passenger'
87
+ <% end -%>
88
+
89
+ gem 'annotate', require: false
90
+ gem 'bullet'
91
+ gem 'dotenv-rails'
92
+ gem 'letter_opener'
93
+ gem 'overcommit', require: false
94
+ gem 'pry-rails'
95
+ gem 'rack-mini-profiler', require: false
96
+ end
97
+
98
+ <%- if depends_on_system_test? -%>
99
+ group :test do
100
+ # Adds support for Capybara system testing and selenium driver
101
+ gem 'capybara', '>= 2.15'
102
+ gem 'selenium-webdriver'
103
+ # Easy installation and use of chromedriver to run system tests with Chrome
104
+ gem 'chromedriver-helper'
105
+ end
106
+ <%- end -%>
107
+ <% end -%>
108
+
109
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
110
+ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
@@ -0,0 +1,233 @@
1
+ <%= app_name.humanize %>
2
+ ===
3
+
4
+ ## 系統需求
5
+
6
+ * Ruby <%= RUBY_VERSION %>
7
+ <%- case options[:database] -%>
8
+ <%- when 'postgresql' then -%>* PostgreSQL 9.6+
9
+ <%- when 'mysql' then -%>* MySQL 5.7+
10
+ <%- end -%>
11
+ * Node.js 8.0 (以上)
12
+
13
+ ## 環境設定
14
+
15
+ 以下的設定皆以 macOS 為主。
16
+
17
+ ### Homebrew
18
+
19
+ 在 macOS 需要有 [Homebrew](https://brew.sh/index_zh-tw) 來輔助安裝環境。
20
+
21
+ ```bash
22
+ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
23
+ ```
24
+
25
+ 執行完畢後可以透過 `brew doctor` 確認是否可用。
26
+
27
+ ### Ruby
28
+
29
+ 為了配合多個版本的環境,建議使用 `rbenv` 或者 `rvm` 來管理 Ruby 環境。
30
+
31
+ ```bash
32
+ # 選用 rbenv
33
+ brew install rbenv
34
+
35
+ # 選用 rvm
36
+ brew install rvm
37
+ ```
38
+
39
+ 完成後請參考終端機顯示的訊息設定 `.bashrc` 或者其他 Shell 設定檔。
40
+
41
+ ```bash
42
+ # 選用 rbenv
43
+ rbenv install <%= RUBY_VERSION %>
44
+
45
+ # 選用 rvm
46
+ rvm install <%= RUBY_VERSION %>
47
+ ```
48
+
49
+ 完成後需要先將 Bundler 安裝到新安裝的 Ruby 環境中(rvm 可能會先預裝完畢)
50
+
51
+ ```bash
52
+ # 先確認是否在正確的 Ruby 版本執行
53
+ ruby -v
54
+ # => ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin17]
55
+
56
+ gem install bundler
57
+ ```
58
+ <%- case options[:database] -%>
59
+ <%- when 'postgresql' -%>
60
+ ### PostgreSQL
61
+
62
+ 請注意不要直接輸入 `postgresql` 否則會安裝到最新版的 PostgreSQL
63
+
64
+ ```bash
65
+ # 安裝
66
+ brew install postgresql@9.6
67
+
68
+ # 啟動伺服器
69
+ brew services start postgresql@9.6
70
+ ```
71
+
72
+ <%- when 'mysql' -%>
73
+ ### MySQL
74
+
75
+ 請注意不要直接輸入 `mysql` 否則會安裝到最新版的 MySQL
76
+
77
+ ```bash
78
+ # 安裝
79
+ brew install mysql@5.7
80
+
81
+ # 啟動伺服器
82
+ brew services start mysql@5.7
83
+ ```
84
+
85
+ 啟動之前請參考終端機顯示的安裝訊息來初始化 MySQL 資料庫
86
+
87
+ <%- end -%>
88
+ ### Node.js
89
+
90
+ Rails 需要 JavaScript Runtime 一般會使用 Node.js。
91
+
92
+ ```bash
93
+ brew install node
94
+ ```
95
+
96
+ <%- if options[:webpack] -%>
97
+ Webpacker 使用 Yarn 管理安裝的 JavaScript 套件,我們還需要將 Yarn 安裝進來。
98
+
99
+ ```bash
100
+ brew install yarn
101
+ ```
102
+
103
+ <%- end -%>
104
+ ### Rails
105
+
106
+ 請先透過 git 將專案下載到本地端。
107
+
108
+ ```bash
109
+ # 切換到專案目錄
110
+ cd <%= app_name %>
111
+
112
+ # 安裝 Rails 所需套件
113
+ bundle install
114
+
115
+ # 設定 git hook
116
+ bundle exec overcommit --install
117
+ ```
118
+
119
+ Overcommit 會做以下檢查:
120
+
121
+ 1. commit 前:使用 rubocop 檢查語法
122
+ 2. push 前:使用 brakeman 檢查安全性問題
123
+
124
+ ```bash
125
+ # 設定資料庫
126
+ bundle exec rake db:create
127
+
128
+ # 更新資料庫到最新版
129
+ bundle exec rake db:migrate
130
+ ```
131
+
132
+ <%- if options[:database] == 'mysql' -%>
133
+ #### 執行 bundle install 時 mysql2 gem 安裝失敗
134
+
135
+ 這可能是因為我們指定了 MySQL 版本,但是作業系統裡面並不知道有 MySQL 的存在,造成自動安裝失敗。
136
+ 我們可以透過以下指令手動安裝:
137
+
138
+ ```
139
+ # 0.5.2 是目前的版本,之後有更新情參考錯誤訊息指示的版本
140
+ gem install mysql2 -v 0.5.2 -- --with-mysql-config=/usr/local/opt/mysql@5.7/bin/mysql_config
141
+ ```
142
+
143
+ <%- end -%>
144
+ ## 執行專案
145
+
146
+ ### Rails 伺服器
147
+
148
+ ```bash
149
+ # 這是 rails server 的縮寫
150
+ rails s
151
+ ```
152
+
153
+ 開啟後預設可以透過 `http://localhost:3000` 看到網站
154
+
155
+ #### PendingMigration 錯誤
156
+
157
+ 這是因為最新版本的資料庫已經被更改,但是本機的資料庫還沒有被更新。
158
+
159
+ ```bash
160
+ bundle exec rake db:migrate
161
+ ```
162
+
163
+ 執行 `db:migrate` 指令更新資料庫即可。
164
+
165
+ <%- if options[:webpack] -%>
166
+ ### Webpack 伺服器
167
+
168
+ 因為專案使用了 Webpacker 所以需要開啟 Webpack 伺服器來轉換 JavaScript
169
+
170
+ ```bash
171
+ ./bin/webpack-dev-server
172
+ ```
173
+
174
+ 和 Rails 伺服器同時打開後,就可以看到正常的網站畫面
175
+
176
+ <%- end -%>
177
+ <%- unless options[:skip_rspec] -%>
178
+ ### 運行測試
179
+
180
+ 這個專案使用 RSpec 進行測試,可以透過執行以下指令運行
181
+
182
+ ```bash
183
+ bundle exec rspec
184
+ ```
185
+
186
+ <%- end -%>
187
+ ### Ruby 語法檢查
188
+
189
+ 這個功能會在 commit 前自動執行,必要時可以手動進行
190
+
191
+ ```bash
192
+ bundle exec rubocop
193
+ ```
194
+
195
+ ### Ruby 安全性檢查
196
+
197
+ 這個功能會在 push 前自動執行,必要時可以手動進行
198
+
199
+ ```bash
200
+ bundle exec brakeman
201
+ ```
202
+
203
+ ### 建立 .env 檔案
204
+
205
+ 建立 `.env` 檔案,並依據 local 開發環境需求修改設定內容。
206
+
207
+ ```bash
208
+ cp .env.example .env
209
+ ```
210
+
211
+ <%- unless options[:skip_capistrano] -%>
212
+ ## 部署
213
+
214
+ 這個專案使用 Capistrano 進行部署,請先確定有權限透過 `deployer` 帳號 SSH 到伺服器上。
215
+
216
+ ```bash
217
+ cap [ENV] deploy
218
+ ```
219
+
220
+ 假設要部署到測試(Staging)環境,請使用以下指令
221
+
222
+ ```bash
223
+ cap staging deploy
224
+ ```
225
+
226
+ 如果是正式環境,則使用以下指令
227
+
228
+ ```bash
229
+ cap production deploy
230
+ ```
231
+
232
+ 過程中會詢問要使用的 Git Branch,預設為 master branch,如果需要測試某個 Git Branch 請先將他上傳到 GitHub 上,並且和團隊成員確認後手動輸入 branch 再進行部署跟測試。
233
+ <%- end -%>