Skip to content

Commit

Permalink
Merge pull request #15952 from Bo98/gemgroup-file
Browse files Browse the repository at this point in the history
Move gem group setting to separate, cacheable file
  • Loading branch information
MikeMcQuaid authored Sep 13, 2023
2 parents 24de357 + 9cf0d34 commit b415a0d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
20 changes: 13 additions & 7 deletions Library/Homebrew/dev-cmd/install-bundler-gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ 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). " \
"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
Expand All @@ -22,13 +28,13 @@ def install_bundler_gems_args
def install_bundler_gems
args = install_bundler_gems_args.parse

groups = args.groups

# Clear previous settings. We want to fully replace - not append.
Homebrew::Settings.delete(:gemgroups) if groups
groups = args.groups || args.add_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
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.write(setting, value, repo: HOMEBREW_REPOSITORY)
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
Expand Down
34 changes: 32 additions & 2 deletions Library/Homebrew/utils/gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -151,6 +154,33 @@ def install_bundler!
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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions completions/bash/brew
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ _brew_install_bundler_gems() {
case "${cur}" in
-*)
__brewcomp "
--add-groups
--debug
--groups
--help
Expand Down
3 changes: 2 additions & 1 deletion completions/fish/brew.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
6 changes: 4 additions & 2 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`]

Expand Down
8 changes: 6 additions & 2 deletions manpages/brew.1
Original file line number Diff line number Diff line change
Expand Up @@ -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\.
Expand Down

0 comments on commit b415a0d

Please sign in to comment.