more tweaking, better rack

This commit is contained in:
tdreyno 2009-10-15 16:31:05 -07:00
parent b1d300718f
commit 16b8ba89a8
11 changed files with 79 additions and 88 deletions

View file

@ -6,7 +6,7 @@ ENV['MM_ENV'] = "build"
require File.join(File.dirname(__FILE__), "..", "lib", "middleman") require File.join(File.dirname(__FILE__), "..", "lib", "middleman")
require 'middleman/builder' require 'middleman/builder'
Middleman::Base.init! #Middleman::Base.init!
Middleman::Builder.init! Middleman::Builder.init!
Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV)) Middleman::Generators.run_cli(Dir.pwd, 'mm-build', 1, %w(build --force).concat(ARGV))

View file

@ -2,6 +2,9 @@
require 'optparse' require 'optparse'
# Require Middleman
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development' env = ENV['MM_ENV'] || ENV['RACK_ENV'] || 'development'
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] } options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
@ -15,23 +18,22 @@ OptionParser.new { |opts|
opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e| opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
env = e env = e
} }
opts.on("--debug", "Debug mode") {
::Middleman::Base.set :logging, true
}
opts.parse! ARGV opts.parse! ARGV
} }
ENV['RACK_ENV'] = env ENV['RACK_ENV'] = env
# Require Middleman
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
class Middleman::Base class Middleman::Base
set :root, Dir.pwd set :root, Dir.pwd
set :logging, false
end end
require 'shotgun' require 'shotgun'
config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru') config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru')
app = Shotgun.new(config, lambda { |inner_app| Middleman::Base.new }) app = Shotgun.new(config, lambda { |inner_app| Middleman::Base })
require 'thin' require 'thin'
Thin::Logging.silent = true Thin::Logging.silent = true

View file

@ -8,6 +8,8 @@ module Middleman
class Base < Sinatra::Base class Base < Sinatra::Base
set :app_file, __FILE__ set :app_file, __FILE__
set :root, Dir.pwd set :root, Dir.pwd
set :reload, false
set :logging, false
set :environment, ENV['MM_ENV'] || :development set :environment, ENV['MM_ENV'] || :development
set :supported_formats, %w(erb) set :supported_formats, %w(erb)
set :index_file, "index.html" set :index_file, "index.html"
@ -16,60 +18,34 @@ module Middleman
set :images_dir, "images" set :images_dir, "images"
set :build_dir, "build" set :build_dir, "build"
set :http_prefix, "/" set :http_prefix, "/"
require 'sinatra/content_for'
helpers Sinatra::ContentFor
require 'middleman/helpers' use Rack::ConditionalGet if environment == :development
helpers Middleman::Helpers
# Static files in public/ set :features, [:compass]
enable :static
require 'middleman/rack/static'
use Middleman::Rack::Static
@@features = []
def self.enable(*opts) def self.enable(*opts)
@@features << opts self.features << opts
super super
end end
def self.disable(*opts) def self.disable(*opts)
@@features -= opts self.features -= opts
super super
end end
# Features enabled by default
enable :sprockets
# Features disabled by default
disable :slickmap
disable :cache_buster
disable :minify_css
disable :minify_javascript
disable :relative_assets
disable :maruku
disable :smush_pngs
# Default build features
configure :build do
enable :minify_css
enable :minify_javascript
enable :cache_buster
end
# Rack helper for adding mime-types during local preview # Rack helper for adding mime-types during local preview
def self.mime(ext, type) def self.mime(ext, type)
ext = ".#{ext}" unless ext.to_s[0] == ?. ext = ".#{ext}" unless ext.to_s[0] == ?.
Rack::Mime::MIME_TYPES[ext.to_s] = type Rack::Mime::MIME_TYPES[ext.to_s] = type
end end
# Convenience function to discover if a tempalte exists for the requested renderer (haml, sass, etc) # Convenience function to discover if a tempalte exists for the requested renderer (haml, sass, etc)
def template_exists?(path, renderer=nil) def template_exists?(path, renderer=nil)
template_path = path.dup template_path = path.dup
template_path << ".#{renderer}" if renderer template_path << ".#{renderer}" if renderer
File.exists? File.join(options.views, template_path) File.exists? File.join(options.views, template_path)
end end
# Base case renderer (do nothing), Should be over-ridden # Base case renderer (do nothing), Should be over-ridden
module StaticRender module StaticRender
def render_path(path) def render_path(path)
@ -81,11 +57,9 @@ module Middleman
end end
end end
include StaticRender include StaticRender
# This will match all requests not overridden in the project's init.rb # This will match all requests not overridden in the project's init.rb
not_found do not_found do
self.class.init!(true, false)
# Normalize the path and add index if we're looking at a directory # Normalize the path and add index if we're looking at a directory
path = request.path path = request.path
path << options.index_file if path.match(%r{/$}) path << options.index_file if path.match(%r{/$})
@ -100,30 +74,54 @@ module Middleman
status 404 status 404
end end
end end
@@inited = false
# Require the features for this project
def self.init!(quiet=false, rerun=true)
return if @@inited && !rerun
# Check for and evaluate local configuration
local_config = File.join(self.root, "init.rb")
if File.exists? local_config
puts "== Reading: Local config" unless quiet
class_eval File.read(local_config)
end
# loop over enabled feature
@@features.flatten.each do |feature_name|
puts "== Enabling: #{feature_name.capitalize}" unless quiet
require "middleman/features/#{feature_name}"
end
@@inited = true
end
end end
end end
# Haml is required & includes helpers # Haml is required & includes helpers
require "middleman/haml" require "middleman/haml"
require "middleman/sass" require "middleman/sass"
require 'sinatra/content_for'
require 'middleman/helpers'
require 'middleman/rack/static'
require 'middleman/rack/sprockets'
class Middleman::Base
helpers Sinatra::ContentFor
helpers Middleman::Helpers
use Middleman::Rack::Static
use Middleman::Rack::Sprockets
# Features disabled by default
disable :slickmap
disable :cache_buster
disable :minify_css
disable :minify_javascript
disable :relative_assets
disable :maruku
disable :smush_pngs
# Default build features
configure :build do
enable :minify_css
enable :minify_javascript
enable :cache_buster
end
def self.new(*args, &bk)
# Check for and evaluate local configuration
local_config = File.join(self.root, "init.rb")
if File.exists? local_config
puts "== Reading: Local config" if logging?
class_eval File.read(local_config)
end
# loop over enabled feature
self.features.flatten.each do |feature_name|
puts "== Enabling: #{feature_name.capitalize}" if logging?
require "middleman/features/#{feature_name}"
end
super
end
end

