Add --only-cops,--except-cops options for brew style and simplify cop names

This commit is contained in:
Gautham Goli 2017-05-03 11:33:00 +05:30
parent c3330c289d
commit fed668b330
14 changed files with 81 additions and 46 deletions

View File

@ -8,16 +8,16 @@ AllCops:
require: ./Homebrew/rubocops.rb require: ./Homebrew/rubocops.rb
FormulaAuditStrict/CorrectBottleBlock: FormulaAuditStrict/BottleBlock:
Enabled: true Enabled: true
FormulaAuditStrict/FormulaDesc: FormulaAuditStrict/Desc:
Enabled: true Enabled: true
Homebrew/FormulaComponentsOrder: FormulaAuditStrict/ComponentsOrder:
Enabled: true Enabled: true
Homebrew/ComponentsRedundancy: FormulaAuditStrict/ComponentsRedundancy:
Enabled: true Enabled: true
Metrics/AbcSize: Metrics/AbcSize:

View File

@ -1,4 +1,4 @@
#: * `style` [`--fix`] [`--display-cop-names`] [<files>|<taps>|<formulae>]: #: * `style` [`--fix`] [`--display-cop-names`] [`--only-cops=`[COP1,COP2..]|`--except-cops=`[COP1,COP2..]] [<files>|<taps>|<formulae>]:
#: Check formulae or files for conformance to Homebrew style guidelines. #: Check formulae or files for conformance to Homebrew style guidelines.
#: #:
#: <formulae> and <files> may not be combined. If both are omitted, style will run #: <formulae> and <files> may not be combined. If both are omitted, style will run
@ -11,6 +11,10 @@
#: If `--display-cop-names` is passed, the RuboCop cop name for each violation #: If `--display-cop-names` is passed, the RuboCop cop name for each violation
#: is included in the output. #: is included in the output.
#: #:
#: If `--only-cops` is passed, only the given Rubocop cop(s)' violations would be checked.
#:
#: If `--except-cops` is passed, the given Rubocop cop(s)' checks would be skipped.
#:
#: Exits with a non-zero status if any style violations are found. #: Exits with a non-zero status if any style violations are found.
require "utils" require "utils"
@ -32,7 +36,20 @@ module Homebrew
ARGV.formulae.map(&:path) ARGV.formulae.map(&:path)
end end
Homebrew.failed = check_style_and_print(target, fix: ARGV.flag?("--fix")) only_cops = ARGV.value("only-cops").to_s.split(",")
except_cops = ARGV.value("except-cops").to_s.split(",")
if !only_cops.empty? && !except_cops.empty?
odie "--only-cops and --except-cops cannot be used simultaneously!"
end
options = { fix: ARGV.flag?("--fix") }
if !only_cops.empty?
options[:only_cops] = only_cops
elsif !except_cops.empty?
options[:except_cops] = except_cops
end
Homebrew.failed = check_style_and_print(target, options)
end end
# Checks style for a list of files, printing simple RuboCop output. # Checks style for a list of files, printing simple RuboCop output.
@ -56,18 +73,22 @@ module Homebrew
] ]
args << "--auto-correct" if fix args << "--auto-correct" if fix
if options[:exclude].eql?(:FormulaAuditStrict) && !(options.key?(:except) || options.key?(:only)) if options[:except_cops]
args << "--except" << :FormulaAuditStrict options[:except_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop, "") }
end cops_to_exclude = options[:except_cops].select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end
if options[:except] args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty?
cops_to_exclude = options[:except].select { |cop| RuboCop::Cop::Cop.registry.names.include?(cop) } elsif options[:only_cops]
args << "--except" << cops_to_exclude.join(" ") unless cops_to_exclude.empty? options[:only_cops].map! { |cop| RuboCop::Cop::Cop.registry.qualified_cop_name(cop, "") }
end cops_to_include = options[:only_cops].select do |cop|
RuboCop::Cop::Cop.registry.names.include?(cop) ||
RuboCop::Cop::Cop.registry.departments.include?(cop.to_sym)
end
if options[:only] args << "--only" << cops_to_include.join(",") unless cops_to_include.empty?
cops_to_include = options[:only].select { |cop| RuboCop::Cop::Cop.registry.names.include?(cop) }
args << "--only" << cops_to_include.join(" ") unless cops_to_include.empty?
end end
if files.nil? if files.nil?

View File

