Skip to content

Commit 40d385d

Browse files
committed
bundle/commands/exec: check that Brewfile is installed with --check
`brew bundle exec` behaves correctly only after doing `brew bundle install`. Running `brew bundle check` can be slow, so let's add a `--check` flag to `brew bundle exec` which will also run `brew bundle check` before `brew bundle exec` to ensure that the `Brewfile` has been installed before proceeding.
1 parent c8d8c09 commit 40d385d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Library/Homebrew/bundle/commands/check.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Check
1010
ARROW = "→"
1111
FAILURE_MESSAGE = "brew bundle can't satisfy your Brewfile's dependencies."
1212

13-
def self.run(global: false, file: nil, no_upgrade: false, verbose: false)
13+
def self.run(global: false, file: nil, no_upgrade: false, verbose: false, quiet: false)
1414
output_errors = verbose
1515
exit_on_first_error = !verbose
1616
check_result = Homebrew::Bundle::Checker.check(
@@ -24,9 +24,9 @@ def self.run(global: false, file: nil, no_upgrade: false, verbose: false)
2424
check_result.errors.each { |package| puts "#{ARROW} #{package}" } if output_errors
2525
puts "Satisfy missing dependencies with `brew bundle install`."
2626
exit 1
27-
else
28-
puts "The Brewfile's dependencies are satisfied."
2927
end
28+
29+
puts "The Brewfile's dependencies are satisfied." if !quiet
3030
end
3131
end
3232
end

Library/Homebrew/bundle/commands/exec.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ module Exec
4747

4848
PATH_LIKE_ENV_REGEX = /.+#{File::PATH_SEPARATOR}/
4949

50-
def self.run(*args, global: false, file: nil, subcommand: "")
50+
def self.run(*args, global: false, file: nil, check: false, subcommand: "")
51+
if check
52+
require "bundle/commands/check"
53+
Homebrew::Bundle::Commands::Check.run(global:, file:, quiet: true)
54+
end
55+
5156
# Cleanup Homebrew's global environment
5257
HOMEBREW_ENV_CLEANUP.each { |key| ENV.delete(key) }
5358

Library/Homebrew/cmd/bundle.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ class Bundle < AbstractCommand
5151
`brew bundle remove` <name> [...]:
5252
Remove entries that match `name` from your `Brewfile`. Use `--formula`, `--cask`, `--tap`, `--mas`, `--whalebrew` or `--vscode` to remove only entries of the corresponding type. Passing `--formula` also removes matches against formula aliases and old formula names.
5353
54-
`brew bundle exec` <command>:
54+
`brew bundle exec` [--check] <command>:
5555
Run an external command in an isolated build environment based on the `Brewfile` dependencies.
5656
5757
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.
5858
59-
`brew bundle sh`:
59+
`brew bundle sh` [--check]:
6060
Run your shell in a `brew bundle exec` environment.
6161
62-
`brew bundle env`:
62+
`brew bundle env` [--check]:
6363
Print the environment variables that would be set in a `brew bundle exec` environment.
6464
EOS
6565
flag "--file=",
@@ -119,6 +119,9 @@ class Bundle < AbstractCommand
119119
description: "`dump` does not add `restart_service` to formula lines."
120120
switch "--zap",
121121
description: "`cleanup` casks using the `zap` command instead of `uninstall`."
122+
switch "--check",
123+
description: "Check that all dependencies in the Brewfile are installed before " \
124+
"running `exec`, `sh`, or `env`."
122125

123126
conflicts "--all", "--no-vscode"
124127
conflicts "--vscode", "--no-vscode"
@@ -127,6 +130,8 @@ class Bundle < AbstractCommand
127130
named_args %w[install dump cleanup check exec list sh env edit]
128131
end
129132

133+
BUNDLE_EXEC_COMMANDS = %w[exec sh env].freeze
134+
130135
sig { override.void }
131136
def run
132137
# Keep this inside `run` to keep --help fast.
@@ -137,6 +142,10 @@ def run
137142
raise UsageError, "This command does not take more than 1 subcommand argument."
138143
end
139144

145+
if args.check? && BUNDLE_EXEC_COMMANDS.exclude?(subcommand)
146+
raise UsageError, "`--check` can be used only with #{BUNDLE_EXEC_COMMANDS.join(", ")}."
147+
end
148+
140149
global = args.global?
141150
file = args.file
142151
args.zap?
@@ -211,7 +220,7 @@ def run
211220
when "check"
212221
require "bundle/commands/check"
213222
Homebrew::Bundle::Commands::Check.run(global:, file:, no_upgrade:, verbose:)
214-
when "exec", "sh", "env"
223+
when *BUNDLE_EXEC_COMMANDS
215224
named_args = case subcommand
216225
when "exec"
217226
_subcommand, *named_args = args.named

0 commit comments

Comments
 (0)