Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gh394] Remove the Figaro gem dependency - Andy changes #411

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .env.development.example

This file was deleted.

28 changes: 28 additions & 0 deletions .env.development.local.example.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is shared with the team and should be used as a template to create `.env.development.local` file
# Clone this file to `.env.development.local` and replace the values with your own

# Database
DB_NAME=<%= APP_NAME %>_development
DB_POOL=5
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres

# Redis
REDIS_URL=redis://localhost:6379

# Mail
MAILER_DEFAULT_HOST=localhost
MAILER_DEFAULT_PORT=3000
MAILER_SENDER=Test <[email protected]>

# Locales
AVAILABLE_LOCALES=en
DEFAULT_LOCALE=en
FALLBACK_LOCALES=en

# Used to verify the integrity of signed cookies. so ensure a secure value is set
SECRET_KEY_BASE=replace_with_lengthy_secure_hex

PORT=3000
19 changes: 13 additions & 6 deletions .env → .env.test.local.tt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# DO NOT STORE SECRETS HERE.
# Use .env.development for local secrets.
# Database
DB_NAME=<%= APP_NAME %>_test
DB_POOL=5
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres

# Redis
REDIS_URL=redis://localhost:6379

# CI/CD
TEST_RETRY=0
CI=false

# Mail
MAILER_DEFAULT_HOST=localhost
MAILER_DEFAULT_PORT=3000
Expand All @@ -17,4 +19,9 @@ AVAILABLE_LOCALES=en
DEFAULT_LOCALE=en
FALLBACK_LOCALES=en

# Used to verify the integrity of signed cookies. so ensure a secure value is set
SECRET_KEY_BASE=replace_with_lengthy_secure_hex

# Test
TEST_RETRY=0
CI=false
2 changes: 1 addition & 1 deletion .gitignore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
/coverage

# Ignore environment variables files
.env.development
.env.development.local
IGNORE
end
3 changes: 1 addition & 2 deletions .template/addons/docker/Dockerfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ RUN rm -rf tmp/docker
<%- if WEB_VARIANT -%>

# Compile assets
RUN bundle exec rails i18n:js:export
RUN bundle exec rails assets:precompile --trace
RUN bin/docker-assets-precompile
RUN yarn postcss

<%- end -%>
Expand Down
4 changes: 2 additions & 2 deletions .template/addons/docker/docker-compose.test.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ services:
- POSTGRES_DB=<%= APP_NAME %>_test
- POSTGRES_PASSWORD=postgres
ports:
- "5432"
- "5432:5432"

redis:
image: redis:<%= REDIS_VERSION %>
container_name: <%= APP_NAME %>_redis
ports:
- "6379"
- "6379:6379"

test:
build:
Expand Down
2 changes: 0 additions & 2 deletions .template/addons/docker/docker-compose.yml.tt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ services:
image: ${DOCKER_REGISTRY_HOST}/${DOCKER_IMAGE}:${BRANCH_TAG}
container_name: <%= APP_NAME %>_web
command: bin/start.sh
ports:
- "${PORT}:${PORT}"
stdin_open: true
tty: true
depends_on:
Expand Down
12 changes: 0 additions & 12 deletions .template/addons/docker/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,3 @@
template 'docker-compose.test.yml.tt'
template 'docker-compose.yml.tt'
template '.dockerignore.tt'

append_to_file '.env' do
<<~ENVEXAMPLE

# Docker
APP_NAME=#{APP_NAME}
DOCKER_REGISTRY_HOST=#{DOCKER_REGISTRY_HOST}
DOCKER_IMAGE=#{DOCKER_IMAGE}
BRANCH_TAG=latest
PORT=80
ENVEXAMPLE
end
12 changes: 8 additions & 4 deletions .template/spec/base/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
expect(file('.reek.yml')).to exist
end

it 'creates .env file' do
expect(file('.env')).to exist
it 'creates .env.development.local file' do
expect(file('.env.development.local')).to exist
end

it 'creates .env.development.example file' do
expect(file('.env.development.example')).to exist
it 'creates .env.development.local.example file' do
expect(file('.env.development.local.example')).to exist
end

it 'creates .env.test.local file' do
expect(file('.env.test.local')).to exist
end
end
4 changes: 3 additions & 1 deletion Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ gem 'puma' # Use Puma as the app server
gem 'mini_magick' # A ruby wrapper for ImageMagick or GraphicsMagick command line
gem 'pagy' # A pagination gem that is very light and fast
gem 'discard' # Soft deletes for ActiveRecord
gem 'dotenv-rails' # Shim to load environment variables from .env into ENV in development
gem 'sidekiq' # background processing for Ruby
gem 'sassc' # bootsnap dependency
gem 'bootsnap', require: false # Reduces boot times through caching; required in config/boot.rb
Expand Down Expand Up @@ -66,6 +65,9 @@ group :development, :test do
gem 'danger-suggester' # Suggest code changes based on configured code formatter.
gem 'danger-simplecov_json' # Report your Ruby app test suite code coverage in Danger.
gem 'danger-undercover' # Report missing test coverage of new changes in Danger

