From 5ef070380c4ebed01e56ff376db6363e836692f9 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 1 Apr 2024 10:02:09 -0700 Subject: [PATCH] Port Homebrew::Cmd::RbenvSync --- Library/Homebrew/cmd/rbenv-sync.rb | 95 ++++++++++---------- Library/Homebrew/test/cmd/rbenv-sync_spec.rb | 8 ++ 2 files changed, 55 insertions(+), 48 deletions(-) create mode 100644 Library/Homebrew/test/cmd/rbenv-sync_spec.rb diff --git a/Library/Homebrew/cmd/rbenv-sync.rb b/Library/Homebrew/cmd/rbenv-sync.rb index f133a145be..bf2bbf9338 100644 --- a/Library/Homebrew/cmd/rbenv-sync.rb +++ b/Library/Homebrew/cmd/rbenv-sync.rb @@ -1,68 +1,67 @@ -# typed: true +# typed: strict # frozen_string_literal: true -require "cli/parser" +require "abstract_command" require "formula" 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) } - def rbenv_sync_args - Homebrew::CLI::Parser.new do - 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 + also be symlinked to 3.2.0. + EOS - 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 - named_args :none - end - end + sig { override.void } + def run + rbenv_root = Pathname(ENV.fetch("HOMEBREW_RBENV_ROOT", Pathname(Dir.home)/".rbenv")) - sig { void } - def rbenv_sync - 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? - # 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 - 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_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{,@*}") - .flat_map(&:children) - .each { |path| link_rbenv_versions(path, rbenv_versions) } + private - 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 + sig { params(path: Pathname, rbenv_versions: Pathname).void } + def link_rbenv_versions(path, rbenv_versions) + rbenv_versions.mkpath - 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 - 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}" - (0..patch_version).each do |patch| - link_path = rbenv_versions/"#{major_version}.#{minor_version}.#{patch}" - - FileUtils.rm_f link_path - FileUtils.ln_sf path, link_path + FileUtils.rm_f link_path + FileUtils.ln_sf path, link_path + end + end end end end diff --git a/Library/Homebrew/test/cmd/rbenv-sync_spec.rb b/Library/Homebrew/test/cmd/rbenv-sync_spec.rb new file mode 100644 index 0000000000..7196f77e25 --- /dev/null +++ b/Library/Homebrew/test/cmd/rbenv-sync_spec.rb @@ -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