riding shotgun
This commit is contained in:
parent
ca34f2fe61
commit
2400c670b4
6 changed files with 77 additions and 46 deletions
2
Rakefile
2
Rakefile
|
@ -11,6 +11,8 @@ begin
|
|||
gem.authors = ["Thomas Reynolds"]
|
||||
gem.rubyforge_project = "middleman"
|
||||
gem.executables = %w(mm-init mm-build mm-server)
|
||||
gem.add_dependency("thin")
|
||||
gem.add_dependency("shotgun")
|
||||
gem.add_dependency("templater")
|
||||
gem.add_dependency("sprockets")
|
||||
gem.add_dependency("sinatra")
|
||||
|
|
|
@ -7,13 +7,70 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
|
|||
|
||||
class Middleman::Base
|
||||
set :root, Dir.pwd
|
||||
set :logging, false
|
||||
enable :static
|
||||
end
|
||||
|
||||
OptionParser.new { |op|
|
||||
op.on('-e env') { |val| set :environment, val.to_sym }
|
||||
op.on('-s server') { |val| set :server, val }
|
||||
op.on('-p port') { |val| set :port, val.to_i }
|
||||
}.parse!(ARGV.dup)
|
||||
env = ENV['RACK_ENV'] || 'development'
|
||||
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
|
||||
|
||||
# Start Middleman
|
||||
run!
|
||||
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.parse! ARGV
|
||||
}
|
||||
|
||||
require 'rack'
|
||||
require 'rack/utils'
|
||||
require 'rack/request'
|
||||
require 'rack/response'
|
||||
require 'rack/showexceptions'
|
||||
|
||||
module Middleman
|
||||
class Static
|
||||
def initialize(app, options={})
|
||||
@app = app
|
||||
root = Middleman::Base.public
|
||||
@file_server = Rack::File.new(root)
|
||||
end
|
||||
|
||||
def call(env)
|
||||
path = env["PATH_INFO"]
|
||||
if path.include?("favicon.ico") || File.exists?(File.join(Middleman::Base.public, path))
|
||||
@file_server.call(env)
|
||||
else
|
||||
@app.call(env)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
app_wrapper = lambda do |inner_app|
|
||||
Rack::Builder.new {
|
||||
use Rack::ShowExceptions
|
||||
use Middleman::Static
|
||||
run Middleman::Base
|
||||
}.to_app
|
||||
end
|
||||
|
||||
ENV['RACK_ENV'] = env
|
||||
|
||||
require 'shotgun'
|
||||
require 'thin'
|
||||
|
||||
config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru')
|
||||
app = Shotgun.new(config, app_wrapper)
|
||||
|
||||
Thin::Logging.silent = true
|
||||
|
||||
Rack::Handler::Thin.run app, options do |inst|
|
||||
puts "== The Middleman is standing watch on port #{options[:Port]}"
|
||||
end
|
|
@ -62,12 +62,11 @@ module Middleman
|
|||
end
|
||||
end
|
||||
include StaticRender
|
||||
|
||||
# Disable static asset handling in Rack, so we can customize it here
|
||||
disable :static
|
||||
|
||||
# This will match all requests not overridden in the project's init.rb
|
||||
not_found do
|
||||
self.class.init!(true, false)
|
||||
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
path = request.path
|
||||
path << options.index_file if path.match(%r{/$})
|
||||
|
@ -90,33 +89,11 @@ module Middleman
|
|||
end
|
||||
end
|
||||
|
||||
# Copy, pasted & edited version of the setup in Sinatra.
|
||||
# Basically just changed the app's name and call out feature & configuration init.
|
||||
def self.run!(options={}, &block)
|
||||
init!
|
||||
set options
|
||||
handler = detect_rack_handler
|
||||
handler_name = handler.name.gsub(/.*::/, '')
|
||||
puts "== The Middleman is standing watch on port #{port}"
|
||||
handler.run self, :Host => host, :Port => port do |server|
|
||||
trap(:INT) do
|
||||
## Use thins' hard #stop! if available, otherwise just #stop
|
||||
server.respond_to?(:stop!) ? server.stop! : server.stop
|
||||
puts "\n== The Middleman has ended his patrol"
|
||||
end
|
||||
|
||||
if block_given?
|
||||
block.call
|
||||
## Use thins' hard #stop! if available, otherwise just #stop
|
||||
server.respond_to?(:stop!) ? server.stop! : server.stop
|
||||
end
|
||||
end
|
||||
rescue Errno::EADDRINUSE => e
|
||||
puts "== The Middleman is already standing watch on port #{port}!"
|
||||
end
|
||||
|
||||
@@inited = false
|
||||
# Require the features for this project
|
||||
def self.init!(quiet=false)
|
||||
def self.init!(quiet=false, rerun=true)
|
||||
return if @@inited && !rerun
|
||||
|
||||
# Built-in helpers
|
||||
require 'middleman/helpers'
|
||||
helpers Middleman::Helpers
|
||||
|
@ -140,6 +117,8 @@ module Middleman
|
|||
require "middleman/features/#{feature_name}"
|
||||
end
|
||||
end
|
||||
|
||||
@@inited = true
|
||||
end
|
||||
end
|
||||
end
|
2
lib/middleman/config.ru
Normal file
2
lib/middleman/config.ru
Normal file
|
@ -0,0 +1,2 @@
|
|||
require 'middleman'
|
||||
run Middleman::Base
|
|
@ -16,6 +16,7 @@ module Middleman
|
|||
if File.extname(path) == '.js' && File.exists?(source)
|
||||
secretary = ::Sprockets::Secretary.new( :asset_root => options.public,
|
||||
:source_files => [source] )
|
||||
# may need to rejigger since sprockets will only read views/ now
|
||||
secretary.concatenation.to_s
|
||||
else
|
||||
super
|
||||
|
|
|
@ -18,14 +18,4 @@ class Templater::Actions::Template
|
|||
browser.get(request_path)
|
||||
browser.last_response.body
|
||||
end
|
||||
|
||||
def identical?
|
||||
if File.exists?(destination)
|
||||
extension = File.extname(source)
|
||||
return true if !%w(.sass .js .haml).include?(extension) && File.exists?(source) && File.mtime(source) < File.mtime(destination)
|
||||
File.read(destination) == render
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue