Port Homebrew::Cmd::NodenvSync

This commit is contained in:
Douglas Eichelberger 2024-04-01 09:07:45 -07:00
parent a43224cb4a
commit c5dfac1f2c
2 changed files with 56 additions and 49 deletions

View File

@ -1,69 +1,68 @@
# 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 NodenvSync < AbstractCommand
cmd_args do
description <<~EOS
Create symlinks for Homebrew's installed NodeJS versions in `~/.nodenv/versions`.
sig { returns(CLI::Parser) } Note that older version symlinks will also be created so e.g. NodeJS 19.1.0 will
def nodenv_sync_args also be symlinked to 19.0.0.
Homebrew::CLI::Parser.new do EOS
description <<~EOS
Create symlinks for Homebrew's installed NodeJS versions in `~/.nodenv/versions`.
Note that older version symlinks will also be created so e.g. NodeJS 19.1.0 will named_args :none
also be symlinked to 19.0.0. end
EOS
named_args :none sig { override.void }
end def run
end nodenv_root = Pathname(ENV.fetch("HOMEBREW_NODENV_ROOT", Pathname(Dir.home)/".nodenv"))
sig { void } # Don't run multiple times at once.
def nodenv_sync nodenv_sync_running = nodenv_root/".nodenv_sync_running"
nodenv_root = Pathname(ENV.fetch("HOMEBREW_NODENV_ROOT", Pathname(Dir.home)/".nodenv")) return if nodenv_sync_running.exist?
# Don't run multiple times at once. begin
nodenv_sync_running = nodenv_root/".nodenv_sync_running" nodenv_versions = nodenv_root/"versions"
return if nodenv_sync_running.exist? nodenv_versions.mkpath
FileUtils.touch nodenv_sync_running
begin HOMEBREW_CELLAR.glob("node{,@*}")
nodenv_versions = nodenv_root/"versions" .flat_map(&:children)
nodenv_versions.mkpath .each { |path| link_nodenv_versions(path, nodenv_versions) }
FileUtils.touch nodenv_sync_running
nodenv_sync_args.parse nodenv_versions.children
.select(&:symlink?)
.reject(&:exist?)
.each { |path| FileUtils.rm_f path }
ensure
nodenv_sync_running.unlink if nodenv_sync_running.exist?
end
end
HOMEBREW_CELLAR.glob("node{,@*}") private
.flat_map(&:children)
.each { |path| link_nodenv_versions(path, nodenv_versions) }
nodenv_versions.children sig { params(path: Pathname, nodenv_versions: Pathname).void }
.select(&:symlink?) def link_nodenv_versions(path, nodenv_versions)
.reject(&:exist?) nodenv_versions.mkpath
.each { |path| FileUtils.rm_f path }
ensure
nodenv_sync_running.unlink if nodenv_sync_running.exist?
end
end
sig { params(path: Pathname, nodenv_versions: Pathname).void } version = Keg.new(path).version
def link_nodenv_versions(path, nodenv_versions) major_version = version.major.to_i
nodenv_versions.mkpath minor_version = version.minor.to_i || 0
patch_version = version.patch.to_i || 0
version = Keg.new(path).version (0..minor_version).each do |minor|
major_version = version.major.to_i (0..patch_version).each do |patch|
minor_version = version.minor.to_i || 0 link_path = nodenv_versions/"#{major_version}.#{minor}.#{patch}"
patch_version = version.patch.to_i || 0
(0..minor_version).each do |minor| FileUtils.rm_f link_path
(0..patch_version).each do |patch| FileUtils.ln_sf path, link_path
link_path = nodenv_versions/"#{major_version}.#{minor}.#{patch}" end
end
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/nodenv-sync"
require "cmd/shared_examples/args_parse"
RSpec.describe Homebrew::Cmd::NodenvSync do
it_behaves_like "parseable arguments"
end