Class: Rpush::CLI

Inherits:
Thor
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/rpush/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config_pathObject



14
15
16
# File 'lib/rpush/cli.rb', line 14

def self.default_config_path
  detect_rails? ? 'config/initializers/rpush.rb' : 'config/rpush.rb'
end

.detect_rails?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/rpush/cli.rb', line 10

def self.detect_rails?
  ['bin/rails', 'script/rails'].any? { |path| File.exist?(path) }
end

Instance Method Details

#initObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rpush/cli.rb', line 71

def init
  check_ruby_version
  require 'rails/generators'

  puts "* #{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* #{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') if install_migrations

  puts "\n* #{green('Next steps:')}"
  puts "  - Run '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

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rpush/cli.rb', line 24

def start
  check_ruby_version

  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'
    puts green('')
  end

  load_config
  require 'rpush/daemon'
  Rpush::Daemon.start
end

#stopObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rpush/cli.rb', line 42

def stop
  check_ruby_version
  load_config
  ensure_pid_file

  if File.exist?(Rpush.config.pid_file)
    pid = File.read(Rpush.config.pid_file).strip.to_i
    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 green('')
  else
    STDERR.puts("* Rpush isn't running? #{Rpush.config.pid_file} does not exist.")
    return
  end
end