Port Homebrew::Cmd::PyenvSync

This commit is contained in:
Douglas Eichelberger 2024-04-01 10:00:07 -07:00
parent 427b527335
commit 6c260db277
2 changed files with 55 additions and 49 deletions

View File

@ -1,68 +1,66 @@
# 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 PyenvSync < AbstractCommand
cmd_args do
description <<~EOS
Create symlinks for Homebrew's installed Python versions in `~/.pyenv/versions`.
sig { returns(CLI::Parser) } Note that older patch version symlinks will be created and linked to the minor
def pyenv_sync_args version so e.g. Python 3.11.0 will also be symlinked to 3.11.3.
Homebrew::CLI::Parser.new do EOS
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 named_args :none
version so e.g. Python 3.11.0 will also be symlinked to 3.11.3. end
EOS
named_args :none sig { override.void }
end def run
end pyenv_root = Pathname(ENV.fetch("HOMEBREW_PYENV_ROOT", Pathname(Dir.home)/".pyenv"))
sig { void } # Don't run multiple times at once.
def pyenv_sync pyenv_sync_running = pyenv_root/".pyenv_sync_running"
pyenv_root = Pathname(ENV.fetch("HOMEBREW_PYENV_ROOT", Pathname(Dir.home)/".pyenv")) return if pyenv_sync_running.exist?
# Don't run multiple times at once. begin
pyenv_sync_running = pyenv_root/".pyenv_sync_running" pyenv_versions = pyenv_root/"versions"
return if pyenv_sync_running.exist? 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.children
pyenv_versions = pyenv_root/"versions" .select(&:symlink?)
pyenv_versions.mkpath .reject(&:exist?)
FileUtils.touch pyenv_sync_running .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{,@*}") sig { params(path: Pathname, pyenv_versions: Pathname).void }
.flat_map(&:children) def link_pyenv_versions(path, pyenv_versions)
.each { |path| link_pyenv_versions(path, pyenv_versions) } pyenv_versions.mkpath
pyenv_versions.children version = Keg.new(path).version
.select(&:symlink?) major_version = version.major.to_i
.reject(&:exist?) minor_version = version.minor.to_i
.each { |path| FileUtils.rm_f path } patch_version = version.patch.to_i
ensure
pyenv_sync_running.unlink if pyenv_sync_running.exist?
end
end
sig { params(path: Pathname, pyenv_versions: Pathname).void } (0..patch_version).each do |patch|
def link_pyenv_versions(path, pyenv_versions) link_path = pyenv_versions/"#{major_version}.#{minor_version}.#{patch}"
pyenv_versions.mkpath
version = Keg.new(path).version FileUtils.rm_f link_path
major_version = version.major.to_i FileUtils.ln_sf path, link_path
minor_version = version.minor.to_i end
patch_version = version.patch.to_i end
(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
end end
end end
end end

View File

@ -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