Skip to content

Commit

Permalink
Set up Rubocop for the template (#320)
Browse files Browse the repository at this point in the history
* Setup rubocop and fix warnings

* Add template rubocop script

* Fix rubocop warnings and adjust rubocop config

* wip

* Fix failed tests

* Update readme

* Revert workflow trigger

* Update readme

* Update target ruby version to 3

* Update readme
  • Loading branch information
rosle authored Mar 21, 2022
1 parent 0e40c04 commit 43311cb
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BUNDLE_GEMFILE: ".template/Gemfile"
42 changes: 42 additions & 0 deletions .github/workflows/test_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Test Template

on: push

env:
RUBY_VERSION: 3.0.1

jobs:
test:
name: Test template
runs-on: ubuntu-latest

steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout Repository
uses: actions/[email protected]

- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ env.RUBY_VERSION }}
bundler-cache: true

- name: Cache gems
uses: actions/cache@v2
with:
path: vendor/bundle
key: ${{ runner.os }}-template-${{ env.RUBY_VERSION }}-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-template-${{ env.RUBY_VERSION }}-
- name: Install gems
run: |
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3
- name: Run RuboCop
run: bundle exec rubocop --config .template/.rubocop.yml --parallel
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require:
require:
- rubocop-rails
- rubocop-rspec
- rubocop-performance
Expand All @@ -15,7 +15,7 @@ AllCops:
- 'node_modules/**/*'
- 'config/**/*'
- 'tmp/**/*'
TargetRubyVersion: 2.7
TargetRubyVersion: 3
NewCops: enable

Style/Documentation:
Expand Down
53 changes: 53 additions & 0 deletions .template/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
AllCops:
NewCops: enable
TargetRubyVersion: 3
Exclude:
- 'vendor/**/*'

Lint/NonDeterministicRequireOrder:
Exclude:
- '../spec/rails_helper.rb'

Metrics/MethodLength:
Exclude:
- '../template.rb'
- '**/template.rb'
- 'hooks/**/*'

Metrics/AbcSize:
Enabled: false

Metrics/BlockLength:
Enabled: false

Metrics/CyclomaticComplexity:
Exclude:
- '../template.rb'
- '**/template.rb'

Metrics/PerceivedComplexity:
Exclude:
- '../template.rb'
- '**/template.rb'

Naming/FileName:
Enabled: false

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

# TODO: Enable this rule later
Style/GlobalVars:
Enabled: false

# TODO: Enable this rule later
Style/RegexpLiteral:
Enabled: false

Style/TrivialAccessors:
Exclude:
- '../template.rb'
- '**/template.rb'
3 changes: 2 additions & 1 deletion .template/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source 'https://rubygems.org'

gem 'docker-api' # A lightweight Ruby client for the Docker Remote API
gem 'rspec' # BDD for Ruby
gem 'rspec-wait' # Wait for conditions in RSpec
gem 'rubocop', require: false # A Ruby static code analyzer and formatter, based on the community Ruby style guide.
gem 'serverspec' # RSpec tests for your servers
gem 'docker-api' # A lightweight Ruby client for the Docker Remote API
21 changes: 21 additions & 0 deletions .template/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
diff-lcs (1.4.4)
docker-api (2.2.0)
excon (>= 0.47.0)
Expand All @@ -11,6 +12,12 @@ GEM
net-ssh (>= 2.6.5, < 7.0.0)
net-ssh (6.1.0)
net-telnet (0.1.1)
parallel (1.21.0)
parser (3.1.0.0)
ast (~> 2.4.1)
rainbow (3.1.1)
regexp_parser (2.2.0)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand All @@ -29,6 +36,18 @@ GEM
rspec-support (3.9.3)
rspec-wait (0.0.9)
rspec (>= 3, < 4)
rubocop (1.25.0)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.15.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
serverspec (2.41.5)
multi_json
rspec (~> 3.0)
Expand All @@ -40,6 +59,7 @@ GEM
net-ssh (>= 2.7)
net-telnet (= 0.1.1)
sfl
unicode-display_width (2.1.0)

