From 483c40fb03b83d109795ece1351dcc1ca318a143 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Wed, 18 Jan 2017 15:54:47 +0530 Subject: [PATCH 1/4] Add custom cop to refactor revision to rebuild in bottle block --- Library/.rubocop.yml | 6 +++ Library/Homebrew/rubocops/bottle_block_cop.rb | 54 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Library/Homebrew/rubocops/bottle_block_cop.rb diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index 097eff9953..012fcd6095 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -6,6 +6,12 @@ AllCops: - '**/Casks/**/*' - '**/vendor/**/*' +require: + - ./Homebrew/rubocops/bottle_block_cop.rb + +CustomCops/CorrectBottleBlock: + Enabled: true + Metrics/AbcSize: Enabled: false diff --git a/Library/Homebrew/rubocops/bottle_block_cop.rb b/Library/Homebrew/rubocops/bottle_block_cop.rb new file mode 100644 index 0000000000..7cb97ea89e --- /dev/null +++ b/Library/Homebrew/rubocops/bottle_block_cop.rb @@ -0,0 +1,54 @@ +module RuboCop + module Cop + module CustomCops + class CorrectBottleBlock < Cop + MSG = 'Use rebuild instead of revision in bottle block'.freeze + + def on_block(node) + return if block_length(node).zero? + method, _args, _body = *node + + keyword, method_name = *method + + if method_name.equal?(:bottle) and has_revision?(_body) + add_offense(node, :expression) + end + end + + private + + def autocorrect(node) + ->(corrector) do + # Check for revision + method, _args, _body = *node + if has_revision?(_body) + replace_revision(corrector, node) + end + end + end + + def has_revision?(body) + body.children.each do |method_call_node| + _receiver, _method_name, *args = *method_call_node + if _method_name == :revision + return true + end + end + false + end + + def replace_revision(corrector, node) + new_source = String.new + node.source.each_line do |line| + if line =~ /\A\s*revision/ + line = line.sub('revision','rebuild') + end + new_source << line + end + corrector.insert_before(node.source_range, new_source) + corrector.remove(node.source_range) + end + end + end + end +end From 0b3d9031e26b47d0ebf48276616c1c4da01ce336 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Wed, 18 Jan 2017 15:55:32 +0530 Subject: [PATCH 2/4] Add --fix option to brew audit command --- Library/Homebrew/dev-cmd/audit.rb | 10 +++++-- Library/Homebrew/rubocops/bottle_block_cop.rb | 30 +++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 88d9a535c2..c2d6aeaedc 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -1,11 +1,14 @@ -#: * `audit` [`--strict`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] []: +#: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] []: #: Check for Homebrew coding style violations. This should be #: run before submitting a new formula. #: #: If no are provided, all of them are checked. #: #: If `--strict` is passed, additional checks are run, including RuboCop -#: style checks. +#: style checks and custom cop checks. +#: +#: If `--fix` is passed, style violations and custom cop violations will be +#: automatically fixed using RuboCop's `--auto-correct` feature. #: #: If `--online` is passed, additional slower checks that require a network #: connection are run. @@ -62,8 +65,9 @@ module Homebrew end if strict + options = { fix: ARGV.flag?("--fix"), realpath: true } # Check style in a single batch run up front for performance - style_results = check_style_json(files, realpath: true) + style_results = check_style_json(files, options) end ff.each do |f| diff --git a/Library/Homebrew/rubocops/bottle_block_cop.rb b/Library/Homebrew/rubocops/bottle_block_cop.rb index 7cb97ea89e..6b699ab5ea 100644 --- a/Library/Homebrew/rubocops/bottle_block_cop.rb +++ b/Library/Homebrew/rubocops/bottle_block_cop.rb @@ -1,36 +1,34 @@ module RuboCop module Cop - module CustomCops + module CustomCops class CorrectBottleBlock < Cop - MSG = 'Use rebuild instead of revision in bottle block'.freeze + MSG = "Use rebuild instead of revision in bottle block".freeze def on_block(node) return if block_length(node).zero? - method, _args, _body = *node + method, _args, body = *node + _keyword, method_name = *method - keyword, method_name = *method - - if method_name.equal?(:bottle) and has_revision?(_body) - add_offense(node, :expression) - end + return unless method_name.equal?(:bottle) && revision?(body) + add_offense(node, :expression) end private def autocorrect(node) - ->(corrector) do + lambda do |corrector| # Check for revision - method, _args, _body = *node - if has_revision?(_body) + _method, _args, body = *node + if revision?(body) replace_revision(corrector, node) end end end - def has_revision?(body) + def revision?(body) body.children.each do |method_call_node| - _receiver, _method_name, *args = *method_call_node - if _method_name == :revision + _receiver, method_name, _args = *method_call_node + if method_name == :revision return true end end @@ -38,10 +36,10 @@ module RuboCop end def replace_revision(corrector, node) - new_source = String.new + new_source = "" node.source.each_line do |line| if line =~ /\A\s*revision/ - line = line.sub('revision','rebuild') + line = line.sub("revision", "rebuild") end new_source << line end From 1f5cf4fd40fc3528e700a421afc9a128c159cac9 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Wed, 18 Jan 2017 22:37:11 +0530 Subject: [PATCH 3/4] Update docs and manpages to include --fix option --- Library/Homebrew/dev-cmd/audit.rb | 4 +-- Library/Homebrew/rubocops/bottle_block_cop.rb | 32 +++++-------------- docs/brew.1.html | 5 ++- manpages/brew-cask.1 | 2 +- manpages/brew.1 | 7 ++-- 5 files changed, 20 insertions(+), 30 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index c2d6aeaedc..5ad12d4b6b 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -5,9 +5,9 @@ #: If no are provided, all of them are checked. #: #: If `--strict` is passed, additional checks are run, including RuboCop -#: style checks and custom cop checks. +#: style checks. #: -#: If `--fix` is passed, style violations and custom cop violations will be +#: If `--fix` is passed, style violations will be #: automatically fixed using RuboCop's `--auto-correct` feature. #: #: If `--online` is passed, additional slower checks that require a network diff --git a/Library/Homebrew/rubocops/bottle_block_cop.rb b/Library/Homebrew/rubocops/bottle_block_cop.rb index 6b699ab5ea..e16672683d 100644 --- a/Library/Homebrew/rubocops/bottle_block_cop.rb +++ b/Library/Homebrew/rubocops/bottle_block_cop.rb @@ -9,42 +9,26 @@ module RuboCop method, _args, body = *node _keyword, method_name = *method - return unless method_name.equal?(:bottle) && revision?(body) - add_offense(node, :expression) + return unless method_name == :bottle + check_revision?(body) end private def autocorrect(node) lambda do |corrector| - # Check for revision - _method, _args, body = *node - if revision?(body) - replace_revision(corrector, node) - end + correction = node.source.sub("revision", "rebuild") + corrector.insert_before(node.source_range, correction) + corrector.remove(node.source_range) end end - def revision?(body) + def check_revision?(body) body.children.each do |method_call_node| _receiver, method_name, _args = *method_call_node - if method_name == :revision - return true - end + next unless method_name == :revision + add_offense(method_call_node, :expression) end - false - end - - def replace_revision(corrector, node) - new_source = "" - node.source.each_line do |line| - if line =~ /\A\s*revision/ - line = line.sub("revision", "rebuild") - end - new_source << line - end - corrector.insert_before(node.source_range, new_source) - corrector.remove(node.source_range) end end end diff --git a/docs/brew.1.html b/docs/brew.1.html index 2e036eb558..2e89a1933e 100644 --- a/docs/brew.1.html +++ b/docs/brew.1.html @@ -453,7 +453,7 @@ the prefix and repository are the same directory.

