class Rpush::CLI

Public Class Methods

default_config_path() click to toggle source
# File lib/rpush/cli.rb, line 12
def self.default_config_path
  detect_rails? ? 'config/initializers/rpush.rb' : 'config/rpush.rb'
end
detect_rails?() click to toggle source
# File lib/rpush/cli.rb, line 8
def self.detect_rails?
  ['bin/rails', 'script/rails'].any? { |path| File.exist?(path) }
end

Public Instance Methods

init() click to toggle source
# File lib/rpush/cli.rb, line 54
def init
  underscore_option_names
  check_ruby_version
  require 'rails/generators'

  puts "* " + ANSI.green { 'Installing config...' }
  $RPUSH_CONFIG_PATH = default_config_path # rubocop:disable Style/GlobalVars
  Rails::Generators.invoke('rpush_config')

  install_migrations = options['active_record']

  unless options.key?('active_record')
    has_answer = false
    until has_answer
      STDOUT.write "\n* #{ANSI.green { 'Install ActiveRecord migrations?' }} [y/n]: "
      STDOUT.flush
      answer = STDIN.gets.chomp.downcase
      has_answer = %w(y n).include?(answer)
    end

    install_migrations = answer == 'y'
  end

  Rails::Generators.invoke('rpush_migration', ['--force']) if install_migrations

  puts "\n* #{ANSI.green { 'Next steps:' }}"
  puts "  - Run 'bundle exec rake db:migrate'." if install_migrations
  puts "  - Review and update your configuration in #{default_config_path}."
  puts "  - Create your first app, see https://github.com/rpush/rpush for examples."
  puts "  - Run 'rpush help' for commands and options."
end
push() click to toggle source
# File lib/rpush/cli.rb, line 87
def push
  config_setup
  Rpush.config.foreground = true

  Rpush.push
end
start() click to toggle source
# File lib/rpush/cli.rb, line 22
def start
  config_setup

  require 'rpush/daemon'
  Rpush::Daemon.start
end
status() click to toggle source
# File lib/rpush/cli.rb, line 95
def status
  config_setup

  require 'rpush/daemon'
  rpc = Rpush::Daemon::Rpc::Client.new(rpush_process_pid)
  status = rpc.status
  rpc.close
  puts humanize_json(status)
end
stop() click to toggle source
# File lib/rpush/cli.rb, line 31
def stop
  config_setup
  pid = rpush_process_pid
  return unless pid

  STDOUT.write "* Stopping Rpush (pid #{pid})... "
  STDOUT.flush
  Process.kill('TERM', pid)

  loop do
    begin
      Process.getpgid(pid)
      sleep 0.05
    rescue Errno::ESRCH
      break
    end
  end

  puts ANSI.green { '✔' }
end
version() click to toggle source
# File lib/rpush/cli.rb, line 106
def version
  puts Rpush::VERSION
end

Private Instance Methods

check_ruby_version() click to toggle source
# File lib/rpush/cli.rb, line 155
def check_ruby_version
  STDERR.puts(ANSI.yellow { 'WARNING: ' } + "You are using an old and unsupported version of Ruby.") if RUBY_VERSION < '2.2.2' && RUBY_ENGINE == 'ruby'
end
config_setup() click to toggle source
# File lib/rpush/cli.rb, line 112
def config_setup
  underscore_option_names
  check_ruby_version
  configure_rpush
end
configure_rpush() click to toggle source
# File lib/rpush/cli.rb, line 118
def configure_rpush
  load_rails_environment || load_standalone
end
default_config_path() click to toggle source
# File lib/rpush/cli.rb, line 151
def default_config_path
  self.class.default_config_path
end
detect_rails?() click to toggle source
# File lib/rpush/cli.rb, line 147
def detect_rails?
  self.class.detect_rails?
end
humanize_json(node, str = '', depth = 0) click to toggle source
# File lib/rpush/cli.rb, line 190
def humanize_json(node, str = '', depth = 0) # rubocop:disable Metrics/PerceivedComplexity
  if node.is_a?(Hash)
    node = node.sort_by { |_, v| [Array, Hash].include?(v.class) ? 1 : 0 }
    node.each do |k, v|
      if [Array, Hash].include?(v.class)
        str << "\n#{'  ' * depth}#{k}:\n"
        humanize_json(v, str, depth + 1)
      else
        str << "#{'  ' * depth}#{k}: #{v}\n"
      end
    end
  elsif node.is_a?(Array)
    node.each do |v|
      str << "\n" if v.is_a?(Hash)
      humanize_json(v, str, depth)
    end
  else
    str << "#{'  ' * depth}#{node}\n"
  end

  str
end
load_rails_environment() click to toggle source
# File lib/rpush/cli.rb, line 122
def load_rails_environment
  if detect_rails? && options['rails_env']
    STDOUT.write "* Booting Rails '#{options[:rails_env]}' environment... "
    STDOUT.flush
    ENV['RAILS_ENV'] = options['rails_env']
    load 'config/environment.rb'
    Rpush.config.update(options)
    puts ANSI.green { '✔' }

    return true
  end

  false
end
load_standalone() click to toggle source
# File lib/rpush/cli.rb, line 137
def load_standalone
  if !File.exist?(options[:config])
    STDERR.puts(ANSI.red { 'ERROR: ' } + "#{options[:config]} does not exist. Please run 'rpush init' to generate it or specify the --config option.")
    exit 1
  else
    load options[:config]
    Rpush.config.update(options)
  end
end
rpush_process_pid() click to toggle source
# File lib/rpush/cli.rb, line 176
def rpush_process_pid
  if Rpush.config.pid_file.blank?
    STDERR.puts(ANSI.red { 'ERROR: ' } + 'config.pid_file is not set.')
    exit 1
  end

  unless File.exist?(Rpush.config.pid_file)
    STDERR.puts("* Rpush isn't running? #{Rpush.config.pid_file} does not exist.")
    exit 1
  end

  File.read(Rpush.config.pid_file).strip.to_i
end
underscore_option_names() click to toggle source
# File lib/rpush/cli.rb, line 159
def underscore_option_names
  # Underscore option names so that they map directly to Configuration options.
  new_options = options.dup

  options.each do |k, v|
    new_k = k.to_s.tr('-', '_')

    if k != new_k
      new_options.delete(k)
      new_options[new_k] = v
    end
  end

  new_options.freeze
  self.options = new_options
end