# Environment loading
gem 'dotenv-rails' # Shim to load environment variables from .env into ENV in development
end

group :test do
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build_production:
bin/docker-prepare && \
docker-compose build

# Test the new generated app could be built and run the test itself.
test_variant_app:
cd $(APP_NAME) && \
docker-compose -f docker-compose.test.yml run test
Expand All @@ -38,6 +39,7 @@ base_spec = spec/base/**/*_spec.rb
web_spec = spec/variants/web/**/*_spec.rb
api_spec = spec/variants/api/**/*_spec.rb

# Test the new generated app could be built and start and test the generated structure.
test_template:
cd $(APP_NAME) && \
docker-compose -f docker-compose.test.yml up --detach db redis && \
Expand All @@ -48,7 +50,7 @@ test_template:
if [ $(VARIANT) = web ]; then \
bundle exec rspec --pattern="${base_spec}, ${web_spec}, ${base_addon_spec}, ${web_addon_spec}" --format progress; \
elif [ $(VARIANT) = api ]; then \
bundle exec rspec --pattern="${base_spec}, ${api_spec}, ${base_addon_spec}, ${api_addon_spec}"; \
bundle exec rspec --pattern="${base_spec}, ${api_spec}, ${base_addon_spec}, ${api_addon_spec}" --format progress; \
fi;

cleanup:
Expand Down
2 changes: 0 additions & 2 deletions Makefile.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include .env

.PHONY: dev env/setup env/teardown codebase codebase/fix

dev:
Expand Down
33 changes: 33 additions & 0 deletions bin/docker-assets-precompile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# For building production docker image
#
# It sets the envs inside the docker image for precompiling the assets
# Because to precompile the assets, Rails initializes the app.
# And it requires the envs as we always use `ENV.fetch` to setup the variables
#
# Related issue: https://github.com/rails/rails/issues/32947
# TODO: https://github.com/nimblehq/rails-templates/issues/326
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, DHH gave a solution that works with a dummy key: rails/rails#32947 (comment)


require 'yaml'

rails_env = ENV.fetch('RAILS_ENV', 'production')

if rails_env == 'production'
# Read the contents of the .env file
File.readlines('.env.development.local.example').each do |line|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@andyduong1920 andyduong1920 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the env_file but not sure why the ENV can't find there...still need time to figure out @olivierobert :D

# Skip empty lines and comments
next if line.strip.empty? || line.start_with?('#')

# Extract the key and value from each line
key, value = line.split('=', 2).map(&:strip)

# Set the environment variable
ENV[key] = value
end

ENV['DATABASE_URL'] = 'postgres://postgres:postgres@postgres:5432/postgres'
end

exit system('bin/rails i18n:js:export && bin/rails assets:precompile')
3 changes: 0 additions & 3 deletions bin/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ if [ -f tmp/pids/server.pid ]; then
rm -f tmp/pids/server.pid
fi

bundle install
bundle update

# Run pending migrations (if any) and start rails
bundle exec rails db:migrate
bundle exec rails s -p $PORT -b 0.0.0.0
1 change: 1 addition & 0 deletions bin/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
copy_file 'bin/worker.sh', mode: :preserve
copy_file 'bin/dev', mode: :preserve
copy_file 'bin/docker-prepare', mode: :preserve
copy_file 'bin/docker-assets-precompile', mode: :preserve
23 changes: 23 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV['DB_POOL'] %>

development:
<<: *default
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: <%= ENV['DB_NAME'] %>

test:
<<: *default
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
username: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: <%= ENV['DB_NAME'] %>

production:
url: <%= ENV['DATABASE_URL'] %>
23 changes: 0 additions & 23 deletions config/database.yml.tt

This file was deleted.

2 changes: 1 addition & 1 deletion config/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

apply 'config/application.rb'

template 'config/database.yml.tt', force: true
copy_file 'config/database.yml', force: true
copy_file 'config/sidekiq.yml'

apply 'config/environments/development.rb'
Expand Down
5 changes: 3 additions & 2 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def apply_template!(template_root)

copy_file '.flayignore'
copy_file 'Dangerfile'
copy_file '.env'
copy_file '.env.development.example'
template '.env.development.local.example.tt'
template '.env.development.local.example.tt', '.env.development.local'
template '.env.test.local.tt'
copy_file '.rubocop.yml'
copy_file '.reek.yml'

Expand Down