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 # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "cli/parser" require "abstract_command"
require "formula" require "formula"
module Homebrew 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) } conflicts "--formula", "--cask"
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" named_args [:formula, :cask]
end
named_args [:formula, :cask] sig { override.void }
end def run
end if args.no_named?
exec_browser HOMEBREW_WWW
return
end
sig { void } # to_formulae_and_casks is typed to possibly return Kegs (but won't without explicitly asking)
def home formulae_or_casks = T.cast(args.named.to_formulae_and_casks, T::Array[T.any(Formula, Cask::Cask)])
args = home_args.parse 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(*T.unsafe(homepages))
exec_browser HOMEBREW_WWW end
return
end
# to_formulae_and_casks is typed to possibly return Kegs (but won't without explicitly asking) def name_of(formula_or_cask)
formulae_or_casks = T.cast(args.named.to_formulae_and_casks, T::Array[T.any(Formula, Cask::Cask)]) if formula_or_cask.is_a? Formula
homepages = formulae_or_casks.map do |formula_or_cask| "Formula #{formula_or_cask.name}"
puts "Opening homepage for #{name_of(formula_or_cask)}" else
formula_or_cask.homepage "Cask #{formula_or_cask.token}"
end end
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}"
end end
end end
end end

View File

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

View File

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