<

DEVELOPER COMMANDS

-
audit [--strict] [--online] [--new-formula] [--display-cop-names] [--display-filename] [formulae]

Check formulae for Homebrew coding style violations. This should be +

audit [--strict] [--fix] [--online] [--new-formula] [--display-cop-names] [--display-filename] [formulae]

Check formulae for Homebrew coding style violations. This should be run before submitting a new formula.

If no formulae are provided, all of them are checked.

@@ -461,6 +461,9 @@ run before submitting a new formula.

If --strict is passed, additional checks are run, including RuboCop style checks.

+

If --fix is passed, style violations will be +automatically fixed using RuboCop's --auto-correct feature.

+

If --online is passed, additional slower checks that require a network connection are run.

diff --git a/manpages/brew-cask.1 b/manpages/brew-cask.1 index 63aad2c56c..05ec581c0b 100644 --- a/manpages/brew-cask.1 +++ b/manpages/brew-cask.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "BREW\-CASK" "1" "January 2017" "Homebrew" "brew-cask" +.TH "BREW\-CASK" "1" "February 2017" "Homebrew" "brew-cask" . .SH "NAME" \fBbrew\-cask\fR \- a friendly binary installer for macOS diff --git a/manpages/brew.1 b/manpages/brew.1 index f122337fbf..fe14ef3f88 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "BREW" "1" "January 2017" "Homebrew" "brew" +.TH "BREW" "1" "February 2017" "Homebrew" "brew" . .SH "NAME" \fBbrew\fR \- The missing package manager for macOS @@ -625,7 +625,7 @@ Print the version number of Homebrew to standard output and exit\. .SH "DEVELOPER COMMANDS" . .TP -\fBaudit\fR [\fB\-\-strict\fR] [\fB\-\-online\fR] [\fB\-\-new\-formula\fR] [\fB\-\-display\-cop\-names\fR] [\fB\-\-display\-filename\fR] [\fIformulae\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] [\fIformulae\fR] Check \fIformulae\fR for Homebrew coding style violations\. This should be run before submitting a new formula\. . .IP @@ -635,6 +635,9 @@ If no \fIformulae\fR are provided, all of them are checked\. If \fB\-\-strict\fR is passed, additional checks are run, including RuboCop style checks\. . .IP +If \fB\-\-fix\fR is passed, style violations will be automatically fixed using RuboCop\'s \fB\-\-auto\-correct\fR feature\. +. +.IP If \fB\-\-online\fR is passed, additional slower checks that require a network connection are run\. . .IP From 5dc358c1b7254aa622504cd98528779caf6dee00 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sun, 12 Feb 2017 11:07:03 +0530 Subject: [PATCH 4/4] Update rubocop config --- Library/.rubocop.yml | 9 ++++----- Library/Homebrew/rubocops.rb | 1 + Library/Homebrew/rubocops/bottle_block_cop.rb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 Library/Homebrew/rubocops.rb diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index 012fcd6095..30b9ec81e7 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -6,10 +6,9 @@ AllCops: - '**/Casks/**/*' - '**/vendor/**/*' -require: - - ./Homebrew/rubocops/bottle_block_cop.rb +require: ./Homebrew/rubocops.rb -CustomCops/CorrectBottleBlock: +Homebrew/CorrectBottleBlock: Enabled: true Metrics/AbcSize: @@ -47,7 +46,7 @@ Lint/AssignmentInCondition: Enabled: false Lint/EndAlignment: - AlignWith: variable + EnforcedStyleAlignWith: variable Lint/ParenthesesAsGroupedExpression: Enabled: false @@ -69,7 +68,7 @@ Style/BlockDelimiters: EnforcedStyle: line_count_based Style/CaseIndentation: - IndentWhenRelativeTo: end + EnforcedStyle: end Style/ClassAndModuleChildren: EnforcedStyle: nested diff --git a/Library/Homebrew/rubocops.rb b/Library/Homebrew/rubocops.rb new file mode 100644 index 0000000000..1a28dd2134 --- /dev/null +++ b/Library/Homebrew/rubocops.rb @@ -0,0 +1 @@ +require_relative "./rubocops/bottle_block_cop" diff --git a/Library/Homebrew/rubocops/bottle_block_cop.rb b/Library/Homebrew/rubocops/bottle_block_cop.rb index e16672683d..55eb551524 100644 --- a/Library/Homebrew/rubocops/bottle_block_cop.rb +++ b/Library/Homebrew/rubocops/bottle_block_cop.rb @@ -1,6 +1,6 @@ module RuboCop module Cop - module CustomCops + module Homebrew class CorrectBottleBlock < Cop MSG = "Use rebuild instead of revision in bottle block".freeze