Port Homebrew::DevCmd::Sh

This commit is contained in:
Douglas Eichelberger 2024-03-21 21:41:54 -07:00
parent 177bab38c7
commit 1436b06e90
4 changed files with 92 additions and 92 deletions

View File

@ -1,41 +1,40 @@
# 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
module_function module DevCmd
class Ruby < AbstractCommand
cmd_args do
usage_banner "`ruby` [<options>] (`-e` <text>|<file>)"
description <<~EOS
Run a Ruby instance with Homebrew's libraries loaded. For example,
`brew ruby -e "puts :gcc.f.deps"` or `brew ruby script.rb`.
sig { returns(CLI::Parser) } Run e.g. `brew ruby -- --version` to pass arbitrary arguments to `ruby`.
def ruby_args EOS
Homebrew::CLI::Parser.new do flag "-r=",
usage_banner "`ruby` [<options>] (`-e` <text>|<file>)" description: "Load a library using `require`."
description <<~EOS flag "-e=",
Run a Ruby instance with Homebrew's libraries loaded. For example, description: "Execute the given text string as a script."
`brew ruby -e "puts :gcc.f.deps"` or `brew ruby script.rb`.
Run e.g. `brew ruby -- --version` to pass arbitrary arguments to `ruby`. named_args :file
EOS end
flag "-r=",
description: "Load a library using `require`."
flag "-e=",
description: "Execute the given text string as a script."
named_args :file sig { override.void }
def run
ruby_sys_args = []
ruby_sys_args << "-r#{args.r}" if args.r
ruby_sys_args << "-e #{args.e}" if args.e
ruby_sys_args += args.named
exec(*HOMEBREW_RUBY_EXEC_ARGS,
"-I", $LOAD_PATH.join(File::PATH_SEPARATOR),
"-rglobal", "-rdev-cmd/irb",
*ruby_sys_args)
end
end end
end end
def ruby
args = ruby_args.parse
ruby_sys_args = []
ruby_sys_args << "-r#{args.r}" if args.r
ruby_sys_args << "-e #{args.e}" if args.e
ruby_sys_args += args.named
exec(*HOMEBREW_RUBY_EXEC_ARGS,
"-I", $LOAD_PATH.join(File::PATH_SEPARATOR),
"-rglobal", "-rdev-cmd/irb",
*ruby_sys_args)
end
end end

View File

@ -1,72 +1,71 @@
# typed: true # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "extend/ENV" require "extend/ENV"
require "formula" require "formula"
require "cli/parser" require "cli/parser"
module Homebrew module Homebrew
module_function module DevCmd
class Sh < AbstractCommand
cmd_args do
description <<~EOS
Enter an interactive shell for Homebrew's build environment. Use years-battle-hardened
build logic to help your `./configure && make && make install`
and even your `gem install` succeed. Especially handy if you run Homebrew
in an Xcode-only configuration since it adds tools like `make` to your `PATH`
which build systems would not find otherwise.
EOS
flag "--env=",
description: "Use the standard `PATH` instead of superenv's when `std` is passed."
flag "-c=", "--cmd=",
description: "Execute commands in a non-interactive shell."
sig { returns(CLI::Parser) } named_args :file, max: 1
def sh_args end
Homebrew::CLI::Parser.new do
description <<~EOS sig { override.void }
Enter an interactive shell for Homebrew's build environment. Use years-battle-hardened def run
build logic to help your `./configure && make && make install` ENV.activate_extensions!(env: args.env)
and even your `gem install` succeed. Especially handy if you run Homebrew
in an Xcode-only configuration since it adds tools like `make` to your `PATH` ENV.deps = Formula.installed.select { |f| f.keg_only? && f.opt_prefix.directory? } if superenv?(args.env)
which build systems would not find otherwise. ENV.setup_build_environment
EOS if superenv?(args.env)
flag "--env=", # superenv stopped adding brew's bin but generally users will want it
description: "Use the standard `PATH` instead of superenv's when `std` is passed." ENV["PATH"] = PATH.new(ENV.fetch("PATH")).insert(1, HOMEBREW_PREFIX/"bin").to_s
flag "-c=", "--cmd=", end
description: "Execute commands in a non-interactive shell."
ENV["VERBOSE"] = "1" if args.verbose?
named_args :file, max: 1
end preferred_shell = Utils::Shell.preferred_path(default: "/bin/bash")
end
if args.cmd.present?
def sh safe_system(preferred_shell, "-c", args.cmd)
args = sh_args.parse elsif args.named.present?
safe_system(preferred_shell, args.named.first)
ENV.activate_extensions!(env: args.env) else
shell_type = Utils::Shell.preferred
ENV.deps = Formula.installed.select { |f| f.keg_only? && f.opt_prefix.directory? } if superenv?(args.env) subshell = case shell_type
ENV.setup_build_environment when :zsh
if superenv?(args.env) "PS1='brew %B%F{green}%~%f%b$ ' #{preferred_shell} -d -f"
# superenv stopped adding brew's bin but generally users will want it when :bash
ENV["PATH"] = PATH.new(ENV.fetch("PATH")).insert(1, HOMEBREW_PREFIX/"bin").to_s "PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell} --noprofile --norc"
end else
"PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell}"
ENV["VERBOSE"] = "1" if args.verbose? end
puts <<~EOS
preferred_shell = Utils::Shell.preferred_path(default: "/bin/bash") Your shell has been configured to use Homebrew's build environment;
this should help you build stuff. Notably though, the system versions of
if args.cmd.present? gem and pip will ignore our configuration and insist on using the
safe_system(preferred_shell, "-c", args.cmd) environment they were built under (mostly). Sadly, scons will also
elsif args.named.present? ignore our configuration.
safe_system(preferred_shell, args.named.first) When done, type `exit`.
else EOS
shell_type = Utils::Shell.preferred $stdout.flush
subshell = case shell_type safe_system subshell
when :zsh end
"PS1='brew %B%F{green}%~%f%b$ ' #{preferred_shell} -d -f"
when :bash
"PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell} --noprofile --norc"
else
"PS1=\"brew \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell}"
end end
puts <<~EOS
Your shell has been configured to use Homebrew's build environment;
this should help you build stuff. Notably though, the system versions of
gem and pip will ignore our configuration and insist on using the
environment they were built under (mostly). Sadly, scons will also
ignore our configuration.
When done, type `exit`.
EOS
$stdout.flush
safe_system subshell
end end
end end
end end

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "dev-cmd/ruby"
RSpec.describe "brew ruby" do RSpec.describe Homebrew::DevCmd::Ruby do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "executes ruby code with Homebrew's libraries loaded", :integration_test do it "executes ruby code with Homebrew's libraries loaded", :integration_test do

View File

@ -1,8 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
require "dev-cmd/sh"
RSpec.describe "brew sh" do RSpec.describe Homebrew::DevCmd::Sh do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "runs a shell with the Homebrew environment", :integration_test do it "runs a shell with the Homebrew environment", :integration_test do