|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rbconfig" |
| 4 | + |
| 5 | +module Rubydex |
| 6 | + # Launcher-only bridge for `exe/rubydex_mcp`. |
| 7 | + # |
| 8 | + # Do not require this file from `lib/rubydex.rb`. The main Ruby API owns graph construction through |
| 9 | + # `Rubydex::Graph`; this file exists only so the executable wrapper can inspect Bundler before it execs the Rust MCP |
| 10 | + # server. The Rust server still performs the indexing pass. |
| 11 | + module MCPServerBridge |
| 12 | + PASSTHROUGH_ARGUMENTS = ["-h", "--help", "-V", "--version"].freeze |
| 13 | + |
| 14 | + extend self |
| 15 | + |
| 16 | + def executable_name |
| 17 | + host_os = RbConfig::CONFIG.fetch("host_os") |
| 18 | + host_os.match?(/mswin|mingw|cygwin/) ? "rubydex_mcp.exe" : "rubydex_mcp" |
| 19 | + end |
| 20 | + |
| 21 | + def binary_path |
| 22 | + File.expand_path("bin/#{executable_name}", __dir__) |
| 23 | + end |
| 24 | + |
| 25 | + def passthrough?(argv) |
| 26 | + argv.any? { |arg| PASSTHROUGH_ARGUMENTS.include?(arg) } |
| 27 | + end |
| 28 | + |
| 29 | + def setup_bundler_context(workspace_path) |
| 30 | + root = File.expand_path(workspace_path) |
| 31 | + root = File.dirname(root) if File.file?(root) |
| 32 | + |
| 33 | + gemfile = File.join(root, "Gemfile") |
| 34 | + ENV["BUNDLE_GEMFILE"] ||= gemfile if File.file?(gemfile) |
| 35 | + |
| 36 | + require "bundler/setup" |
| 37 | + require "bundler" |
| 38 | + |
| 39 | + true |
| 40 | + rescue LoadError => e |
| 41 | + warn("Warning: failed to load Bundler context: #{e.message}") |
| 42 | + false |
| 43 | + rescue Bundler::BundlerError => e |
| 44 | + warn("Warning: failed to load Bundler context: #{e.message}") |
| 45 | + false |
| 46 | + end |
| 47 | + |
| 48 | + def compute_index_paths(argv) |
| 49 | + paths = argv.empty? ? [Dir.pwd] : argv |
| 50 | + paths = paths.map { |path| File.expand_path(path) } |
| 51 | + |
| 52 | + paths.concat(dependency_index_paths) |
| 53 | + paths.uniq |
| 54 | + end |
| 55 | + |
| 56 | + def dependency_index_paths |
| 57 | + Bundler.load.specs.flat_map do |spec| |
| 58 | + spec.require_paths.filter_map do |path| |
| 59 | + next if File.absolute_path?(path) |
| 60 | + |
| 61 | + full_path = File.join(spec.full_gem_path, path) |
| 62 | + full_path if File.directory?(full_path) |
| 63 | + end |
| 64 | + end.uniq |
| 65 | + rescue Bundler::BundlerError => e |
| 66 | + warn("Warning: failed to collect Bundler dependency paths: #{e.message}") |
| 67 | + [] |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments