irt 1.1.7 → 1.2.0.p01

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -140,7 +140,10 @@ When you close the session with 'exit' (or 'x' or 'q'), IRT will not pass back a
140
140
 
141
141
  ### Binding Sessions (yellow)
142
142
 
143
- You can open a binding session from any file being evaluated with the directive 'irt binding'.
143
+ You can open a binding session from any file with the directive 'irt binding': you don't even need to use the IRT executable.
144
+ It works also from the rails code, while the server is running. (See Rails)
145
+
146
+
144
147
  The 'self' of the new session will be the 'self' at the line you called it, so you can play with local variables
145
148
  and methods as you would do it at that line.
146
149
 
@@ -597,6 +600,35 @@ By default IRT will output the rails log (colored in blue) right in the console.
597
600
  You can switch the rails log ON or OFF by using the 'rails\_log\_on' (or 'rlo') and 'rails\_log\_off' (or 'rlf')
598
601
  commands in any session, besides you can set the option IRT.rails_log to true or false in the ~/.irtrc file.
599
602
 
603
+ ### Rails Server Sessions
604
+
605
+ The server sessions are a quick way to interact with your application while your server is running,
606
+ without the need to launch a the irt executable: you can do almost everything you can from a regular IRT session
607
+ launched from the irt executable.
608
+
609
+ If you want to open a session from your Rails code or from a template while the server is running,
610
+ you don't have to use the IRT executable: you can just add an 'irt binding' statement where you want
611
+ (even in a erb template), load the page in the browser and IRT will open a Binding Session right in the server's console.
612
+
613
+ # will open an IRT Binding Session in the server window
614
+ irt binding
615
+
616
+ Besides, if you want to open an Interactive Session, you have just to type Ctrl-C in the server console and
617
+ you will be asked if you want to shutdown the server or open an IRT session.
618
+
619
+ => Booting Mongrel
620
+ => Rails 3.0.4 application starting in development on http://0.0.0.0:3000
621
+ => Call with -d to detach
622
+ => Ctrl-C to shutdown server
623
+ ^C
624
+ #> Server suspended
625
+ ?> [s]hutdown, [i]rt or [c]ancel? [<enter>=s|i|c]
626
+
627
+ Notice that the execution of the web response is halted until you exit from the session.
628
+ When you exit, the response process will be resumed, and the server will return to its normal behaviour.
629
+
630
+ Note: The Server Sessions are known to work with WEBrick and Mongrel (using Rack)
631
+
600
632
  ### Rails 3
601
633
 
602
634
  You must add the gem to your Gemfile, to make the bundler happy:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.7
1
+ 1.2.0.p01
data/bin/irt CHANGED
@@ -2,21 +2,10 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'fileutils'
5
- require 'tempfile'
6
5
  require 'optparse'
7
- require 'prompter'
8
- require 'dye'
6
+ require 'irt/utils'
9
7
 
10
- prompter = Prompter.new do |pr|
11
- def pr.say_echo(result, opts={})
12
- out = sprintf " #> %s", result
13
- say out, :style => :yellow
14
- end
15
- end
16
-
17
- version = File.read(File.expand_path('../../VERSION', __FILE__)).strip
18
-
19
- copy = Dye.dye "irt #{version} (c) 2010-2011 Domizio Demichelis", :blue, :bold
8
+ copy = IRT::Utils.copyright
20
9
 
21
10
  options = {}
22
11
 
@@ -36,7 +25,7 @@ EOB
36
25
  end
37
26
 
38
27
  options[:irb_options] = nil
39
- opts.on( '-b', '--irb-options [OPTIONS]', 'Sets the irb Options' ) do |opt|
28
+ opts.on( '-b', '--irb-options [OPTIONS]', 'Sets the irb or rails console options' ) do |opt|
40
29
  options[:irb_options] = opt
41
30
  end
42
31
 
@@ -51,7 +40,7 @@ EOB
51
40
  end
52
41
 
53
42
  opts.on( '-v', '--version', 'Shows the version and exits' ) do
54
- puts version
43
+ puts IRT::Utils.version
55
44
  exit
56
45
  end
57
46
 
@@ -69,47 +58,34 @@ puts copy
69
58
 
70
59
  paths = if ARGV.empty?
71
60
  options[:interactive_eof] = true
72
- tmp_file = Tempfile.new(['', '.irt'])
73
- tmp_file << "\n" # one empty line makes irb of 1.9.2 happy
74
- tmp_file.flush
75
- tmp_path = tmp_file.path
76
- at_exit do
77
- require 'irt/utils'
78
- if File.read(tmp_path).length > 1
79
- prompter.yes? %(The template file has been modified, do you want to save it?) do
80
- prompter.choose %(Enter the file path to save:), /[\w0-9_]/ do |as_file|
81
- IRT::Utils.save_as(tmp_path, as_file, prompter){ system(ENV['IRT_COMMAND']) }
82
- end
83
- end
84
- end
85
- end
86
- [ tmp_path ]
61
+ [ IRT::Utils.create_tmp_file ]
87
62
  else
