1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# frozen_string_literal: true
require "open3"
#
# Common stuff for a linter
#
module LinterMixin
def run
offenses = []
applicable_files.each do |file|
if clean?(file)
print "."
else
offenses << file
print "F"
end
end
print "\n"
return if offenses.empty?
raise failure_message_for(offenses)
end
private
def applicable_files
Open3.capture2("git grep -Il ''")[0].split
end
def failure_message_for(offenses)
msg = "#{self.class.name} detected offenses. "
msg += if respond_to?(:fixing_cmd)
"Run `#{fixing_cmd(offenses)}` to fix them."
else
"Affected files: #{offenses.join(' ')}"
end
msg
end
end
#
# Checks trailing new lines in files
#
class MissingTrailingCarriageReturn
include LinterMixin
def clean?(file)
File.read(file) =~ /\n\Z/m
end
end
require 'rubocop/rake_task'
RuboCop::RakeTask.new
desc "Lints ActiveAdmin code base"
task lint: ["rubocop", "lint:missing_trailing_carriage_return", "lint:rspec"]
namespace :lint do
desc "Check for missing trailing new lines"
task :missing_trailing_carriage_return do
puts "Checking for missing trailing carriage returns..."
MissingTrailingCarriageReturn.new.run
end
desc "RSpec specs for linting project files"
task :rspec do
puts "Linting project files..."
sh(
"bundle",
"exec",
"rspec",
*Dir.glob("spec/*.lint.rb")
)
end
end
|