Class: MagLove::Middleware::LiveReload

Inherits:
Object
  • Object
show all
Defined in:
lib/maglove/middleware/live_reload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ LiveReload

Returns a new instance of LiveReload.



8
9
10
11
12
13
# File 'lib/maglove/middleware/live_reload.rb', line 8

def initialize(app, options = {})
  @app = app
  @options = options
  @theme = options[:theme]
  @templates = options[:templates]
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/maglove/middleware/live_reload.rb', line 6

def app
  @app
end

#current_templateObject (readonly)

Returns the value of attribute current_template.



6
7
8
# File 'lib/maglove/middleware/live_reload.rb', line 6

def current_template
  @current_template
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/maglove/middleware/live_reload.rb', line 6

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/maglove/middleware/live_reload.rb', line 15

def call(env)
  if env["PATH_INFO"] == @options[:mount]
    request = Rack::Request.new(env)
    @ws = Faye::WebSocket.new(request.env, [], {})
    @ws.onmessage = lambda do |event|
      message = JSON.parse(event.data)
      command = message["command"]
      handle_command(command, message)
    end
    @ws.onclose = ->(event) { clear_watcher! if @watcher }
    @ws.rack_response
  else
    @app.call(env)
  end
end

#clear_watcher!Object



31
32
33
34
35
36
37
# File 'lib/maglove/middleware/live_reload.rb', line 31

def clear_watcher!
  @ws = nil
  @watcher.stop
  @thread.join
  @watcher = nil
  @thread = nil
end

#handle_command(command, data = {}) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/maglove/middleware/live_reload.rb', line 44

def handle_command(command, data = {})
  if command == "init"
    send_command("init", { templates: @templates })
  elsif command == "watch"
    @current_template = data["template"]
    watch
  end
end

#send_command(command, data = {}) ⇒ Object



39
40
41
42
# File 'lib/maglove/middleware/live_reload.rb', line 39

def send_command(command, data = {})
  data[:command] = command
  @ws.send(JSON.dump(data)) if @ws
end

#watchObject



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
98
99
100
101
102
# File 'lib/maglove/middleware/live_reload.rb', line 53

def watch
  base_version = Maglove.theme.base_version
  patterns = [
    "src/themes/#{@theme}/**/*.{haml,html,coffee,js,less,scss,css,yml}",
    "src/themes/#{@theme}/images/**/*.{jpg,jpeg,gif,png,svg}",
    "src/base/#{base_version}/**/*.{coffee,js,less,scss,css}",
    "src/base/#{base_version}/images/**/*.{jpg,jpeg,gif,png,svg}"
  ]
  @watcher = FileWatcher.new(patterns.map(&:to_s))
  @thread = Thread.new(@watcher) do |fw|
    fw.watch do |filename, event|
      if Workspace.dir(Dir.pwd).file(filename).exists? and event != :delete
        if filename =~ %r{^src/base/#{base_version}/.*\.coffee}
          path = "theme.coffee"
        elsif filename =~ %r{^src/base/#{base_version}/.*\.js}
          path = "theme.coffee"
        elsif filename =~ %r{^src/base/#{base_version}/.*\.less}
          path = "theme.less"
        elsif filename =~ %r{^src/base/#{base_version}/.*\.scss}
          path = "theme.scss"
        elsif filename =~ %r{^src/themes/#{@theme}/.*\.less}
          path = "theme.less"
        elsif filename =~ %r{^src/themes/#{@theme}/.*\.scss}
          path = "theme.scss"
        elsif filename =~ %r{^src/themes/#{@theme}/.*\.coffee}
          path = "theme.coffee"
        elsif filename =~ %r{^src/themes/#{@theme}/.*\.js}
          path = "theme.coffee"
        elsif filename =~ %r{^src/themes/#{@theme}/blocks/.*\.haml}
          path = "templates/#{current_template}.haml"
        else
          path = filename.gsub("src/themes/#{@theme}/", '')
        end
        asset = MagLove::Asset::Theme.new(path, { base: false })
        if asset.write!
          puts "#{path} updated"
          case asset.output_type
          when "html"
            template = path.match(%r{templates/(.*)\.haml})[1]
            send_command("html", { template: template, contents: asset.contents })
          when "css"
            send_command("css", { contents: asset.contents })
          when "js"
            send_command("js", { contents: asset.contents })
          end
        end
      end
    end
  end
end