From 61d4d7a51522580bff5dfae8aaa0256ca8a597a2 Mon Sep 17 00:00:00 2001 From: Jeff Parr Date: Wed, 28 Jun 2023 15:27:59 -0700 Subject: [PATCH] Basic rails gem setup --- .env.template | 4 ++ .erb-lint.yml | 24 ++++++++ .railsrc | 4 ++ .rubocop.yml | 46 +++++++++++++++ .ruby-version | 2 +- README.md | 9 ++- Rakefile | 10 ++-- config/initializers/dotenv.rb | 4 ++ spec/support/factory_bot.rb | 3 + spec/support/shoulda.rb | 6 ++ template.rb | 102 +++++++++++++++++++++++++++++++--- test/template_test.rb | 8 +-- 12 files changed, 200 insertions(+), 22 deletions(-) create mode 100644 .env.template create mode 100644 .erb-lint.yml create mode 100644 .railsrc create mode 100644 .rubocop.yml create mode 100644 config/initializers/dotenv.rb create mode 100644 spec/support/factory_bot.rb create mode 100644 spec/support/shoulda.rb diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..2f9ad1e --- /dev/null +++ b/.env.template @@ -0,0 +1,4 @@ +# list environment variables in a .env file +# required variables can be specified in `initializers/dotenv.rb` +# generate a template file with `$ dotenv -t .env` +# SOMEVARIABLE=xxxxxsecretvariablexxxxx diff --git a/.erb-lint.yml b/.erb-lint.yml new file mode 100644 index 0000000..7ccffcf --- /dev/null +++ b/.erb-lint.yml @@ -0,0 +1,24 @@ +--- +EnableDefaultLinters: true +linters: + Rubocop: + enabled: true + rubocop_config: + inherit_from: + - .rubocop.yml + Layout/InitialIndentation: + Enabled: false + Layout/LineLength: + Enabled: false + Layout/TrailingEmptyLines: + Enabled: false + Layout/TrailingWhitespace: + Enabled: false + Naming/FileName: + Enabled: false + Style/FrozenStringLiteralComment: + Enabled: false + Lint/UselessAssignment: + Enabled: false + Rails/OutputSafety: + Enabled: false \ No newline at end of file diff --git a/.railsrc b/.railsrc new file mode 100644 index 0000000..b6425b3 --- /dev/null +++ b/.railsrc @@ -0,0 +1,4 @@ +--skip-bundle +--skip-test +--database=postgresql +--skip-jbuilder \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..84371f8 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,46 @@ +require: + - standard + - rubocop-rspec + - rubocop-rails + - rubocop-i18n + - rubocop-capybara + - rubocop-factory_bot + +inherit_gem: + standard: config/base.yml + +AllCops: + NewCops: enable + Exclude: + - db/schema.rb + +Style/StringLiterals: + Enabled: true + EnforcedStyle: single_quotes + ConsistentQuotesInMultiline: false + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: single_quotes + +Rails/Present: + Exclude: + - bin/* + +RSpec/NestedGroups: + Exclude: + - spec/requests/**/*.rb + +RSpec/ExampleLength: + Exclude: + - spec/system/**/*.rb + +I18n/RailsI18n: + Enabled: true + Exclude: + - bin/* + - db/* + - spec/**/* + +I18n/GetText: + Enabled: false \ No newline at end of file diff --git a/.ruby-version b/.ruby-version index 75a22a2..ef538c2 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.0.3 +3.1.2 diff --git a/README.md b/README.md index 3558ce5..6942047 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,10 @@ A Rails Application Template for use at Ministry of Velocity Reference: [Rails Application Templates](https://guides.rubyonrails.org/rails_application_templates.html) -Thank you to the following open source projects for inspiration and setup: +```bash +rails new --skip-test --skip-jbuilder -d postgresql -c tailwind -m ./template.rb +``` -[Jumpstart](https://github.com/excid3/jumpstart). -[https://github.com/mattbrictson/rails-template](https://github.com/mattbrictson/rails-template) +```bash +rails new --skip-test --skip-jbuilder -d postgresql -c tailwind -m 'https://raw.githubusercontent.com/jparr/minifast-rails/main/template.rb' +``` \ No newline at end of file diff --git a/Rakefile b/Rakefile index e0622f8..197ef6f 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,9 @@ -require "rake/testtask" +require 'rake/testtask' Rake::TestTask.new(:test) do |t| - t.libs << "test" - t.libs << "lib" - t.test_files = FileList["test/**/*_test.rb"] + t.libs << 'test' + t.libs << 'lib' + t.test_files = FileList['test/**/*_test.rb'] end -task :default => :test +task default: :test diff --git a/config/initializers/dotenv.rb b/config/initializers/dotenv.rb new file mode 100644 index 0000000..a493ccc --- /dev/null +++ b/config/initializers/dotenv.rb @@ -0,0 +1,4 @@ +# list keys here that are required by the application + +# Dotenv.require_keys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET") +# diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 0000000..c7890e4 --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end diff --git a/spec/support/shoulda.rb b/spec/support/shoulda.rb new file mode 100644 index 0000000..7d045f3 --- /dev/null +++ b/spec/support/shoulda.rb @@ -0,0 +1,6 @@ +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end diff --git a/template.rb b/template.rb index 6c6ace6..b6f35fb 100644 --- a/template.rb +++ b/template.rb @@ -1,24 +1,108 @@ +require 'fileutils' +# Copied from: https://github.com/mattbrictson/rails-template +# Add this template directory to source_paths so that Thor actions like +# copy_file and template resolve against our source files. If this file was +# invoked remotely via HTTP, that means the files are not present locally. +# In that case, use `git clone` to download them to a local temporary dir. +def add_template_repository_to_source_path + if __FILE__ =~ %r{\Ahttps?://} + require 'tmpdir' + source_paths.unshift(tempdir = Dir.mktmpdir('rails-template-')) + at_exit { FileUtils.remove_entry(tempdir) } + git clone: [ + '--quiet', + 'https://github.com/jparr/minifast-rails.git', + tempdir + ].map(&:shellescape).join(' ') + + if (branch = __FILE__[%r{minifast-rails/(.+)/template.rb}, 1]) + Dir.chdir(tempdir) { git checkout: branch } + end + else + source_paths.unshift(File.dirname(__FILE__)) + end +end + def rails_version @rails_version ||= Gem::Version.new(Rails::VERSION::STRING) end def rails_7_or_newer? - Gem::Requirement.new(">= 7.0.0").satisfied_by? rails_version + Gem::Requirement.new('>= 7.0.0').satisfied_by? rails_version end -def add_gems +unless rails_7_or_newer? + say_error '-----------------------------', :red + say_error 'Please use Rails 7.0 or newer', :red + say_error '-----------------------------', :red + # raise 'hell' +end + +add_template_repository_to_source_path +gem_group :development, :test do + gem 'dotenv-rails' + gem 'rspec-rails' + gem 'factory_bot_rails' + gem 'faker' + + gem 'erb_lint', require: false + gem 'pronto', require: false, group: :pronto + gem 'pronto-rubocop', require: false, group: :pronto + gem 'pronto-erb_lint', require: false, group: :pronto + gem 'rubocop-performance', require: false, group: :pronto + gem 'rubocop-i18n', require: false, group: :pronto + gem 'rubocop-rails', require: false, group: :pronto + gem 'rubocop-rspec', require: false, group: :pronto + gem 'standard', group: :pronto end -unless rails_7_or_newer? - say_error "-----------------------------", :red - say_error "Please use Rails 7.0 or newer", :red - say_error "-----------------------------", :red - # raise 'hell' +gem_group :test do + gem 'capybara' + gem 'selenium-webdriver' + gem 'shoulda-matchers', '~> 5.0' + gem 'webdrivers' end -add_gems +gem 'view_component', '~> 3.2.0' +gem 'hotwire-livereload' after_bundle do - say "App successfully created!", :green + generate 'rspec:install' + directory 'spec/support' + copy_file '.erb-lint.yml' + copy_file '.rubocop.yml' + + run 'bundle binstubs erb_lint' + run 'bundle binstubs rubocop' + run 'bundle binstubs rspec-core' + + rails_command 'livereload:install' + + inject_into_file 'config/application.rb', after: 'config.load_defaults 7.0' do + <<-RUBY + config.view_component.generate.sidecar = true + + config.generators do |generator| + generator.helper false + generator.test_framework :rspec, + view_specs: false, + routing_specs: false + end + RUBY + end + + append_to_file '.gitignore' do + <<-GIT + .env + GIT + end + copy_file '.env.template' + copy_file 'config/initializers/dotenv.rb' + + # todo: better readme template + + run 'bin/rubocop -A' + + say 'App successfully created!', :green end diff --git a/test/template_test.rb b/test/template_test.rb index 3413265..1dee925 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -1,8 +1,8 @@ -require "minitest/autorun" +require 'minitest/autorun' class TemplateTest < Minitest::Test def setup - system("[ -d test_app ] && rm -rf test_app") + system('[ -d test_app ] && rm -rf test_app') end def teardown @@ -11,9 +11,9 @@ def teardown def test_generator_succeeds output, error = capture_subprocess_io do - system("rails new test_app -m template.rb") + system('rails new test_app -m template.rb') end - assert_includes output, "App successfully created!" + assert_includes output, 'App successfully created!' # puts error end end