123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Server::Core Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/server/core.rb

Overview

The core of server process. It starts TCP server and perform socket communication.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Class Method Details

.token

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 22

def self.token
  @token ||= SecureRandom.hex(4)
end

Instance Attribute Details

#server_mode?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 70

def server_mode?
  true
end

#use_json_format?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 101

def use_json_format?
  return true if ARGV.include?('--format=json') || ARGV.include?('--format=j')
  return false unless (index = ARGV.index('--format') || ARGV.index('-f'))

  format = ARGV[index + 1]

  JSON_FORMATS.include?(format)
end

Instance Method Details

#detach_server (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 43

def detach_server
  write_port_and_token_files

  pid = fork do
    Process.daemon(true)
    $stderr.reopen(Cache.stderr_path, 'w')
    process_input
  end

  Process.waitpid(pid)
end

#process_input (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 59

def process_input
  Cache.write_pid_file do
    read_socket(@server.accept) until @server.closed?
  end
end

#read_socket(socket) (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 85

def read_socket(socket)
  SocketReader.new(socket).read!
rescue InvalidTokenError
  socket.puts 'token is not valid.'
rescue ServerStopRequest
  @server.close
rescue UnknownServerCommandError => e
  socket.puts e.message
rescue Errno::EPIPE => e
  warn e.inspect
rescue StandardError => e
  socket.puts e.full_message
ensure
  socket.close
end

#run_server (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 65

def run_server
  write_port_and_token_files
  process_input
end

#start(host, port, detach: true)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 30

def start(host, port, detach: true)
  $PROGRAM_NAME = "rubocop --server #{Cache.project_dir}"

  require_relative '../../rubocop'
  start_server(host, port)

  return unless server_mode?

  detach ? detach_server : run_server
end

#start_server(host, port) (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 74

def start_server(host, port)
  @server = TCPServer.open(host, port)

  # JSON format does not expected output message when IDE integration with server mode.
  # See: https://github.com/rubocop/rubocop/issues/11164
  return if use_json_format?

  output_stream = ARGV.include?('--stderr') ? $stderr : $stdout
  output_stream.puts "RuboCop server starting on #{@server.addr[3]}:#{@server.addr[1]}."
end

#token

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 26

def token
  self.class.token
end

#write_port_and_token_files (private)

[ GitHub ]

  
# File 'lib/rubocop/server/core.rb', line 55

def write_port_and_token_files
  Cache.write_port_and_token_files(port: @server.addr[1], token: token)
end