-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.rb
210 lines (172 loc) · 4.68 KB
/
basic.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
gsub_file 'Gemfile', "# gem 'therubyracer', platforms: :ruby",
"gem 'mini_racer', platforms: :ruby"
gem_group :development, :test do
gem 'brakeman'
gem 'bullet'
gem 'rspec-rails'
gem 'simplecov'
gem 'factory_girl_rails'
gem 'pry-rails'
gem 'rubocop', require: false
end
gem_group :test do
gem 'capybara'
gem 'selenium-webdriver'
end
inject_into_file 'config/database.yml',
after: "default: &default\n" do
<<-YAML
host: db
username: #{app_name}
password: password
YAML
end
environment <<-RUBY
config.generators do |g|
g.test_framework = :rspec
g.javascript_engine = :js
end
RUBY
%w[development test].each do |e|
environment(nil, env: e) do
<<~RUBY
config.after_initialize do
Bullet.enable = true
Bullet.raise = true
Bullet.rails_logger = true
end
RUBY
end
end
file 'docker-compose.yml', <<-YML
version: '2'
volumes:
pg_data: {}
services:
redis:
image: redis
db:
image: postgres:9.5
ports:
- "5432:5432"
volumes:
- pg_data:/var/lib/postgresql/data/pgdata
environment:
POSTGRES_USER: #{app_name}
POSTGRES_PASSWORD: password
POSTGRES_DB: #{app_name}_development
PGDATA: /var/lib/postgresql/data/pgdata
app:
build:
context: .
dockerfile: Dockerfile.dev
command: bundle exec rails s -b 0.0.0.0
volumes:
- .:/app:z
ports:
- "3000:3000"
links:
- redis
- db
environment:
- DOCKERIZED=true
YML
@maintainer = ask('Dockerfile maintainer? Eg. John Doe <[email protected]>')
file 'Dockerfile', <<-DOCKER
# Build minimal production image. NOTICE that you have to precompile rails assets
# before building this image.
#
# rails assets:precompile
FROM ruby:2.4-alpine3.6
MAINTAINER "#{@maintainer}"
ENV BUILD_PACKAGES="curl-dev ruby-dev build-base bash zlib-dev libxml2-dev libxslt-dev yaml-dev postgresql-dev" \
RUBY_PACKAGES="ruby yaml libstdc++ postgresql-libs libxml2 tzdata"
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN apk update && \
apk upgrade && \
apk add $RUBY_PACKAGES && \
apk add --virtual build-deps $BUILD_PACKAGES && \
echo "gem: --no-document" >> /etc/gemrc && \
bundle install --without development test assets && \
apk del build-deps && \
rm -rf /var/cache/apk/*
# Setup Rails application
# =======================
ADD . /app
EXPOSE 3000
ENV RAILS_LOG_TO_STDOUT on
ENV RAILS_ENV production
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
DOCKER
file 'Dockerfile.dev', <<-DOCKER
# vim: ft=dockerfile
FROM ruby:2.4-alpine3.6
MAINTAINER "#{@maintainer}"
ENV BUILD_PACKAGES="curl-dev ruby-dev build-base bash" \
DEV_PACKAGES="zlib-dev libxml2-dev libxslt-dev yaml-dev postgresql-dev" \
RUBY_PACKAGES="ruby-json yaml tzdata nodejs yarn"
# Update and install base packages and nokogiri gem that requires a
# native compilation
RUN apk update && \
apk upgrade && \
apk add --update\
$BUILD_PACKAGES \
$DEV_PACKAGES \
$RUBY_PACKAGES && \
rm -rf /var/cache/apk/* && \
echo "gem: --no-document" >> /etc/gemrc
# Setup Rails application
# =======================
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
EXPOSE 3000
ENV RAILS_LOG_TO_STDOUT on
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
DOCKER
build_docker = yes?('Build Docker images?')
after_bundle do
generate 'rspec:install'
file 'spec/support/factory_girl.rb', <<-FACTORY_GIRL
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
FACTORY_GIRL
inject_into_file 'spec/spec_helper.rb',
before: 'RSpec.configure do |config|' do
<<~RB
require 'simplecov'
SimpleCov.start 'rails'
RB
end
inject_into_file 'spec/rails_helper.rb',
after: /# Add additional requires.*/ do
<<~RB
require 'support/factory_girl'
require 'selenium/webdriver'
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
desired_capabilities: capabilities
end
Capybara.javascript_driver = :headless_chrome
RB
end
run 'brakeman --rake' unless File.exist? 'lib/tasks/brakeman.rake'
if build_docker
run 'docker-compose build'
run 'docker-compose run -e RAILS_ENV=test app rake db:create'
end
end