From 1ff1033c48fd8cbc53bfb46bf3f3bc8d6f17b887 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Thu, 23 Jan 2025 16:48:27 +0000 Subject: [PATCH] cmd/bundle: add `brew bundle sh` and `brew bundle env` helper commands. These are (mostly) shorthands for `brew bundle exec sh` and `brew bundle exec env` that provide `brew`-like shell and environment features on top of `brew bundle exec`. --- cmd/bundle.rb | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 8128c4291..94f0fadcf 100755 --- a/cmd/bundle.rb +++ b/cmd/bundle.rb @@ -53,6 +53,12 @@ class BundleCmd < AbstractCommand Run an external command in an isolated build environment based on the `Brewfile` dependencies. This sanitized build environment ignores unrequested dependencies, which makes sure that things you didn't specify in your `Brewfile` won't get picked up by commands like `bundle install`, `npm install`, etc. It will also add compiler flags which will help with finding keg-only dependencies like `openssl`, `icu4c`, etc. + + `brew bundle sh`: + Run your shell in a `brew bundle exec` environment. + + `brew bundle env`: + Print the environment variables that would be set in a `brew bundle exec` environment. EOS flag "--file=", description: "Read the `Brewfile` from this location. Use `--file=-` to pipe to stdin/stdout." @@ -110,7 +116,7 @@ class BundleCmd < AbstractCommand conflicts "--all", "--no-vscode" conflicts "--vscode", "--no-vscode" - named_args %w[install dump cleanup check exec list] + named_args %w[install dump cleanup check exec list sh env] end sig { override.void } @@ -178,8 +184,27 @@ def run Bundle::Commands::Cleanup.run(global:, file:, force:, zap:) when "check" Bundle::Commands::Check.run(global:, file:, no_upgrade:, verbose:) - when "exec" - _subcommand, *named_args = args.named + when "exec", "sh", "env" + named_args = case subcommand + when "exec" + _subcommand, *named_args = args.named + named_args + when "sh" + preferred_shell = Utils::Shell.preferred_path(default: "/bin/bash") + subshell = case Utils::Shell.preferred + when :zsh + "PS1='brew bundle %B%F{green}%~%f%b$ ' #{preferred_shell} -d -f" + when :bash + "PS1=\"brew bundle \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell} --noprofile --norc" + else + "PS1=\"brew bundle \\[\\033[1;32m\\]\\w\\[\\033[0m\\]$ \" #{preferred_shell}" + end + $stdout.flush + ENV["HOMEBREW_FORCE_API_AUTO_UPDATE"] = nil + [subshell] + when "env" + ["env"] + end Bundle::Commands::Exec.run(*named_args, global:, file:) when "list" Bundle::Commands::List.run(