diff --git a/.gitignore b/.gitignore index 80675e31e1..f81d112749 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ # Ignore Bundler files **/.bundle/bin **/.bundle/cache +**/vendor/bundle/ruby/.homebrew_gem_groups **/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 45cf04e19c..78165720c1 100644 --- a/Library/Homebrew/dev-cmd/install-bundler-gems.rb +++ b/Library/Homebrew/dev-cmd/install-bundler-gems.rb @@ -13,7 +13,13 @@ module Homebrew 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). " \ + "Replaces any previously installed groups." + comma_array "--add-groups", + description: "Installs the specified comma-separated list of gem groups, " \ + "in addition to those already installed." + + conflicts "--groups", "--add-groups" named_args :none end @@ -22,13 +28,13 @@ module Homebrew def install_bundler_gems args = install_bundler_gems_args.parse - groups = args.groups + groups = args.groups || args.add_groups || [] - # Clear previous settings. We want to fully replace - not append. - Homebrew::Settings.delete(:gemgroups) if groups - - groups ||= [] - groups |= Homebrew.valid_gem_groups if groups.delete("all") + if groups.delete("all") + groups |= Homebrew.valid_gem_groups + elsif args.groups # if we have been asked to replace + Homebrew.forget_user_gem_groups! + end Homebrew.install_bundler_gems!(groups: groups) end diff --git a/Library/Homebrew/settings.rb b/Library/Homebrew/settings.rb index 4e5914899d..61491773d6 100644 --- a/Library/Homebrew/settings.rb +++ b/Library/Homebrew/settings.rb @@ -31,7 +31,7 @@ module Homebrew def self.delete(setting, repo: HOMEBREW_REPOSITORY) return unless (repo/".git/config").exist? - return if read(setting, repo: repo).blank? + return if read(setting, repo: repo).nil? Kernel.system("git", "-C", repo.to_s, "config", "--unset-all", "homebrew.#{setting}", exception: true) end diff --git a/Library/Homebrew/utils/gems.rb b/Library/Homebrew/utils/gems.rb index 75be425cbd..fe415ba8a4 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" + GEM_GROUPS_FILE = (HOMEBREW_LIBRARY_PATH/"vendor/bundle/ruby/.homebrew_gem_groups").freeze + private_constant :GEM_GROUPS_FILE + module_function # @api private @@ -151,6 +154,33 @@ module Homebrew ENV["BUNDLER_VERSION"] = old_bundler_version end + def user_gem_groups + @user_gem_groups ||= if GEM_GROUPS_FILE.exist? + GEM_GROUPS_FILE.readlines(chomp: true) + else + # Backwards compatibility. This else block can be replaced by `[]` by the end of 2023. + 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) + GEM_GROUPS_FILE.write(groups.join("\n")) + end + + def forget_user_gem_groups! + if GEM_GROUPS_FILE.exist? + GEM_GROUPS_FILE.truncate(0) + else + # Backwards compatibility. This else block can be removed by the end of 2023. + 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 @@ module Homebrew 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 @@ module Homebrew 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/bash/brew b/completions/bash/brew index f7eea0fa45..97364b8c04 100644 --- a/completions/bash/brew +++ b/completions/bash/brew @@ -1287,6 +1287,7 @@ _brew_install_bundler_gems() { case "${cur}" in -*) __brewcomp " + --add-groups --debug --groups --help diff --git a/completions/fish/brew.fish b/completions/fish/brew.fish index 3e6244d54c..25851c55d4 100644 --- a/completions/fish/brew.fish +++ b/completions/fish/brew.fish @@ -919,8 +919,9 @@ __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 add-groups -d 'Installs the specified comma-separated list of gem groups, in addition to those already installed' __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). Replaces any previously installed groups' __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 e4dde1dec0..658343b91a 100644 --- a/completions/zsh/_brew +++ b/completions/zsh/_brew @@ -1139,8 +1139,9 @@ _brew_install() { # brew install-bundler-gems _brew_install_bundler_gems() { _arguments \ + '(--groups)--add-groups[Installs the specified comma-separated list of gem groups, in addition to those already installed]' \ '--debug[Display any debugging information]' \ - '--groups[Installs the specified comma-separated list of gem groups (default: last used)]' \ + '(--add-groups)--groups[Installs the specified comma-separated list of gem groups (default: last used). Replaces any previously installed groups]' \ '--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 27de70ebd5..4a334660da 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -1300,12 +1300,14 @@ The generated files are written to the current directory. Generate Homebrew's manpages and shell completions. -### `install-bundler-gems` [`--groups=`] +### `install-bundler-gems` [`--groups=`] [`--add-groups=`] 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). Replaces any previously installed groups. +* `--add-groups`: + Installs the specified comma-separated list of gem groups, in addition to those already installed. ### `irb` [`--examples`] [`--pry`] diff --git a/manpages/brew.1 b/manpages/brew.1 index cf9d4549a4..92e3ff6af3 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -1858,12 +1858,16 @@ Generate API data without writing it to files\. .SS "\fBgenerate\-man\-completions\fR" Generate Homebrew\'s manpages and shell completions\. . -.SS "\fBinstall\-bundler\-gems\fR [\fB\-\-groups=\fR]" +.SS "\fBinstall\-bundler\-gems\fR [\fB\-\-groups=\fR] [\fB\-\-add\-groups=\fR]" 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)\. Replaces any previously installed groups\. +. +.TP +\fB\-\-add\-groups\fR +Installs the specified comma\-separated list of gem groups, in addition to those already installed\. . .SS "\fBirb\fR [\fB\-\-examples\fR] [\fB\-\-pry\fR]" Enter the interactive Homebrew Ruby shell\.