From fa85d00b957126ac56c53d4eb5faaa7f4d6f1ac2 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Sun, 7 Nov 2021 21:00:01 +0800 Subject: [PATCH] style: implement shellcheck autofix in `brew style --fix` --- Library/Homebrew/style.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/style.rb b/Library/Homebrew/style.rb index 872004000c..1eb36b3383 100644 --- a/Library/Homebrew/style.rb +++ b/Library/Homebrew/style.rb @@ -62,7 +62,7 @@ module Homebrew shellcheck_result = if ruby_files.any? && shell_files.none? output_type == :json ? [] : true else - run_shellcheck(shell_files, output_type) + run_shellcheck(shell_files, output_type, fix: fix) end shfmt_result = if ruby_files.any? && shell_files.none? @@ -164,17 +164,24 @@ module Homebrew end end - def run_shellcheck(files, output_type) + def run_shellcheck(files, output_type, fix: false) 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 when :print - system shellcheck, "--format=tty", *args + system shellcheck, "--format=tty", *args, *files $CHILD_STATUS.success? when :json - result = system_command shellcheck, args: ["--format=json", *args] + result = system_command shellcheck, args: ["--format=json", *args, *files] json = json_result!(result) # Convert to same format as RuboCop offenses.