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 require 'rails/generators' puts "* " + Rainbow('Installing config...').green $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') answer = ask("\n* #{Rainbow('Install ActiveRecord migrations?').green}", limited_to: %w[y n]) install_migrations = answer == 'y' end Rails::Generators.invoke('rpush_migration', ['--force']) if install_migrations puts "\n* #{Rainbow('Next steps:').green}" 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 79 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 87 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 Rainbow('✔').green end
version()
click to toggle source
# File lib/rpush/cli.rb, line 98 def version puts Rpush::VERSION end
Private Instance Methods
config_setup()
click to toggle source
# File lib/rpush/cli.rb, line 104 def config_setup underscore_option_names configure_rpush end
configure_rpush()
click to toggle source
# File lib/rpush/cli.rb, line 109 def configure_rpush load_rails_environment || load_standalone end
default_config_path()
click to toggle source
# File lib/rpush/cli.rb, line 142 def default_config_path self.class.default_config_path end
detect_rails?()
click to toggle source
# File lib/rpush/cli.rb, line 138 def detect_rails? self.class.detect_rails? end
humanize_json(node, str = '', depth = 0)
click to toggle source
# File lib/rpush/cli.rb, line 177 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 113 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 Rainbow('✔').green return true end false end
load_standalone()
click to toggle source
# File lib/rpush/cli.rb, line 128 def load_standalone if !File.exist?(options[:config]) STDERR.puts(Rainbow('ERROR: ').red + "#{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 163 def rpush_process_pid if Rpush.config.pid_file.blank? STDERR.puts(Rainbow('ERROR: ').red + '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 146 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