@ -1,4 +1,4 @@
#: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] [`--only=`<method>|`--except=`<method] [<formulae>]: #: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] [`--only=`<method>|`--except=`<method>] [`--only-cops=`[COP1,COP2..]|`--except-cops=`[COP1,COP2..]] [<formulae>]:
#: Check <formulae> for Homebrew coding style violations. This should be #: Check <formulae> for Homebrew coding style violations. This should be
#: run before submitting a new formula. #: run before submitting a new formula.
#: #:
@ -27,9 +27,9 @@
#: #:
#: If `--except` is passed, the methods named `audit_<method>` will not be run. #: If `--except` is passed, the methods named `audit_<method>` will not be run.
#: #:
#: If `--only-cops` is passed, only the mentioned cops' violations would be checked. #: If `--only-cops` is passed, only the given Rubocop cop(s)' violations would be checked.
#: #:
#: If `--except-cops` is passed, the mentioned cops' checks would be skipped. #: If `--except-cops` is passed, the given Rubocop cop(s)' checks would be skipped.
#: #:
#: `audit` exits with a non-zero status if any errors are found. This is useful, #: `audit` exits with a non-zero status if any errors are found. This is useful,
#: for instance, for implementing pre-commit hooks. #: for instance, for implementing pre-commit hooks.
@ -76,17 +76,21 @@ module Homebrew
only_cops = ARGV.value("only-cops").to_s.split(",") only_cops = ARGV.value("only-cops").to_s.split(",")
except_cops = ARGV.value("except-cops").to_s.split(",") except_cops = ARGV.value("except-cops").to_s.split(",")
if !only_cops.empty? && !except_cops.empty? if !only_cops.empty? && !except_cops.empty?
odie "--only-cops and --except-cops cannot be used simulataneously!" odie "--only-cops and --except-cops cannot be used simultaneously!"
elsif (!only_cops.empty? || !except_cops.empty?) && strict
odie "--only-cops/--except-cops and --strict cannot be used simultaneously"
end end
if strict options = { fix: ARGV.flag?("--fix"), realpath: true }
options = { fix: ARGV.flag?("--fix"), realpath: true }
else if !only_cops.empty?
options = { fix: ARGV.flag?("--fix"), realpath: true, exclude: :FormulaAuditStrict } options[:only_cops] = only_cops
elsif !except_cops.empty?
options[:except_cops] = except_cops
elsif !strict
options[:except_cops] = [:FormulaAuditStrict]
end end
options[:only] = only_cops unless only_cops.empty?
options[:except] = except_cops unless except_cops.empty?
# Check style in a single batch run up front for performance # Check style in a single batch run up front for performance
style_results = check_style_json(files, options) style_results = check_style_json(files, options)
@ -1272,7 +1276,7 @@ class FormulaAuditor
only_audits = ARGV.value("only").to_s.split(",") only_audits = ARGV.value("only").to_s.split(",")
except_audits = ARGV.value("except").to_s.split(",") except_audits = ARGV.value("except").to_s.split(",")
if !only_audits.empty? && !except_audits.empty? if !only_audits.empty? && !except_audits.empty?
odie "--only and --except cannot be used simulataneously!" odie "--only and --except cannot be used simultaneously!"
end end
methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name| methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name|

View File

@ -7,7 +7,7 @@ module RuboCop
# #
# - `rebuild` should be used instead of `revision` in `bottle` block # - `rebuild` should be used instead of `revision` in `bottle` block
class CorrectBottleBlock < FormulaCop class BottleBlock < FormulaCop
MSG = "Use rebuild instead of revision in bottle block".freeze MSG = "Use rebuild instead of revision in bottle block".freeze
def audit_formula(_node, _class_node, _parent_class_node, formula_class_body_node) def audit_formula(_node, _class_node, _parent_class_node, formula_class_body_node)

View File

