66 lines
1.8 KiB
Ruby
Raw Normal View History

2024-03-18 20:31:44 -07:00
# typed: strict
# frozen_string_literal: true
2024-03-18 20:31:44 -07:00
require "abstract_command"
2019-04-17 18:25:08 +09:00
require "cli/parser"
2019-01-18 22:17:46 +05:30
module Homebrew
2024-03-18 20:31:44 -07:00
module DevCmd
class Cat < AbstractCommand
include FileUtils
2020-11-12 10:40:41 -05:00
2024-03-18 20:31:44 -07:00
cmd_args do
description <<~EOS
Display the source of a <formula> or <cask>.
EOS
2020-11-27 11:41:08 -05:00
2024-03-18 20:31:44 -07:00
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
2020-11-19 16:01:04 +01:00
2024-03-18 20:31:44 -07:00
conflicts "--formula", "--cask"
2019-01-18 22:17:46 +05:30
2024-03-18 20:31:44 -07:00
named_args [:formula, :cask], min: 1, without_api: true
2023-03-13 18:31:26 -07:00
end
2020-11-19 16:01:04 +01:00
2024-03-18 20:31:44 -07:00
sig { override.void }
def run
cd HOMEBREW_REPOSITORY do
pager = if Homebrew::EnvConfig.bat?
ENV["BAT_CONFIG_PATH"] = Homebrew::EnvConfig.bat_config_path
ENV["BAT_THEME"] = Homebrew::EnvConfig.bat_theme
ensure_formula_installed!(
"bat",
reason: "displaying <formula>/<cask> source",
# The user might want to capture the output of `brew cat ...`
# Redirect stdout to stderr
output_to_stderr: true,
).opt_bin/"bat"
else
"cat"
end
2024-03-18 20:31:44 -07:00
args.named.to_paths.each do |path|
next path if path.exist?
2024-03-18 20:31:44 -07:00
path = path.basename(".rb") if args.cask?
2024-03-18 20:31:44 -07:00
ofail "#{path}'s source doesn't exist on disk."
end
if Homebrew.failed?
$stderr.puts "The name may be wrong, or the tap hasn't been tapped. Instead try:"
treat_as = "--cask " if args.cask?
treat_as = "--formula " if args.formula?
$stderr.puts " brew info --github #{treat_as}#{args.named.join(" ")}"
return
end
2024-03-18 20:31:44 -07:00
safe_system pager, *args.named.to_paths
end
end
2023-03-13 18:31:26 -07:00
end
end
end