Class: Gnome::Wallpaper::Changer::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/gnome-wallpaper-changer/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



7
8
9
# File 'lib/gnome-wallpaper-changer/runner.rb', line 7

def initialize argv
  @argv = argv
end

Instance Method Details

#parse_optionsObject



69
70
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
101
102
103
104
105
# File 'lib/gnome-wallpaper-changer/runner.rb', line 69

def parse_options
  @options = { }
  OptionParser.new do |opt|
    opt.banner = "Usage: gnome-wallpaper-changer [options]"

    opt.on "-f", "--foreground", "Do not detach from the console" do
      @options[:foreground] = true
    end

    opt.on "-r", "--reset", "Reset the configuration" do
      @options[:reset] = true
    end

    opt.on "-p", "--port N", Integer, "Change and remember the HTTP port", "(default: 12345)" do |port|
      @options[:port] = port
    end

    opt.on "-k", "--kill", "Kill a running instance of the changer and exit" do
      @options[:kill] = true
    end

    opt.on "--autostart", "Internal use" do
      @options[:autostart] = true
    end

    opt.on_tail "-h", "--help", "Prints this message" do
      puts opt
      exit
    end

    opt.on_tail "-v", "--version", "Prints the program version" do
      puts "gnome-wallpaper-changer #{VERSION}"
      exit
    end

  end.parse! @argv
end

#run!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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/gnome-wallpaper-changer/runner.rb', line 11

def run!

  parse_options

  if @options[:reset]
    Configuration.reset!
  end

  if @options[:kill] || @options[:autostart]
    begin
      Thin::Server.kill PID_FILE, 0 if File.exists?( PID_FILE )
    rescue Errno::ESRCH, Errno::EPERM => e
      #noop
    end
    FileUtils.rm PID_FILE if File.exists?( PID_FILE )
    exit if @options[:kill]
  end

  Configuration.load

  if @options[:port]
    Configuration.port = @options[:port]
  end

  if $stdin.isatty
    puts "Go to http://localhost:#{Configuration.port} to change the settings."
  end

  EM.next_tick do
    if Configuration.active?
      Updater.update
    end
  end

  server = Thin::Server.new '127.0.0.1', Configuration.port do
    use Rack::CommonLogger
    use Reloader
    run Controller
  end
  
  if @options[:foreground]
    #noop
  elsif @options[:autostart]
    File.open PID_FILE, "w" do |io|
      io.puts Process.pid
    end
    FileUtils.rm LOG_FILE if File.exists?( LOG_FILE )
    Daemonize.redirect_io LOG_FILE
  else
    FileUtils.rm LOG_FILE if File.exists?( LOG_FILE )
    server.pid_file = PID_FILE
    server.log_file = LOG_FILE
    server.daemonize
  end
  server.start

end