diff --git a/features/step_definitions/builder_steps.rb b/features/step_definitions/builder_steps.rb index 66adb090..013706c8 100644 --- a/features/step_definitions/builder_steps.rb +++ b/features/step_definitions/builder_steps.rb @@ -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| diff --git a/lib/middleman.rb b/lib/middleman.rb index 3b7379b9..48969231 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -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" @@ -91,6 +94,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" @@ -130,9 +139,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" diff --git a/lib/middleman/core_extensions/compass.rb b/lib/middleman/core_extensions/compass.rb new file mode 100644 index 00000000..cc6911d0 --- /dev/null +++ b/lib/middleman/core_extensions/compass.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/core_extensions/features.rb b/lib/middleman/core_extensions/features.rb index 46172b3f..9bc4c184 100644 --- a/lib/middleman/core_extensions/features.rb +++ b/lib/middleman/core_extensions/features.rb @@ -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 diff --git a/lib/middleman/core_extensions/rack_map.rb b/lib/middleman/core_extensions/rack_map.rb new file mode 100644 index 00000000..5a9c8627 --- /dev/null +++ b/lib/middleman/core_extensions/rack_map.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/core_extensions/sprockets.rb b/lib/middleman/core_extensions/sprockets.rb new file mode 100644 index 00000000..d8e27dc2 --- /dev/null +++ b/lib/middleman/core_extensions/sprockets.rb @@ -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 \ No newline at end of file diff --git a/lib/middleman/features/asset_host.rb b/lib/middleman/features/asset_host.rb index 4cd94289..c6f53e8f 100644 --- a/lib/middleman/features/asset_host.rb +++ b/lib/middleman/features/asset_host.rb @@ -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 diff --git a/lib/middleman/features/cache_buster.rb b/lib/middleman/features/cache_buster.rb index d2f198b4..926e0c7d 100755 --- a/lib/middleman/features/cache_buster.rb +++ b/lib/middleman/features/cache_buster.rb @@ -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 diff --git a/lib/middleman/features/minify_css.rb b/lib/middleman/features/minify_css.rb index df82aae0..2756b514 100644 --- a/lib/middleman/features/minify_css.rb +++ b/lib/middleman/features/minify_css.rb @@ -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 diff --git a/lib/middleman/renderers/sass.rb b/lib/middleman/renderers/sass.rb index 3142338d..4ac42e8e 100644 --- a/lib/middleman/renderers/sass.rb +++ b/lib/middleman/renderers/sass.rb @@ -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) : - Middleman::Server.views + 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 \ No newline at end of file diff --git a/lib/middleman/server.rb b/lib/middleman/server.rb index 1156d76d..481ff9c4 100644 --- a/lib/middleman/server.rb +++ b/lib/middleman/server.rb @@ -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 diff --git a/middleman.gemspec b/middleman.gemspec index fa5c027c..0d740233 100644 --- a/middleman.gemspec +++ b/middleman.gemspec @@ -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"])