style: implement shellcheck autofix in brew style --fix

This commit is contained in:
XuehaiPan 2021-11-07 21:00:01 +08:00
parent cb249836cb
commit fa85d00b95

View File

@ -62,7 +62,7 @@ module Homebrew
shellcheck_result = if ruby_files.any? && shell_files.none? shellcheck_result = if ruby_files.any? && shell_files.none?
output_type == :json ? [] : true output_type == :json ? [] : true
else else
run_shellcheck(shell_files, output_type) run_shellcheck(shell_files, output_type, fix: fix)
end end
shfmt_result = if ruby_files.any? && shell_files.none? shfmt_result = if ruby_files.any? && shell_files.none?
@ -164,17 +164,24 @@ module Homebrew
end end
end end
def run_shellcheck(files, output_type) def run_shellcheck(files, output_type, fix: false)
files = shell_scripts if files.blank? files = shell_scripts if files.blank?
args = ["--shell=bash", "--enable=all", "--external-sources", "--source-path=#{HOMEBREW_LIBRARY}", "--", *files] files = files.map(&:realpath)
args = ["--shell=bash", "--enable=all", "--external-sources", "--source-path=#{HOMEBREW_LIBRARY}", "--"]
if fix
patch = system_command shellcheck, args: ["--format=diff", *args, *files]
system_command "patch", args: ["-d", "/", "-p0"], input: patch.stdout
end
case output_type case output_type
when :print when :print
system shellcheck, "--format=tty", *args system shellcheck, "--format=tty", *args, *files
$CHILD_STATUS.success? $CHILD_STATUS.success?
when :json when :json
result = system_command shellcheck, args: ["--format=json", *args] result = system_command shellcheck, args: ["--format=json", *args, *files]
json = json_result!(result) json = json_result!(result)
# Convert to same format as RuboCop offenses. # Convert to same format as RuboCop offenses.