Class: Eggplant::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/eggplant/cli.rb

Instance Method Summary collapse

Instance Method Details

#build(html_file = './index.html') ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/eggplant/cli.rb', line 36

def build html_file='./index.html'
  path = File.expand_path '.'
  html_file = File.expand_path html_file
  puts "Build presentation in #{path}"
  Eggplant::Server.configure path, options[:ui]
  File.unlink html_file if File.exists?(html_file)
  Eggplant::Server.build_html html_file
end

#herokuObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/eggplant/cli.rb', line 101

def heroku
  path = File.expand_path '.'
  config_path = File.join path, 'config.ru'
  gemfile_path = File.join path, 'Gemfile'
  File.unlink(config_path) if File.exists?(config_path)
  File.open(config_path, 'w') do |f|
    f << "require 'eggplant/server'\n"
    f << "Eggplant::Server.configure File.expand_path('.'), #{options[:ui].inspect}\n"
    f << "run Eggplant::Server\n"
  end
end

#new(path = '.') ⇒ Object



29
30
31
32
# File 'lib/eggplant/cli.rb', line 29

def new path='.'
  path = File.expand_path path
  puts "Created new presentation in #{path}"
end

#showObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
# File 'lib/eggplant/cli.rb', line 50

def show
  path = File.expand_path('.')
  puts "Serving presentation from #{path}"

  EventMachine.run do
    # Server options
    server_options = {
      :port => options[:port],
      :presentation_root => path,
      :ui => options[:ui]
    }
    if options[:mode] == 'show'
      server_options[:environment] = :production
    end

    # Eggplant::Server
    Eggplant::Server.run! server_options

    # Eggplant::WebSocket
    @channel = Eggplant::Channel.new
    @count = 0
    @current_slide = 0
    
    EventMachine::WebSocket.start(:host => '0.0.0.0', :port => options[:remote_port]) do |ws|
      ws.onopen do
        sid = @channel.subscribe do |msg|
          ws.send msg
        end
        @count += 1
        @channel.push({:type => 'join', :total_clients => @count, :current_slide => @current_slide}.to_json)

        ws.onmessage do |msg|
          message = JSON.parse msg
          if message['type'] == 'goto'
            @current_slide = message['idx']
          end
          @channel.push_except sid, "#{msg}"
        end

        ws.onclose do
          @channel.unsubscribe sid
          @count -= 1
          @channel.push({:type => 'leave', :total_clients => @count}.to_json)
        end
      end
    end
  end
end