diff --git a/.gitignore b/.gitignore index 80675e31e1a07c..38df7f49048993 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ # Ignore Bundler files **/.bundle/bin **/.bundle/cache +**/vendor/bundle/ruby/.homebrew_gemgroups **/vendor/bundle/ruby/*/bundler.lock **/vendor/bundle/ruby/*/bin **/vendor/bundle/ruby/*/build_info/ diff --git a/Library/Homebrew/dev-cmd/install-bundler-gems.rb b/Library/Homebrew/dev-cmd/install-bundler-gems.rb index 45cf04e19cec2d..12fdddca1bc37b 100644 --- a/Library/Homebrew/dev-cmd/install-bundler-gems.rb +++ b/Library/Homebrew/dev-cmd/install-bundler-gems.rb @@ -13,7 +13,8 @@ def install_bundler_gems_args Install Homebrew's Bundler gems. EOS comma_array "--groups", - description: "Installs the specified comma-separated list of gem groups (default: last used)." + description: "Installs the specified comma-separated list of gem groups (default: last used). " \ + "Prepend with `+` to install these groups in addition to what is already installed." named_args :none end @@ -23,12 +24,14 @@ def install_bundler_gems args = install_bundler_gems_args.parse groups = args.groups + append_groups = groups&.first&.delete_prefix!("+") + all_groups = groups&.delete("all") - # Clear previous settings. We want to fully replace - not append. - Homebrew::Settings.delete(:gemgroups) if groups + # Clear previous settings. We want to fully replace - not append, unless `+` is prepended + Homebrew.forget_user_gem_groups! if groups && !all_groups && !append_groups groups ||= [] - groups |= Homebrew.valid_gem_groups if groups.delete("all") + groups |= Homebrew.valid_gem_groups if all_groups Homebrew.install_bundler_gems!(groups: groups) end diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index 75be425cbdb5b6..8d498a383c5074 100644 --- a/Library/Homebrew/utils/gems.rb +++ b/Library/Homebrew/utils/gems.rb @@ -12,6 +12,9 @@ module Homebrew # After updating this, run `brew vendor-gems --update=--bundler`. HOMEBREW_BUNDLER_VERSION = "2.4.18" + GEMGROUPS_FILE = "#{HOMEBREW_LIBRARY_PATH}/vendor/bundle/ruby/.homebrew_gemgroups" + private_constant :GEMGROUPS_FILE + module_function # @api private @@ -151,6 +154,33 @@ def install_bundler! ENV["BUNDLER_VERSION"] = old_bundler_version end + def user_gem_groups + @user_gem_groups ||= if File.exist?(GEMGROUPS_FILE) + File.readlines(GEMGROUPS_FILE, chomp: true) + else + # Backwards compatibility + require "settings" + groups = Homebrew::Settings.read(:gemgroups)&.split(";") || [] + write_user_gem_groups(groups) + Homebrew::Settings.delete(:gemgroups) + groups + end + end + + def write_user_gem_groups(groups) + File.write(GEMGROUPS_FILE, groups.join("\n")) + end + + def forget_user_gem_groups! + if File.exist?(GEMGROUPS_FILE) + File.truncate(GEMGROUPS_FILE, 0) + else + # Backwards compatibility + require "settings" + Homebrew::Settings.delete(:gemgroups) + end + end + def install_bundler_gems!(only_warn_on_failure: false, setup_path: true, groups: []) old_path = ENV.fetch("PATH", nil) old_gem_path = ENV.fetch("GEM_PATH", nil) @@ -174,7 +204,7 @@ def install_bundler_gems!(only_warn_on_failure: false, setup_path: true, groups: require "settings" # Combine the passed groups with the ones stored in settings - groups |= (Homebrew::Settings.read(:gemgroups)&.split(";") || []) + groups |= (user_gem_groups & valid_gem_groups) groups.sort! ENV["BUNDLE_GEMFILE"] = gemfile @@ -223,7 +253,7 @@ def install_bundler_gems!(only_warn_on_failure: false, setup_path: true, groups: end if bundle_installed - Homebrew::Settings.write(:gemgroups, groups.join(";")) + write_user_gem_groups(groups) @bundle_installed_groups = groups end end diff --git a/completions/fish/brew.fish b/completions/fish/brew.fish index 9cc48bb54c9440..73f4140bd1dd42 100644 --- a/completions/fish/brew.fish +++ b/completions/fish/brew.fish @@ -921,7 +921,7 @@ __fish_brew_complete_arg 'install; and not __fish_seen_argument -l formula -l fo __fish_brew_complete_cmd 'install-bundler-gems' 'Install Homebrew\'s Bundler gems' __fish_brew_complete_arg 'install-bundler-gems' -l debug -d 'Display any debugging information' -__fish_brew_complete_arg 'install-bundler-gems' -l groups -d 'Installs the specified comma-separated list of gem groups (default: last used)' +__fish_brew_complete_arg 'install-bundler-gems' -l groups -d 'Installs the specified comma-separated list of gem groups (default: last used). Prepend with `+` to install these groups in addition to what is already installed' __fish_brew_complete_arg 'install-bundler-gems' -l help -d 'Show this message' __fish_brew_complete_arg 'install-bundler-gems' -l quiet -d 'Make some output more quiet' __fish_brew_complete_arg 'install-bundler-gems' -l verbose -d 'Make some output more verbose' diff --git a/completions/zsh/_brew b/completions/zsh/_brew index c93c27f7031ab9..afe5941e061f31 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -1141,7 +1141,7 @@ _brew_install() { _brew_install_bundler_gems() { _arguments \ '--debug[Display any debugging information]' \ - '--groups[Installs the specified comma-separated list of gem groups (default: last used)]' \ + '--groups[Installs the specified comma-separated list of gem groups (default: last used). Prepend with `+` to install these groups in addition to what is already installed]' \ '--help[Show this message]' \ '--quiet[Make some output more quiet]' \ '--verbose[Make some output more verbose]' diff --git a/docs/Manpage.md b/docs/Manpage.md index c43429d78656cb..f1b0ba5dced213 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -1307,7 +1307,7 @@ Generate Homebrew's manpages and shell completions. Install Homebrew's Bundler gems. * `--groups`: - Installs the specified comma-separated list of gem groups (default: last used). + Installs the specified comma-separated list of gem groups (default: last used). Prepend with `+` to install these groups in addition to what is already installed. ### `irb` [`--examples`] [`--pry`] diff --git a/manpages/brew.1 b/manpages/brew.1 index d75c3baa60a2ab..279cf9141065eb 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -1879,7 +1879,7 @@ Install Homebrew\'s Bundler gems\. . .TP \fB\-\-groups\fR -Installs the specified comma\-separated list of gem groups (default: last used)\. +Installs the specified comma\-separated list of gem groups (default: last used)\. Prepend with \fB+\fR to install these groups in addition to what is already installed\. . .SS "\fBirb\fR [\fB\-\-examples\fR] [\fB\-\-pry\fR]" Enter the interactive Homebrew Ruby shell\.