Port Homebrew::Cmd::Leaves
This commit is contained in:
parent
0fd082a1ff
commit
8ab9d2cbad
@ -9,7 +9,7 @@ module Homebrew
|
|||||||
# - Each Command lives in an isolated namespace.
|
# - Each Command lives in an isolated namespace.
|
||||||
# - Each Command implements a defined interface.
|
# - Each Command implements a defined interface.
|
||||||
# - `args` is available as an ivar, and thus does not need to be passed as an argument to helper methods.
|
# - `args` is available as an ivar, and thus does not need to be passed as an argument to helper methods.
|
||||||
# - Subclasses no longer need to reference CLI::Parser directly.
|
# - Subclasses no longer need to reference `CLI::Parser` or parse args explicitly.
|
||||||
#
|
#
|
||||||
# To subclass, implement a `run` method and provide a `cmd_args` block to document the command and its allowed args.
|
# To subclass, implement a `run` method and provide a `cmd_args` block to document the command and its allowed args.
|
||||||
# To generate method signatures for command args, run `brew typecheck --update`.
|
# To generate method signatures for command args, run `brew typecheck --update`.
|
||||||
|
|||||||
@ -1,51 +1,49 @@
|
|||||||
# typed: true
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "abstract_command"
|
||||||
require "formula"
|
require "formula"
|
||||||
require "cask_dependent"
|
require "cask_dependent"
|
||||||
require "cli/parser"
|
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module Cmd
|
||||||
|
class Leaves < AbstractCommand
|
||||||
|
cmd_args do
|
||||||
|
description <<~EOS
|
||||||
|
List installed formulae that are not dependencies of another installed formula or cask.
|
||||||
|
EOS
|
||||||
|
switch "-r", "--installed-on-request",
|
||||||
|
description: "Only list leaves that were manually installed."
|
||||||
|
switch "-p", "--installed-as-dependency",
|
||||||
|
description: "Only list leaves that were installed as dependencies."
|
||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
conflicts "--installed-on-request", "--installed-as-dependency"
|
||||||
def leaves_args
|
|
||||||
Homebrew::CLI::Parser.new do
|
|
||||||
description <<~EOS
|
|
||||||
List installed formulae that are not dependencies of another installed formula or cask.
|
|
||||||
EOS
|
|
||||||
switch "-r", "--installed-on-request",
|
|
||||||
description: "Only list leaves that were manually installed."
|
|
||||||
switch "-p", "--installed-as-dependency",
|
|
||||||
description: "Only list leaves that were installed as dependencies."
|
|
||||||
|
|
||||||
conflicts "--installed-on-request", "--installed-as-dependency"
|
named_args :none
|
||||||
|
end
|
||||||
|
|
||||||
named_args :none
|
sig { override.void }
|
||||||
|
def run
|
||||||
|
leaves_list = Formula.installed - Formula.installed.flat_map(&:runtime_formula_dependencies)
|
||||||
|
casks_runtime_dependencies = Cask::Caskroom.casks.flat_map do |cask|
|
||||||
|
CaskDependent.new(cask).runtime_dependencies.map(&:to_formula)
|
||||||
|
end
|
||||||
|
leaves_list -= casks_runtime_dependencies
|
||||||
|
leaves_list.select!(&method(:installed_on_request?)) if args.installed_on_request?
|
||||||
|
leaves_list.select!(&method(:installed_as_dependency?)) if args.installed_as_dependency?
|
||||||
|
|
||||||
|
leaves_list.map(&:full_name)
|
||||||
|
.sort
|
||||||
|
.each(&method(:puts))
|
||||||
|
end
|
||||||
|
|
||||||
|
def installed_on_request?(formula)
|
||||||
|
Tab.for_keg(formula.any_installed_keg).installed_on_request
|
||||||
|
end
|
||||||
|
|
||||||
|
def installed_as_dependency?(formula)
|
||||||
|
Tab.for_keg(formula.any_installed_keg).installed_as_dependency
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def installed_on_request?(formula)
|
|
||||||
Tab.for_keg(formula.any_installed_keg).installed_on_request
|
|
||||||
end
|
|
||||||
|
|
||||||
def installed_as_dependency?(formula)
|
|
||||||
Tab.for_keg(formula.any_installed_keg).installed_as_dependency
|
|
||||||
end
|
|
||||||
|
|
||||||
def leaves
|
|
||||||
args = leaves_args.parse
|
|
||||||
|
|
||||||
leaves_list = Formula.installed - Formula.installed.flat_map(&:runtime_formula_dependencies)
|
|
||||||
casks_runtime_dependencies = Cask::Caskroom.casks.flat_map do |cask|
|
|
||||||
CaskDependent.new(cask).runtime_dependencies.map(&:to_formula)
|
|
||||||
end
|
|
||||||
leaves_list -= casks_runtime_dependencies
|
|
||||||
leaves_list.select!(&method(:installed_on_request?)) if args.installed_on_request?
|
|
||||||
leaves_list.select!(&method(:installed_as_dependency?)) if args.installed_as_dependency?
|
|
||||||
|
|
||||||
leaves_list.map(&:full_name)
|
|
||||||
.sort
|
|
||||||
.each(&method(:puts))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "cmd/leaves"
|
||||||
require "cmd/shared_examples/args_parse"
|
require "cmd/shared_examples/args_parse"
|
||||||
|
|
||||||
RSpec.describe "brew leaves" do
|
RSpec.describe Homebrew::Cmd::Leaves do
|
||||||
it_behaves_like "parseable arguments"
|
it_behaves_like "parseable arguments"
|
||||||
|
|
||||||
context "when there are no installed Formulae", :integration_test do
|
context "when there are no installed Formulae", :integration_test do
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user