Created
March 28, 2022 11:36
-
-
Save dieter-medium/efaf4597fede3871aaa2f8d6fb0531dc to your computer and use it in GitHub Desktop.
A application template file for rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def say_custom(text) | |
say "\033[1m\033[36mcustom template\033[0m #{text}" | |
end | |
say_custom "Adding additional gems" | |
gem_group :development do | |
gem "selenium-webdriver" | |
gem "webdrivers" | |
gem "simplecov", require: false | |
end | |
gem_group :development, :test do | |
gem "ffaker" | |
gem "rspec-rails" | |
gem "factory_bot_rails" | |
gem "dotenv-rails" | |
gem "standardrb" | |
gem "debase" | |
gem "ruby-debug-ide" | |
end | |
say_custom "Force mysql usage" | |
prepend_to_file "Gemfile", after: /gem .*sqlite3/ do | |
<<~CODE | |
# Use mysql as the database for Active Record | |
gem "mysql2", "~> 0.5" | |
CODE | |
end | |
gsub_file "Gemfile", /^\s*.*Use sqlite3.*\n/i, "" | |
gsub_file "Gemfile", /^\s*.*gem .*sqlite3.*\n/i, "" | |
say_custom "Override database.yml" | |
remove_file "config/database.yml" | |
create_file "config/database.yml" do | |
<<~CODE | |
# MySQL. Versions 5.5.8 and up are supported. | |
# | |
# Install the MySQL driver | |
# gem install mysql2 | |
# | |
# Ensure the MySQL gem is defined in your Gemfile | |
# gem "mysql2" | |
# | |
# And be sure to use new-style password hashing: | |
# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html | |
# | |
default: &default | |
url: <%= ENV["DATABASE_URL"] %> | |
development: | |
<<: *default | |
url: <%= ENV["DEVELOPMENT_DATABASE_URL"] %> | |
# Warning: The database defined as "test" will be erased and | |
# re-generated from your development database when you run "rake". | |
# Do not set this db to the same as development or production. | |
test: | |
<<: *default | |
url: <%= ENV["TEST_DATABASE_URL"] %> | |
# As with config/credentials.yml, you never want to store sensitive information, | |
# like your database password, in your source code. If your source code is | |
# ever seen by anyone, they now have access to your database. | |
# | |
# Instead, provide the password or a full connection URL as an environment | |
# variable when you boot the app. For example: | |
# | |
# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" | |
# | |
# If the connection URL is provided in the special DATABASE_URL environment | |
# variable, Rails will automatically merge its configuration values on top of | |
# the values provided in this file. Alternatively, you can specify a connection | |
# URL environment variable explicitly: | |
# | |
# production: | |
# url: <%= ENV["MY_APP_DATABASE_URL"] %> | |
# | |
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database | |
# for a full overview on how database connection configuration can be specified. | |
# | |
production: | |
<<: *default | |
CODE | |
end | |
say_custom "Fix sassc issue with OSX" | |
application <<~CODE | |
# issues with sassc and OSX | |
# [BUG] Segmentation fault at 0x0 | |
# | |
# * https://github.com/rails/sprockets/issues/633 | |
# * https://github.com/sass/sassc-ruby/issues/207 | |
config.assets.configure do |assets_config| | |
assets_config.export_concurrent = false | |
end | |
CODE | |
uncomment_lines "Gemfile", /sassc-rails/ | |
after_bundle do | |
say_custom "Execute additional initializers" | |
unless options[:skip_bundle] | |
generate "rspec:install" | |
say_custom "Configure simplecov" | |
prepend_to_file "spec/spec_helper.rb" do | |
<<~CODE | |
require "simplecov" | |
SimpleCov.start "rails" do | |
add_group "Services", "app/services" | |
add_group "Subscribers", "app/subscribers" | |
end | |
CODE | |
end | |
say_custom "Edit spec/rails_helper.rb" | |
prepend_to_file "spec/rails_helper.rb", after: /Add additional requires below this line/ do | |
<<~CODE | |
require_relative "support/chrome" | |
CODE | |
end | |
prepend_to_file "spec/rails_helper.rb", after: /RSpec.configure/ do | |
<<~CODE | |
# factory bot | |
config.include FactoryBot::Syntax::Methods | |
config.before(:suite) do | |
FactoryBot.find_definitions | |
end | |
# treat service specs like model specs | |
config.define_derived_metadata(file_path: Regexp.new("/spec/services/")) do |metadata| | |
metadata[:type] = :model | |
end | |
CODE | |
end | |
say_custom "Add rspec support files" | |
file 'spec/support/chrome.rb', <<-CODE | |
RSpec.configure do |config| | |
config.before(:each, type: :system) do |example| | |
if example.metadata[:js] | |
if ENV["SHOW_BROWSER"] == "true" # some | |
driven_by :selenium_chrome do |option| | |
# within docker chrome runs as root for now | |
option.add_argument("no-sandbox") | |
option.add_argument("disable-gpu") | |
end | |
else | |
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400] do |option| | |
option.add_argument("no-sandbox") | |
option.add_argument("disable-gpu") | |
end | |
end | |
else | |
# for none js rack is faster and enough | |
driven_by(:rack_test) | |
end | |
end | |
end | |
CODE | |
# setup factorybot boilerplate | |
file "spec/factories.rb", <<~CODE | |
FactoryBot.define do | |
end | |
CODE | |
end | |
# setup .env boilerplate | |
file ".env", <<~CODE | |
# DEFAULT connection settings, should be overridden within | |
# env.<environment>.local | |
# priority: last | |
DEVELOPMENT_DATABASE_URL="mysql2://root:password@db/#{app_name}_dev?pool=5&encoding=utf8mb4" | |
TEST_DATABASE_URL="mysql2://root:password@db/#{app_name}_test?pool=5&encoding=utf8mb4" | |
CODE | |
file ".env.local", "# private (don't checkin) settings for all envs\n# priority: 2nd" | |
%w[development test].each do |env| | |
file ".env.#{env}", "# shared settings for #{env}\n# priority: 3rd" | |
file ".env.#{env}.local", "# private (don't checkin) settings for #{env}\n# priority: 1st" | |
end | |
bundle_command "exec standardrb --fix" | |
bundle_command "exec standardrb --generate-todo" | |
append_to_file '.gitignore', <<~CODE | |
# dot env files | |
.env.local | |
.env.*.local | |
#rspec metadata | |
spec/examples.txt | |
coverage | |
.idea/ | |
CODE | |
git :init | |
git add: '.' | |
git commit: "-a -m 'Initial commit'" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment