Class: MagLove::Middleware::LiveReload
- Inherits:
-
Object
- Object
- MagLove::Middleware::LiveReload
- Includes:
- Workspace
- Defined in:
- lib/maglove/middleware/live_reload.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #clear_watcher! ⇒ Object
- #handle_command(command, data = {}) ⇒ Object
-
#initialize(app, options = {}) ⇒ LiveReload
constructor
A new instance of LiveReload.
- #send_command(command, data = {}) ⇒ Object
- #watch ⇒ Object
Methods included from Workspace
#gem_dir, #theme_base_dir, #theme_base_file, #theme_config, #theme_dir, #theme_file, #workspace_dir, #workspace_file
Constructor Details
#initialize(app, options = {}) ⇒ LiveReload
Returns a new instance of LiveReload.
9 10 11 12 13 14 |
# File 'lib/maglove/middleware/live_reload.rb', line 9 def initialize(app, = {}) @app = app = @theme = [:theme] @templates = [:templates] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/maglove/middleware/live_reload.rb', line 7 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/maglove/middleware/live_reload.rb', line 7 def end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/maglove/middleware/live_reload.rb', line 16 def call(env) if env["PATH_INFO"] == [:mount] request = Rack::Request.new(env) @ws = Faye::WebSocket.new(request.env, [], {}) @ws. = lambda do |event| = JSON.parse(event.data) command = ["command"] handle_command(command, ) end @ws.onclose = ->(event) { clear_watcher! if @watcher } @ws.rack_response else @app.call(env) end end |
#clear_watcher! ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/maglove/middleware/live_reload.rb', line 32 def clear_watcher! @ws = nil @watcher.stop @thread.join @watcher = nil @thread = nil end |
#handle_command(command, data = {}) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/maglove/middleware/live_reload.rb', line 45 def handle_command(command, data = {}) if command == "init" send_command("init", { templates: @templates }) elsif command == "watch" watch end end |
#send_command(command, data = {}) ⇒ Object
40 41 42 43 |
# File 'lib/maglove/middleware/live_reload.rb', line 40 def send_command(command, data = {}) data[:command] = command @ws.send(JSON.dump(data)) if @ws end |
#watch ⇒ Object
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 |
# File 'lib/maglove/middleware/live_reload.rb', line 53 def watch patterns = [ theme_file("**/*.{haml,html,coffee,js,less,scss,css,yml}"), theme_file("images/**/*.{jpg,jpeg,gif,png,svg}"), theme_base_file("**/*.{coffee,js,less,scss,css}"), theme_base_file("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_file(".", filename).exists? and event != :delete if filename =~ %r{^src/base/#{theme_config(:base_version)}/.*\.coffee} path = "theme.coffee" elsif filename =~ %r{^src/base/#{theme_config(:base_version)}/.*\.less} path = "theme.less" elsif filename =~ %r{^src/base/#{theme_config(: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" else path = filename.gsub("src/themes/#{@theme}/", '') end asset = theme_file(path).asset if asset.write! 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 |