PLATFORMS
ruby
Expand All @@ -48,6 +68,7 @@ DEPENDENCIES
docker-api
rspec
rspec-wait
rubocop
serverspec

BUNDLED WITH
Expand Down
2 changes: 1 addition & 1 deletion .template/addons/nginx/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'nginx '
end

insert_into_file 'Dockerfile', after: %r(WORKDIR.+\n) do
insert_into_file 'Dockerfile', after: %r{WORKDIR.+\n} do
<<~DOCKERFILE
# Nginx config
Expand Down
2 changes: 1 addition & 1 deletion .template/addons/phrase_app/template.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
after_bundle do
use_source_path __dir__

copy_file '.phraseapp.yml'
apply 'spec/template.rb'
end
3 changes: 3 additions & 0 deletions .template/bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

bundle exec rubocop --config .template/.rubocop.yml $@
4 changes: 2 additions & 2 deletions .template/hooks/before_complete/fix_rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ def fixing_rubocop
after_bundle do
use_source_path __dir__

cops = %w(
cops = %w[
Style/FrozenStringLiteralComment
Style/StringLiterals
Layout/EmptyLineAfterMagicComment
).join(',')
].join(',')

run "rubocop --only #{cops} --auto-correct-all --out tmp/template_rubocop.txt"
end
Expand Down
2 changes: 2 additions & 0 deletions .template/lib/template/errors.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# rubocop:todo Style/ClassAndModuleChildren
class Template::Errors < Template::Messages; end
# rubocop:enable Style/ClassAndModuleChildren
2 changes: 2 additions & 0 deletions .template/lib/template/messages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:todo Style/ClassAndModuleChildren
class Template::Messages
delegate :empty?, to: :messages

Expand All @@ -17,3 +18,4 @@ def to_s

attr_reader :messages
end
# rubocop:enable Style/ClassAndModuleChildren
28 changes: 14 additions & 14 deletions .template/spec/addons/base/github/template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

# TODO: Can't test this as it is now ignored by `.dockerignore`
xit 'modifies the README.md' do
expect(file('README.md')).to contain("## Documentation")
expect(file('README.md')).to contain('## Documentation')

expect(file('README.md')).not_to contain("## Getting Started")
expect(file('README.md')).not_to contain("## Testing")
expect(file('README.md')).not_to contain("## CI/CD")
expect(file('README.md')).not_to contain('## Getting Started')
expect(file('README.md')).not_to contain('## Testing')
expect(file('README.md')).not_to contain('## CI/CD')
end

describe '.github/wiki/Getting-Started.md' do
Expand All @@ -33,9 +33,9 @@
end

it 'contains the correct content extracted from README.md' do
expect(file('.github/wiki/Getting-Started.md')).to contain("### Prerequisites")
expect(file('.github/wiki/Getting-Started.md')).to contain("### Docker")
expect(file('.github/wiki/Getting-Started.md')).to contain("### Development")
expect(file('.github/wiki/Getting-Started.md')).to contain('### Prerequisites')
expect(file('.github/wiki/Getting-Started.md')).to contain('### Docker')
expect(file('.github/wiki/Getting-Started.md')).to contain('### Development')
end
end

Expand All @@ -51,10 +51,10 @@
end

it 'contains the correct content extracted from the workflow README.md' do
expect(file('.github/wiki/CI-CD.md')).to contain("## Test workflow")
expect(file('.github/wiki/CI-CD.md')).to contain("## Test production build workflow")
expect(file('.github/wiki/CI-CD.md')).to contain("## Deploy to Heroku workflow")
expect(file('.github/wiki/CI-CD.md')).to contain("## Publish to Wiki workflow")
expect(file('.github/wiki/CI-CD.md')).to contain('## Test workflow')
expect(file('.github/wiki/CI-CD.md')).to contain('## Test production build workflow')
expect(file('.github/wiki/CI-CD.md')).to contain('## Deploy to Heroku workflow')
expect(file('.github/wiki/CI-CD.md')).to contain('## Publish to Wiki workflow')
end

it 'deletes the workflow README.md' do
Expand All @@ -68,9 +68,9 @@
end

