From 6c260db2770426ffac2a24606c3e004c5db9d94f Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 1 Apr 2024 10:00:07 -0700 Subject: [PATCH] Port Homebrew::Cmd::PyenvSync --- Library/Homebrew/cmd/pyenv-sync.rb | 96 ++++++++++---------- Library/Homebrew/test/cmd/pyenv-sync_spec.rb | 8 ++ 2 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 Library/Homebrew/test/cmd/pyenv-sync_spec.rb diff --git a/Library/Homebrew/cmd/pyenv-sync.rb b/Library/Homebrew/cmd/pyenv-sync.rb index c2837ef436..c9a7c18e24 100644 --- a/Library/Homebrew/cmd/pyenv-sync.rb +++ b/Library/Homebrew/cmd/pyenv-sync.rb @@ -1,68 +1,66 @@ -# typed: true +# typed: strict # frozen_string_literal: true -require "cli/parser" +require "abstract_command" require "formula" module Homebrew - module_function + module Cmd + class PyenvSync < AbstractCommand + cmd_args do + description <<~EOS + Create symlinks for Homebrew's installed Python versions in `~/.pyenv/versions`. - sig { returns(CLI::Parser) } - def pyenv_sync_args - Homebrew::CLI::Parser.new do - description <<~EOS - Create symlinks for Homebrew's installed Python versions in `~/.pyenv/versions`. + Note that older patch version symlinks will be created and linked to the minor + version so e.g. Python 3.11.0 will also be symlinked to 3.11.3. + EOS - Note that older patch version symlinks will be created and linked to the minor - version so e.g. Python 3.11.0 will also be symlinked to 3.11.3. - EOS + named_args :none + end - named_args :none - end - end + sig { override.void } + def run + pyenv_root = Pathname(ENV.fetch("HOMEBREW_PYENV_ROOT", Pathname(Dir.home)/".pyenv")) - sig { void } - def pyenv_sync - pyenv_root = Pathname(ENV.fetch("HOMEBREW_PYENV_ROOT", Pathname(Dir.home)/".pyenv")) + # Don't run multiple times at once. + pyenv_sync_running = pyenv_root/".pyenv_sync_running" + return if pyenv_sync_running.exist? - # Don't run multiple times at once. - pyenv_sync_running = pyenv_root/".pyenv_sync_running" - return if pyenv_sync_running.exist? + begin + pyenv_versions = pyenv_root/"versions" + pyenv_versions.mkpath + FileUtils.touch pyenv_sync_running + HOMEBREW_CELLAR.glob("python{,@*}") + .flat_map(&:children) + .each { |path| link_pyenv_versions(path, pyenv_versions) } - begin - pyenv_versions = pyenv_root/"versions" - pyenv_versions.mkpath - FileUtils.touch pyenv_sync_running + pyenv_versions.children + .select(&:symlink?) + .reject(&:exist?) + .each { |path| FileUtils.rm_f path } + ensure + pyenv_sync_running.unlink if pyenv_sync_running.exist? + end + end - pyenv_sync_args.parse + private - HOMEBREW_CELLAR.glob("python{,@*}") - .flat_map(&:children) - .each { |path| link_pyenv_versions(path, pyenv_versions) } + sig { params(path: Pathname, pyenv_versions: Pathname).void } + def link_pyenv_versions(path, pyenv_versions) + pyenv_versions.mkpath - pyenv_versions.children - .select(&:symlink?) - .reject(&:exist?) - .each { |path| FileUtils.rm_f path } - ensure - pyenv_sync_running.unlink if pyenv_sync_running.exist? - end - end + version = Keg.new(path).version + major_version = version.major.to_i + minor_version = version.minor.to_i + patch_version = version.patch.to_i - sig { params(path: Pathname, pyenv_versions: Pathname).void } - def link_pyenv_versions(path, pyenv_versions) - pyenv_versions.mkpath + (0..patch_version).each do |patch| + link_path = pyenv_versions/"#{major_version}.#{minor_version}.#{patch}" - version = Keg.new(path).version - major_version = version.major.to_i - minor_version = version.minor.to_i - patch_version = version.patch.to_i - - (0..patch_version).each do |patch| - link_path = pyenv_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/pyenv-sync_spec.rb b/Library/Homebrew/test/cmd/pyenv-sync_spec.rb new file mode 100644 index 0000000000..a462c2d917 --- /dev/null +++ b/Library/Homebrew/test/cmd/pyenv-sync_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "cmd/pyenv-sync" +require "cmd/shared_examples/args_parse" + +RSpec.describe Homebrew::Cmd::PyenvSync do + it_behaves_like "parseable arguments" +end