Skip to content

Commit

Permalink
Basic rails gem setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Parr committed Jun 28, 2023
1 parent 4f1ab57 commit 61d4d7a
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions .erb-lint.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .railsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--skip-bundle
--skip-test
--database=postgresql
--skip-jbuilder
46 changes: 46 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.3
3.1.2
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <myproject> --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 <myproject> --skip-test --skip-jbuilder -d postgresql -c tailwind -m 'https://raw.githubusercontent.com/jparr/minifast-rails/main/template.rb'
```
10 changes: 5 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions config/initializers/dotenv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# list keys here that are required by the application

# Dotenv.require_keys("SERVICE_APP_ID", "SERVICE_KEY", "SERVICE_SECRET")
#
3 changes: 3 additions & 0 deletions spec/support/factory_bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
6 changes: 6 additions & 0 deletions spec/support/shoulda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
102 changes: 93 additions & 9 deletions template.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions test/template_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

0 comments on commit 61d4d7a

Please sign in to comment.