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) ⇒ Server

Returns a new instance of Server.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nrepl-lazuli/server.rb', line 24

def initialize(port: DEFAULT_PORT, host: DEFAULT_HOST, debug: false, binding: nil, pwd: Dir.pwd)
  @port  = port
  @pwd = pwd
  @host  = host
  @debug = debug
  @connections = Set.new
  @binding = binding
  @bindings = {}
  NREPL.class_variable_set(:@@connections, @connections)
  NREPL.class_variable_set(:@@bindings, @bindings)
end

Instance Attribute Details

#debugObject (readonly) Also known as: debug?

Returns the value of attribute debug.



13
14
15
# File 'lib/nrepl-lazuli/server.rb', line 13

def debug
  @debug
end

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/nrepl-lazuli/server.rb', line 13

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/nrepl-lazuli/server.rb', line 13

def port
  @port
end

Class Method Details

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



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

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

.start(**kwargs) ⇒ Object



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

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

Instance Method Details

#auto_create_bindings!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nrepl-lazuli/server.rb', line 67

def auto_create_bindings!
  dir_regex = Regexp.new("^#{Regexp.escape(@pwd)}")
  @call_trace = TracePoint.new(:call) do |tp|
    @bindings[tp.path] ||= {}
    @bindings[tp.path][tp.lineno-1] = {binding: tp.binding, id: "#{tp.path}:#{tp.lineno}"}
    if tp.path =~ dir_regex
      @connections.each do |connection|
        connection.send_msg(
          'op' => 'hit_auto_watch',
          'file' => tp.path,
          'line' => tp.lineno-1,
          'status' => ['done']
        )
      end
    end
  end
  @call_trace.enable

  # @ex_trace = TracePoint.new(:raise) do |tp|
  #   $foo = [tp.lineno, tp.event, tp.raised_exception, tp.binding, caller_locations]
  #   # p $foo
  # end
  # @ex_trace.enable
end

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nrepl-lazuli/server.rb', line 42

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

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

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

  s = TCPServer.new(host, port)
  loop do
    Thread.start(s.accept) do |client|
      connection = Connection.new(client, debug: debug?, binding: @binding, bindings: @bindings)
      @connections << connection
      connection.treat_messages!
      @connections.delete(connection)
    end
  end
ensure
  File.unlink(PORT_FILENAME)
end

#stopObject



92
93
94
95
# File 'lib/nrepl-lazuli/server.rb', line 92

def stop
  Thread.exit
  exit(0)
end