diff --git a/Library/Homebrew/cmd/--env.rb b/Library/Homebrew/cmd/--env.rb index 84a4e46f7e..90f505942d 100644 --- a/Library/Homebrew/cmd/--env.rb +++ b/Library/Homebrew/cmd/--env.rb @@ -1,56 +1,56 @@ # typed: strict # frozen_string_literal: true +require "abstract_command" require "extend/ENV" require "build_environment" require "utils/shell" -require "cli/parser" module Homebrew - module_function + module Cmd + class Env < AbstractCommand + sig { override.returns(String) } + def self.command_name = "--env" - sig { returns(CLI::Parser) } - def __env_args - Homebrew::CLI::Parser.new do - description <<~EOS - Summarise Homebrew's build environment as a plain list. + cmd_args do + description <<~EOS + Summarise Homebrew's build environment as a plain list. - If the command's output is sent through a pipe and no shell is specified, - the list is formatted for export to `bash`(1) unless `--plain` is passed. - EOS - flag "--shell=", - description: "Generate a list of environment variables for the specified shell, " \ - "or `--shell=auto` to detect the current shell." - switch "--plain", - description: "Generate plain output even when piped." + If the command's output is sent through a pipe and no shell is specified, + the list is formatted for export to `bash`(1) unless `--plain` is passed. + EOS + flag "--shell=", + description: "Generate a list of environment variables for the specified shell, " \ + "or `--shell=auto` to detect the current shell." + switch "--plain", + description: "Generate plain output even when piped." - named_args :formula - end - end + named_args :formula + end - sig { void } - def __env - args = __env_args.parse + sig { override.void } + def run + ENV.activate_extensions! + ENV.deps = args.named.to_formulae if superenv?(nil) + ENV.setup_build_environment - ENV.activate_extensions! - ENV.deps = args.named.to_formulae if superenv?(nil) - ENV.setup_build_environment + shell = if args.plain? + nil + elsif args.shell.nil? + :bash unless $stdout.tty? + elsif args.shell == "auto" + Utils::Shell.parent || Utils::Shell.preferred + elsif args.shell + Utils::Shell.from_path(T.must(args.shell)) + end - shell = if args.plain? - nil - elsif args.shell.nil? - :bash unless $stdout.tty? - elsif args.shell == "auto" - Utils::Shell.parent || Utils::Shell.preferred - elsif args.shell - Utils::Shell.from_path(args.shell) - end - - if shell.nil? - BuildEnvironment.dump ENV.to_h - else - BuildEnvironment.keys(ENV.to_h).each do |key| - puts Utils::Shell.export_value(key, ENV.fetch(key), shell) + if shell.nil? + BuildEnvironment.dump ENV.to_h + else + BuildEnvironment.keys(ENV.to_h).each do |key| + puts Utils::Shell.export_value(key, ENV.fetch(key), shell) + end + end end end end diff --git a/Library/Homebrew/test/cmd/--env_spec.rb b/Library/Homebrew/test/cmd/--env_spec.rb index dcddb15fd2..9df3625a70 100644 --- a/Library/Homebrew/test/cmd/--env_spec.rb +++ b/Library/Homebrew/test/cmd/--env_spec.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true +require "cmd/--env" require "cmd/shared_examples/args_parse" -RSpec.describe "brew --env" do +RSpec.describe Homebrew::Cmd::Env do it_behaves_like "parseable arguments" describe "--shell=bash", :integration_test do