Skip to content

Commit

Permalink
Move gem group setting to separate, cacheable file
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo98 committed Sep 4, 2023
1 parent 27902f0 commit 272bd8e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 10 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_gemgroups
**/vendor/bundle/ruby/*/bundler.lock
**/vendor/bundle/ruby/*/bin
**/vendor/bundle/ruby/*/build_info/
Expand Down
11 changes: 7 additions & 4 deletions Library/Homebrew/dev-cmd/install-bundler-gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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"

GEMGROUPS_FILE = "#{HOMEBREW_LIBRARY_PATH}/vendor/bundle/ruby/.homebrew_gemgroups"
private_constant :GEMGROUPS_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 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)
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
2 changes: 1 addition & 1 deletion completions/fish/brew.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
2 changes: 1 addition & 1 deletion docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`]

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

0 comments on commit 272bd8e

Please sign in to comment.