Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move gem group setting to separate, cacheable file #15952

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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 = 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 || []

Check warning on line 31 in Library/Homebrew/dev-cmd/install-bundler-gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/install-bundler-gems.rb#L31

Added line #L31 was not covered by tests

groups ||= []
groups |= Homebrew.valid_gem_groups if groups.delete("all")
if groups.delete("all")

Check warning on line 33 in Library/Homebrew/dev-cmd/install-bundler-gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/install-bundler-gems.rb#L33

Added line #L33 was not covered by tests
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 @@
# 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 @@
ENV["BUNDLER_VERSION"] = old_bundler_version
end

def user_gem_groups
@user_gem_groups ||= if GEM_GROUPS_FILE.exist?

Check warning on line 158 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L158

Added line #L158 was not covered by tests
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

Check warning on line 166 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L164-L166

Added lines #L164 - L166 were not covered by tests
end
end

def write_user_gem_groups(groups)
GEM_GROUPS_FILE.write(groups.join("\n"))

Check warning on line 171 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L171

Added line #L171 was not covered by tests
end

def forget_user_gem_groups!
if GEM_GROUPS_FILE.exist?

Check warning on line 175 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L175

Added line #L175 was not covered by tests
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)

Check warning on line 180 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L180

Added line #L180 was not covered by tests
MikeMcQuaid marked this conversation as resolved.
Show resolved Hide resolved
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 @@
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)

Check warning on line 207 in Library/Homebrew/utils/gems.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/gems.rb#L207

Added line #L207 was not covered by tests
groups.sort!

ENV["BUNDLE_GEMFILE"] = gemfile
Expand Down Expand Up @@ -223,7 +253,7 @@
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