Skip to content

Commit baf8023

Browse files
authored
Merge pull request #771 from Shopify/03-23-add_interactive_mode_to_the_cli
Add interactive CLI mode
2 parents bf9768f + 208a3d3 commit baf8023

4 files changed

Lines changed: 53 additions & 24 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem "rubocop"
1212
gem "rubocop-shopify"
1313
gem "extconf_compile_commands_json"
1414
gem "rbs"
15+
gem "irb"
1516

1617
# Gems that aren't supported on Windows
1718
platforms :ruby do

Gemfile.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ GEM
77
remote: https://rubygems.org/
88
specs:
99
ast (2.4.3)
10+
date (3.5.1)
11+
erb (6.0.2)
1012
extconf_compile_commands_json (0.0.7)
13+
io-console (0.8.2)
14+
irb (1.17.0)
15+
pp (>= 0.6.0)
16+
prism (>= 1.3.0)
17+
rdoc (>= 4.0.0)
18+
reline (>= 0.4.2)
1119
json (2.19.4)
1220
language_server-protocol (3.17.0.5)
1321
lint_roller (1.1.0)
@@ -24,7 +32,13 @@ GEM
2432
parser (3.3.11.1)
2533
ast (~> 2.4.1)
2634
racc
35+
pp (0.6.3)
36+
prettyprint
37+
prettyprint (0.2.0)
2738
prism (1.9.0)
39+
psych (5.3.1)
40+
date
41+
stringio
2842
racc (1.8.1)
2943
rainbow (3.1.1)
3044
rake (13.3.1)
@@ -33,7 +47,13 @@ GEM
3347
rbs (3.10.3)
3448
logger
3549
tsort
50+
rdoc (7.2.0)
51+
erb
52+
psych (>= 4.0.0)
53+
tsort
3654
regexp_parser (2.12.0)
55+
reline (0.6.3)
56+
io-console (~> 0.5)
3757
rubocop (1.84.0)
3858
json (~> 2.3)
3959
language_server-protocol (~> 3.17.0.2)
@@ -53,6 +73,7 @@ GEM
5373
ruby-progressbar (1.13.0)
5474
ruby_memcheck (3.0.1)
5575
nokogiri
76+
stringio (3.2.0)
5677
tsort (0.2.0)
5778
unicode-display_width (3.2.0)
5879
unicode-emoji (~> 4.1)
@@ -64,6 +85,7 @@ PLATFORMS
6485

6586
DEPENDENCIES
6687
extconf_compile_commands_json
88+
irb
6789
minitest
6890
rake (~> 13.3)
6991
rake-compiler

dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ up:
44
- ruby
55
- bundler
66
- rust
7+
- packages:
8+
- libyaml
79
# met? checks: 1) binary exists, 2) no file under rust/ (excluding rust/target/) is newer than the binary
810
- custom:
911
name: Install rubydex MCP server

exe/rdx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# frozen_string_literal: true
33

44
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
5+
56
require "optparse"
67

7-
OptionParser.new do |parser|
8-
parser.banner = "Usage: [path1, path2]"
8+
options = {}
99

10+
OptionParser.new do |parser|
1011
parser.on("--version", "Print the gem's version") do
1112
require "rubydex/version"
1213
puts "v#{Rubydex::VERSION}"
@@ -17,31 +18,34 @@ OptionParser.new do |parser|
1718
puts parser
1819
exit
1920
end
21+
22+
parser.on("-i", "--interactive", "Open an interactive session with a populated graph for the current workspace") do
23+
options[:interactive] = true
24+
end
2025
end.parse!
2126

2227
require "rubydex"
2328

24-
workspace_path = ARGV.first
25-
26-
# If a workspace path is passed, we need to ensure that Bundler is setup in that context so that we find the
27-
# dependencies for that project
28-
if workspace_path
29-
gemfile_path = File.join(workspace_path, "Gemfile")
30-
31-
if File.exist?(gemfile_path)
32-
ENV["BUNDLE_GEMFILE"] = gemfile_path
33-
34-
begin
35-
Bundler.setup
36-
rescue Bundler::BundlerError => e
37-
$stderr.puts(<<~MESSAGE)
38-
Bundle setup failed for #{gemfile_path}. Indexing of dependencies may be partial
39-
Error:
40-
#{e.message}
41-
MESSAGE
42-
end
43-
end
29+
def __with_timer(message, &block)
30+
print(message)
31+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
32+
block.call
33+
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - start
34+
puts " finished in #{duration.round(2)}ms"
4435
end
4536

46-
graph = Rubydex::Graph.new(workspace_path: workspace_path)
47-
graph.index_workspace
37+
graph = Rubydex::Graph.new
38+
__with_timer("Indexing workspace...") { graph.index_workspace }
39+
__with_timer("Resolving graph...") { graph.resolve }
40+
41+
if options[:interactive]
42+
begin
43+
require "irb"
44+
IRB.setup(nil)
45+
IRB.conf[:IRB_NAME] = "rubydex"
46+
workspace = IRB::WorkSpace.new(binding)
47+
IRB::Irb.new(workspace).run(IRB.conf)
48+
rescue LoadError
49+
abort("Interactive mode requires `irb` to be in the bundle")
50+
end
51+
end

0 commit comments

Comments
 (0)