From 9659268a03fb58bf70f4d6026be97d59e6039fd5 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 13 Jul 2011 18:29:59 -0700 Subject: [PATCH] Implement Guard on config.rb --- bin/mm-server | 48 +++++------------------------------ lib/middleman.rb | 1 + lib/middleman/guard.rb | 57 ++++++++++++++++++++++++++++++++++++++++++ middleman.gemspec | 2 ++ 4 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 lib/middleman/guard.rb diff --git a/bin/mm-server b/bin/mm-server index e4f97307..399b9d46 100755 --- a/bin/mm-server +++ b/bin/mm-server @@ -5,10 +5,8 @@ require 'optparse' # Require Middleman require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman') -sandboxed_server = Middleman.server - env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development' -options = { :Port => 4567, :AccessLog => [] } +options = {} # TODO: Switch to Thor OptionParser.new { |opts| @@ -16,60 +14,26 @@ OptionParser.new { |opts| opts.separator "" opts.separator "Rack options:" opts.on("-p", "--port PORT", "use PORT (default: 4567)") { |port| - options[:Port] = port + options[:port] = port } opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| env = e } - opts.on("--debug", "Debug mode") { - sandboxed_server.set :logging, true - } opts.parse! ARGV } ENV['RACK_ENV'] = env -@current_path = Dir.pwd -@path_parts = @current_path.split("/") -@found_root = false - -while (!@found_root && (@path_parts.length > 0)) - @current_path = File.join(*@path_parts) - - source_folder = File.join(@current_path, "source") - - if File.exists?(source_folder) - @found_root = true - next - end - - @path_parts.pop -end - -if !@found_root - $stderr.puts "== Error: Could not find a Middleman project structure, perhaps you are in the wrong folder?" - exit -end - -# If the old init.rb exists, issue warning -old_config = File.join(@current_path, "init.rb") -if File.exists? old_config - $stderr.puts "== Error: The init.rb file (deprecated) needs to be be renamed to config.rb" +if !File.exists?("config.rb") + $stderr.puts "== Error: Could not find a Middleman project config, perhaps you are in the wrong folder?" exit end # If the old directories exists, use it, but issue warning -old_views = File.join(@current_path, "views") -old_public = File.join(@current_path, "public") -if File.exists?(old_views) || File.exists?(old_public) +if File.exists?("views") || File.exists?("public") $stderr.puts "== Error: The views and public folders are have been combined. Create a new 'source' folder, add the contents of views and public to it and then remove the empty views and public folders." exit end -sandboxed_server.set :root, @current_path -options[:app] = sandboxed_server.new -# options[:server] = 'webrick' - -puts "== The Middleman is standing watch on port #{options[:Port]}" -Rack::Server.new(options).start +Middleman::Guard.start(options) \ No newline at end of file diff --git a/lib/middleman.rb b/lib/middleman.rb index 737766f2..54e81be9 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -66,6 +66,7 @@ module Middleman # Auto-load modules on-demand autoload :Base, "middleman/base" autoload :Builder, "middleman/builder" + autoload :Guard, "middleman/guard" # Custom Renderers module Renderers diff --git a/lib/middleman/guard.rb b/lib/middleman/guard.rb new file mode 100644 index 00000000..423ecabe --- /dev/null +++ b/lib/middleman/guard.rb @@ -0,0 +1,57 @@ +require "guard" +require "guard/guard" + +module Middleman::Guard + def self.start(options={}) + options_hash = "" + options.each do |k,v| + options_hash << ", :#{k} => '#{v}'" + end + + ::Guard.start({ + :guardfile_contents => %Q{ + guard 'MiddlemanServer'#{options_hash} do + watch("config.rb") + end + } + }) + end +end + +module Guard + class MiddlemanServer < Guard + def initialize(watchers = [], options = {}) + super + @options = { + :port => '4567' + }.update(options) + end + + def start + server_start + end + + def run_on_change(paths) + server_stop + server_start + end + + private + def server_start + 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 + ::Rack::Server.new(@server_options).start + end + end + + def server_stop + puts "== The Middleman is shutting down" + Process.kill("KILL", @server_job) + Process.wait @server_job + @server_job = nil + @server_options[:app] = nil + end + end +end \ No newline at end of file diff --git a/middleman.gemspec b/middleman.gemspec index 9ae495ab..f75143d8 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -34,6 +34,8 @@ Gem::Specification.new do |s| s.add_runtime_dependency("compass", ["~> 0.11.3"]) s.add_runtime_dependency("sprockets", ["2.0.0.beta.10"]) s.add_runtime_dependency("httparty", ["~> 0.7.0"]) + s.add_runtime_dependency("guard", [">= 0"]) + s.add_runtime_dependency("guard-livereload", [">= 0"]) s.add_development_dependency("coffee-filter", ["~> 0.1.1"]) s.add_development_dependency("cucumber", ["~> 0.10.0"]) s.add_development_dependency("rake", ["0.8.7"])