Skip to content

Commit

Permalink
Merge branch 'main' into support-google-analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Nov 16, 2023
2 parents eeefd6b + 62446f3 commit fcce2a7
Show file tree
Hide file tree
Showing 38 changed files with 424 additions and 112 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/check-with-osv-detector.yml

This file was deleted.

25 changes: 10 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Rails Template CI
on:
workflow_dispatch:
pull_request:
push:
branches:
Expand All @@ -18,6 +19,14 @@ permissions:
contents: read # to fetch code (actions/checkout)

jobs:
audit_dependencies:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false
- name: Audit dependencies for security vulnerabilities
uses: g-rath/check-with-osv-detector@main
test:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -56,49 +65,35 @@ jobs:
variant:
- name: defaults
config_path: "ackama_rails_template.config.yml"
skips: --skip-javascript
- name: all
config_path: "ci/configs/all.yml"
skips: --skip-javascript
- name: all-typescript
config_path: "ci/configs/all-typescript.yml"
skips: --skip-javascript
- name: basic
config_path: "ci/configs/basic.yml"
skips: --skip-javascript
- name: basic-typescript
config_path: "ci/configs/basic-typescript.yml"
skips: --skip-javascript
- name: github_actions
config_path: "ci/configs/github_actions.yml"
skips: --skip-javascript
- name: react
config_path: "ci/configs/react.yml"
skips: --skip-javascript
- name: react-typescript
config_path: "ci/configs/react-typescript.yml"
skips: --skip-javascript
- name: sidekiq
config_path: "ci/configs/sidekiq.yml"
skips: --skip-javascript
- name: devise
config_path: "ci/configs/devise.yml"
skips: --skip-javascript
- name: basic_with_skips
config_path: "ci/configs/basic.yml"
skips: --skip-spring --skip-javascript
- name: bootstrap
config_path: "ci/configs/bootstrap.yml"
skips: --skip-javascript
- name: bootstrap-typescript
config_path: "ci/configs/bootstrap-typescript.yml"
skips: --skip-javascript
- name: capistrano
config_path: "ci/configs/deploy_with_capistrano.yml"
skips: --skip-javascript
- name: ackama_ec2_capistrano
config_path: "ci/configs/deploy_with_ackama_ec2_capistrano.yml"
skips: --skip-javascript
services:
db:
image: postgres
Expand Down Expand Up @@ -155,7 +150,7 @@ jobs:
# "react", "sidekiq" etc.
APP_NAME: ${{ matrix.variant.name }}-demo
CONFIG_PATH: ${{ matrix.variant.config_path }}
SKIPS: ${{ matrix.variant.skips }}
SKIPS: "--skip-javascript --skip-docker ${{ matrix.variant.skips }}"
PGUSER: postgres
PGPASSWORD: postgres
PGHOST: localhost
Expand Down
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18.16.0
18.17.1
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,6 @@ RSpec/MultipleMemoizedHelpers:

FactoryBot/SyntaxMethods:
Enabled: false

