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

Library/Homebrew: move stdin ruby scripts to files under utils. #17204

Merged
merged 1 commit into from
May 2, 2024
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
3 changes: 0 additions & 3 deletions Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,6 @@ then
unset HOMEBREW_AUTO_UPDATE_CASK_TAP
fi

# Disable Ruby options we don't need.
export HOMEBREW_RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt"

if [[ -z "${HOMEBREW_RUBY_WARNINGS}" ]]
then
export HOMEBREW_RUBY_WARNINGS="-W1"
Expand Down
12 changes: 2 additions & 10 deletions Library/Homebrew/cmd/setup-ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#: command.
#:

# HOMEBREW_LIBRARY is from the user environment.
# HOMEBREW_RUBY_PATH is set by utils/ruby.sh
# RUBY_DISABLE_OPTIONS is set by brew.sh
# HOMEBREW_LIBRARY is set by brew.sh
# HOMEBREW_BREW_FILE is set by extend/ENV/super.rb
# shellcheck disable=SC2154
homebrew-setup-ruby() {
Expand Down Expand Up @@ -37,13 +35,7 @@ homebrew-setup-ruby() {
fi
fi

GEM_VERSION="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_RUBY_DISABLE_OPTIONS}" /dev/stdin <<<'require "rbconfig"; puts RbConfig::CONFIG["ruby_version"]')"
echo "${GEM_VERSION}"
GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}"
BUNDLE_GEMFILE="${HOMEBREW_LIBRARY}/Homebrew/Gemfile"

export GEM_HOME
export BUNDLE_GEMFILE
setup-gem-home-bundle-gemfile

if ! bundle check &>/dev/null
then
Expand Down
10 changes: 2 additions & 8 deletions Library/Homebrew/dev-cmd/rubocop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@

# HOMEBREW_LIBRARY is from the user environment.
# HOMEBREW_RUBY_PATH is set by utils/ruby.sh
# RUBY_DISABLE_OPTIONS is set by brew.sh
# HOMEBREW_BREW_FILE is set by extend/ENV/super.rb
# shellcheck disable=SC2154
homebrew-rubocop() {
source "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby.sh"
setup-ruby-path
setup-gem-home-bundle-gemfile

GEM_VERSION="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_RUBY_DISABLE_OPTIONS}" /dev/stdin <<<'require "rbconfig"; puts RbConfig::CONFIG["ruby_version"]')"
GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}"
BUNDLE_GEMFILE="${HOMEBREW_LIBRARY}/Homebrew/Gemfile"
BUNDLE_WITH="style"

export GEM_HOME
export BUNDLE_GEMFILE
export BUNDLE_WITH

if ! bundle check &>/dev/null
then
"${HOMEBREW_BREW_FILE}" install-bundler-gems --add-groups=style
"${HOMEBREW_BREW_FILE}" install-bundler-gems --add-groups="${BUNDLE_WITH}"
fi

export PATH="${GEM_HOME}/bin:${PATH}"
Expand Down
22 changes: 11 additions & 11 deletions Library/Homebrew/utils/lock.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# create a lock using `flock(2)`. A name is required as first argument.
# the lock will be automatically unlocked when the shell process quits.
# Noted due to the fixed FD, a shell process can only create one lock.
# HOMEBREW_LIBRARY is by brew.sh
# HOMEBREW_PREFIX is set by extend/ENV/super.rb
# shellcheck disable=SC2154
lock() {
Expand All @@ -16,7 +17,7 @@ Fix permissions by running:
sudo chown -R ${USER-\$(whoami)} ${HOMEBREW_PREFIX}/var/homebrew
EOS
fi
# 200 is the file descriptor used in the lock.
# 200 is the file descriptor (FD) used in the lock.
# This FD should be used exclusively for lock purpose.
# Any value except 0(stdin), 1(stdout) and 2(stderr) can do the job.
# Noted, FD is unique per process but it will be shared to subprocess.
Expand All @@ -37,26 +38,25 @@ EOS
}

