Port Homebrew::Cmd::Home

This commit is contained in:
Douglas Eichelberger 2024-03-29 18:34:33 -07:00
parent d0c1af4f9e
commit 74218c0483
3 changed files with 41 additions and 40 deletions

View File

@ -1,54 +1,51 @@
# typed: true
# frozen_string_literal: true
require "cli/parser"
require "abstract_command"
require "formula"
module Homebrew
module_function
module Cmd
class Home < AbstractCommand
cmd_args do
description <<~EOS
Open a <formula> or <cask>'s homepage in a browser, or open
Homebrew's own homepage if no argument is provided.
EOS
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
sig { returns(CLI::Parser) }
def home_args
Homebrew::CLI::Parser.new do
description <<~EOS
Open a <formula> or <cask>'s homepage in a browser, or open
Homebrew's own homepage if no argument is provided.
EOS
switch "--formula", "--formulae",
description: "Treat all named arguments as formulae."
switch "--cask", "--casks",
description: "Treat all named arguments as casks."
conflicts "--formula", "--cask"
conflicts "--formula", "--cask"
named_args [:formula, :cask]
end
named_args [:formula, :cask]
end
end
sig { override.void }
def run
if args.no_named?
exec_browser HOMEBREW_WWW
return
end
sig { void }
def home
args = home_args.parse
# to_formulae_and_casks is typed to possibly return Kegs (but won't without explicitly asking)
formulae_or_casks = T.cast(args.named.to_formulae_and_casks, T::Array[T.any(Formula, Cask::Cask)])
homepages = formulae_or_casks.map do |formula_or_cask|
puts "Opening homepage for #{name_of(formula_or_cask)}"
formula_or_cask.homepage
end
if args.no_named?
exec_browser HOMEBREW_WWW
return
end
exec_browser(*T.unsafe(homepages))
end
# to_formulae_and_casks is typed to possibly return Kegs (but won't without explicitly asking)
formulae_or_casks = T.cast(args.named.to_formulae_and_casks, T::Array[T.any(Formula, Cask::Cask)])
homepages = formulae_or_casks.map do |formula_or_cask|
puts "Opening homepage for #{name_of(formula_or_cask)}"
formula_or_cask.homepage
end
exec_browser(*T.unsafe(homepages))
end
def name_of(formula_or_cask)
if formula_or_cask.is_a? Formula
"Formula #{formula_or_cask.name}"
else
"Cask #{formula_or_cask.token}"
def name_of(formula_or_cask)
if formula_or_cask.is_a? Formula
"Formula #{formula_or_cask.name}"
else
"Cask #{formula_or_cask.token}"
end
end
end
end
end

View File

@ -1,8 +1,11 @@
# frozen_string_literal: true
require "cmd/help"
require "cmd/shared_examples/args_parse"
RSpec.describe Homebrew::Cmd::HelpCmd, :integration_test do
it_behaves_like "parseable arguments"
describe "help" do
it "prints help for a documented Ruby command" do
expect { brew "help", "cat" }

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true
require "cmd/home"
require "cmd/shared_examples/args_parse"
RSpec.describe "brew home" do
RSpec.describe Homebrew::Cmd::Home do
let(:testballhome_homepage) do
Formula["testballhome"].homepage
end