2020-11-25 17:03:23 +01:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-20 13:00:01 -05:00
|
|
|
require "json"
|
2017-05-29 01:21:36 +02:00
|
|
|
require "open3"
|
2018-06-05 23:19:18 -04:00
|
|
|
require "style"
|
2019-04-17 18:25:08 +09:00
|
|
|
require "cli/parser"
|
2016-04-18 17:39:21 -04:00
|
|
|
|
2015-01-02 13:36:26 +00:00
|
|
|
module Homebrew
|
2020-10-20 12:03:48 +02:00
|
|
|
extend T::Sig
|
|
|
|
|
2016-09-26 01:44:51 +02:00
|
|
|
module_function
|
|
|
|
|
2020-10-20 12:03:48 +02:00
|
|
|
sig { returns(CLI::Parser) }
|
2018-11-07 23:44:34 +05:30
|
|
|
def style_args
|
|
|
|
Homebrew::CLI::Parser.new do
|
|
|
|
usage_banner <<~EOS
|
2019-01-30 21:32:35 +00:00
|
|
|
`style` [<options>] [<file>|<tap>|<formula>]
|
2018-11-07 23:44:34 +05:30
|
|
|
|
|
|
|
Check formulae or files for conformance to Homebrew style guidelines.
|
|
|
|
|
2019-01-30 21:32:35 +00:00
|
|
|
Lists of <file>, <tap> and <formula> may not be combined. If none are
|
2018-11-07 23:44:34 +05:30
|
|
|
provided, `style` will run style checks on the whole Homebrew library,
|
|
|
|
including core code and all formulae.
|
|
|
|
EOS
|
|
|
|
switch "--fix",
|
2019-04-30 08:44:35 +01:00
|
|
|
description: "Fix style violations automatically using RuboCop's auto-correct feature."
|
2018-11-07 23:44:34 +05:30
|
|
|
switch "--display-cop-names",
|
2019-04-30 08:44:35 +01:00
|
|
|
description: "Include the RuboCop cop name for each violation in the output."
|
2020-11-29 14:21:06 -05:00
|
|
|
switch "--reset-cache",
|
|
|
|
description: "Reset the RuboCop cache."
|
2020-12-13 23:05:08 +09:00
|
|
|
switch "--formula", "--formulae",
|
|
|
|
description: "Treat all named arguments as formulae."
|
|
|
|
switch "--cask", "--casks",
|
|
|
|
description: "Treat all named arguments as casks."
|
2018-11-07 23:44:34 +05:30
|
|
|
comma_array "--only-cops",
|
2019-04-30 08:44:35 +01:00
|
|
|
description: "Specify a comma-separated <cops> list to check for violations of only the "\
|
|
|
|
"listed RuboCop cops."
|
2018-11-07 23:44:34 +05:30
|
|
|
comma_array "--except-cops",
|
2019-04-30 08:44:35 +01:00
|
|
|
description: "Specify a comma-separated <cops> list to skip checking for violations of the "\
|
|
|
|
"listed RuboCop cops."
|
2020-07-30 18:40:10 +02:00
|
|
|
|
2020-12-13 23:05:08 +09:00
|
|
|
conflicts "--formula", "--cask"
|
2018-11-07 23:44:34 +05:30
|
|
|
conflicts "--only-cops", "--except-cops"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-02 13:36:26 +00:00
|
|
|
def style
|
2020-07-30 18:40:10 +02:00
|
|
|
args = style_args.parse
|
2018-11-07 23:44:34 +05:30
|
|
|
|
2020-03-04 17:28:15 +00:00
|
|
|
target = if args.no_named?
|
2016-09-27 18:48:06 +02:00
|
|
|
nil
|
2015-01-02 13:36:26 +00:00
|
|
|
else
|
2020-12-17 21:14:18 +09:00
|
|
|
args.named.to_paths
|
2015-01-02 13:36:26 +00:00
|
|
|
end
|
|
|
|
|
2018-11-07 23:44:34 +05:30
|
|
|
only_cops = args.only_cops
|
|
|
|
except_cops = args.except_cops
|
2017-05-03 11:33:00 +05:30
|
|
|
|
2020-08-02 03:36:09 +02:00
|
|
|
options = {
|
2020-11-29 14:21:06 -05:00
|
|
|
fix: args.fix?,
|
|
|
|
display_cop_names: args.display_cop_names?,
|
|
|
|
reset_cache: args.reset_cache?,
|
|
|
|
debug: args.debug?,
|
|
|
|
verbose: args.verbose?,
|
2020-08-02 03:36:09 +02:00
|
|
|
}
|
2018-11-07 23:44:34 +05:30
|
|
|
if only_cops
|
2017-05-03 11:33:00 +05:30
|
|
|
options[:only_cops] = only_cops
|
2018-11-07 23:44:34 +05:30
|
|
|
elsif except_cops
|
2017-05-03 11:33:00 +05:30
|
|
|
options[:except_cops] = except_cops
|
2020-11-29 22:35:45 +01:00
|
|
|
else
|
2020-04-13 14:33:21 +01:00
|
|
|
options[:except_cops] = %w[FormulaAuditStrict]
|
2017-05-03 11:33:00 +05:30
|
|
|
end
|
|
|
|
|
2019-10-04 09:19:02 +02:00
|
|
|
Homebrew.failed = !Style.check_style_and_print(target, options)
|
2015-01-02 13:36:26 +00:00
|
|
|
end
|
|
|
|
end
|