brew/Library/Homebrew/cmd/rbenv-sync.rb
Mike McQuaid bbf5a9f479
env_config: use environment variables consistently
- use e.g. `$HOMEBREW_*` for cases where only the environment variable
  is the entire backtick-quoted string
- use e.g. `${HOMEBREW_*}` for cases where the environment variable is
  part of a backtick-quoted string to make clear what parts are variable
  and what parts are not
- use `export HOMEBREW_*=...` for cases where we're talking about
  setting the environment variable (because it likely needs to be
  exported to work how they want)

Inspired by https://github.com/Homebrew/homebrew-bundle/pull/1579 making
similar changes for Homebrew/homebrew-bundle.
2025-01-27 14:21:27 +00:00

70 lines
2.0 KiB
Ruby

# typed: strict
# frozen_string_literal: true
require "abstract_command"
require "formula"
module Homebrew
module Cmd
class RbenvSync < AbstractCommand
cmd_args do
description <<~EOS
Create symlinks for Homebrew's installed Ruby versions in `${HOME}/.rbenv/versions`.
Note that older version symlinks will also be created so e.g. Ruby 3.2.1 will
also be symlinked to 3.2.0.
EOS
named_args :none
end
sig { override.void }
def run
rbenv_root = Pathname(ENV.fetch("HOMEBREW_RBENV_ROOT", Pathname(Dir.home)/".rbenv"))
# Don't run multiple times at once.
rbenv_sync_running = rbenv_root/".rbenv_sync_running"
return if rbenv_sync_running.exist?
begin
rbenv_versions = rbenv_root/"versions"
rbenv_versions.mkpath
FileUtils.touch rbenv_sync_running
HOMEBREW_CELLAR.glob("ruby{,@*}")
.flat_map(&:children)
.each { |path| link_rbenv_versions(path, rbenv_versions) }
rbenv_versions.children
.select(&:symlink?)
.reject(&:exist?)
.each { |path| FileUtils.rm_f path }
ensure
rbenv_sync_running.unlink if rbenv_sync_running.exist?
end
end
private
sig { params(path: Pathname, rbenv_versions: Pathname).void }
def link_rbenv_versions(path, rbenv_versions)
rbenv_versions.mkpath
version = Keg.new(path).version
major_version = version.major.to_i
minor_version = version.minor.to_i
patch_version = version.patch.to_i || 0
(0..patch_version).each do |patch|
link_path = rbenv_versions/"#{major_version}.#{minor_version}.#{patch}"
# Don't clobber existing user installations.
next if link_path.exist? && !link_path.symlink?
FileUtils.rm_f link_path
FileUtils.ln_sf path, link_path
end
end
end
end
end