extract compass into core_ext, allow other features to execute after its init, add map support to sinatra, prep Sprockets feature

This commit is contained in:
Thomas Reynolds 2011-07-10 15:10:49 -07:00
parent 7828b07dad
commit 01eccafde2
12 changed files with 171 additions and 57 deletions

View file

@ -14,7 +14,7 @@ end
Given /^cleanup built test app$/ do
target = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "fixtures", "test-app", "build")
# FileUtils.rm_rf(target)
FileUtils.rm_rf(target)
end
Then /^"([^"]*)" should exist and include "([^"]*)"$/ do |target_file, expected|

View file

@ -73,6 +73,9 @@ module Middleman
end
module CoreExtensions
# Add Rack::Builder.map support
autoload :RackMap, "middleman/core_extensions/rack_map"
# Custom Feature API
autoload :Features, "middleman/core_extensions/features"
@ -92,6 +95,12 @@ module Middleman
# Extended version of Padrino's rendering
autoload :Rendering, "middleman/core_extensions/rendering"
# Compass framework for Sass
autoload :Compass, "middleman/core_extensions/compass"
# Sprockets 2
autoload :Sprockets, "middleman/core_extensions/sprockets"
# Pass custom options to views
autoload :Routing, "middleman/core_extensions/routing"
end
@ -131,9 +140,6 @@ module Middleman
# Proxy web services requests in dev mode only
autoload :Proxy, "middleman/features/proxy"
# Sprockets 2
# autoload :Sprockets, "middleman/features/sprockets"
# Automatically resize images for mobile devises
# autoload :TinySrc, "middleman/features/tiny_src"

View file

@ -0,0 +1,59 @@
module Middleman::CoreExtensions::Compass
class << self
def registered(app)
app.extend ClassMethods
require "compass"
# Susy grids
begin
require "susy"
rescue LoadError
end
app.after_feature_init do
views_root = File.basename(app.views)
::Compass.configuration do |config|
config.cache = false # For sassc files
config.project_path = app.root
config.sass_dir = File.join(views_root, app.css_dir)
config.output_style = :nested
config.fonts_dir = File.join(views_root, app.fonts_dir)
config.css_dir = File.join(views_root, app.css_dir)
config.images_dir = File.join(views_root, app.images_dir)
config.http_images_path = app.http_images_path rescue File.join(app.http_prefix || "/", app.images_dir)
config.http_stylesheets_path = app.http_css_path rescue File.join(app.http_prefix || "/", app.css_dir)
config.asset_cache_buster :none
config.add_import_path(config.sass_dir)
end
# configure :build do
# build_root = File.basename(self.build_dir)
# ::Compass.configuration do |config|
# config.css_dir = File.join(build_root, self.css_dir)
# config.images_dir = File.join(build_root, self.images_dir)
# end
# end
app.execute_after_compass_init!
app.set :sass, ::Compass.configuration.to_sass_engine_options
end
end
alias :included :registered
end
module ClassMethods
# Add a block/proc to be run after features have been setup
def after_compass_init(&block)
@run_after_compass ||= []
@run_after_compass << block
end
def execute_after_compass_init!
@run_after_compass ||= []
@run_after_compass.each { |block| class_eval(&block) }
end
end
end

View file

@ -84,7 +84,7 @@ module Middleman::CoreExtensions::Features
end
# Add in defaults
default_extensions.each do |ext|
default_features.each do |ext|
activate ext
end

View file

@ -0,0 +1,34 @@
module Middleman::CoreExtensions::RackMap
class << self
def registered(app)
app.extend ClassMethods
end
alias :included :registered
end
module ClassMethods
def map(path, &block)
@maps ||= []
@maps << [path, block]
end
def maps
@maps || []
end
# Creates a Rack::Builder instance with all the middleware set up and
# an instance of this class as end point.
def build(*args, &bk)
builder = ::Rack::Builder.new
builder.use ::Sinatra::ShowExceptions if show_exceptions?
builder.use ::Rack::CommonLogger if logging?
builder.use ::Rack::Head
middleware.each { |c,a,b| builder.use(c, *a, &b) }
maps.each { |p,b| builder.map(p, &b) }
builder.map "/" do
run Middleman::Server.new!(*args, &bk)
end
builder
end
end
end

View file

@ -0,0 +1,29 @@
require "sprockets"
module Middleman::CoreExtensions::Sprockets
class << self
def registered(app)
# app.map '/assets' do
# run ::Sprockets::Environment.new
# end
end
alias :included :registered
end
class Environment < Sprockets::Environment
# Pass in the project you want the pipeline to manage.
def initialize(app, mode = :debug)
# Views/ ?
super app.root
# Disable css for now
# unregister_processor "text/css", Sprockets::DirectiveProcessor
# configure search paths
# append_path File.dirname project_path
# append_path File.join project_path, 'assets'
end
end
end

View file

@ -1,7 +1,7 @@
module Middleman::Features::AssetHost
class << self
def registered(app)
app.after_feature_init do
app.after_compass_init do
if app.asset_host.is_a?(Proc)
::Compass.configuration.asset_host(&app.asset_host)
end

View file

@ -25,7 +25,7 @@ module Middleman::Features::CacheBuster
end
end
app.after_feature_init do
app.after_compass_init do
::Compass.configuration do |config|
config.asset_cache_buster do |path, real_path|
real_path = real_path.path if real_path.is_a? File

View file

@ -1,7 +1,7 @@
module Middleman::Features::MinifyCss
class << self
def registered(app)
app.after_feature_init do
app.after_compass_init do
::Compass.configuration.output_style = :compressed
end
end

View file

@ -1,41 +1,11 @@
require "sass"
require "sass/plugin"
require "compass"
module Middleman::Renderers::Sass
class << self
def registered(app)
# Susy grids
begin
require "susy"
rescue LoadError
end
app.after_feature_init do
views_root = File.basename(app.views)
::Compass.configuration do |config|
config.cache = false # For sassc files
config.project_path = app.root
config.sass_dir = File.join(views_root, app.css_dir)
config.output_style = :nested
config.fonts_dir = File.join(views_root, app.fonts_dir)
config.css_dir = File.join(views_root, app.css_dir)
config.images_dir = File.join(views_root, app.images_dir)
config.http_images_path = app.http_images_path rescue File.join(app.http_prefix || "/", app.images_dir)
config.http_stylesheets_path = app.http_css_path rescue File.join(app.http_prefix || "/", app.css_dir)
config.asset_cache_buster :none
config.add_import_path(config.sass_dir)
end
# configure :build do
# build_root = File.basename(self.build_dir)
# ::Compass.configuration do |config|
# config.css_dir = File.join(build_root, self.css_dir)
# config.images_dir = File.join(build_root, self.images_dir)
# end
# end
end
# Default sass options
app.set :sass, {}
end
alias :included :registered
end
@ -44,14 +14,16 @@ module Middleman::Renderers::Sass
def sass_options
return super if basename.nil?
location_of_sass_file = Middleman::Server.environment == :build ?
File.join(Middleman::Server.root, Middleman::Server.build_dir) :
location_of_sass_file = if Middleman::Server.environment == :build
File.join(Middleman::Server.root, Middleman::Server.build_dir)
else
Middleman::Server.views
end
parts = basename.split('.')
parts.pop
css_filename = File.join(location_of_sass_file, Middleman::Server.css_dir, parts.join("."))
super.merge(::Compass.configuration.to_sass_engine_options).merge(:css_filename => css_filename)
super.merge(Middleman::Server.settings.sass).merge(:css_filename => css_filename)
end
def evaluate(scope, locals, &block)
@ -74,7 +46,7 @@ module Middleman::Renderers::Sass
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
end
# Use compass settings in Haml filters
# Use sass settings in Haml filters
# Other, tilt-based filters (like those used in Slim) will
# work automatically.
module Middleman::Renderers::Haml
@ -82,8 +54,8 @@ module Middleman::Renderers::Haml
include ::Haml::Filters::Base
def render(text)
compass_options = ::Compass.configuration.to_sass_engine_options
::Sass::Engine.new(text, compass_options).render
sass_options = Middleman::Server.settings.sass
::Sass::Engine.new(text, sass_options).render
end
end
end

View file

@ -3,6 +3,20 @@ require "sinatra/base"
module Middleman
class Server < Sinatra::Base
class << self
# Override Sinatra's set to accept a block
# Specifically for the asset_host feature
def set(option, value=self, &block)
if block_given?
value = Proc.new { block }
end
super(option, value, &nil)
end
def build?; environment == :build; end
end
# Basic Sinatra config
set :app_file, __FILE__
set :root, ENV["MM_DIR"] || Dir.pwd
@ -28,12 +42,21 @@ module Middleman
set :views, "source"
# Add Rack::Builder.map to Sinatra
register Middleman::CoreExtensions::RackMap
# Activate custom features
register Middleman::CoreExtensions::Features
# Setup custom rendering
register Middleman::CoreExtensions::Rendering
# Compass framework
register Middleman::CoreExtensions::Compass
# Sprockets asset handling
register Middleman::CoreExtensions::Sprockets
# Setup asset path pipeline
register Middleman::CoreExtensions::Assets
@ -53,16 +76,6 @@ module Middleman
:lorem
]
# Override Sinatra's set to accept a block
# Specifically for the asset_host feature
def self.set(option, value=self, &block)
if block_given?
value = Proc.new { block }
end
super(option, value, &nil)
end
# Default layout name
set :layout, :layout

View file

@ -31,6 +31,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency("haml", ["~> 3.1.0"])
s.add_runtime_dependency("sass", ["~> 3.1.0"])
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_development_dependency("spork", ["~> 0.9.0.rc8"])
s.add_development_dependency("cucumber", ["~> 0.10.0"])