View file

@ -1,7 +1,7 @@
class << Middleman::Base class << Middleman::Base
alias_method :pre_cache_buster_asset_url, :asset_url alias_method :pre_cache_buster_asset_url, :asset_url
def asset_url(path, prefix="") def asset_url(path, prefix="", request=nil)
http_path = pre_cache_buster_asset_url(path, prefix) http_path = pre_cache_buster_asset_url(path, prefix, request)
if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path)) if http_path.include?("://") || !%w(.css .png .jpg .js .gif).include?(File.extname(http_path))
http_path http_path
else else

View file

@ -1,5 +1,3 @@
require 'compass'
class Middleman::Base class Middleman::Base
configure do configure do
::Compass.configuration do |config| ::Compass.configuration do |config|

View file

@ -1,23 +1,17 @@
class Middleman::Base ::Compass.configuration do |config|
if compass? config.relative_assets = true
configure do
::Compass.configuration do |config|
config.relative_assets = true
end
end
end
end end
class << Middleman::Base class << Middleman::Base
alias_method :pre_relative_asset_url, :asset_url alias_method :pre_relative_asset_url, :asset_url
def asset_url(path, prefix="") def asset_url(path, prefix="", request=nil)
path = pre_relative_asset_url(path, prefix) path = pre_relative_asset_url(path, prefix, request)
if path.include?("://") if path.include?("://")
path path
else else
path = path[1,path.length-1] if path[0,1] == '/' path = path[1,path.length-1] if path[0,1] == '/'
request_path = request.path_info.dup request_path = request.path_info.dup
request_path << options.index_file if path.match(%r{/$}) request_path << self.class.index_file if path.match(%r{/$})
request_path.gsub!(%r{^/}, '') request_path.gsub!(%r{^/}, '')
parts = request_path.split('/') parts = request_path.split('/')

View file

@ -1,6 +1,6 @@
module Middleman module Middleman
class Base class Base
def self.asset_url(path, prefix="") def self.asset_url(path, prefix="", request=nil)
base_url = File.join(self.http_prefix, prefix) base_url = File.join(self.http_prefix, prefix)
path.include?("://") ? path : File.join(base_url, path) path.include?("://") ? path : File.join(base_url, path)
end end
@ -21,8 +21,8 @@ module Middleman
classes.join(' ') classes.join(' ')
end end
def asset_url(*args) def asset_url(path, prefix="")
self.class.asset_url(*args) self.class.asset_url(path, prefix, request)
end end
def link_to(title, url="#", params={}) def link_to(title, url="#", params={})

View file

@ -1,10 +1,10 @@
begin begin
require 'sprockets' require 'sprockets'
require 'middleman/features/sprockets+ruby19' # Sprockets ruby 1.9 duckpunch require 'middleman/rack/sprockets+ruby19' # Sprockets ruby 1.9 duckpunch
rescue LoadError rescue LoadError
puts "Sprockets not available. Install it with: gem install sprockets" puts "Sprockets not available. Install it with: gem install sprockets"
end end
module Middleman module Middleman
module Rack module Rack
class Sprockets class Sprockets
@ -31,5 +31,4 @@ module Middleman
end end
end end
Middleman::Base.use Middleman::Rack::Sprockets
Middleman::Base.supported_formats << "js" Middleman::Base.supported_formats << "js"

View file

@ -1,5 +1,5 @@
require "sass" require "sass"
require "middleman/compass" require 'compass'
module Middleman module Middleman
module Sass module Sass

View file

@ -6,13 +6,13 @@ base.set :root, File.join(File.dirname(__FILE__), "fixtures", "sample")
describe "Cache Buster Feature" do describe "Cache Buster Feature" do
it "should not append query string if off" do it "should not append query string if off" do
base.disable :cache_buster base.disable :cache_buster
base.init!(true, true) base.new
base.asset_url("stylesheets/static.css").should_not include("?") base.asset_url("stylesheets/static.css").should_not include("?")
end end
it "should append query string if on" do it "should append query string if on" do
base.enable :cache_buster base.enable :cache_buster
base.init!(true, true) base.new
base.asset_url("stylesheets/static.css").should include("?") base.asset_url("stylesheets/static.css").should include("?")
end end
end end