Skip to content

Commit

Permalink
Refactor heredoc (#319)
Browse files Browse the repository at this point in the history
* Refactor heredoc

* Fix lint warning

* Add README and update heredocs on the main template file
  • Loading branch information
rosle authored Mar 16, 2022
1 parent 3bf65e1 commit 0e40c04
Show file tree
Hide file tree
Showing 32 changed files with 227 additions and 207 deletions.
14 changes: 7 additions & 7 deletions .gitignore.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
append_to_file '.gitignore' do
<<~EOT
<<~IGNORE
# Ignore folder information and IDE-specific files
.DS_Store
.idea/*
# Ignore folder information and IDE-specific files
.DS_Store
.idea/*
# Ignore the test coverage results from SimpleCov
/coverage
EOT
# Ignore the test coverage results from SimpleCov
/coverage
IGNORE
end
8 changes: 4 additions & 4 deletions .template/addons/devise/Gemfile.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
insert_into_file 'Gemfile', after: /# Authentications & Authorizations.*\n/ do
<<~GEM
<<~RUBY
gem 'devise' # Authentication solution for Rails with Warden
GEM
RUBY
end

insert_into_file 'Gemfile', after: /# Translations.*\n/ do
<<~GEM
<<~RUBY
# gem 'devise-i18n' # Translations for Devise
GEM
RUBY
end
12 changes: 6 additions & 6 deletions .template/addons/devise/spec_support.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
file 'spec/support/devise.rb', <<-CODE
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
end
CODE
file 'spec/support/devise.rb', <<~RUBY
RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
end
RUBY
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
insert_into_file 'bin/start.sh', after: "fi\n" do
<<-EOT
<<~SHELL
./bin/inject_port_into_nginx.sh
./bin/inject_port_into_nginx.sh
nginx -c /etc/nginx/conf.d/default.conf
EOT
nginx -c /etc/nginx/conf.d/default.conf
SHELL
end

gsub_file('bin/start.sh', 'bundle exec rails s -p $PORT -b 0.0.0.0', 'bundle exec rails s -p 3000 -b 0.0.0.0')
2 changes: 1 addition & 1 deletion .template/addons/nginx/bin/template.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use_source_path __dir__

apply 'start.sh'
apply 'start.sh.rb'
copy_file 'bin/inject_port_into_nginx.sh', mode: :preserve
8 changes: 4 additions & 4 deletions .template/addons/nginx/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
end

insert_into_file 'Dockerfile', after: %r(WORKDIR.+\n) do
<<-EOT
<<~DOCKERFILE
# Nginx config
COPY config/nginx/app.conf.template /etc/nginx/conf.d/default.conf
EOT
# Nginx config
COPY config/nginx/app.conf.template /etc/nginx/conf.d/default.conf
DOCKERFILE
end

gsub_file 'Dockerfile', /PORT=80/ do
Expand Down
34 changes: 17 additions & 17 deletions .template/addons/phrase_app/spec/codebase/codebase_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
insert_into_file 'spec/codebase/codebase_spec.rb', before: %r{end\Z} do
<<-EOT
<<~RUBY.indent(2)
# rubocop:disable RSpec/ExampleLength
it 'does not offend PhraseApp Pull configuration' do
locale_tags = Dir[File.expand_path(Rails.root.join('config', 'locales', '*.yml'))]
.map { |path| path.split('/').last } # ["campaign.en.yml", "article.en.yml", "campaign.th.yml"]
.map { |path| path.split('.').first } # ["campaign", "article", "campaign"]
.uniq # ["campaign", "article"]
.sort # ["article", "campaign"]
phrase_app_pull_tags = YAML
.load_file(Rails.root.join('.phraseapp.yml'))
.dig('phraseapp', 'pull', 'targets')
.map { |target| target.dig('params', 'tags') }
.sort
# rubocop:disable RSpec/ExampleLength
it 'does not offend PhraseApp Pull configuration' do
locale_tags = Dir[File.expand_path(Rails.root.join('config', 'locales', '*.yml'))]
.map { |path| path.split('/').last } # ["campaign.en.yml", "article.en.yml", "campaign.th.yml"]
.map { |path| path.split('.').first } # ["campaign", "article", "campaign"]
.uniq # ["campaign", "article"]
.sort # ["article", "campaign"]
phrase_app_pull_tags = YAML
.load_file(Rails.root.join('.phraseapp.yml'))
.dig('phraseapp', 'pull', 'targets')
.map { |target| target.dig('params', 'tags') }
.sort
expect(phrase_app_pull_tags).to eq locale_tags
end
# rubocop:enable RSpec/ExampleLength
EOT
expect(phrase_app_pull_tags).to eq locale_tags
end
# rubocop:enable RSpec/ExampleLength
RUBY
end
4 changes: 2 additions & 2 deletions .template/addons/slim/Dangerfile.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

insert_into_file 'Dangerfile', after: /eslint.lint.*\n/ do
<<~DANGER_PLUGIN
<<~RUBY
# Runs slim-lint on modified and added files in the PR
slim_lint.lint
DANGER_PLUGIN
RUBY
end
8 changes: 4 additions & 4 deletions .template/addons/slim/Gemfile.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true

insert_into_file 'Gemfile', after: /gem 'pundit'.*\n/ do
<<~GEM
<<~RUBY
# Templating
gem 'slim' # light weight template engine
GEM
RUBY
end

############################
# Group: :development, :test
############################

insert_into_file 'Gemfile', after: /gem 'danger'.*\n/ do
<<~GEM.indent(2)
<<~RUBY.indent(2)
gem 'danger-slim_lint' # Lint slim files.
GEM
RUBY
end
8 changes: 4 additions & 4 deletions .template/spec/base/config/environments/development_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
private

def mailer_default_url_config
<<~EOT
<<~RUBY
config.action_mailer.default_url_options = {
host: ENV.fetch('MAILER_DEFAULT_HOST'),
port: ENV.fetch('MAILER_DEFAULT_PORT')
}
EOT
RUBY
end

def bullet_config
<<~EOT
<<~RUBY
# Configure Bullet gem to detect N+1 queries
config.after_initialize do
Bullet.enable = true
Expand All @@ -38,6 +38,6 @@ def bullet_config
Bullet.rails_logger = true
Bullet.add_footer = true
end
EOT
RUBY
end
end
20 changes: 10 additions & 10 deletions .template/spec/base/config/environments/production_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@
it 'configures the mailer default url options' do
expect(subject).to contain(mailer_default_url_config)
end

it 'adds the i18n configuration' do
expect(subject).to contain(i18n_config)
end

it 'removes the config.i18n.fallbacks = true' do
expect(subject).not_to contain("config.i18n.fallbacks = true")
end

private

def mailer_default_url_config
<<~EOT
<<~RUBY
config.action_mailer.default_url_options = {
host: ENV.fetch('MAILER_DEFAULT_HOST'),
port: ENV.fetch('MAILER_DEFAULT_PORT')
}
EOT
RUBY
end

def i18n_config
<<~EOT
<<~RUBY
# eg: AVAILABLE_LOCALES = 'en,th'
config.i18n.available_locales = ENV.fetch('AVAILABLE_LOCALES').split(',')
# eg: DEFAULT_LOCALE = 'en'
config.i18n.default_locale = ENV.fetch('DEFAULT_LOCALE')
# eg: FALLBACK_LOCALES = 'en,th'
config.i18n.fallbacks = ENV.fetch('FALLBACK_LOCALES').split(',')
EOT
RUBY
end
end
8 changes: 4 additions & 4 deletions .template/spec/base/config/environments/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
private

def mailer_default_url_config
<<~EOT
<<~RUBY
config.action_mailer.default_url_options = {
host: ENV.fetch('MAILER_DEFAULT_HOST'),
port: ENV.fetch('MAILER_DEFAULT_PORT')
}
EOT
RUBY
end

def bullet_config
<<~EOT
<<~RUBY
# Configure Bullet gem to detect N+1 queries
config.after_initialize do
Bullet.enable = true
Bullet.bullet_logger = true
Bullet.raise = true
Bullet.unused_eager_loading_enable = false
end
EOT
RUBY
end
end
8 changes: 4 additions & 4 deletions .template/spec/variants/web/spec/codebase/codebase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
private

def scss_lint_example
<<~EOT
<<~RUBY
it 'does not offend stylelint' do
expect(`yarn run stylelint ./`).to be_empty
end
EOT
RUBY
end

def eslint_example
<<~EOT
<<~RUBY
it 'does not offend eslint' do
expect(`yarn run eslint ./`).to include 'Done'
end
EOT
RUBY
end
end
6 changes: 3 additions & 3 deletions .template/variants/api/app/template.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Controllers
directory 'app/controllers/concerns'
inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController' do
<<-EOT
include Localization
EOT
<<~RUBY.indent(2)
include Localization
RUBY
end
4 changes: 2 additions & 2 deletions .template/variants/web/.gitignore.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
append_to_file '.gitignore' do
<<~EOT
<<~IGNORE
# Ignore i18n.js generated files
# If deploy to heroku with git, please remove this as it prevents the files to be committed
/app/javascript/translations/*
Expand All @@ -9,5 +9,5 @@
# Ignore Node dependencies
/node_modules
EOT
IGNORE
end
4 changes: 2 additions & 2 deletions .template/variants/web/.tool-versions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
append_to_file '.tool-versions' do
<<~EOT
<<~TOOL_VERSION
nodejs #{NODE_VERSION}
EOT
TOOL_VERSION
end
4 changes: 2 additions & 2 deletions .template/variants/web/Dangerfile.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
insert_into_file 'Dangerfile', after: %r{suggester.suggest.*\n} do
<<~EOT
<<~RUBY
# Runs ESLint on modified and added files in the PR
eslint.lint
EOT
RUBY
end
20 changes: 10 additions & 10 deletions .template/variants/web/Gemfile.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
insert_into_file 'Gemfile', after: %r{gem 'rails-i18n'.*\n} do
<<~EOT
<<~RUBY
gem 'i18n-js', '3.9.0' # A library to provide the I18n translations on the Javascript
EOT
RUBY
end

insert_into_file 'Gemfile', after: %r{gem 'pundit'.*\n} do
<<~EOT
<<~RUBY
# Assets
gem 'sprockets-rails' # The original asset pipeline for Rails
Expand All @@ -14,33 +14,33 @@
# gem 'turbo-rails' # Hotwire's SPA-like page accelerator
# gem 'stimulus-rails' # Hotwire's modest JavaScript framework
# gem 'image_processing' # Use Active Storage variants
EOT
RUBY
end

############################
# Group: :development, :test
############################

insert_into_file 'Gemfile', after: %r{gem 'danger'.*\n} do
<<~EOT.indent(2)
<<~RUBY.indent(2)
gem 'danger-eslint' # ESLint
EOT
RUBY
end

insert_into_file 'Gemfile', after: %r{gem 'spring-watcher-listen'.*\n} do
<<~EOT.indent(2)
<<~RUBY.indent(2)
# gem 'web-console' # Use console on exceptions pages
EOT
RUBY
end

##############
# Group: :test
##############

insert_into_file 'Gemfile', after: %r{gem 'rspec-retry'.*\n} do
<<~EOT.indent(2)
<<~RUBY.indent(2)
gem 'capybara' # Integration testing
gem 'selenium-webdriver' # Ruby bindings for Selenium/WebDriver
gem 'webdrivers' # Run Selenium tests more easily with automatic installation and updates for all supported webdrivers
EOT
RUBY
end
4 changes: 2 additions & 2 deletions .template/variants/web/Procfile.dev.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
append_to_file 'Procfile.dev' do
<<~EOT
<<~PROCFILE
js: yarn build --watch
css: yarn build:css --watch
EOT
PROCFILE
end
Loading

0 comments on commit 0e40c04

Please sign in to comment.