_create_lock() {
local lock_fd="$1"
local lock_file_descriptor="$1"
local name="$2"
local ruby="/usr/bin/ruby"
local python="/usr/bin/python"
[[ -x "${ruby}" ]] || ruby="$(type -P ruby)"
[[ -x "${python}" ]] || python="$(type -P python)"

# Use /dev/stdin, otherwise Ruby can error if uid != euid.
# Can't use "-" as that's also blocked:
# https://github.com/ruby/ruby/blob/e51435177e88fc845528dff7cf2bc2b75dd36144/ruby.c#L2333-L2335
if [[ -x "${ruby}" ]] && "${ruby}" /dev/stdin <<<"exit(RUBY_VERSION >= '1.8.7')"
local utils_lock_sh="${HOMEBREW_LIBRARY}/Homebrew/utils/lock_sh"
local oldest_ruby_with_flock="1.8.7"
if [[ -x "${ruby}" ]] && "${ruby}" "${utils_lock_sh}/ruby_check_version.rb" "${oldest_ruby_with_flock}"
then
"${ruby}" /dev/stdin <<<"File.new(${lock_fd}).flock(File::LOCK_EX | File::LOCK_NB) || exit(1)"
"${ruby}" "${utils_lock_sh}/ruby_lock_file_descriptor.rb" "${lock_file_descriptor}"
elif [[ -x "$(type -P flock)" ]]
then
flock -n "${lock_file_descriptor}"
elif [[ -x "${python}" ]]
then
"${python}" -c "import fcntl; fcntl.flock(${lock_fd}, fcntl.LOCK_EX | fcntl.LOCK_NB)"
elif [[ -x "$(type -P flock)" ]]
then
flock -n "${lock_fd}"
else
onoe "Cannot create ${name} lock, please avoid running Homebrew in parallel."
onoe "Cannot create ${name} lock due to missing/too old ruby/flock/python, please avoid running Homebrew in parallel."
fi
}
5 changes: 5 additions & 0 deletions Library/Homebrew/utils/lock_sh/ruby_check_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# typed: strict
# frozen_string_literal: true

ruby_version_to_check = ARGV.first
exit(ruby_version_to_check < RUBY_VERSION)

Check warning on line 5 in Library/Homebrew/utils/lock_sh/ruby_check_version.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/lock_sh/ruby_check_version.rb#L4-L5

Added lines #L4 - L5 were not covered by tests
6 changes: 6 additions & 0 deletions Library/Homebrew/utils/lock_sh/ruby_lock_file_descriptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# typed: strict
# frozen_string_literal: true

file_descriptor = ARGV.first.to_i
file = File.new(file_descriptor)
file.flock(File::LOCK_EX | File::LOCK_NB) || exit(1)

Check warning on line 6 in Library/Homebrew/utils/lock_sh/ruby_lock_file_descriptor.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/lock_sh/ruby_lock_file_descriptor.rb#L4-L6

Added lines #L4 - L6 were not covered by tests
12 changes: 12 additions & 0 deletions Library/Homebrew/utils/ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# When bumping to a new major/minor version, also update the bounds in the Gemfile
export HOMEBREW_REQUIRED_RUBY_VERSION=3.1

# Disable Ruby options we don't need.
export HOMEBREW_RUBY_DISABLE_OPTIONS="--disable=gems,rubyopt"

# HOMEBREW_LIBRARY is from the user environment
# shellcheck disable=SC2154
test_ruby() {
Expand Down Expand Up @@ -145,3 +148,12 @@ If there's no Homebrew Portable Ruby available for your processor:
export HOMEBREW_RUBY_PATH
[[ -n "${HOMEBREW_LINUX}" && -n "${TERMINFO_DIRS}" ]] && export TERMINFO_DIRS
}

setup-gem-home-bundle-gemfile() {
GEM_VERSION="$("${HOMEBREW_RUBY_PATH}" "${HOMEBREW_RUBY_DISABLE_OPTIONS}" "${HOMEBREW_LIBRARY}/Homebrew/utils/ruby_sh/ruby_gem_version.rb")"
GEM_HOME="${HOMEBREW_LIBRARY}/Homebrew/vendor/bundle/ruby/${GEM_VERSION}"
BUNDLE_GEMFILE="${HOMEBREW_LIBRARY}/Homebrew/Gemfile"

export GEM_HOME
export BUNDLE_GEMFILE
}
5 changes: 5 additions & 0 deletions Library/Homebrew/utils/ruby_sh/ruby_gem_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# typed: strict
# frozen_string_literal: true

require "rbconfig"
puts RbConfig::CONFIG["ruby_version"]

Check warning on line 5 in Library/Homebrew/utils/ruby_sh/ruby_gem_version.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/utils/ruby_sh/ruby_gem_version.rb#L4-L5

Added lines #L4 - L5 were not covered by tests