Class: NREPL::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/nrepl-lazuli/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, debug: false, binding: nil, pwd: Dir.pwd, tracing: true, loader: nil) ⇒ Server

Returns a new instance of Server.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nrepl-lazuli/server.rb', line 34

def initialize(
  port: DEFAULT_PORT,
  host: DEFAULT_HOST,
  debug: false,
  binding: nil,
  pwd: Dir.pwd,
  tracing: true,
  loader: nil
)
  @port  = port
  $pwd = pwd
  @host  = host
  @debug = debug
  @connections = Set.new
  @binding = binding
  loc = caller_locations.reject { |x| x.absolute_path =~ /nrepl\-lazuli/ }[0]
  loc = caller_locations[0] if(!loc)
  path = loc.absolute_path
  line = loc.lineno - 1
  @bindings = {path => {line => {"NREPL::Server.start" => binding}}}
  @bindings_by_id = {
    "NREPL::Server.start" => {
      binding: binding,
      file: path,
      row: line
    }
  }
  @tracing = tracing
  @loader = loader
  Thread.current[:nrepl_server] = self
  NREPL.class_variable_set(:@@connections, @connections)
  NREPL.class_variable_set(:@@bindings, @bindings)
  NREPL.class_variable_set(:@@bindings_by_id, @bindings_by_id)
end

Instance Attribute Details

#call_traceObject (readonly)

Returns the value of attribute call_trace.



15
16
17
# File 'lib/nrepl-lazuli/server.rb', line 15

def call_trace
  @call_trace
end

#debugObject (readonly) Also known as: debug?

Returns the value of attribute debug.



15
16
17
# File 'lib/nrepl-lazuli/server.rb', line 15

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/nrepl-lazuli/server.rb', line 15

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



15
16
17
# File 'lib/nrepl-lazuli/server.rb', line 15

def port
  @port
end

Class Method Details

.bind!(binding, **kwargs) ⇒ Object



30
31
32
# File 'lib/nrepl-lazuli/server.rb', line 30

def self.bind!(binding, **kwargs)
  new(**kwargs.merge(binding: binding)).start
end

.spawn(args = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/nrepl-lazuli/server.rb', line 18

def self.spawn(args = {})
  t = Thread.new {
    new(**args).start
  }
  sleep 0.1
  t[:nrepl_server]
end

.start(**kwargs) ⇒ Object



26
27
28
# File 'lib/nrepl-lazuli/server.rb', line 26

def self.start(**kwargs)
  new(**kwargs).start
end

Instance Method Details

#auto_create_bindings!Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/nrepl-lazuli/server.rb', line 111

def auto_create_bindings!
  dir_regex = Regexp.new("^#{Regexp.escape($pwd)}")
  trace_proc = proc do |tp|
    path = tp.path
    next if tp.path =~ /^(\<|.eval)/
    path = File.join($pwd, path) if File.dirname(path) == '.'
    id = "#{path}:#{tp.lineno}"
    b = tp.binding
    @bindings_by_id[id] = {binding: b, file: path, row: tp.lineno-1}
    @bindings[path] ||= {}
    @bindings[path][tp.lineno-1] ||= {}
    @bindings[path][tp.lineno-1][id] = b
    path
  end
  @class_trace = TracePoint.new(:class, &trace_proc)
  @class_trace.enable

  @call_trace = TracePoint.new(:call) do |tp|
    path = trace_proc.call(tp)
    if path =~ dir_regex
      @connections.each do |connection|
        connection.send_msg(
          'op' => 'hit_auto_watch',
          'file' => path,
          'line' => tp.lineno-1,
          'status' => ['done']
        )
      end
    end
  end
  @call_trace.enable if @tracing

  if @loader
    need_to_pause = true
    original_trace = nil
    @loader.on_load do
      next unless need_to_pause
      need_to_pause = false
      original_trace = call_trace.enabled?
      @call_trace.disable
      Thread.new do
        sleep 5
        @call_trace.enable if original_trace
        need_to_pause = true
      end
    end
  end

  @ex_trace = TracePoint.new(:raise) do |tp|
    e = tp.raised_exception
    @@last_exception = [:error, e.inspect.sub(/\>$/, ' stack=' + e.backtrace.inspect+'>')]
  end
  @ex_trace.enable
end

#startObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nrepl-lazuli/server.rb', line 75

def start
  puts "nREPL server started on port #{port} on host #{host} - nrepl://#{host}:#{port}"
  puts "Running in debug mode" if debug?
  record_port

  @old_out, @old_err = $stdout, $stderr
  $stdout = FakeStdout.new(@connections, STDOUT, "out")
  $stderr = FakeStdout.new(@connections, STDERR, "err")
  auto_create_bindings!

  Signal.trap("INT") { stop }
  Signal.trap("TERM") { stop }

  @socket = TCPServer.new(host, port)
  loop do
    break if @socket.closed?
    Thread.start(@socket.accept) do |client|
      connection = Connection.new(
        client,
        server: self,
        debug: @debug,
        binding: @binding,
        bindings_by_id: @bindings_by_id,
        bindings: @bindings
      )
      @connections << connection
      connection.treat_messages!
      @connections.delete(connection)
    end
  end
rescue IOError
  puts "Server closed" if debug?
ensure
  File.unlink(PORT_FILENAME)
end

#stopObject



166
167
168
169
170
# File 'lib/nrepl-lazuli/server.rb', line 166

def stop
  @connections = []
  $stdout, $stderr = @old_out, @old_err
  @socket.close
end