2009-10-22 20:25:15 -04:00
#!/usr/bin/env ruby
require 'optparse'
# Require Middleman
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development'
options = { :Port => 4567, :AccessLog => [] }
2011-05-31 01:33:11 -04:00
# TODO: Switch to Thor
2009-10-22 20:25:15 -04:00
OptionParser.new { |opts|
opts.banner = "Usage: mm-server [rack options]"
opts.separator ""
opts.separator "Rack options:"
opts.on("-p", "--port PORT", "use PORT (default: 4567)") { |port|
options[:Port] = port
}
opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
env = e
}
opts.on("--debug", "Debug mode") {
2010-09-05 22:28:38 -04:00
::Middleman::Server.set :logging, true
2009-10-22 20:25:15 -04:00
}
2011-02-21 10:19:18 -05:00
2009-10-22 20:25:15 -04:00
opts.parse! ARGV
}
ENV['RACK_ENV'] = env
2011-04-10 18:27:18 -04:00
@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)
2011-05-31 01:33:11 -04:00
source_folder = File.join(@current_path, "source")
2011-04-10 18:27:18 -04:00
2011-05-31 01:33:11 -04:00
if File.exists?(source_folder)
2011-04-10 18:27:18 -04:00
@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
2009-10-22 20:25:15 -04:00
end
2011-05-31 01:33:11 -04:00
# If the old init.rb exists, issue warning
2011-04-10 18:27:18 -04:00
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"
exit
end
2011-05-31 01:33:11 -04:00
# 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)
$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
2011-04-10 18:27:18 -04:00
Middleman::Server.root = @current_path
2011-06-10 01:48:35 -04:00
options[:app] = Middleman::Server.new
# options[:server] = 'webrick'
2009-10-22 20:25:15 -04:00
2011-06-10 01:48:35 -04:00
puts "== The Middleman is standing watch on port #{options[:Port]}"
Rack::Server.new(options).start