class RuboCop::Server::CLI

The CLI is a class responsible of handling server command line interface logic. @api private

Constants

EXCLUSIVE_OPTIONS
SERVER_OPTIONS
STATUS_ERROR
STATUS_SUCCESS

Same exit status value as `RuboCop::CLI`.

Public Class Methods

new() click to toggle source
# File lib/rubocop/server/cli.rb, line 30
def initialize
  @exit = false
end

Public Instance Methods

exit?() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# File lib/rubocop/server/cli.rb, line 66
def exit?
  @exit
end
run(argv = ARGV) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# File lib/rubocop/server/cli.rb, line 35
def run(argv = ARGV)
  unless Server.support_server?
    return error('RuboCop server is not supported by this Ruby.') if use_server_option?(argv)

    return STATUS_SUCCESS
  end

  Cache.cache_root_path = fetch_cache_root_path_from(argv)
  deleted_server_arguments = delete_server_argument_from(argv)

  if deleted_server_arguments.size >= 2
    return error("#{deleted_server_arguments.join(', ')} cannot be specified together.")
  end

  server_command = deleted_server_arguments.first

  if EXCLUSIVE_OPTIONS.include?(server_command) && argv.count > allowed_option_count
    return error("#{server_command} cannot be combined with other options.")
  end

  if server_command.nil?
    server_command = ArgumentsEnv.read_as_arguments.delete('--server') ||
                     ArgumentsFile.read_as_arguments.delete('--server')
  end

  run_command(server_command)

  STATUS_SUCCESS
end

Private Instance Methods

allowed_option_count() click to toggle source
# File lib/rubocop/server/cli.rb, line 116
def allowed_option_count
  Cache.cache_root_path ? 2 : 1
end
delete_server_argument_from(all_arguments) click to toggle source
# File lib/rubocop/server/cli.rb, line 106
def delete_server_argument_from(all_arguments)
  SERVER_OPTIONS.each_with_object([]) do |server_option, server_arguments|
    server_arguments << all_arguments.delete(server_option)
  end.compact
end
error(message) click to toggle source
# File lib/rubocop/server/cli.rb, line 120
def error(message)
  @exit = true
  warn Rainbow(message).red

  STATUS_ERROR
end
fetch_cache_root_path_from(arguments) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity, Metrics/MethodLength:

# File lib/rubocop/server/cli.rb, line 95
def fetch_cache_root_path_from(arguments)
  cache_root = arguments.detect { |argument| argument.start_with?('--cache-root') }
  return unless cache_root

  if cache_root.start_with?('--cache-root=')
    cache_root.split('=')[1]
  else
    arguments[arguments.index(cache_root) + 1]
  end
end
run_command(server_command) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength:

# File lib/rubocop/server/cli.rb, line 73
def run_command(server_command)
  case server_command
  when '--server'
    Server::ClientCommand::Start.new.run unless Server.running?
  when '--no-server'
    Server::ClientCommand::Stop.new.run if Server.running?
  when '--restart-server'
    @exit = true
    Server::ClientCommand::Restart.new.run
  when '--start-server'
    @exit = true
    Server::ClientCommand::Start.new.run
  when '--stop-server'
    @exit = true
    Server::ClientCommand::Stop.new.run
  when '--server-status'
    @exit = true
    Server::ClientCommand::Status.new.run
  end
end
use_server_option?(argv) click to toggle source
# File lib/rubocop/server/cli.rb, line 112
def use_server_option?(argv)
  (argv & SERVER_OPTIONS).any?
end