88
63
  ARGV.map {|p| File.expand_path(p) }
89
64
  end
90
65
 
91
66
  files = paths.map do |path|
92
67
  unless File.exists?(path)
93
- next if prompter.no? %(Do you want to create the file "#{path}"?), :hint => '[<enter=y|n]', :default => 'y'
68
+ next if IRT::Prompter.no? %(Do you want to create the file "#{path}"?), :hint => '[<enter=y|n]', :default => 'y'
94
69
  options[:interactive_eof] = true
95
70
  dirname = File.dirname(path)
96
71
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
97
72
  File.open(path, 'w') {|f| f.puts "\n" } # one empty line makes irb of 1.9.2 happy
98
73
  end
99
74
  File.directory?(path) ? Dir.glob(File.join(path, '**/*.irt')) : path
100
- end.flatten
75
+ end.flatten.compact
101
76
 
102
77
  if files.empty?
103
- prompter.say_notice 'No *.irt files to run'
78
+ IRT::Prompter.say_notice 'No *.irt files to run'
104
79
  exit
105
80
  end
106
81
 
107
82
  cmd_format = if File.exists?('./config/environment.rb') && !options[:no_rails]
83
+ ENV['RAILS_ENV'] = options[:rails_env]
108
84
  if File.exists?('./script/rails')
109
85
  gemfile = File.read('Gemfile')
110
86
  unless gemfile.match(/\bgem\b.+\birt\b/)
