Port Homebrew::DevCmd::Cat
This commit is contained in:
parent
df42e9cfb3
commit
55a0991902
@ -1,62 +1,65 @@
|
|||||||
# typed: true
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "abstract_command"
|
||||||
require "cli/parser"
|
require "cli/parser"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
sig { returns(CLI::Parser) }
|
module DevCmd
|
||||||
def self.cat_args
|
class Cat < AbstractCommand
|
||||||
Homebrew::CLI::Parser.new do
|
include FileUtils
|
||||||
description <<~EOS
|
|
||||||
Display the source of a <formula> or <cask>.
|
|
||||||
EOS
|
|
||||||
|
|
||||||
switch "--formula", "--formulae",
|
cmd_args do
|
||||||
description: "Treat all named arguments as formulae."
|
description <<~EOS
|
||||||
switch "--cask", "--casks",
|
Display the source of a <formula> or <cask>.
|
||||||
description: "Treat all named arguments as casks."
|
EOS
|
||||||
|
|
||||||
conflicts "--formula", "--cask"
|
switch "--formula", "--formulae",
|
||||||
|
description: "Treat all named arguments as formulae."
|
||||||
|
switch "--cask", "--casks",
|
||||||
|
description: "Treat all named arguments as casks."
|
||||||
|
|
||||||
named_args [:formula, :cask], min: 1, without_api: true
|
conflicts "--formula", "--cask"
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.cat
|
named_args [:formula, :cask], min: 1, without_api: true
|
||||||
args = cat_args.parse
|
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
args.named.to_paths.each do |path|
|
sig { override.void }
|
||||||
next path if path.exist?
|
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
|
||||||
|
|
||||||
path = path.basename(".rb") if args.cask?
|
args.named.to_paths.each do |path|
|
||||||
|
next path if path.exist?
|
||||||
|
|
||||||
ofail "#{path}'s source doesn't exist on disk."
|
path = path.basename(".rb") if args.cask?
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
safe_system pager, *args.named.to_paths
|
||||||
|
end
|
||||||
end
|
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
|
|
||||||
|
|
||||||
safe_system pager, *args.named.to_paths
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "cmd/shared_examples/args_parse"
|
require "cmd/shared_examples/args_parse"
|
||||||
|
require "dev-cmd/cat"
|
||||||
|
|
||||||
RSpec.describe "brew cat" do
|
RSpec.describe Homebrew::DevCmd::Cat do
|
||||||
it_behaves_like "parseable arguments"
|
it_behaves_like "parseable arguments", argv: ["foo"]
|
||||||
|
|
||||||
it "prints the content of a given Formula", :integration_test do
|
it "prints the content of a given Formula", :integration_test do
|
||||||
formula_file = setup_test_formula "testball"
|
formula_file = setup_test_formula "testball"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user