it 'contains the correct content extracted from README.md' do
expect(file('.github/wiki/Testing.md')).to contain("### Docker-based tests on the CI server")
expect(file('.github/wiki/Testing.md')).to contain("### Test")
expect(file('.github/wiki/Testing.md')).to contain("### Automated Code Review Setup")
expect(file('.github/wiki/Testing.md')).to contain('### Docker-based tests on the CI server')
expect(file('.github/wiki/Testing.md')).to contain('### Test')
expect(file('.github/wiki/Testing.md')).to contain('### Automated Code Review Setup')
end
end
end
2 changes: 1 addition & 1 deletion .template/spec/addons/base/phrase_app/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
it 'creates .phraseapp.yml file' do
expect(file('.phraseapp.yml')).to exist
end

it 'adds PhraseApp to codebase_spec' do
expect(file('spec/codebase/codebase_spec.rb')).to contain("it 'does not offend PhraseApp Pull configuration' do")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe 'localization concern' do
subject { file('app/controllers/concerns/localization.rb') }

it 'contains the around_action' do
expect(subject).to contain('around_action :switch_locale')
end
Expand Down
2 changes: 1 addition & 1 deletion .template/spec/base/config/environments/production_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
end

it 'removes the config.i18n.fallbacks = true' do
expect(subject).not_to contain("config.i18n.fallbacks = true")
expect(subject).not_to contain('config.i18n.fallbacks = true')
end

private
Expand Down
14 changes: 7 additions & 7 deletions .template/variants/web/.gitignore.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
append_to_file '.gitignore' do
<<~IGNORE
# Ignore i18n.js generated files
# If deploy to heroku with git, please remove this as it prevents the files to be committed
/app/javascript/translations/*
# Ignore i18n.js generated files
# If deploy to heroku with git, please remove this as it prevents the files to be committed
/app/javascript/translations/*
# Ignore asset builds
/app/assets/builds/*
# Ignore asset builds
/app/assets/builds/*
# Ignore Node dependencies
/node_modules
# Ignore Node dependencies
/node_modules
IGNORE
end
9 changes: 8 additions & 1 deletion .template/variants/web/package.json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@

source_stylesheet = 'app/assets/stylesheets/application.scss'
bundled_stylesheet = 'app/assets/builds/application.css'
bundled_stylesheet_options = [
'--no-source-map',
'--load-path=node_modules'
]

run %(npm set-script build "node app/javascript/build.js")
run %(npm set-script build:css "sass #{source_stylesheet} #{bundled_stylesheet} --no-source-map --load-path=node_modules")
run %(
npm set-script build:css \
"sass #{source_stylesheet} #{bundled_stylesheet} #{bundled_stylesheet_options.join(' ')}"
)
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ with building complex applications over the years.
- Install rails `7.0.1`
- Install node `16.13.2` (For creating web application)

> 📝 If running on Apple M1, to build docker image, please make sure to set platform to AMD64 by `export DOCKER_DEFAULT_PLATFORM=linux/amd64`
### Use the template

In order to use the template, initialize a new app with the following parameters:

```
```sh
rails new <app_name> -m https://raw.githubusercontent.com/nimblehq/rails-templates/master/template.rb
```

Expand All @@ -33,7 +35,7 @@ Supported template options:

To apply the template on an existing application, run following rails command:

```
```sh
rails app:template LOCATION=https://raw.githubusercontent.com/nimblehq/rails-templates/master/template.rb

# To apply on an api application
Expand Down Expand Up @@ -172,6 +174,21 @@ e.g. `TOOL_VERSION` for `.tool-version` file.
For the normal string, name it after the content
e.g. `ERROR` for template error message.

## Testing the Template

To run [RuboCop](https://github.com/rubocop/rubocop) against the template:

```sh
.template/bin/rubocop
```

Any RuboCop command options can be passed:

```sh
# Run RuboCop with auto correct
.template/bin/rubocop -a
```

## License

This project is Copyright (c) 2014 and onwards. It is free software,
Expand Down
Loading

0 comments on commit 43311cb

Please sign in to comment.