111
- prompter.say_warning %(The Gemfile doesn't look to include any 'gem "irt"' statement.\nIRT will probably not work until you add it!)
112
- prompter.yes?("Do you want to add irt to your Gemfile?", :hint => '[enter=y|n]', :default => 'y') do
87
+ IRT::Prompter.say_warning %(The Gemfile doesn't look to include any 'gem "irt"' statement.\nIRT will probably not work until you add it!)
88
+ IRT::Prompter.yes?("Do you want to add irt to your Gemfile?", :hint => '[enter=y|n]', :default => 'y') do
113
89
  File.open('Gemfile', 'a') do |f|
114
90
  f.puts %(gem "irt")
115
91
  end
@@ -117,20 +93,16 @@ cmd_format = if File.exists?('./config/environment.rb') && !options[:no_rails]
117
93
  end
118
94
  'rails c %s %s %s'
119
95
  elsif File.exists?('./script/console')
120
- 'ruby script/console --irb="irt_rails2 %s"'
96
+ 'ruby script/console --irb="irt_rails2 %2$s" %1$s %3$s'
121
97
  end
122
98
  else
123
99
  'irt_irb %s %s'
124
100
  end
125
101
 
126
- if cmd_format.match(/^ruby script\/console/)
127
- ENV['RAILS_ENV'] ||= options[:rails_env]
128
- end
129
-
130
102
  ENV['IRT_INTERACTIVE_EOF'] = options[:interactive_eof].inspect if options[:interactive_eof]
131
103
 
132
104
  files.each do |file|
133
- ENV['IRT_COMMAND'] = sprintf cmd_format, file, options[:irb_options], options[:rails_env]
105
+ ENV['IRT_COMMAND'] = sprintf cmd_format, options[:irb_options], file, options[:rails_env]
134
106
  unless system(ENV['IRT_COMMAND'])
135
107
  puts Dye.sgr(:clear) if Dye.color?
136
108
  exit(1)
data/bin/irt_irb CHANGED
File without changes
data/irt.gemspec CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
12
12
  s.description = 'If you use IRT in place of irb or rails console, you will have more tools that will make your life a lot easier.'
13
13
 
14
14
  s.add_runtime_dependency('differ', [">= 0.1.1"])
15
- s.add_runtime_dependency('dye', [">= 0.1.1"])
16
- s.add_runtime_dependency('prompter', [">= 0.1.2"])
15
+ s.add_runtime_dependency('dye', [">= 0.1.3"])
16
+ s.add_runtime_dependency('prompter', [">= 0.1.4"])
17
17
  if IRT::RubyVersion >= '1.9.2'
18
18
  s.add_runtime_dependency('bri', [">= 0.1.5"])
19
19
  else
@@ -21,6 +21,7 @@ module IRT
21
21
  alias_method :open_session, :irt # legacy method
22
22
 
23
23
  def vdiff(a,b)
24
+ ensure_session
24
25
  puts IRT::Differ.new(a,b, :value, {:a_marker => 'a',
25
26
  :b_marker => 'b',
26
27
  :a_label => '',
@@ -29,17 +30,29 @@ module IRT
29
30
  alias_method :vd, :vdiff
30
31
 
31
32
  # rerun the same shell command
32
- def rerun
33
+ def restart
33
34
  ensure_session
35
+ ensure_cli
34
36
  IRB.irb_at_exit
35
- str = "Rerunning: `#{ENV['IRT_COMMAND']}`"
37
+ str = "Restarting: `#{ENV['IRT_COMMAND']}`"
36
38
  puts
37
39
  puts IRT.dye(" #{str} ", "*** #{str} ***", :error_color, :bold, :reversed)
38
40
  puts
39
41
  exec ENV["IRT_COMMAND"]
40
42
  end
43
+ alias_method :r!, :restart
44
+
45
+ def rerun
46
+ ensure_session
47
+ IRT::Session.run_file
48
+ end
41
49
  alias_method :rr, :rerun
42
50
 
51
+ def run(file_path)
52
+ ensure_session
53
+ IRT::Session.run_file file_path
54
+ end
55
+
43
56
  def sh(*args)
44
57
  system *args
45
58
  end
@@ -14,20 +14,19 @@ module IRT
14
14
  end
15
15
  alias_method :cll, :copy_all_lines
16
16
 
17
- %w[vi nano edit].each do |n|
18
- eval <<-EOE, binding, __FILE__, __LINE__+1
19
- def #{n}(*args)
20
- ensure_session
21
- run_editor(:#{n}, *args)
22
- end
23
- EOE
24
- eval <<-EOE, binding, __FILE__, __LINE__+1
25
- def c#{n}(*args)
26
- ensure_session
27
- copy_lines
28
- #{n} *args
29
- end
30
- EOE
17
+ [:vi, :nano, :edit].each do |n|
18
+
19
+ define_method(n) do |*args|
20
+ ensure_session
21
+ run_editor(n, *args)
22
+ end
23
+
24
+ define_method(:"c#{n}") do |*args|
25
+ ensure_session
26
+ copy_lines
27
+ send n, *args
28
+ end
29
+
31
30
  end
32
31
  alias_method :nn, :nano
33
32
  alias_method :ed, :edit
@@ -79,7 +79,11 @@ module IRT
79
79
  x|q Aliases for exit (from the current session)
80
80
  xx|qq Aliases for abort (abort the irt process)
81
81
  status|ss Prints the session status line
82
- rerun|rr Reruns the same file
82
+ run file Run an irt file (exiting from the current sessions)
83
+ rerun|rr Reruns the current irt file (exiting from the
84
+ current sessions)
85
+ restart|r! Restart the executable, reload IRT (and Rails) and
86
+ rerun the current file
83
87
  irt_help|hh Shows this screen
84
88
  sh command Alias for system("command") (no quotes needed)
85
89
 
@@ -45,8 +45,9 @@ module IRT
45
45
  alias_method :tt, :add_test
46
46
 
47
47
  def save_as(path)
48
- require 'irt/utils'
49
- IRT::Utils.save_as(IRT.irt_file, path, IRT.prompter){ rerun }
48
+ IRT::Utils.save_as(path) do
49
+ IRT::Session.run_file path
50
+ end
50
51
  end
51
52
  alias_method :sa, :save_as
52
53
 
@@ -42,9 +42,8 @@ module IRT
42
42
 
43
43
  def load_helper_files
44
44
  return unless IRT.autoload_helper_files
45
- irt_file_path = Pathname.new($0).realpath
46
45
  container_path = Pathname.getwd.parent
47
- down_path = irt_file_path.relative_path_from container_path
46
+ down_path = IRT.irt_file.relative_path_from container_path
48
47
  down_path.dirname.descend do |p|
49
48
  helper_path = container_path.join(p, 'irt_helper.rb')
50
49
  begin
@@ -69,7 +68,7 @@ module IRT
69
68
  @@diffs += 1
70
69
  puts IRT.dye("#{tno}. DIFFS!", :diff_color, :bold) + IRT.dye(" #{d}\n ", :diff_color) +
71
70
  IRT.dye(" at #{context.irb_path}: #{context.last_line_no} ", :file_color, :reversed, :bold)
72
- puts IRT.differ.new(saved, actual, kind).output
71
+ puts IRT::Differ.new(saved, actual, kind).output
73
72
  IRT::Session.enter(:interactive) if IRT.irt_on_diffs
74
73
  end
75
74
  rescue Exception
@@ -19,9 +19,16 @@ module IRB
19
19
 
20
20
  alias_method :x, :irb_exit
21
21
  alias_method :q, :irb_exit
22
+ alias_method :irb, :irt
23
+
24
+ alias_method :original_abort, :abort
25
+ def abort
26
+ ensure_cli
27
+ IRT::Session.exit_all = true
28
+ original_abort
29
+ end
22
30
  alias_method :xx, :abort
23
31
  alias_method :qq, :abort
24
- alias_method :irb, :irt
25
32
 
26
33
  def method_missing(method, *args, &block)
27
34
  IRB.conf[:MAIN_CONTEXT] && IRB.conf[:MAIN_CONTEXT].irt_mode == :file && IRT::Directives.respond_to?(method) ?
@@ -38,5 +45,12 @@ module IRB
38
45
  end
39
46
  end
40
47
 
48
+ def ensure_cli
49
+ unless IRT.cli?
50
+ m = caller[0].match(/`(\w*)'$/).captures[0]
51
+ raise IRT::SessionModeError, ":#{m} command not available. IRT didn't start with the CLI"
52
+ end
53
+ end
54
+
41
55
  end
42
56
  end
@@ -103,7 +103,7 @@ private
103
103
  bktr = e.backtrace.reject do |m|
104
104
  workspace.filter_backtrace(m).nil? || !IRT.debug && File.expand_path(m).match(/^#{IRT.lib_path}/)
105
105
  end
106
- e.set_backtrace( e.class.name.match(/^IRT::/) ? bktr : map_backtrace(bktr) )
106
+ e.set_backtrace map_backtrace(bktr)
107
107
  end
108
108
 
109
109
  def map_backtrace(bktr)
@@ -117,7 +117,9 @@ private
117
117
  index = sprintf index_format, " [#{i}]"
118
118
  end
119
119
  mapped_bktr << "#{m}#{index}"
120
+ break if m.match /^\(irt\#\d+\)/
120
121
  end
122
+ # mapped_bktr.last << "\n" if mapped_bktr.last && !mapped_bktr.last.match(/\n$/)
121
123
  mapped_bktr
122
124
  end
123
125
 
@@ -145,7 +147,7 @@ private
145
147
  i = -1
146
148
  str.each_line do |l|
147
149
  @last_line_no = line_no + i+=1
148
- unless l =~ /^\s*(#{quoted_option_string(IRT.log.ignored_commands)})\b/
150
+ if irt_mode == :file || l !~ /^\s*(#{quoted_option_string(IRT.log.ignored_commands)})\b/
149
151
  IRT.log.add_line l, @last_line_no
150
152
  end
151
153
  end
@@ -5,22 +5,22 @@ require 'irt/extensions/irb/commands'
5
5
  module IRB #:nodoc:
6
6
 
7
7
  class << self
8
- alias_method :irb_init_config, :init_config
9
- alias_method :irb_setup, :setup
8
+ alias_method :original_init_config, :init_config
9
+ alias_method :original_setup, :setup
10
10
  end
11
11
 
12
12
  def IRB.setup(ap_path=nil)
13
- irb_setup(ap_path)
14
- IRT.before_run
13
+ original_setup(ap_path)
14
+ IRT.setup
15
15
  end
16
16
 
17
17
  def IRB.init_config(ap_path)
18
- irb_init_config(ap_path)
18
+ original_init_config(ap_path)
19
19
 
20
20
  @CONF[:AP_NAME] = 'irt'
21
21
  @CONF[:PROMPT][:IRT] = { :PROMPT_I => "%02n >> ",
22
22
  :PROMPT_S => ' "> ',
23
- :PROMPT_C => "%02n ?> ",
23
+ :PROMPT_C => "%02n .> ",
24
24
  :PROMPT_N => "%02n -> ",
25
25
  :RETURN => " => %s\n",
26
26
  :RETURN_I => " #> %s\n" }
@@ -30,12 +30,12 @@ module IRB #:nodoc:
30
30
  @CONF[:SAVE_HISTORY] = 100
31
31
  @CONF[:HISTORY_FILE] = File.expand_path '~/.irt-history'
32
32
  @CONF[:AT_EXIT] ||= []
33
- @CONF[:AT_EXIT] << proc{ IRT::Session.enter(:interactive) if IRB.CurrentContext.irt_mode == :file } if !!ENV['IRT_INTERACTIVE_EOF']
33
+ @CONF[:AT_EXIT] << proc{ IRT::Session.enter(:interactive) \
34
+ if IRB.CurrentContext.irt_mode == :file && !IRT::Session.exit_all?} \
35
+ if !!ENV['IRT_INTERACTIVE_EOF']
34
36
  @CONF[:AT_EXIT] << proc{ IRT::Directives.test_summary }
35
- @CONF[:AT_EXIT] << proc{ print "\e[0m" if Dye.color? } # reset colors
36
37
  @CONF[:RC_NAME_GENERATOR] = proc {|rc| File.expand_path '~/.irtrc' }
37
38
 
38
- IRT.init_config
39
39
  end
40
40
 
41
41
  end
@@ -12,6 +12,7 @@ module Kernel
12
12
 
13
13
  def irt(bind)
14
14
  raise IRT::ArgumentTypeError, "You must pass binding instead of #{bind.class.name} object" unless bind.is_a?(Binding)
15
+ IRT::Utils.load_irt
15
16
  IRT::Session.enter :binding, bind
16
17
  end
17
18
 
@@ -12,16 +12,22 @@ class Method
12
12
  end
13
13
  arr = Array.new(n)
14
14
  set_trace_func proc{ |event, file, line, meth_name, binding, classname|
15
- if event.eql?('call') && name.to_s.match(meth_name.to_s)
16
- f = file
17
- l = line
18
- set_trace_func nil
19
- throw :method_located
15
+ if name == meth_name
16
+ case event
17
+ when 'call'
18
+ f = file
19
+ l = line
20
+ throw :method_located
21
+ when 'c-call'
22
+ f = "(c-func)"
23
+ throw :method_located
24
+ end
20
25
  end
21
26
  }
22
27
  catch(:method_located) { call *arr }
23
- set_trace_func nil
24
28
  [f,l]
29
+ ensure
30
+ set_trace_func nil
25
31
  end
26
32
 
27
33
  def info
@@ -4,6 +4,8 @@ class ActiveSupport::BufferedLogger
4
4
 
5
5
  def add(*args)
6
6
  message = original_add(*args)
7
+ # no inline log when in rails server and not interactive mode
8
+ return message if IRB.CurrentContext.nil? || IRT.rails_server && IRB.CurrentContext.irt_mode != :interactive
7
9
  if IRT.rails_log
8
10
  if IRT.dye_rails_log
9
11
  plain_message = Dye.strip_ansi(message).chomp
@@ -16,22 +18,90 @@ class ActiveSupport::BufferedLogger
16
18
 
17
19
  end
18
20
 
21
+ module Kernel
22
+
23
+ alias_method :original_irt, :irt
24
+ def irt(bind)
25
+ raise IRT::ArgumentTypeError, "You must pass binding instead of #{bind.class.name} object" unless bind.is_a?(Binding)
26
+ IRT.send(:rails_server_notice_wrap) do
27
+ IRT::Utils.load_irt
28
+ IRT::Session.enter :binding, bind
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ require 'rack/server'
35
+ module Rack
36
+ class Server
37
+ alias_method :original_server, :server
38
+ def server
39
+ # override the SIGINT trap in the Rack::Server.start method allowing multiple choices
40
+ # since #server is also called after the Rack::Server.start trap
41
+ IRT::Utils.load_irt(false)
42
+ IRT.rails_server_sigint_trap = trap('SIGINT') { IRT.rails_signal_handle }
43
+ IRT.rails_server = original_server
44
+ end
45
+ end
46
+ end
47
+
19
48
  module IRT
49
+
50
+ def rails_signal_handle
51
+ puts
52
+ rails_server_notice_wrap do
53
+ trap('SIGINT'){}
54
+ input = IRT::Prompter.choose " [s]hutdown, [i]rt or [c]ancel?", /^(s|i|c)$/i,
55
+ :hint => '[<enter>=s|i|c]', :default => 's'
56
+ trap('SIGINT') { rails_signal_handle }
57
+ case input
58
+ when 's'
59
+ IRT.rails_server_sigint_trap.call
60
+ when 'i'
61
+ Session.enter :interactive
62
+ end
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def rails_server_notice_wrap
69
+ return yield unless rails_server
70
+ IRT::Prompter.say_notice "Server suspended"
71
+ yield
72
+ IRT::Prompter.say_notice "Server resumed"
73
+ end
74
+
75
+
76
+ module Session
77
+
78
+ alias_method :original_run_file, :run_file
79
+ def run_file(*args)
80
+ original_run_file *args
81
+ ensure
82
+ if IRT.rails_server
83
+ IRB.irb_at_exit
84
+ enter :interactive
85
+ end
86
+ end
87
+
88
+ end
89
+
20
90
  module Commands
21
91
  module Rails
22
92
 
23
- extend self
93
+ extend self # ignored_echo_commands
24
94
 
25
95
  def rails_log_on
26
96
  IRT.rails_log = true
27
- "Rails Log ON"
97
+ IRT::Prompter.say_notice "Rails Log ON"
28
98
  end
29
99
  alias_method :rlon, :rails_log_on
30
100
  alias_method :rlo, :rails_log_on
31
101
 
32
102
  def rails_log_off
33
103
  IRT.rails_log = false
34
- "Rails Log OFF"
104
+ IRT::Prompter.say_notice "Rails Log OFF"
35
105
  end
36
106
  alias_method :rloff, :rails_log_off
37
107
  alias_method :rlf, :rails_log_off
data/lib/irt/log.rb CHANGED
@@ -6,17 +6,17 @@ module IRT
6
6
 
7
7
  def initialize
8
8
  @ignored_echo_commands = FileUtils.own_methods + [:_]
9
- @ignored_echo_commands += IRT::Commands::Rails.own_methods if defined?(IRT::Commands::Rails)
10
9
  @ignored_echo_commands = @ignored_echo_commands.map(&:to_sym)
11
10
  @ignored_commands = @ignored_echo_commands +
12
11
  IRB::ExtendCommandBundle.instance_methods +
13
12
  [ :p, :pp, :ap, :y, :puts, :print, :irt, :irb ]
13
+ @ignored_commands += IRT::Commands::Rails.own_methods if defined?(IRT::Commands::Rails)
14
14
  @ignored_commands = @ignored_commands.map(&:to_sym)
15
15
  @non_setting_commands = @ignored_commands + IRT::Directives.own_methods
16
16
  @non_setting_commands = @non_setting_commands.map(&:to_sym)
17
17
  @tail_size = tail_size || 10
18
18
  self << FileHunk.new(IRT.irt_file)
19
- @status = [[File.basename(IRT.irt_file), :file]]
19
+ @status = [[IRT.irt_file.basename, :file]]
20
20
  end
21
21
 
22
22
  def add_hunk
@@ -0,0 +1,32 @@
1
+ require 'prompter'
2
+
3
+ module IRT
4
+ class Prompter
5
+
6
+ extend ::Prompter::Methods
7
+
8
+ if IRT.respond_to?(:dye_styles)
9
+ ::Prompter.dye_styles[:say_notice_style] = IRT.dye_styles[:ignored_color]
10
+ ::Prompter.dye_styles[:ask_style] = IRT.dye_styles[:interactive_color]
11
+ end
12
+
13
+ def self.say_echo(result, opts={})
14
+ if defined?(IRB)
15
+ IRB.CurrentContext.send :output_ignored_echo_value, result
16
+ else
17
+ say_notice result
18
+ end
19
+ end
20
+
21
+ def self.say_notice(message="", opts={})
22
+ opts = { :prefix => ' #> ' }.merge opts
23
+ super message, opts
24
+ end
25
+
26
+ def self.ask(prompt, opts={})
27
+ opts = { :prefix => ' ?> ' }.merge opts
28
+ super prompt, opts
29
+ end
30
+
31
+ end
32
+ end
data/lib/irt/session.rb CHANGED
@@ -2,6 +2,14 @@ module IRT
2
2
  module Session
3
3
  extend self
4
4
 
5
+ @@exit_all = false
6
+ def exit_all?
7
+ @@exit_all
8
+ end
9
+ def exit_all=(bool)
10
+ @@exit_all = bool
11
+ end
12
+
5
13
  def enter(mode, obj=nil)
6
14
  IRT.log.print if IRT.tail_on_irt
7
15
  ws = obj ? IRB::WorkSpace.new(obj) : IRB.CurrentContext.workspace
@@ -26,23 +34,51 @@ module IRT
26
34
  IRT.log.add_hunk
27
35
  IRT.log.status << [new_context.irb_name, mode]
28
36
  IRT.log.print_status unless mode == :file
37
+ old_trap = trap('SIGINT'){new_context.irb.signal_handle}
29
38
  catch(:IRB_EXIT) { new_context.irb.eval_input }
39
+ # rethrow if there is a parent context, and it is a reading file
40
+ begin throw(:IRB_EXIT) ; rescue ArgumentError ; end if @@exit_all
30
41
  ensure
31
- IRT::Session.exit
42
+ IRT::Session.exit unless @@exit_all
43
+ trap 'SIGINT', old_trap if old_trap
32
44
  end
33
45
 
34
46
  def exit
35
47
  exiting_context = IRB.conf[:MAIN_CONTEXT]
36
48
  resuming_context = exiting_context.parent_context
49
+ return unless resuming_context
37
50
  exiting_mode = exiting_context.irt_mode
38
51
  resuming_context.set_last_value( exiting_context.last_value ) \
39
52
  unless (exiting_mode == :inspect || exiting_mode == :binding)
53
+ IRB.conf[:MAIN_CONTEXT] = resuming_context
40
54
  IRT.log.pop_status
41
55
  IRT.log.print_status unless resuming_context.irt_mode == :file
42
- IRB.conf[:MAIN_CONTEXT] = resuming_context
43
56
  IRT.log.add_hunk
44
57
  end
45
58
 
59
+ def run_file(file_path=IRT.irt_file)
60
+ raise Errno::ENOENT, "No such file or directory - #{file_path}" unless File.exist?(file_path)
61
+ openfile = proc do
62
+ @@exit_all = false
63
+ IRB.conf[:AT_EXIT].pop
64
+ IRT.init_file(file_path)
65
+ irb = IRB::Irb.new(nil, IRT.irt_file.to_s)
66
+ irb.context.irt_mode = :file
67
+ irb.context.irb_path = IRT.irt_file.to_s
68
+ irb.context.irb_name = IRT.irt_file.basename
69
+ IRB.conf[:MAIN_CONTEXT] = irb.context
70
+ begin
71
+ catch(:IRB_EXIT) { irb.eval_input }
72
+ ensure
73
+ IRB.irb_at_exit
74
+ end
75
+ end
76
+ IRB.conf[:AT_EXIT].push(openfile)
77
+ @@exit_all = true
78
+ throw(:IRB_EXIT)
79
+ end
80
+
81
+
46
82
  private
47
83
 
48
84
  # used for open the last file for editing
data/lib/irt/utils.rb CHANGED
@@ -1,21 +1,62 @@
1
+ require 'irt/prompter'
2
+
1
3
  module IRT
2
4
  module Utils
3
5
 
4
6
  extend self
5
7
 
6
- def save_as(file, as_file_local, prompter)
8
+ # this will create a tmp file and start IRB
9
+ # but it will be left in file mode at EOF (sort of irt-standby)
10
+ def load_irt(run=true)
11
+ return if IRT.initialized
12
+ puts IRT::Utils.copyright
13
+ ARGV.clear
14
+ tmp_path = create_tmp_file run
15
+ ARGV.push tmp_path
16
+ IRB.start
17
+ end
18
+
19
+ def create_tmp_file(run=true)
20
+ require 'tempfile'
21
+ tmp_file = Tempfile.new(['', '.irt'])
22
+ tmp_file << "\n" # one empty line makes irb of 1.9.2 happy
23
+ tmp_file.flush
24
+ tmp_path = tmp_file.path
25
+ at_exit { check_save_tmp_file(tmp_file, run) }
26
+ tmp_path
27
+ end
28
+
29
+ def save_as(as_file_local, run=true)
7
30
  as_file = File.expand_path(as_file_local)
8
31
  if File.exists?(as_file)
9
- return false if prompter.no? %(Do you want to overwrite "#{as_file}"?), :hint => '[y|<enter=n]', :default => 'n'
32
+ return false if IRT::Prompter.no? %(Do you want to overwrite "#{as_file}"?), :hint => '[y|<enter=n]', :default => 'n'
10
33
  end
11
34
  dirname = File.dirname(as_file)
12
35
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
13
- FileUtils.cp file, as_file
14
- if prompter.yes? %(Do you want to run the file "#{as_file_local}" now?)
15
- ENV['IRT_COMMAND'] = ENV['IRT_COMMAND'].sub(/#{Regexp.quote(file)}/, as_file)
16
- yield
36
+ FileUtils.cp IRT.irt_file, as_file
37
+ if run && IRT::Prompter.yes?( %(Do you want to run the file "#{as_file_local}" now?) )
38
+ IRT::Session.run_file as_file
39
+ end
40
+ end
41
+
42
+ def version
43
+ @version ||= File.read(File.expand_path('../../../VERSION', __FILE__)).strip
44
+ end
45
+
46
+ def copyright
47
+ @copyrignt ||= Dye.dye "irt #{version} (c) 2010-2011 Domizio Demichelis", :blue, :bold
48
+ end
49
+
50
+ private
51
+
52
+ def check_save_tmp_file(tmp_file, run)
53
+ if tmp_file.size > 1
54
+ IRT::Prompter.yes? %(The template file has been modified, do you want to save it?) do
55
+ IRT::Prompter.choose %(Enter the file path to save:), /[\w0-9_]/ do |as_file|
56
+ save_as(as_file, run)
57
+ end
58
+ end
17
59
  end
18
- true
19
60
  end
20
61
 
21
62
  end
data/lib/irt.rb CHANGED
@@ -1,3 +1,5 @@
1
+ at_exit{ Dye.print_reset_colors }
2
+
1
3
  require 'rubygems'
2
4
 
3
5
  require 'pp'
@@ -16,6 +18,7 @@ require 'irt/differ'
16
18
  require 'irt/directives'
17
19
  require 'irt/session'
18
20
  require 'irt/ruby_version'
21
+ require 'irt/utils'
19
22
 
20
23
  module IRT
21
24
 
@@ -41,18 +44,21 @@ module IRT
41
44
 
42
45
  extend self
43
46
 
44
- attr_accessor :irt_on_diffs, :tail_on_irt, :fix_readline_prompt, :debug, :rails_log, :dye_rails_log,
45
- :full_exit, :exception_raised, :session_no, :autoload_helper_files, :dye_styles,
47
+ attr_accessor :irt_on_diffs, :tail_on_irt, :fix_readline_prompt, :debug,
48
+ :rails_log, :dye_rails_log, :rails_server, :rails_server_sigint_trap,
49
+ :full_exit, :session_no, :autoload_helper_files, :dye_styles,
46
50
  :copy_to_clipboard_command, :nano_command_format, :vi_command_format, :edit_command_format, :ri_command_format
47
- attr_reader :log, :irt_file, :differ
51
+ attr_reader :log, :irt_file, :initialized
52
+
53
+ def cli?
54
+ !!ENV['IRT_COMMAND']
55
+ end
48
56
 
49
57
  def force_color=(bool)
50
58
  Dye.color = bool
51
59
  end
52
60
 
53
61
  def init_config
54
- @session_no = 0
55
- @differ = IRT::Differ
56
62
  @irt_on_diffs = true
57
63
  @tail_on_irt = false
58
64
  @fix_readline_prompt = false
@@ -97,12 +103,7 @@ module IRT
97
103
  @debug = false
98
104
  end
99
105
 
100
- def before_run
101
- @irt_file = IRB.conf[:SCRIPT]
102
- require 'irt/extensions/rails' if defined?(ActiveSupport::BufferedLogger)
103
- @log = Log.new
104
- @log.print_running_file
105
- IRT::Directives.load_helper_files
106
+ def setup
106
107
  IRB::ExtendCommandBundle.class_eval do
107
108
  [:p, :y, :pp, :ap].each do |m|
108
109
  next unless begin
@@ -115,6 +116,17 @@ module IRT
115
116
  end
116
117
  end
117
118
  end
119
+ @initialized = true
120
+ init_file
121
+ end
122
+
123
+ def init_file(file = nil)
124
+ @session_no = 0
125
+ irt_file = file.nil? ? IRB.conf[:SCRIPT] : (IRB.conf[:SCRIPT] = file)
126
+ @irt_file = Pathname.new(irt_file).realpath
127
+ @log = Log.new
128
+ IRT::Directives.load_helper_files
129
+ @log.print_running_file
118
130
  end
119
131
 
120
132
  def lib_path
@@ -128,15 +140,8 @@ module IRT
128
140
  yml.gsub(/ +\n/, "\n")
129
141
  end
130
142
 
131
- def prompter
132
- @prompter ||= begin
133
- require 'prompter'
134
- Prompter.new do |pr|
135
- def pr.say_echo(result, opts={})
136
- IRB.CurrentContext.send :output_ignored_echo_value, result
137
- end
138
- end
139
- end
140
- end
141
-
142
143
  end
144
+
145
+ IRT.init_config
146
+ require 'irt/prompter'
147
+ require 'irt/extensions/rails' if defined?(ActiveSupport::BufferedLogger)
metadata CHANGED
@@ -1,8 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irt
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 1.1.7
4
+ hash: 957451098
5
+ prerelease: true
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 0
10
+ - p01
11
+ version: 1.2.0.p01
6
12
  platform: ruby
7
13
  authors:
8
14
  - Domizio Demichelis
@@ -10,7 +16,7 @@ autorequire:
10
16
  bindir: bin
11
17
  cert_chain: []
12
18
 
13
- date: 2011-02-10 00:00:00 -04:00
19
+ date: 2011-02-17 00:00:00 -04:00
14
20
  default_executable:
15
21
  dependencies:
16
22
  - !ruby/object:Gem::Dependency
@@ -21,6 +27,11 @@ dependencies:
21
27
  requirements:
22
28
  - - ">="
23
29
  - !ruby/object:Gem::Version
30
+ hash: 25
31
+ segments:
32
+ - 0
33
+ - 1
34
+ - 1
24
35
  version: 0.1.1
25
36
  type: :runtime
26
37
  version_requirements: *id001
@@ -32,7 +43,12 @@ dependencies:
32
43
  requirements:
33
44
  - - ">="
34
45
  - !ruby/object:Gem::Version
35
- version: 0.1.1
46
+ hash: 29
47
+ segments:
48
+ - 0
49
+ - 1
50
+ - 3
51
+ version: 0.1.3
36
52
  type: :runtime
37
53
  version_requirements: *id002
38
54
  - !ruby/object:Gem::Dependency
@@ -43,18 +59,29 @@ dependencies:
43
59
  requirements:
44
60
  - - ">="
45
61
  - !ruby/object:Gem::Version
46
- version: 0.1.2
62
+ hash: 19
63
+ segments:
64
+ - 0
65
+ - 1
66
+ - 4
67
+ version: 0.1.4
47
68
  type: :runtime
48
69
  version_requirements: *id003
49
70
  - !ruby/object:Gem::Dependency
50
- name: bri
71
+ name: fastri
51
72
  prerelease: false
52
73
  requirement: &id004 !ruby/object:Gem::Requirement
53
74
  none: false
54
75
  requirements:
55
76
  - - ">="
56
77
  - !ruby/object:Gem::Version
57
- version: 0.1.5
78
+ hash: 81
79
+ segments:
80
+ - 0
81
+ - 3
82
+ - 1
83
+ - 1
84
+ version: 0.3.1.1
58
85
  type: :runtime
59
86
  version_requirements: *id004
60
87
  description: If you use IRT in place of irb or rails console, you will have more tools that will make your life a lot easier.
@@ -101,6 +128,7 @@ files:
101
128
  - lib/irt/hunks.rb
102
129
  - lib/irt/init.rb
103
130
  - lib/irt/log.rb
131
+ - lib/irt/prompter.rb
104
132
  - lib/irt/ruby_version.rb
105
133
  - lib/irt/session.rb
106
134
  - lib/irt/utils.rb
@@ -118,17 +146,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
146
  requirements:
119
147
  - - ">="
120
148
  - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
121
152
  version: "0"
122
153
  required_rubygems_version: !ruby/object:Gem::Requirement
123
154
  none: false
124
155
  requirements:
125
156
  - - ">="
126
157
  - !ruby/object:Gem::Version
158
+ hash: 23
159
+ segments:
160
+ - 1
161
+ - 3
162
+ - 6
127
163
  version: 1.3.6
128
164
  requirements: []
129
165
 
130
166
  rubyforge_project:
131
- rubygems_version: 1.5.0
167
+ rubygems_version: 1.3.7
132
168
  signing_key:
133
169
  specification_version: 3
134
170
  summary: Interactive Ruby Tools - Improved irb and rails console with a lot of easy and powerful tools.