class RuboCop::Server::Core
The core of server process. It starts TCP server and perform socket communication. @api private
Constants
- JSON_FORMATS
Public Class Methods
Source
# File lib/rubocop/server/core.rb, line 23 def self.token @token ||= SecureRandom.hex(4) end
Public Instance Methods
Source
# File lib/rubocop/server/core.rb, line 31 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
Private Instance Methods
Source
# File lib/rubocop/server/core.rb, line 44 def detach_server write_port_and_token_files pid = fork do if defined?(RubyVM::YJIT.enable) RubyVM::YJIT.enable end Process.daemon(true) $stderr.reopen(Cache.stderr_path, 'w') process_input end Process.waitpid(pid) end
Source
# File lib/rubocop/server/core.rb, line 64 def process_input Cache.write_pid_file do read_socket(@server.accept) until @server.closed? end end
Source
# File lib/rubocop/server/core.rb, line 90 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
Source
# File lib/rubocop/server/core.rb, line 70 def run_server write_port_and_token_files process_input end
Source
# File lib/rubocop/server/core.rb, line 79 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
Source
# File lib/rubocop/server/core.rb, line 106 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
Source
# File lib/rubocop/server/core.rb, line 60 def write_port_and_token_files Cache.write_port_and_token_files(port: @server.addr[1], token: token) end