@ -2,12 +2,12 @@ require_relative "./extend/formula_cop"
module RuboCop module RuboCop
module Cop module Cop
module Homebrew module FormulaAuditStrict
# This cop checks for correct order of components in a Formula # This cop checks for correct order of components in a Formula
# #
# - component_precedence_list has component hierarchy in a nested list # - component_precedence_list has component hierarchy in a nested list
# where each sub array contains components' details which are at same precedence level # where each sub array contains components' details which are at same precedence level
class FormulaComponentsOrder < FormulaCop class ComponentsOrder < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, formula_class_body_node) def audit_formula(_node, _class_node, _parent_class_node, formula_class_body_node)
component_precedence_list = [ component_precedence_list = [
[{ name: :include, type: :method_call }], [{ name: :include, type: :method_call }],

View File

@ -2,7 +2,7 @@ require_relative "./extend/formula_cop"
module RuboCop module RuboCop
module Cop module Cop
module Homebrew module FormulaAuditStrict
# This cop checks if redundant components are present and other component errors # This cop checks if redundant components are present and other component errors
# #
# - `url|checksum|mirror` should be inside `stable` block # - `url|checksum|mirror` should be inside `stable` block

View File

@ -11,7 +11,7 @@ module RuboCop
# - Checks if `desc` begins with an article # - Checks if `desc` begins with an article
# - Checks for correct usage of `command-line` in `desc` # - Checks for correct usage of `command-line` in `desc`
# - Checks if `desc` contains the formula name # - Checks if `desc` contains the formula name
class FormulaDesc < FormulaCop class Desc < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body) def audit_formula(_node, _class_node, _parent_class_node, body)
desc_call = find_node_method_by_name(body, :desc) desc_call = find_node_method_by_name(body, :desc)

View File

@ -3,7 +3,7 @@ require "rubocop/rspec/support"
require_relative "../../extend/string" require_relative "../../extend/string"
require_relative "../../rubocops/bottle_block_cop" require_relative "../../rubocops/bottle_block_cop"
describe RuboCop::Cop::FormulaAuditStrict::CorrectBottleBlock do describe RuboCop::Cop::FormulaAuditStrict::BottleBlock do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "When auditing Bottle Block" do context "When auditing Bottle Block" do

View File

@ -3,7 +3,7 @@ require "rubocop/rspec/support"
require_relative "../../extend/string" require_relative "../../extend/string"
require_relative "../../rubocops/components_order_cop" require_relative "../../rubocops/components_order_cop"
describe RuboCop::Cop::Homebrew::FormulaComponentsOrder do describe RuboCop::Cop::FormulaAuditStrict::ComponentsOrder do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "When auditing formula components order" do context "When auditing formula components order" do

View File

@ -3,7 +3,7 @@ require "rubocop/rspec/support"
require_relative "../../extend/string" require_relative "../../extend/string"
require_relative "../../rubocops/components_redundancy_cop" require_relative "../../rubocops/components_redundancy_cop"
describe RuboCop::Cop::Homebrew::ComponentsRedundancy do describe RuboCop::Cop::FormulaAuditStrict::ComponentsRedundancy do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "When auditing formula components common errors" do context "When auditing formula components common errors" do

View File

@ -3,7 +3,7 @@ require "rubocop/rspec/support"
require_relative "../../extend/string" require_relative "../../extend/string"
require_relative "../../rubocops/formula_desc_cop" require_relative "../../rubocops/formula_desc_cop"
describe RuboCop::Cop::FormulaAuditStrict::FormulaDesc do describe RuboCop::Cop::FormulaAuditStrict::Desc do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "When auditing formula desc" do context "When auditing formula desc" do

View File

@ -402,7 +402,7 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
If `--env=std` is passed, use the standard `PATH` instead of superenv's. If `--env=std` is passed, use the standard `PATH` instead of superenv's.
* `style` [`--fix`] [`--display-cop-names`] [`files`|`taps`|`formulae`]: * `style` [`--fix`] [`--display-cop-names`] [`--only-cops=`[COP1,COP2..]|`--except-cops=`[COP1,COP2..]] [`files`|`taps`|`formulae`]:
Check formulae or files for conformance to Homebrew style guidelines. Check formulae or files for conformance to Homebrew style guidelines.
`formulae` and `files` may not be combined. If both are omitted, style will run `formulae` and `files` may not be combined. If both are omitted, style will run
@ -415,6 +415,10 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
If `--display-cop-names` is passed, the RuboCop cop name for each violation If `--display-cop-names` is passed, the RuboCop cop name for each violation
is included in the output. is included in the output.
If `--only-cops` is passed, only the given Rubocop cop(s)' violations would be checked.
If `--except-cops` is passed, the given Rubocop cop(s)' checks would be skipped.
Exits with a non-zero status if any style violations are found. Exits with a non-zero status if any style violations are found.
* `switch` `name` `version`: * `switch` `name` `version`:
@ -606,7 +610,7 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
## DEVELOPER COMMANDS ## DEVELOPER COMMANDS
* `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] [`--only=``method`|`--except=``method] [<formulae`]: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] [`--only=``method`|`--except=``method`] [`--only-cops=`[COP1,COP2..]|`--except-cops=`[COP1,COP2..]] [`formulae`]:
Check `formulae` for Homebrew coding style violations. This should be Check `formulae` for Homebrew coding style violations. This should be
run before submitting a new formula. run before submitting a new formula.
@ -635,9 +639,9 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note
If `--except` is passed, the methods named `audit_`method`` will not be run. If `--except` is passed, the methods named `audit_`method`` will not be run.
If `--only-cops` is passed, only the mentioned cops' violations would be checked. If `--only-cops` is passed, only the given Rubocop cop(s)' violations would be checked.
If `--except-cops` is passed, the mentioned cops' checks would be skipped. If `--except-cops` is passed, the given Rubocop cop(s)' checks would be skipped.
`audit` exits with a non-zero status if any errors are found. This is useful, `audit` exits with a non-zero status if any errors are found. This is useful,
for instance, for implementing pre-commit hooks. for instance, for implementing pre-commit hooks.

View File

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3 .\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3
. .
.TH "BREW\-CASK" "1" "April 2017" "Homebrew" "brew-cask" .TH "BREW\-CASK" "1" "May 2017" "Homebrew" "brew-cask"
. .
.SH "NAME" .SH "NAME"
\fBbrew\-cask\fR \- a friendly binary installer for macOS \fBbrew\-cask\fR \- a friendly binary installer for macOS

View File

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3 .\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3
. .
.TH "BREW" "1" "April 2017" "Homebrew" "brew" .TH "BREW" "1" "May 2017" "Homebrew" "brew"
. .
.SH "NAME" .SH "NAME"
\fBbrew\fR \- The missing package manager for macOS \fBbrew\fR \- The missing package manager for macOS
@ -415,7 +415,7 @@ Start a Homebrew build environment shell\. Uses our years\-battle\-hardened Home
If \fB\-\-env=std\fR is passed, use the standard \fBPATH\fR instead of superenv\'s\. If \fB\-\-env=std\fR is passed, use the standard \fBPATH\fR instead of superenv\'s\.
. .
.TP .TP
\fBstyle\fR [\fB\-\-fix\fR] [\fB\-\-display\-cop\-names\fR] [\fIfiles\fR|\fItaps\fR|\fIformulae\fR] \fBstyle\fR [\fB\-\-fix\fR] [\fB\-\-display\-cop\-names\fR] [\fB\-\-only\-cops=\fR[COP1,COP2\.\.]|\fB\-\-except\-cops=\fR[COP1,COP2\.\.]] [\fIfiles\fR|\fItaps\fR|\fIformulae\fR]
Check formulae or files for conformance to Homebrew style guidelines\. Check formulae or files for conformance to Homebrew style guidelines\.
. .
.IP .IP
@ -428,6 +428,12 @@ If \fB\-\-fix\fR is passed, style violations will be automatically fixed using R
If \fB\-\-display\-cop\-names\fR is passed, the RuboCop cop name for each violation is included in the output\. If \fB\-\-display\-cop\-names\fR is passed, the RuboCop cop name for each violation is included in the output\.
. .
.IP .IP
If \fB\-\-only\-cops\fR is passed, only the given Rubocop cop(s)\' violations would be checked\.
.
.IP
If \fB\-\-except\-cops\fR is passed, the given Rubocop cop(s)\' checks would be skipped\.
.
.IP
Exits with a non\-zero status if any style violations are found\. Exits with a non\-zero status if any style violations are found\.
. .
.TP .TP
@ -631,7 +637,7 @@ Print the version number of Homebrew to standard output and exit\.
.SH "DEVELOPER COMMANDS" .SH "DEVELOPER COMMANDS"
. .
.TP .TP
\fBaudit\fR [\fB\-\-strict\fR] [\fB\-\-fix\fR] [\fB\-\-online\fR] [\fB\-\-new\-formula\fR] [\fB\-\-display\-cop\-names\fR] [\fB\-\-display\-filename\fR] [\fB\-\-only=\fR\fImethod\fR|\fB\-\-except=\fR\fImethod] [<formulae\fR] \fBaudit\fR [\fB\-\-strict\fR] [\fB\-\-fix\fR] [\fB\-\-online\fR] [\fB\-\-new\-formula\fR] [\fB\-\-display\-cop\-names\fR] [\fB\-\-display\-filename\fR] [\fB\-\-only=\fR\fImethod\fR|\fB\-\-except=\fR\fImethod\fR] [\fB\-\-only\-cops=\fR[COP1,COP2\.\.]|\fB\-\-except\-cops=\fR[COP1,COP2\.\.]] [\fIformulae\fR]
Check \fIformulae\fR for Homebrew coding style violations\. This should be run before submitting a new formula\. Check \fIformulae\fR for Homebrew coding style violations\. This should be run before submitting a new formula\.
. .
.IP .IP
@ -662,10 +668,10 @@ If \fB\-\-only\fR is passed, only the methods named \fBaudit_<method>\fR will be
If \fB\-\-except\fR is passed, the methods named \fBaudit_<method>\fR will not be run\. If \fB\-\-except\fR is passed, the methods named \fBaudit_<method>\fR will not be run\.
. .
.IP .IP
If \fB\-\-only\-cops\fR is passed, only the mentioned cops\' violations would be checked\. If \fB\-\-only\-cops\fR is passed, only the given Rubocop cop(s)\' violations would be checked\.
. .
.IP .IP
If \fB\-\-except\-cops\fR is passed, the mentioned cops\' checks would be skipped\. If \fB\-\-except\-cops\fR is passed, the given Rubocop cop(s)\' checks would be skipped\.
. .
.IP .IP
\fBaudit\fR exits with a non\-zero status if any errors are found\. This is useful, for instance, for implementing pre\-commit hooks\. \fBaudit\fR exits with a non\-zero status if any errors are found\. This is useful, for instance, for implementing pre\-commit hooks\.