diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c3c6565..d48c4a1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Adds Doorkeeper Generator with Devise. ([@TheZero0-ctrl][]) * Adds Avo generator. ([@mausamp][]) * Adds Sentry generator. ([@mausamp][]) +* Adds Dotenv generator. ([@mausamp][]) ## 0.13.0 (March 26th, 2024) * Adds Letter Opener generator. ([@coolprobn][]) diff --git a/README.md b/README.md index a56e7fe2..fd82fb9c 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ The boring generator introduces following generators: - Install Avo: `rails generate boring:avo:install` - Install Doorkeeper with devise: `rails generate boring:devise:doorkeeper:install` - Install Sentry: `rails generate boring:sentry:install --use_env_variable --breadcrumbs_logger=` +- Install Dotenv: `rails generate boring:dotenv:install` ## Screencasts diff --git a/lib/generators/boring/dotenv/install/install_generator.rb b/lib/generators/boring/dotenv/install/install_generator.rb new file mode 100644 index 00000000..6eac0989 --- /dev/null +++ b/lib/generators/boring/dotenv/install/install_generator.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Boring + module Dotenv + class InstallGenerator < Rails::Generators::Base + desc 'Adds dotenv gem to the application' + source_root File.expand_path("templates", __dir__) + + def add_dotenv_gem + say 'Adding dotenv gem', :green + + Bundler.with_unbundled_env do + run 'bundle add dotenv-rails --group development' + end + end + + def configure_dotenv_gem + say 'Configuring dotenv gem', :green + + template '.env', '.env' + + unless File.exist?('.gitignore') + create_file '.gitignore' + end + + FileUtils.cp('.env', '.env.sample') + insert_into_file('.gitignore', "\n/.env\n") + end + end + end +end diff --git a/lib/generators/boring/dotenv/install/templates/.env b/lib/generators/boring/dotenv/install/templates/.env new file mode 100644 index 00000000..26ad5aeb --- /dev/null +++ b/lib/generators/boring/dotenv/install/templates/.env @@ -0,0 +1,3 @@ +# Add your environment variables here +# Example: +# SECRET_KEY_BASE=your_secret_key \ No newline at end of file diff --git a/test/generators/dotenv_install_generator_test.rb b/test/generators/dotenv_install_generator_test.rb new file mode 100644 index 00000000..81276d6d --- /dev/null +++ b/test/generators/dotenv_install_generator_test.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'generators/boring/dotenv/install/install_generator' + +class DotenvInstallGeneratorTest < Rails::Generators::TestCase + tests Boring::Dotenv::InstallGenerator + setup :build_app + teardown :teardown_app + + include GeneratorHelper + + def destination_root + app_path + end + + def test_should_install_dotenv_gem + Dir.chdir(app_path) do + quietly { run_generator } + + assert_gem 'dotenv-rails' + + assert_file '.env' do |content| + assert_match(/# Add your environment variables here/, content) + assert_match(/# SECRET_KEY_BASE=your_secret_key/, content) + end + + assert_file '.env.sample' + + assert_file '.gitignore' do |content| + assert_match(/.env/, content) + end + end + end +end \ No newline at end of file