From e81ba33b0df7b03bb6d40c8ceb61c4bd5c014312 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 13 Jul 2011 18:45:15 -0700 Subject: [PATCH] livereload --- CHANGELOG | 2 ++ bin/mm-build | 2 -- bin/mm-server | 6 +++++- lib/middleman.rb | 3 +++ lib/middleman/guard.rb | 18 ++++++++++++++---- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d29a5134..e4153f05 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ 2.0.0 ===== +- Guard-powered auto-reloading of config.rb +- Guard LiveReload - Sprockets JS - Refactored Dynamically Reloadable Core - Combine views/ and public/ into a single source/ folder. diff --git a/bin/mm-build b/bin/mm-build index 7381f336..196f4c34 100755 --- a/bin/mm-build +++ b/bin/mm-build @@ -4,6 +4,4 @@ ENV['MM_ENV'] = "build" # Require app require File.join(File.dirname(__FILE__), "..", "lib", "middleman") - -require 'middleman' Middleman::Builder.start \ No newline at end of file diff --git a/bin/mm-server b/bin/mm-server index 399b9d46..68208736 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -7,6 +7,7 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman') env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development' options = {} +livereload_options = {} # TODO: Switch to Thor OptionParser.new { |opts| @@ -19,6 +20,9 @@ OptionParser.new { |opts| opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| env = e } + opts.on("--livereload_port PORT", "use PORT (default: 35729)") { |port| + livereload_options[:port] = port + } opts.parse! ARGV } @@ -36,4 +40,4 @@ if File.exists?("views") || File.exists?("public") exit end -Middleman::Guard.start(options) \ No newline at end of file +Middleman::Guard.start(options, livereload_options) \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 54e81be9..026b0793 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -144,6 +144,9 @@ module Middleman # Proxy web services requests in dev mode only autoload :Proxy, "middleman/features/proxy" + + # guard-livereload + autoload :LiveReload, "middleman/features/live_reload" # Automatically resize images for mobile devises # autoload :TinySrc, "middleman/features/tiny_src" diff --git a/lib/middleman/guard.rb b/lib/middleman/guard.rb index 423ecabe..dfb59945 100644 --- a/lib/middleman/guard.rb +++ b/lib/middleman/guard.rb @@ -1,25 +1,35 @@ require "guard" require "guard/guard" +require "guard/livereload" module Middleman::Guard - def self.start(options={}) + def self.start(options={}, livereload={}) options_hash = "" options.each do |k,v| options_hash << ", :#{k} => '#{v}'" end + + livereload_options_hash = "" + livereload.each do |k,v| + livereload_options_hash << ", :#{k} => '#{v}'" + end ::Guard.start({ :guardfile_contents => %Q{ - guard 'MiddlemanServer'#{options_hash} do + guard 'middleman'#{options_hash} do watch("config.rb") end + + guard 'livereload'#{livereload_options_hash} do + watch(%r{^source/(.*)$}) + end } }) end end module Guard - class MiddlemanServer < Guard + class Middleman < Guard def initialize(watchers = [], options = {}) super @options = { @@ -41,7 +51,7 @@ module Guard puts "== The Middleman is standing watch on port #{@options[:port]}" @server_options = { :Port => @options[:port], :AccessLog => [] } @server_job = fork do - @server_options[:app] = Middleman.server.new + @server_options[:app] = ::Middleman.server.new ::Rack::Server.new(@server_options).start end end