render dynamic assets like scss and coffeescript files
did
committed Feb 16, 2015
commit 48c4d723aca5a2bfbb6798b8e63da1b69fcf32ab
Showing 12
changed files with
202 additions
and 136 deletions
.gitignore
+1
-0
| @@ | @@ -2,6 +2,7 @@ |
| *.rbc | |
| .bundle | |
| .config | |
| + | .sass-cache |
| coverage | |
| InstalledFiles | |
| lib/bundler/man | |
Gemfile
+2
-0
| @@ | @@ -8,6 +8,8 @@ group :development do |
| # gem 'locomotivecms_models', '0.0.1.pre.alpha' | |
| # gem 'locomotivecms-liquid', path: '/Users/didier/Documents/LocomotiveCMS/gems/liquid' | |
| gem 'thin' | |
| + | # gem 'sprockets-sass', '~> 1.2.0' |
| + | gem 'yui-compressor', '~> 0.12.0' |
| end | |
| group :test do | |
Gemfile.lock
+2
-0
| @@ | @@ -201,6 +201,7 @@ GEM |
| tins (1.3.3) | |
| tzinfo (1.2.2) | |
| thread_safe (~> 0.1) | |
| + | yui-compressor (0.12.0) |
| PLATFORMS | |
| ruby | |
| @@ | @@ -216,3 +217,4 @@ DEPENDENCIES |
| rake (~> 10.4.2) | |
| rspec (~> 3.1.0) | |
| thin | |
| + | yui-compressor (~> 0.12.0) |
example/server.rb
+13
-8
| @@ | @@ -14,8 +14,6 @@ path = ENV['SITE_PATH'] || File.join(File.expand_path(File.dirname(__FILE__)), ' |
| Locomotive::Steam.configure do |config| | |
| config.mode = :test | |
| - | config.serve_assets = true |
| - | config.assets_path = File.join(path, 'public') |
| end | |
| Locomotive::Common.reset | |
| @@ | @@ -23,13 +21,20 @@ Locomotive::Common.configure do |config| |
| config.notifier = Locomotive::Common::Logger.setup(File.join(path, 'log/steam.log')) | |
| end | |
| - | server = Locomotive::Steam::Server.new(path: path) |
| + | server = Locomotive::Steam::Server.new({ |
| + | path: path, |
| + | serve_assets: true, |
| + | minify_assets: false |
| + | }) |
| - | # Note: alt thin settings |
| - | # server = Thin::Server.new('localhost', '3333', foo) |
| - | # server.threaded = true |
| + | # Note: alt thin settings (Threaded) |
| + | server = Thin::Server.new('localhost', '8080', server.to_app) |
| + | server.threaded = true |
| + | server.start |
| + | # FIXME: Rack::Handler::Thin.run server.to_app (not threaded) |
| - | Rack::Handler::Thin.run server.to_app |
| + | # WEBRick rack handler |
| + | # Rack::Handler::WEBrick.run server.to_app |
| Locomotive::Common::Logger.info 'Server started...' | |
| - | server.start |
| + | |
locomotive/steam.rb b/lib/locomotive/steam.rb
+0
-12
| @@ | @@ -15,21 +15,9 @@ require_relative 'steam/liquid' |
| require_relative 'steam/repositories' | |
| require_relative 'steam/services' | |
| - | # TODO: move into a file named dependencies |
| - | |
| - | # TODO: move them to a different place |
| - | require 'haml' |
| - | require 'compass' |
| - | require 'mimetype_fu' |
| - | require 'mime-types' |
| - | require 'rack/csrf' |
| - | require 'mime/types' |
| - | |
| module Locomotive | |
| module Steam | |
| - | # TEMPLATE_EXTENSIONS = %w(liquid haml) |
| - | |
| class << self | |
| attr_writer :configuration | |
| end | |
locomotive/steam/middlewares/dynamic_assets.rb b/lib/locomotive/steam/middlewares/dynamic_assets.rb
+31
-16
| @@ | @@ -1,39 +1,54 @@ |
| require 'coffee_script' | |
| + | require 'yui/compressor' |
| module Locomotive::Steam | |
| module Middlewares | |
| - | class DynamicAssets # < Base |
| + | class DynamicAssets |
| attr_reader :app, :regexp | |
| - | def initialize(app) |
| + | def initialize(app, options) |
| + | @app = app |
| @regexp = /^\/(javascripts|stylesheets)\/(.*)$/o | |
| - | end |
| - | def call(env) |
| - | app.call(env) |
| - | # _call(env) |
| - | # dup._call(env) # thread-safe purpose |
| + | @assets = ::Sprockets::Environment.new(options[:root]).tap do |env| |
| + | install_yui_compressor(env, options) |
| + | |
| + | %w(fonts stylesheets javascripts).each do |name| |
| + | env.append_path File.join(options[:root], name) |
| + | end |
| + | end |
| end | |
| - | def _call(env) |
| + | def call(env) |
| if env['PATH_INFO'] =~ self.regexp | |
| env['PATH_INFO'] = $2 | |
| + | @assets.call(env) |
| + | else |
| + | app.call(env) |
| + | end |
| + | end |
| + | |
| + | private |
| - | # base_path = env['steam.mounting_point'].path |
| + | def install_yui_compressor(sprockets, options) |
| + | return unless options[:minify] |
| - | # begin |
| - | # sprockets = Locomotive::Mounter::Extensions::Sprockets.environment(base_path) |
| - | # sprockets.call(env) |
| - | # rescue Exception => e |
| - | # raise Locomotive::Steam::DefaultException.new "Unable to serve a dynamic asset. Please check the logs.", e |
| - | # end |
| + | if is_java_installed? |
| + | # minify javascripts and stylesheets |
| + | sprockets.js_compressor = YUI::JavaScriptCompressor.new |
| + | sprockets.css_compressor = YUI::CssCompressor.new |
| else | |
| - | app.call(env) |
| + | message = "[Important] YUICompressor requires java to be installed. The JAVA_HOME variable should also be set.\n" |
| + | Locomotive::Common::Logger.warn message.red |
| end | |
| end | |
| + | def is_java_installed? |
| + | `which java` != '' && (!ENV['JAVA_HOME'].blank? && File.exists?(ENV['JAVA_HOME'])) |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/middlewares/stack.rb b/lib/locomotive/steam/middlewares/stack.rb
+72
-72
| @@ | @@ -1,99 +1,99 @@ |
| - | require 'rack/session/moneta' |
| - | require 'rack/builder' |
| - | require 'rack/lint' |
| - | require 'dragonfly/middleware' |
| + | # require 'rack/session/moneta' |
| + | # require 'rack/builder' |
| + | # require 'rack/lint' |
| + | # require 'dragonfly/middleware' |
| - | module Locomotive |
| - | module Steam |
| - | module Middlewares |
| + | # module Locomotive |
| + | # module Steam |
| + | # module Middlewares |
| - | class Stack |
| + | # class Stack |
| - | def initialize(options) |
| - | @options = prepare_options(options) |
| - | end |
| + | # def initialize(options) |
| + | # @options = prepare_options(options) |
| + | # end |
| - | def create |
| - | options = @options |
| - | # _self = self |
| + | # def create |
| + | # options = @options |
| + | # # _self = self |
| - | Rack::Builder.new do |
| - | use Rack::Lint |
| + | # Rack::Builder.new do |
| + | # use Rack::Lint |
| - | use Steam::Middlewares::Favicon |
| + | # use Steam::Middlewares::Favicon |
| - | # if options[:serve_assets] |
| - | # use Steam::Middlewares::StaticAssets, { |
| - | # urls: ['/images', '/fonts', '/samples', '/media'] |
| - | # } |
| - | # use Steam::Middlewares::DynamicAssets |
| - | # end |
| + | # # if options[:serve_assets] |
| + | # # use Steam::Middlewares::StaticAssets, { |
| + | # # urls: ['/images', '/fonts', '/samples', '/media'] |
| + | # # } |
| + | # # use Steam::Middlewares::DynamicAssets |
| + | # # end |
| - | # use Rack::Csrf, |
| - | # field: 'authenticity_token', |
| - | # skip_if: -> (request) { |
| - | # !(request.post? && request.params[:content_type_slug].present?) |
| - | # } |
| + | # # use Rack::Csrf, |
| + | # # field: 'authenticity_token', |
| + | # # skip_if: -> (request) { |
| + | # # !(request.post? && request.params[:content_type_slug].present?) |
| + | # # } |
| - | # use ::Dragonfly::Middleware, :steam |
| + | # # use ::Dragonfly::Middleware, :steam |
| - | # use Rack::Session::Moneta, options[:moneta] |
| + | # # use Rack::Session::Moneta, options[:moneta] |
| - | # _self.send(:use_steam_middlewares, builder) |
| + | # # _self.send(:use_steam_middlewares, builder) |
| - | use Middlewares::Logging |
| - | use Middlewares::Path |
| + | # use Middlewares::Logging |
| + | # use Middlewares::Path |
| - | # foo = proc do |env| |
| - | # puts "[EndPoint] finishing here..." |
| - | # [ 200, {'Content-Type' => 'text/plain'}, ["b"] ] |
| - | # end |
| + | # # foo = proc do |env| |
| + | # # puts "[EndPoint] finishing here..." |
| + | # # [ 200, {'Content-Type' => 'text/plain'}, ["b"] ] |
| + | # # end |
| - | # run foo |
| + | # # run foo |
| - | run Steam::Middlewares::Renderer.new |
| - | end |
| - | end |
| + | # run Steam::Middlewares::Renderer.new |
| + | # end |
| + | # end |
| - | protected |
| + | # protected |
| - | def use_steam_middlewares(builder) |
| - | # builder.use Middlewares::Logging |
| - | # builder.use Middlewares::Site |
| - | # builder.use Middlewares::Path |
| + | # def use_steam_middlewares(builder) |
| + | # # builder.use Middlewares::Logging |
| + | # # builder.use Middlewares::Site |
| + | # # builder.use Middlewares::Path |
| - | # builder.run Steam::Middlewares::Renderer.new |
| + | # # builder.run Steam::Middlewares::Renderer.new |
| - | # builder.instance_eval do |
| - | # use Middlewares::Logging |
| + | # # builder.instance_eval do |
| + | # # use Middlewares::Logging |
| - | # use Middlewares::Site |
| + | # # use Middlewares::Site |
| - | # # use Middlewares::EntrySubmission |
| + | # # # use Middlewares::EntrySubmission |
| - | # use Middlewares::Path |
| + | # # use Middlewares::Path |
| - | # nil |
| - | # # use Middlewares::Locale |
| - | # # use Middlewares::Timezone |
| + | # # nil |
| + | # # # use Middlewares::Locale |
| + | # # # use Middlewares::Timezone |
| - | # # use Middlewares::Page |
| - | # # use Middlewares::TemplatizedPage |
| - | # end |
| - | # nil |
| - | end |
| + | # # # use Middlewares::Page |
| + | # # # use Middlewares::TemplatizedPage |
| + | # # end |
| + | # # nil |
| + | # end |
| - | # def prepare_options(options) |
| - | # { |
| - | # serve_assets: false, |
| - | # moneta: { |
| - | # store: Moneta.new(:Memory, expires: true) |
| - | # } |
| - | # }.merge(options) |
| - | # end |
| + | # # def prepare_options(options) |
| + | # # { |
| + | # # serve_assets: false, |
| + | # # moneta: { |
| + | # # store: Moneta.new(:Memory, expires: true) |
| + | # # } |
| + | # # }.merge(options) |
| + | # # end |
| - | end |
| + | # end |
| - | end |
| - | end |
| - | end |
| + | # end |
| + | # end |
| + | # end |
locomotive/steam/server.rb b/lib/locomotive/steam/server.rb
+28
-7
| @@ | @@ -1,3 +1,15 @@ |
| + | require 'haml' |
| + | require 'compass' |
| + | require 'mimetype_fu' |
| + | require 'mime-types' |
| + | require 'mime/types' |
| + | |
| + | require 'rack/csrf' |
| + | require 'rack/session/moneta' |
| + | require 'rack/builder' |
| + | require 'rack/lint' |
| + | require 'dragonfly/middleware' |
| + | |
| require_relative 'middlewares' | |
| module Locomotive::Steam | |
| @@ | @@ -15,13 +27,7 @@ module Locomotive::Steam |
| Rack::Builder.new do | |
| use Rack::Lint | |
| - | if server.options[:serve_assets] |
| - | use ::Rack::Static, { |
| - | root: Locomotive::Steam.configuration.assets_path, |
| - | urls: ['/images', '/fonts', '/samples', '/media'] |
| - | } |
| - | # use Middlewares::DynamicAssets # TODO |
| - | end |
| + | server.serve_assets(self) if server.options[:serve_assets] |
| use Middlewares::Favicon | |
| @@ | @@ -40,11 +46,26 @@ module Locomotive::Steam |
| def prepare_options(options) | |
| { | |
| serve_assets: false, | |
| + | minify: false, |
| moneta: { | |
| store: Moneta.new(:Memory, expires: true) | |
| } | |
| }.merge(options) | |
| end | |
| + | def serve_assets(builder) |
| + | public_path = File.join(options[:path], 'public') |
| + | |
| + | builder.use ::Rack::Static, { |
| + | root: public_path, |
| + | urls: ['/images', '/fonts', '/samples', '/media'] |
| + | } |
| + | |
| + | builder.use Middlewares::DynamicAssets, { |
| + | root: public_path, |
| + | minify: options[:minify_assets] |
| + | } |
| + | end |
| + | |
| end | |
| end | |
spec/integration/server/assets_spec.rb
+28
-0
| @@ | @@ -0,0 +1,28 @@ |
| + | require File.dirname(__FILE__) + '/../integration_helper' |
| + | |
| + | describe Locomotive::Steam::Server do |
| + | |
| + | include Rack::Test::Methods |
| + | |
| + | def app |
| + | run_server |
| + | end |
| + | |
| + | it 'renders an image' do |
| + | get '/images/nav_on.png' |
| + | expect(last_response.status).to eq(200) |
| + | end |
| + | |
| + | it 'renders a stylesheet' do |
| + | get '/stylesheets/application.css' |
| + | expect(last_response.status).to eq(200) |
| + | expect(last_response.body).to match('body{background:#f0eee3') |
| + | end |
| + | |
| + | it 'renders a SCSS asset' do |
| + | get '/stylesheets/other/style.css' |
| + | expect(last_response.status).to eq(200) |
| + | expect(last_response.body).to match('body{background:red}') |
| + | end |
| + | |
| + | end |
spec/integration/server/basic_spec.rb
+15
-17
| @@ | @@ -14,27 +14,25 @@ describe Locomotive::Steam::Server do |
| expect(last_response.body).to match(/Upcoming events/) | |
| end | |
| - | it 'renders an image' do |
| - | get '/images/nav_on.png' |
| - | expect(last_response.status).to eq(200) |
| - | end |
| + | describe 'Page not found' do |
| - | # it 'shows the 404 page' do |
| - | # get '/void' |
| - | # expect(last_response.status).to eq(404) |
| - | # expect(last_response.body).to match /page not found/ |
| - | # puts last_response.body.inspect |
| - | # end |
| + | it 'shows the 404 page' do |
| + | get '/void' |
| + | expect(last_response.status).to eq(404) |
| + | expect(last_response.body).to match /page not found/ |
| + | end |
| - | # it 'shows the 404 page with 200 status code when its called explicitly', pending: true do |
| - | # get '/404' |
| - | # last_response.status.should eq(200) |
| - | # last_response.body.should =~ /page not found/ |
| - | # end |
| + | it 'shows the 404 page with a 404 status code when its called explicitly' do |
| + | get '/404' |
| + | expect(last_response.status).to eq(404) |
| + | expect(last_response.body).to match /page not found/ |
| + | end |
| + | |
| + | end |
| - | # it 'shows content', pending: true do |
| + | # it 'shows content' do |
| # get '/about-us/jane-doe' | |
| - | # last_response.body.should =~ /Lorem ipsum dolor sit amet/ |
| + | # expect(last_response.body).to match /Lorem ipsum dolor sit amet/ |
| # end | |
| # it 'shows a content type template', pending: true do | |
spec/spec_helper.rb
+3
-2
| @@ | @@ -9,6 +9,8 @@ SimpleCov.start do |
| Coveralls::SimpleCov::Formatter | |
| ] | |
| + | add_filter 'config/' |
| + | add_filter 'example/' |
| add_filter 'spec/' | |
| add_group "Middlewares", "lib/locomotive/steam/middlewares" | |
| @@ | @@ -29,8 +31,7 @@ require_relative '../lib/locomotive/steam/repositories/filesystem' |
| require_relative 'support' | |
| Locomotive::Steam.configure do |config| | |
| - | config.mode = :test |
| - | config.assets_path = File.join(File.expand_path(File.dirname(__FILE__)), 'fixtures', 'default', 'public') |
| + | config.mode = :test |
| end | |
| RSpec.configure do |config| | |
spec/support/helpers.rb
+7
-2
| @@ | @@ -19,10 +19,15 @@ module Spec |
| end | |
| def run_server | |
| - | setup_common #(File.join(default_fixture_site_path, 'log/steam.log')) |
| + | output = ENV['STEAM_VERBOSE'] ? nil : File.join(default_fixture_site_path, 'log/steam.log') |
| + | setup_common(output) |
| Locomotive::Common::Logger.info 'Server started...' | |
| - | Locomotive::Steam::Server.new(path: default_fixture_site_path, serve_assets: true).to_app |
| + | Locomotive::Steam::Server.new({ |
| + | path: default_fixture_site_path, |
| + | serve_assets: true, |
| + | minify_assets: true |
| + | }).to_app |
| end | |
| def default_fixture_site_path | |