22# frozen_string_literal: true
33
44$LOAD_PATH. unshift ( File . expand_path ( "../lib" , __dir__ ) )
5+
56require "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
2025end . parse!
2126
2227require "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"
4435end
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