Port Homebrew::Cmd::RbenvSync

This commit is contained in:
Douglas Eichelberger 2024-04-01 10:02:09 -07:00
parent 6c260db277
commit 5ef070380c
2 changed files with 55 additions and 48 deletions

View File

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

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
require "cmd/rbenv-sync"
require "cmd/shared_examples/args_parse"
RSpec.describe Homebrew::Cmd::RbenvSync do
it_behaves_like "parseable arguments"
end