Class: MagLove::Middleware::LiveReload
- Inherits:
-
Object
- Object
- MagLove::Middleware::LiveReload
- Defined in:
- lib/maglove/middleware/live_reload.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#current_template ⇒ Object
readonly
Returns the value of attribute current_template.
-
#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
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, = {}) @app = app @options = @theme = [:theme] @templates = [:templates] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
6 7 8 |
# File 'lib/maglove/middleware/live_reload.rb', line 6 def app @app end |
#current_template ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/maglove/middleware/live_reload.rb', line 6 def @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. = 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
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 |
#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 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 |