Rails/SquishedSQLHeredocs:
Enabled: false
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.6)
activesupport (7.0.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Where possible we stick to Rails defaults.
- Install [Overcommit](https://github.com/sds/overcommit) for managing custom
git hooks. Configure it with our default settings:
[overcommit.yml](./overcommit.yml)
- Use [erb_lint](https://github.com/Shopify/erb-lint) to lint HTML in ERB and
run Rubocop on Ruby snippets within ERB
- General testing
- RSpec for tests
- Install [webdrivers](https://github.com/titusfortner/webdrivers)
Expand Down Expand Up @@ -153,7 +155,7 @@ Before running this template, you must have the following installed on your
machine:

- Yarn v1.21.0 or later
- Rails 7.0.x
- Rails 7.1.x

The following are not strictly required to run the template but you will need it
to start the Rails app that this template creates:
Expand Down Expand Up @@ -190,15 +192,15 @@ $ wget https://raw.githubusercontent.com/ackama/rails-template/main/ackama_rails

# Create a new app using the template. Template options will be taken from
# ./ackama_rails_template.config.yml
$ rails new my_app --no-rc --database=postgresql --skip-javascript --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
$ rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb

# Example 2
# #########

# Create a custom config YAML file, saving as ./my_custom_config.yml

# Template options will be taken from ./my_custom_config.yml
$ CONFIG_PATH=./my_custom_config.yml rails new my_app --no-rc --database=postgresql --skip-javascript --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
# Template options will be taken from ../my_custom_config.yml (relative to the new app directory)
$ CONFIG_PATH=../my_custom_config.yml rails new my_app --no-rc --database=postgresql --skip-javascript --skip-docker --template=https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
```

Here are some additional options you can add to this command. We don't
Expand Down Expand Up @@ -226,6 +228,7 @@ To make this the default Rails application template on your system, create a
# ~/.railsrc
-d postgresql
--skip-javascript
--skip-docker
-m https://raw.githubusercontent.com/ackama/rails-template/main/template.rb
```

Expand Down Expand Up @@ -273,7 +276,7 @@ $ CONFIG_PATH="ci/configs/react.yml" APP_NAME="enterprise" ./ci/bin/build-and-te
# because the template is run by `rails new` which uses the rails app dir as
# it's working dir, hence the `../` at the start.
#
$ rm -rf mydemoapp && CONFIG_PATH="../ci/configs/react.yml" rails new mydemoapp -d postgresql --skip-javascript -m ./template.rb
$ rm -rf mydemoapp && CONFIG_PATH="../ci/configs/react.yml" rails new mydemoapp -d postgresql --skip-javascript --skip-docker -m ./template.rb
```

Rubocop is configured for this repo and is run as part of CI. Run rubocop
Expand Down
30 changes: 24 additions & 6 deletions ci/bin/build-and-test
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ template_path = File.join(root_path, "template.rb")
builds_path = File.join(root_path, "tmp/builds")
app_name = ENV.fetch("APP_NAME", "app")
app_path = File.join(builds_path, app_name)
config_path = File.join(root_path, ENV.fetch("CONFIG_PATH", "ci/configs/basic.yml"))
skip_flags = ENV.fetch("SKIPS", "")

config_path = if ENV.fetch("CONFIG_PATH", "") == ""
File.join(root_path, "ackama_rails_template.config.yml")
else
File.join(root_path, ENV.fetch("CONFIG_PATH"))
end

config_env_var = %Q(CONFIG_PATH="#{config_path}") if config_path
skip_flags = ENV.fetch("SKIPS", "--skip-javascript --skip-docker")

puts "=" * 80
puts <<~EO_CONFIG_SUMMARY
Expand All @@ -19,11 +26,11 @@ puts <<~EO_CONFIG_SUMMARY
app_name = #{app_name}
app_path = #{app_path}
template_path = #{template_path}
config_path = #{config_path}
config_path = #{config_path || "None"}
EO_CONFIG_SUMMARY
puts "=" * 80

raise "Missing config YML file" unless File.exist?(config_path) & File.file?(config_path)
raise "Missing config YML file" if config_path && !(File.exist?(config_path) && File.file?(config_path))

puts "Installing latest rails gem"
system "gem install rails --no-document"
Expand All @@ -40,7 +47,7 @@ end

Dir.chdir(builds_path) do |cwd|
puts "Working dir is now #{cwd}"
cmd = %Q(CONFIG_PATH="#{config_path}" RACK_ENV=development RAILS_ENV=development rails new "#{app_name}" -d postgresql #{skip_flags} -m "#{template_path}")
cmd = %Q(#{config_env_var} RACK_ENV=development RAILS_ENV=development rails new "#{app_name}" -d postgresql #{skip_flags} -m "#{template_path}")
puts <<~EO_HELP
Build command:
#{cmd}
Expand All @@ -49,8 +56,19 @@ Dir.chdir(builds_path) do |cwd|
system(cmd, exception: true)
end

puts "Running test suite"
def ensure_everything_is_committed
out = `git status -s`

raise StandardError, "there are uncommitted changes or un-staged files: #{out}" unless out.empty?
end

puts "Running post-generator checks"
Dir.chdir(app_path) do |cwd|
puts "Working dir is now #{cwd}"

puts "Checking everything is committed and there are no un-staged files"
ensure_everything_is_committed

puts "Running test suite"
system("./bin/ci-run", exception: true)
end
28 changes: 21 additions & 7 deletions template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
require "shellwords"
require "pp"

RAILS_REQUIREMENT = "~> 7.0.3".freeze
RAILS_REQUIREMENT = "~> 7.1.1".freeze

##
# This single template file will be downloaded and run by the `rails new`
# command so all code it needs must be inlined we cannot load other files from
# this repo.
#
class Config
DEFAULT_CONFIG_FILE_PATH = "./ackama_rails_template.config.yml".freeze
DEFAULT_CONFIG_FILE_PATH = "../ackama_rails_template.config.yml".freeze

def initialize
config_file_path = File.absolute_path(ENV.fetch("CONFIG_PATH", DEFAULT_CONFIG_FILE_PATH))
Expand Down Expand Up @@ -83,7 +83,6 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
template "variants/backend-base/Gemfile.tt", "Gemfile", force: true

template "variants/backend-base/example.env.tt", "example.env"
template "variants/backend-base/example.env.tt", ".env"
copy_file "variants/backend-base/editorconfig", ".editorconfig"
copy_file "variants/backend-base/gitignore", ".gitignore", force: true
copy_file "variants/backend-base/overcommit.yml", ".overcommit.yml"
Expand All @@ -93,10 +92,10 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met

copy_file "variants/backend-base/.node-version", ".node-version"

copy_file "variants/backend-base/Dockerfile", "Dockerfile"
copy_file "variants/backend-base/Dockerfile", "Dockerfile", force: true
copy_file "variants/backend-base/docker-compose.yml", "docker-compose.yml"
copy_file "variants/backend-base/.osv-detector.yml", ".osv-detector.yml"
copy_file "variants/backend-base/.dockerignore", ".dockerignore"
copy_file "variants/backend-base/.dockerignore", ".dockerignore", force: true

apply "variants/backend-base/Rakefile.rb"
apply "variants/backend-base/config.ru.rb"
Expand Down Expand Up @@ -187,13 +186,26 @@ def apply_template! # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Met
# generate routes and models
apply "variants/code-annotation/template.rb"

# erblint does not like some of Rails auto-generated ERB code e.g.
# `app/views/layouts/mailer.html.erb` so we auto-correct it.
copy_file "variants/backend-base/.erb-lint.yml", ".erb-lint.yml"
run_with_clean_bundler_env "bundle exec erblint --autocorrect ."
git add: "-A ."
git commit: "-n -m 'Set up erblint'"

# Run the README template at the end because it introspects the app to
# discover rake tasks etc.
template "variants/backend-base/README.md.tt", "README.md", force: true
run "yarn run prettier --write ./README.md"
apply_readme_template
end
end

def apply_readme_template
template "variants/backend-base/README.md.tt", "README.md", force: true
run "yarn run prettier --write ./README.md"

git commit: "README.md -n -m 'Update README'"
end

# Normalizes the constraints of the given hash of dependencies so that they
# all have an explicit constraint and define a minor & patch version
#
Expand Down Expand Up @@ -291,6 +303,8 @@ def assert_valid_options
skip_git: false,
skip_test_unit: true,
skip_active_storage: false,
skip_javascript: true,
skip_docker: true,
edge: false
}
valid_options.each do |key, expected|
Expand Down
4 changes: 2 additions & 2 deletions variants/audit-logging/template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source_paths.unshift(File.dirname(__FILE__))

copy_file("variants/audit-logging/lib/auditor_log_formatter.rb", "lib/auditor_log_formatter.rb")
copy_file("variants/audit-logging/lib/audit_log_log_formatter.rb", "lib/audit_log_log_formatter.rb")
copy_file("variants/audit-logging/services/audit_log.rb", "app/services/audit_log.rb")
copy_file("variants/audit-logging/spec/services/audit_log_spec.rb", "spec/services/audit_log_spec.rb")

Expand All @@ -19,7 +19,7 @@

prepend_to_file "config/application.rb" do
<<~RUBY
require_relative "../lib/auditor_log_formatter"
require_relative "../lib/audit_log_log_formatter"
RUBY
end

Expand Down
45 changes: 45 additions & 0 deletions variants/backend-base/.erb-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
exclude:
- '**/vendor/**/*'
- '**/node_modules/**/*'
EnableDefaultLinters: true
linters:
RequireScriptNonce:
enabled: true
PartialInstanceVariable:
enabled: true
HardCodedString:
enabled: false
ErbSafety:
enabled: true
SpaceInHtmlTag:
enabled: false
NoJavascriptTagHelper:
enabled: false
SelfClosingTag:
enabled: true
enforced_style: 'always'
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
Layout/LeadingEmptyLines:
Enabled: false
Rails/OutputSafety:
# Disable this because erb-lint already checks this
Enabled: false
5 changes: 4 additions & 1 deletion variants/backend-base/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby File.read(".ruby-version")

gem "rails", "<%= Rails.version %>"
gem "puma", "~> 5.6"
gem "puma"
gem "pg"
gem 'dotenv-rails', require: "dotenv/rails-now"
gem "bootsnap", require: false
Expand Down Expand Up @@ -60,6 +60,9 @@ group :development, :test do
gem "factory_bot_rails"
gem "pry-rails"
gem "pry-byebug"

# ERB linting. Run via `bundle exec erblint .`
gem "erb_lint", require: false
end

group :test do
Expand Down
2 changes: 1 addition & 1 deletion variants/backend-base/app/middleware/http_basic_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def initialize(app)
end

def call(env)
if username && password
if username.present? && password.present?
auth = Rack::Auth::Basic.new(@app) do |u, p|
u == username && p == password
end
Expand Down
Loading

0 comments on commit fcce2a7

Please sign in to comment.