From 13bf3987d8a9b4433f2a178ccba7ee25211e5815 Mon Sep 17 00:00:00 2001 From: dak2 Date: Sat, 24 Jan 2026 17:01:05 +0900 Subject: [PATCH] Extract BinaryLocator class from Commands module Refactor binary discovery logic into a dedicated class for better separation of concerns. This improves testability and maintainability by isolating platform-specific binary resolution. # Changes - Add BinaryLocator class with find method and candidates logic - Remove find_rust_binary private method from Commands - Remove COMMANDS_DIR constant (now LIB_DIR in BinaryLocator) --- .gitignore | 2 +- lib/methodray/binary_locator.rb | 29 +++++++++++++++++++++++++++++ lib/methodray/commands.rb | 23 +++-------------------- 3 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 lib/methodray/binary_locator.rb diff --git a/.gitignore b/.gitignore index 35acbf9..5982728 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,4 @@ vendor/ tmp/ # Claude -.claude/Claude.local.md +.claude/Claude.local*.md diff --git a/lib/methodray/binary_locator.rb b/lib/methodray/binary_locator.rb new file mode 100644 index 0000000..b4c3650 --- /dev/null +++ b/lib/methodray/binary_locator.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module MethodRay + class BinaryLocator + LIB_DIR = __dir__ + + def initialize + @binary_name = Gem.win_platform? ? 'methodray-cli.exe' : 'methodray-cli' + @legacy_binary_name = Gem.win_platform? ? 'methodray.exe' : 'methodray' + end + + def find + candidates.find { |path| File.executable?(path) } + end + + private + + def candidates + [ + # CLI binary built during gem install (lib/methodray directory) + File.expand_path(@binary_name, LIB_DIR), + # Development: target/release (project root) + File.expand_path("../../target/release/#{@binary_name}", LIB_DIR), + # Development: rust/target/release (legacy standalone binary) + File.expand_path("../../rust/target/release/#{@legacy_binary_name}", LIB_DIR) + ] + end + end +end diff --git a/lib/methodray/commands.rb b/lib/methodray/commands.rb index 260ed86..5a5cd02 100644 --- a/lib/methodray/commands.rb +++ b/lib/methodray/commands.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true +require_relative 'binary_locator' + module MethodRay module Commands - COMMANDS_DIR = __dir__ - class << self def help puts <<~HELP @@ -41,7 +41,7 @@ def clear_cache(args) private def exec_rust_cli(command, args) - binary_path = find_rust_binary + binary_path = BinaryLocator.new.find unless binary_path warn 'Error: CLI binary not found.' @@ -56,23 +56,6 @@ def exec_rust_cli(command, args) exec(binary_path, command, *args) end - - def find_rust_binary - # Platform-specific binary name - cli_binary = Gem.win_platform? ? 'methodray-cli.exe' : 'methodray-cli' - legacy_binary = Gem.win_platform? ? 'methodray.exe' : 'methodray' - - candidates = [ - # CLI binary built during gem install (lib/methodray directory) - File.expand_path(cli_binary, COMMANDS_DIR), - # Development: target/release (project root) - File.expand_path("../../target/release/#{cli_binary}", COMMANDS_DIR), - # Development: rust/target/release (legacy standalone binary) - File.expand_path("../../rust/target/release/#{legacy_binary}", COMMANDS_DIR) - ] - - candidates.find { |path| File.executable?(path) } - end end end end