Module: Homebrew::Style Private
- Extended by:
- SystemCommand::Mixin, Utils::Output::Mixin
- Defined in:
- style.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper module for running RuboCop.
Defined Under Namespace
Constant Summary collapse
- RUBOCOP =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.let((HOMEBREW_LIBRARY_PATH/"utils/rubocop.rb").freeze, Pathname)
- SHELLCHECK_TIMEOUT =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
In CI
brew styletakes ~70s on Homebrew/brew and ~140s on the official taps, nearly all of it RuboCop; each of these linters needs a few seconds. 60- SHFMT_TIMEOUT =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
60- ACTIONLINT_TIMEOUT =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
30
Class Method Summary collapse
- .actionlint ⇒ Pathname private
-
.check_style_and_print(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Boolean
private
Checks style for a list of files, printing simple RuboCop output.
- .check_style_impl(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Offenses, Boolean private
-
.check_style_json(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Offenses
private
Checks style for a list of files, returning results as an Offenses object parsed from its JSON output.
- .github_workflow_files ⇒ Array<Pathname> private
- .json_result!(result) ⇒ T.untyped private
- .run_actionlint!(files, actionlint_path: nil, shellcheck_path: nil, out: $stdout, err: $stderr) ⇒ Boolean private
- .run_rubocop(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Boolean, Array<Hash{String => T.untyped}> private
- .run_shellcheck(files, output_type, fix: false, shellcheck_path: nil, out: $stdout, err: $stderr) ⇒ Boolean, ... private
- .run_shfmt!(files, fix: false, shfmt_path: nil, out: $stdout, err: $stderr) ⇒ Boolean private
- .shell_scripts ⇒ Array<Pathname> private
- .shellcheck ⇒ Pathname private
- .shfmt ⇒ Pathname private
- .single_tap(files) ⇒ Tap? private
Methods included from Utils::Output::Mixin
issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning
Methods included from SystemCommand::Mixin
command_sandbox, system_command, system_command!
Class Method Details
.actionlint ⇒ Pathname
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
571 572 573 574 575 576 577 |
# File 'style.rb', line 571 def self.actionlint require "formula" T.cast(Formula["actionlint"].ensure_installed!(latest: true, reason: "GitHub Actions checks", executable: "actionlint", version_args: ["-version"]), Pathname) end |
.check_style_and_print(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Checks style for a list of files, printing simple RuboCop output. Returns true if violations were found, false otherwise.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'style.rb', line 31 def self.check_style_and_print(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) success = check_style_impl(files, :print, fix:, todo:, except_cops:, only_cops:, reset_cache:, debug:, verbose:) if GitHub::Actions.env_set? && !success check_style_json( files, fix:, todo:, except_cops:, only_cops:, reset_cache:, debug:, verbose: ).each do |path, offenses| offenses.each do |o| line = o.location.line column = o.location.line annotation = GitHub::Actions::Annotation.new(:error, o., file: path, line:, column:) puts annotation if annotation.relevant? end end end T.cast(success, T::Boolean) end |
.check_style_impl(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Offenses, Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'style.rb', line 86 def self.check_style_impl(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) raise ArgumentError, "Invalid output type: #{output_type.inspect}" if [:print, :json].exclude?(output_type) ruby_files = T.let([], T::Array[Pathname]) shell_files = T.let([], T::Array[Pathname]) actionlint_files = T.let([], T::Array[Pathname]) Array(files).map { Pathname(it) } .each do |path| case path.extname when ".rb" ruby_files << path when ".sh" shell_files << path when ".yml" actionlint_files << path if path.realpath.to_s.include?("/.github/workflows/") else if shell_scripts.include?(path) shell_files << path next end ruby_files << path shell_files += if [HOMEBREW_PREFIX, HOMEBREW_REPOSITORY].include?(path) shell_scripts else path.glob("**/*.sh") .reject { |file_path| file_path.to_s.include?("/vendor/") || file_path.directory? } end actionlint_files += (path/".github/workflows").glob("*.y{,a}ml") end end rubocop_needed = files.blank? || ruby_files.any? shell_needed = files.blank? || shell_files.any? actionlint_files = github_workflow_files if files.blank? && actionlint_files.blank? has_actionlint_workflow = actionlint_files.any? do |path| path.to_s.end_with?("/.github/workflows/actionlint.yml") end odebug "actionlint workflow detected. Skipping actionlint checks." if has_actionlint_workflow actionlint_needed = files.blank? || (!has_actionlint_workflow && actionlint_files.any?) # Resolve the linter executables (installing them if necessary) before # spawning threads so those threads cannot race to install formulae. shellcheck_path = (shellcheck if shell_needed || actionlint_needed) shfmt_path = (shfmt_executable if shell_needed) actionlint_path = (actionlint if actionlint_needed) shellcheck_out = StringIO.new shellcheck_err = StringIO.new shfmt_out = StringIO.new shfmt_err = StringIO.new actionlint_out = StringIO.new actionlint_err = StringIO.new # Run the shell and GitHub Actions checks on background threads with # buffered output while RuboCop runs on the main thread. shell_thread = Thread.new do shellcheck_result = if shell_needed run_shellcheck(shell_files, output_type, fix:, shellcheck_path:, out: shellcheck_out, err: shellcheck_err) elsif output_type == :json [] else true end # `shellcheck --fix` and `shfmt --write` may touch the same files so # they must not run concurrently with each other. shfmt_result = !shell_needed || run_shfmt!(shell_files, fix:, shfmt_path:, out: shfmt_out, err: shfmt_err) [shellcheck_result, shfmt_result] end actionlint_thread = Thread.new do !actionlint_needed || run_actionlint!(actionlint_files, actionlint_path:, shellcheck_path:, out: actionlint_out, err: actionlint_err) end rubocop_result = if rubocop_needed run_rubocop(ruby_files, output_type, fix:, todo:, except_cops:, only_cops:, reset_cache:, debug:, verbose:) elsif output_type == :json [] else true end shellcheck_result, shfmt_result = shell_thread.value actionlint_result = actionlint_thread.value [ [shellcheck_out, shellcheck_err], [shfmt_out, shfmt_err], [actionlint_out, actionlint_err], ].each do |out, err| $stdout.print out.string $stderr.print err.string end if output_type == :json Offenses.new( T.cast(rubocop_result, T::Array[T::Hash[String, T.untyped]]) + T.cast(shellcheck_result, T::Array[T::Hash[String, T.untyped]]), ) else rubocop_result && !!shellcheck_result && shfmt_result && actionlint_result end end |
.check_style_json(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Offenses
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Checks style for a list of files, returning results as an Offenses object parsed from its JSON output.
67 68 69 70 71 |
# File 'style.rb', line 67 def self.check_style_json(files, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) T.cast(check_style_impl(files, :json, fix:, todo:, except_cops:, only_cops:, reset_cache:, debug:, verbose:), Offenses) end |
.github_workflow_files ⇒ Array<Pathname>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
545 546 547 |
# File 'style.rb', line 545 def self.github_workflow_files HOMEBREW_REPOSITORY.glob(".github/workflows/*.yml") end |
.json_result!(result) ⇒ T.untyped
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
514 515 516 517 518 519 520 521 |
# File 'style.rb', line 514 def self.json_result!(result) # An exit status of 1 just means violations were found; other numbers mean # execution errors. # JSON needs to be at least 2 characters. result.assert_success! if !(0..1).cover?(result.status.exitstatus) || result.stdout.length < 2 JSON.parse(result.stdout) end |
.run_actionlint!(files, actionlint_path: nil, shellcheck_path: nil, out: $stdout, err: $stderr) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'style.rb', line 482 def self.run_actionlint!(files, actionlint_path: nil, shellcheck_path: nil, out: $stdout, err: $stderr) actionlint_path ||= actionlint shellcheck_path ||= shellcheck files = github_workflow_files if files.blank? tap_configs = files.filter_map do |f| tap = Tap.from_path(f) next unless tap tap_config = tap.path/".github/actionlint.yaml" tap_config if tap_config.exist? end.uniq config_file = if tap_configs.one? tap_configs.fetch(0) else HOMEBREW_REPOSITORY/".github/actionlint.yaml" end # the ignore is to avoid false positives in e.g. actions, homebrew-test-bot args = ["-shellcheck", shellcheck_path, "-config-file", config_file, "-ignore", "image: string; options: string", "-ignore", "label .* is unknown"] args << "-color" if Tty.color? result = run_linter(actionlint_path, args, files, timeout: ACTIONLINT_TIMEOUT) out.print result.stdout err.print result.stderr result.success? end |
.run_rubocop(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) ⇒ Boolean, Array<Hash{String => T.untyped}>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'style.rb', line 210 def self.run_rubocop(files, output_type, fix: false, todo: false, except_cops: nil, only_cops: nil, reset_cache: false, debug: false, verbose: false) require "warnings" Warnings.ignore :parser_syntax do require "rubocop" end require "rubocops/all" args = %w[ --force-exclusion ] args << "--autocorrect-all" if fix args << "--disable-uncorrectable" if todo args += ["--extra-details"] if verbose if except_cops except_cops.map! { |cop| RuboCop::Cop::Registry.global.qualified_cop_name(cop.to_s, "") } cops_to_exclude = except_cops.select do |cop| RuboCop::Cop::Registry.global.names.include?(cop) || RuboCop::Cop::Registry.global.departments.include?(cop.to_sym) end args << "--except" << cops_to_exclude.join(",") unless cops_to_exclude.empty? elsif only_cops only_cops.map! { |cop| RuboCop::Cop::Registry.global.qualified_cop_name(cop.to_s, "") } cops_to_include = only_cops.select do |cop| RuboCop::Cop::Registry.global.names.include?(cop) || RuboCop::Cop::Registry.global.departments.include?(cop.to_sym) end odie "RuboCops #{only_cops.join(",")} were not found" if cops_to_include.empty? args << "--only" << cops_to_include.join(",") end files.map!(&:expand_path) base_dir = Dir.pwd if files.blank? || files == [HOMEBREW_REPOSITORY] files = [HOMEBREW_LIBRARY_PATH] base_dir = HOMEBREW_LIBRARY_PATH elsif files.any? { |f| f.to_s.start_with?(HOMEBREW_REPOSITORY/"docs") || (f.basename.to_s == "docs") } args << "--config" << (HOMEBREW_REPOSITORY/"docs/docs_rubocop_style.yml") elsif files.any? { |f| f.to_s.start_with? HOMEBREW_LIBRARY_PATH } base_dir = HOMEBREW_LIBRARY_PATH elsif (tap = single_tap(files)) # RuboCop roots its project index at the working directory for this # config (see `tap_rubocop_style.yml`). A tap has to be indexed on its # own: rooted at `Library`, the index spans every installed tap and the # cross-file cops pair one tap's constants and methods with another's as # reassignments and duplicates. The paths stay as given, so the shared # config's `Taps/...` exclusions keep matching a symlinked tap. args << "--config" << (HOMEBREW_LIBRARY/"tap_rubocop_style.yml") base_dir = tap.path else args << "--config" << (HOMEBREW_LIBRARY/".rubocop.yml") base_dir = HOMEBREW_LIBRARY if files.any? { |f| f.to_s.start_with? HOMEBREW_LIBRARY } end HOMEBREW_CACHE.mkpath cache_dir = HOMEBREW_CACHE.realpath/"style" cache_env = if (!cache_dir.exist? && cache_dir.parent.writable?) || cache_dir.writable? args << "--parallel" FileUtils.rm_rf cache_dir if reset_cache { "XDG_CACHE_HOME" => cache_dir.to_s } else args << "--cache" << "false" {} end args += files ruby_args = HOMEBREW_RUBY_EXEC_ARGS.dup case output_type when :print args << "--debug" if debug # Don't show the default formatter's progress dots # on CI or if only checking a single file. args << "--format" << "clang" if ENV["CI"] || files.one? { |f| !f.directory? } args << "--color" if Tty.color? system cache_env, *ruby_args, "--", RUBOCOP, *args, chdir: base_dir $CHILD_STATUS.success? when :json result = system_command ruby_args.shift, args: [*ruby_args, "--", RUBOCOP, "--format", "json", *args], env: cache_env, chdir: base_dir json = json_result!(result) json["files"].each do |file| file["path"] = File.absolute_path(file["path"], base_dir) end end end |
.run_shellcheck(files, output_type, fix: false, shellcheck_path: nil, out: $stdout, err: $stderr) ⇒ Boolean, ...
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'style.rb', line 331 def self.run_shellcheck(files, output_type, fix: false, shellcheck_path: nil, out: $stdout, err: $stderr) shellcheck_path ||= shellcheck files = shell_scripts if files.blank? files = files.map(&:realpath) # use absolute file paths args = [ "--shell=bash", "--enable=all", "--external-sources", "--source-path=#{HOMEBREW_LIBRARY}", ] if fix # patch options: # -g 0 (--get=0) : suppress environment variable `PATCH_GET` # -f (--force) : we know what we are doing, force apply patches # -d / (--directory=/) : change to root directory, since we use absolute file paths # -p0 (--strip=0) : do not strip path prefixes, since we are at root directory # NOTE: We use short flags for compatibility. patch_command = %w[patch -g 0 -f -d / -p0] patches = shellcheck_chunks(shellcheck_path, files, ["--format=diff", *args]).map(&:stdout).join Utils.safe_popen_write(*patch_command) { |p| p.write(patches) } if patches.present? end case output_type when :print print_args = ["--format=tty", *args] print_args << "--color=always" if Tty.color? results = shellcheck_chunks(shellcheck_path, files, print_args) results.each do |result| out.print result.stdout err.print result.stderr end results.all?(&:success?) when :json results = shellcheck_chunks(shellcheck_path, files, ["--format=json", *args]) json = results.flat_map { |result| json_result!(result) } # Convert to same format as RuboCop offenses. severity_hash = { "style" => "refactor", "info" => "convention" } json.group_by { |v| v["file"] } .map do |k, v| { "path" => k, "offenses" => v.map do |o| o.delete("file") o["cop_name"] = "SC#{o.delete("code")}" level = o.delete("level") o["severity"] = severity_hash.fetch(level, level) line = o.delete("line") column = o.delete("column") o["corrected"] = false o["correctable"] = o.delete("fix").present? o["location"] = { "start_line" => line, "start_column" => column, "last_line" => o.delete("endLine"), "last_column" => o.delete("endColumn"), "line" => line, "column" => column, } o end, } end end end |
.run_shfmt!(files, fix: false, shfmt_path: nil, out: $stdout, err: $stderr) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'style.rb', line 457 def self.run_shfmt!(files, fix: false, shfmt_path: nil, out: $stdout, err: $stderr) shfmt_path ||= shfmt_executable files = shell_scripts if files.blank? # Do not format completions and Dockerfile files.delete(HOMEBREW_REPOSITORY/"completions/bash/brew") files.delete(HOMEBREW_REPOSITORY/"Dockerfile") args = ["--language-dialect", "bash", "--indent", "2", "--case-indent", "--"] args.unshift("--write") if fix # need to add before "--" result = run_linter(shfmt, args, files, timeout: SHFMT_TIMEOUT, env: { "HOMEBREW_SHFMT" => shfmt_path.to_s }) out.print result.stdout err.print result.stderr result.success? end |
.shell_scripts ⇒ Array<Pathname>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'style.rb', line 524 def self.shell_scripts [ HOMEBREW_BREW_FILE.realpath, HOMEBREW_REPOSITORY/"completions/bash/brew", HOMEBREW_REPOSITORY/"Dockerfile", *HOMEBREW_REPOSITORY.glob(".devcontainer/**/*.sh"), *HOMEBREW_REPOSITORY.glob(".github/scripts/*.sh"), *HOMEBREW_REPOSITORY.glob("package/scripts/*"), *HOMEBREW_LIBRARY.glob("Homebrew/**/*.sh").reject { |path| path.to_s.include?("/vendor/") }, *HOMEBREW_LIBRARY.glob("Homebrew/shims/**/*").map(&:realpath).uniq .reject(&:directory?) .reject { |path| path.basename.to_s == "cc" } .select do |path| %r{^#! ?/bin/(?:ba)?sh( |$)}.match?(path.read(13)) end, *HOMEBREW_LIBRARY.glob("Homebrew/{dev-,}cmd/*.sh"), *HOMEBREW_LIBRARY.glob("Homebrew/{cask/,}utils/*.sh"), ] end |
.shellcheck ⇒ Pathname
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
550 551 552 553 554 555 |
# File 'style.rb', line 550 def self.shellcheck require "formula" T.cast(Formula["shellcheck"].ensure_installed!(latest: true, reason: "shell style checks", executable: "shellcheck"), Pathname) end |
.shfmt ⇒ Pathname
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
558 559 560 |
# File 'style.rb', line 558 def self.shfmt HOMEBREW_LIBRARY/"Homebrew/utils/shfmt.sh" end |
.single_tap(files) ⇒ Tap?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
313 314 315 316 317 318 319 |
# File 'style.rb', line 313 def self.single_tap(files) taps = files.filter_map { |file| Tap.from_path(file) } return if taps.empty? || taps.length != files.length return unless taps.map(&:name).uniq.one? taps.first end |