Skip to content

Commit

Permalink
Switch from RuboCop to Standard (#28)
Browse files Browse the repository at this point in the history
* Swap RuboCop for Standard

* Run `bundle exec standardrb --fix`

* Fix up tests after Standard formatting

* Cleanup code that wasn't auto-fixed

* Update README

* Patch up GHA workflows

* Use double quotes in workflow

* Remove frozen string literal comments

* Fix up tests again

* Test a deliberate Standard violation in CI

* Try fail level E

* Try fail level A, I misunderstood before

* Fix the deliberate Standard violation
  • Loading branch information
nshki authored Nov 13, 2021
1 parent 34f710c commit f456f91
Show file tree
Hide file tree
Showing 25 changed files with 249 additions and 318 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
name: Linting
on: [pull_request]
jobs:
rubocop:
standard:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: 3.0.0
bundler-cache: true
- name: Run RuboCop
uses: reviewdog/action-rubocop@v2
with:
rubocop_version: gemfile
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: 3.0.0
bundler-cache: true
- name: Run Standard
run: bundle exec standardrb --fail-level A
18 changes: 9 additions & 9 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.6', '2.7', '3.0']
ruby-version: ["2.6", "2.7", "3.0"]
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run tests
run: bundle exec rake
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run tests
run: bundle exec rake
36 changes: 4 additions & 32 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
require:
- rubocop-performance
require: standard

inherit_mode:
merge:
- Exclude
inherit_gem:
standard: config/base.yml

AllCops:
NewCops: enable
Exclude:
- 'bin/**/*'
- 'test/mock/app/**/*'
- 'test/mock/examples/**/*'

Gemspec/RequiredRubyVersion:
Enabled: false

Layout/LineLength:
Max: 120

Metrics/AbcSize:
Exclude:
- 'test/**/*'

Metrics/MethodLength:
Max: 25
Exclude:
- 'test/**/*'

Metrics/ModuleLength:
Max: 250

Style/ClassAndModuleChildren:
Exclude:
- 'test/**/*'
DisabledByDefault: true
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# frozen_string_literal: true

source 'https://rubygems.org'
source "https://rubygems.org"

# Specify your gem's dependencies in chusaku.gemspec
gemspec
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add this line to your Rails application's Gemfile:
```ruby
group :development do
# ...
gem 'chusaku', require: false
gem "chusaku", require: false
# ...
end
```
Expand Down
12 changes: 5 additions & 7 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'
require "bundler/gem_tasks"
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
12 changes: 6 additions & 6 deletions bin/chusaku
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/env ruby

require 'rubygems'
require "rubygems"

begin
# Use project's bundle.
require 'bundler'
require "bundler"
Bundler.setup

# Use project's environment.
require 'rake'
load './Rakefile'
require "rake"
load "./Rakefile"
Rake::Task[:environment].invoke
rescue StandardError
rescue
end

require 'chusaku/cli'
require "chusaku/cli"

exit Chusaku::CLI.new.call
7 changes: 3 additions & 4 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'chusaku'
require "bundler/setup"
require "chusaku"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -11,5 +10,5 @@ require 'chusaku'
# require "pry"
# Pry.start

require 'irb'
require "irb"
IRB.start(__FILE__)
49 changes: 23 additions & 26 deletions chusaku.gemspec
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'chusaku/version'
require "chusaku/version"

Gem::Specification.new do |spec|
spec.name = 'chusaku'
spec.name = "chusaku"
spec.version = Chusaku::VERSION
spec.authors = ['Nishiki Liu']
spec.email = ['nishiki@hey.com']
spec.authors = ["Nishiki Liu"]
spec.email = ["hello@nshki.com"]

spec.summary = 'Annotate your Rails controllers with route info.'
spec.description = 'Annotate your Rails controllers with route info.'
spec.homepage = 'https://github.com/nshki/chusaku'
spec.license = 'MIT'
spec.summary = "Annotate your Rails controllers with route info."
spec.description = "Annotate your Rails controllers with route info."
spec.homepage = "https://github.com/nshki/chusaku"
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the
# 'allowed_push_host' to allow pushing to a single host or delete this section
# to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
spec.metadata["allowed_push_host"] = "https://rubygems.org"

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = spec.homepage
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage
spec.metadata["changelog_uri"] = spec.homepage
else
raise 'RubyGems 2.0 or newer is required to protect against ' \
'public gem pushes.'
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end

# Specify which files should be added to the gem when it is released.
Expand All @@ -37,15 +35,14 @@ Gem::Specification.new do |spec|
f.match(%r{^(test|spec|features)/})
end
end
spec.bindir = 'bin'
spec.executables = 'chusaku'
spec.require_paths = ['lib']
spec.bindir = "bin"
spec.executables = "chusaku"
spec.require_paths = ["lib"]

spec.add_development_dependency 'bundler', '~> 2.2'
spec.add_development_dependency 'minitest', '~> 5.14'
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rubocop', '~> 1.7'
spec.add_development_dependency 'rubocop-performance', '~> 1.9'
spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "minitest", "~> 5.14"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "standardrb", "~> 1.0"

spec.add_dependency 'railties', '>= 3.0'
spec.add_dependency "railties", ">= 3.0"
end
24 changes: 11 additions & 13 deletions lib/chusaku.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

require 'chusaku/version'
require 'chusaku/parser'
require 'chusaku/routes'
require "chusaku/version"
require "chusaku/parser"
require "chusaku/routes"

# Handles core functionality of annotating projects.
module Chusaku
Expand All @@ -21,7 +19,7 @@ def call(flags = {})
@flags = flags
@routes = Chusaku::Routes.call
@changes = []
controllers_pattern = 'app/controllers/**/*_controller.rb'
controllers_pattern = "app/controllers/**/*_controller.rb"

Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
controller = %r{controllers/(.*)_controller\.rb}.match(path)[1]
Expand Down Expand Up @@ -92,9 +90,9 @@ def record_change(group:, type:, path:, route_data: [])
def clean_group(group)
return unless group[:type] == :comment

group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, '')
group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, "")
group[:body] =
group[:body].gsub(%r{^\s*# (GET|POST|PATCH/PUT|DELETE) /\S+$\n}, '')
group[:body].gsub(%r{^\s*# (GET|POST|PATCH/PUT|DELETE) /\S+$\n}, "")
end

# Add an annotation to the given group given by Chusaku::Parser that looks
Expand Down Expand Up @@ -125,8 +123,8 @@ def annotate_route(verb:, path:, name:, defaults:)
if defaults&.any?
defaults_str =
defaults
.map { |key, value| "#{key}: #{value.inspect}" }
.join(', ')
.map { |key, value| "#{key}: #{value.inspect}" }
.join(", ")
annotation += " {#{defaults_str}}"
end
annotation += " (#{name})" unless name.nil?
Expand Down Expand Up @@ -174,7 +172,7 @@ def perform_write(path:, content:)
#
# @return [String] 'r' or 'w'
def file_mode
File.instance_methods.include?(:test_write) ? 'r' : 'w'
File.instance_methods.include?(:test_write) ? "r" : "w"
end

# Output results to user.
Expand All @@ -191,7 +189,7 @@ def output_results
#
# @return [String] Copy to be outputted to user
def output_copy
return 'Nothing to annotate.' if @changes.empty?
return "Nothing to annotate." if @changes.empty?

copy = changes_copy
copy += "\nChusaku has finished running."
Expand All @@ -204,7 +202,7 @@ def output_copy
#
# @return [String] Copy of recorded changes
def changes_copy
return '' unless @flags.include?(:verbose)
return "" unless @flags.include?(:verbose)

@changes.map do |change|
<<~CHANGE_OUTPUT
Expand Down
24 changes: 11 additions & 13 deletions lib/chusaku/cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'optparse'
require 'chusaku'
require "optparse"
require "chusaku"

module Chusaku
# Enables flags for the `chusaku` executable.
Expand Down Expand Up @@ -30,7 +28,7 @@ def call(args = ARGV)
check_for_rails_project
Chusaku.call(options)
rescue NotARailsProject
warn('Please run chusaku from the root of your project.')
warn("Please run chusaku from the root of your project.")
STATUS_ERROR
rescue Finished
STATUS_SUCCESS
Expand All @@ -43,8 +41,8 @@ def call(args = ARGV)
# @raise [Chusaku::CLI::NotARailsProject] Exception if not Rails project
# @return [void]
def check_for_rails_project
has_controllers = File.directory?('./app/controllers')
has_rakefile = File.exist?('./Rakefile')
has_controllers = File.directory?("./app/controllers")
has_rakefile = File.exist?("./Rakefile")
raise NotARailsProject unless has_controllers && has_rakefile
end

Expand All @@ -53,7 +51,7 @@ def check_for_rails_project
# @return [OptionParser] Preconfigured OptionParser instance
def optparser
OptionParser.new do |opts|
opts.banner = 'Usage: chusaku [options]'
opts.banner = "Usage: chusaku [options]"
add_dry_run_flag(opts)
add_error_on_annotation_flag(opts)
add_verbose_flag(opts)
Expand All @@ -67,7 +65,7 @@ def optparser
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_dry_run_flag(opts)
opts.on('--dry-run', 'Run without file modifications') do
opts.on("--dry-run", "Run without file modifications") do
@options[:dry] = true
end
end
Expand All @@ -77,7 +75,7 @@ def add_dry_run_flag(opts)
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_error_on_annotation_flag(opts)
opts.on('--exit-with-error-on-annotation', 'Fail if any file was annotated') do
opts.on("--exit-with-error-on-annotation", "Fail if any file was annotated") do
@options[:error_on_annotation] = true
end
end
Expand All @@ -87,7 +85,7 @@ def add_error_on_annotation_flag(opts)
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_verbose_flag(opts)
opts.on('--verbose', 'Print all annotations') do
opts.on("--verbose", "Print all annotations") do
@options[:verbose] = true
end
end
Expand All @@ -97,7 +95,7 @@ def add_verbose_flag(opts)
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_version_flag(opts)
opts.on('-v', '--version', 'Show Chusaku version number and quit') do
opts.on("-v", "--version", "Show Chusaku version number and quit") do
puts(Chusaku::VERSION)
raise Finished
end
Expand All @@ -108,7 +106,7 @@ def add_version_flag(opts)
# @param opts [OptionParser] OptionParser instance
# @return [void]
def add_help_flag(opts)
opts.on('-h', '--help', 'Show this help message and quit') do
opts.on("-h", "--help", "Show this help message and quit") do
puts(opts)
raise Finished
end
Expand Down
Loading

0 comments on commit f456f91

Please sign in to comment.