diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 6b0dac156..2c14207cd 100755 --- a/cmd/bundle.rb +++ b/cmd/bundle.rb @@ -69,8 +69,9 @@ class BundleCmd < AbstractCommand flag "--file=", description: "Read the `Brewfile` from this location. Use `--file=-` to pipe to stdin/stdout." switch "--global", - description: "Read the `Brewfile` from `~/.Brewfile` or " \ - "the `HOMEBREW_BUNDLE_FILE_GLOBAL` environment variable, if set." + description: "Read the `Brewfile` from `~/.Brewfile`, " \ + "in the `HOMEBREW_USER_CONFIG_HOME` directory, " \ + "or the `HOMEBREW_BUNDLE_FILE_GLOBAL` environment variable, if set." switch "-v", "--verbose", description: "`install` prints output from commands as they are run. " \ "`check` lists all missing dependencies." diff --git a/lib/bundle/brewfile.rb b/lib/bundle/brewfile.rb index 913a92771..b57ebf6de 100644 --- a/lib/bundle/brewfile.rb +++ b/lib/bundle/brewfile.rb @@ -7,6 +7,7 @@ module Brewfile def path(dash_writes_to_stdout: false, global: false, file: nil) env_bundle_file_global = ENV.fetch("HOMEBREW_BUNDLE_FILE_GLOBAL", nil) env_bundle_file = ENV.fetch("HOMEBREW_BUNDLE_FILE", nil) + user_config_home = ENV.fetch("HOMEBREW_USER_CONFIG_HOME", nil) filename = if global if env_bundle_file_global.present? @@ -14,8 +15,12 @@ def path(dash_writes_to_stdout: false, global: false, file: nil) else raise "'HOMEBREW_BUNDLE_FILE' cannot be specified with '--global'" if env_bundle_file.present? - Bundle.exchange_uid_if_needed! do - "#{Dir.home}/.Brewfile" + if user_config_home && File.exist?("#{user_config_home}/Brewfile") + "#{user_config_home}/Brewfile" + else + Bundle.exchange_uid_if_needed! do + "#{Dir.home}/.Brewfile" + end end end elsif file.present? diff --git a/spec/bundle/brewfile_spec.rb b/spec/bundle/brewfile_spec.rb index 824b2efc9..bb90f9804 100644 --- a/spec/bundle/brewfile_spec.rb +++ b/spec/bundle/brewfile_spec.rb @@ -11,8 +11,10 @@ let(:dash_writes_to_stdout) { false } let(:env_bundle_file_global_value) { nil } let(:env_bundle_file_value) { nil } + let(:env_user_config_home_value) { "/Users/username/.homebrew" } let(:file_value) { nil } let(:has_global) { false } + let(:config_dir_brewfile_exist) { false } before do allow(ENV).to receive(:fetch).and_return(nil) @@ -20,6 +22,11 @@ .and_return(env_bundle_file_global_value) allow(ENV).to receive(:fetch).with("HOMEBREW_BUNDLE_FILE", any_args) .and_return(env_bundle_file_value) + + allow(ENV).to receive(:fetch).with("HOMEBREW_USER_CONFIG_HOME", any_args) + .and_return(env_user_config_home_value) + allow(File).to receive(:exist?).with("/Users/username/.homebrew/Brewfile") + .and_return(config_dir_brewfile_exist) end context "when `file` is specified with a relative path" do @@ -154,6 +161,15 @@ expect(path).to eq(expected_pathname) end end + + context "when HOMEBREW_USER_CONFIG_HOME/Brewfile exists" do + let(:config_dir_brewfile_exist) { true } + let(:expected_pathname) { Pathname.new("#{env_user_config_home_value}/Brewfile") } + + it "returns the expected path" do + expect(path).to eq(expected_pathname) + end + end end context "when HOMEBREW_BUNDLE_FILE has a value" do