stylo 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/.gitignore +25 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +18 -0
  5. data/Rakefile +46 -0
  6. data/TODO +4 -0
  7. data/cucumber.yml +2 -0
  8. data/features/fixtures/child.css +3 -0
  9. data/features/fixtures/child.js +3 -0
  10. data/features/fixtures/grand_parent.css +5 -0
  11. data/features/fixtures/grand_parent.js +5 -0
  12. data/features/fixtures/grand_parent_with_parent_with_child.css +11 -0
  13. data/features/fixtures/grand_parent_with_parent_with_child.js +11 -0
  14. data/features/fixtures/parent.css +5 -0
  15. data/features/fixtures/parent.js +5 -0
  16. data/features/fixtures/parent_with_child.css +7 -0
  17. data/features/fixtures/parent_with_child.js +7 -0
  18. data/features/fixtures/processed_sass_child.css +2 -0
  19. data/features/fixtures/processed_sass_which_uses_mixin.css +3 -0
  20. data/features/fixtures/sass_child.scss +4 -0
  21. data/features/fixtures/sass_mixins.scss +3 -0
  22. data/features/fixtures/sass_which_uses_mixin.scss +6 -0
  23. data/features/fixtures.rb +5 -0
  24. data/features/javascripts.feature +19 -0
  25. data/features/response_headers.feature +13 -0
  26. data/features/sass_integration.feature +13 -0
  27. data/features/step_definitions/stylo_steps.rb +24 -0
  28. data/features/stylesheets.feature +31 -0
  29. data/features/stylo_cannot_serve_asset.feature +5 -0
  30. data/features/support/env.rb +24 -0
  31. data/lib/stylo/asset_loader.rb +9 -0
  32. data/lib/stylo/combiner.rb +14 -0
  33. data/lib/stylo/config.rb +14 -0
  34. data/lib/stylo/pipeline_steps/caching.rb +11 -0
  35. data/lib/stylo/pipeline_steps/javascript.rb +15 -0
  36. data/lib/stylo/pipeline_steps/sass.rb +18 -0
  37. data/lib/stylo/pipeline_steps/stylesheet.rb +15 -0
  38. data/lib/stylo/processor.rb +47 -0
  39. data/lib/stylo/rack.rb +20 -0
  40. data/lib/stylo/railtie.rb +13 -0
  41. data/lib/stylo/response.rb +41 -0
  42. data/lib/stylo.rb +5 -0
  43. data/spec/asset_loader_spec.rb +16 -0
  44. data/spec/combiner_spec.rb +41 -0
  45. data/spec/pipeline_steps/caching_spec.rb +26 -0
  46. data/spec/pipeline_steps/javascript_spec.rb +84 -0
  47. data/spec/pipeline_steps/sass_spec.rb +94 -0
  48. data/spec/pipeline_steps/stylesheet_spec.rb +84 -0
  49. data/spec/rack_spec.rb +75 -0
  50. data/spec/spec_helper.rb +18 -0
  51. data/spec/stylo_spec_helpers.rb +30 -0
  52. data/stylo.gemspec +116 -0
  53. metadata +198 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## RubyMine
17
+ .idea
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+
24
+ ## PROJECT::SPECIFIC
25
+ tmp
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 mwagg
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = stylo
2
+
3
+ Stylo aims to support server side combining of stylesheets within rails when hosted in an environment
4
+ such as www.heroku.com where we have the problem of a read only file system.
5
+
6
+ == Note on Patches/Pull Requests
7
+
8
+ * Fork the project.
9
+ * Make your feature addition or bug fix.
10
+ * Add tests for it. This is important so I don't break it in a
11
+ future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history.
13
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 mwagg. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'cucumber'
4
+ require 'cucumber/rake/task'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "stylo"
10
+ gem.summary = %Q{Server side stylesheet combining for readonly hosting environments}
11
+ gem.description = %Q{Server side stylesheet combining for readonly hosting environments}
12
+ gem.email = "michael@guerillatactics.co.uk"
13
+ gem.homepage = "http://github.com/mwagg/stylo"
14
+ gem.authors = ["mwagg"]
15
+ gem.add_development_dependency "rspec", ">= 0"
16
+ gem.add_development_dependency "cucumber", ">= 0"
17
+ gem.add_development_dependency "rack-test", ">= 0"
18
+ gem.add_development_dependency "sinatra", ">= 0"
19
+ gem.add_runtime_dependency "haml", ">= 3.0.21"
20
+ gem.version = "0.5"
21
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+ require 'rspec/core/rake_task'
29
+ desc "Run all examples"
30
+ RSpec::Core::RakeTask.new()
31
+
32
+ task :default => [:spec, :features]
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
37
+
38
+ rdoc.rdoc_dir = 'rdoc'
39
+ rdoc.title = "stylo #{version}"
40
+ rdoc.rdoc_files.include('README*')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ end
43
+
44
+ Cucumber::Rake::Task.new(:features) do |t|
45
+ t.profile = :default
46
+ end
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ support switching off combining (for development)
2
+ add minification for javascript
3
+ add minification for css
4
+ make it so sass is only used if the sass gem is not available (without making the pipeline configuration messy)
data/cucumber.yml ADDED
@@ -0,0 +1,2 @@
1
+ default: features --format pretty
2
+
@@ -0,0 +1,3 @@
1
+ #child {
2
+ background: #000000;
3
+ }
@@ -0,0 +1,3 @@
1
+ function child() {
2
+ alert('child');
3
+ }
@@ -0,0 +1,5 @@
1
+ @import "parent.css";
2
+
3
+ #grandparent {
4
+ background: #CCCCCC;
5
+ }
@@ -0,0 +1,5 @@
1
+ ///require "parent.js";
2
+
3
+ function grandParent() {
4
+ alert('grand parent');
5
+ }
@@ -0,0 +1,11 @@
1
+ #child {
2
+ background: #000000;
3
+ }
4
+
5
+ #parent {
6
+ background: #FFFFFF;
7
+ }
8
+
9
+ #grandparent {
10
+ background: #CCCCCC;
11
+ }
@@ -0,0 +1,11 @@
1
+ function child() {
2
+ alert('child');
3
+ }
4
+
5
+ function parent() {
6
+ alert('parent');
7
+ }
8
+
9
+ function grandParent() {
10
+ alert('grand parent');
11
+ }
@@ -0,0 +1,5 @@
1
+ @import "child.css";
2
+
3
+ #parent {
4
+ background: #FFFFFF;
5
+ }
@@ -0,0 +1,5 @@
1
+ ///require "child.js";
2
+
3
+ function parent() {
4
+ alert('parent');
5
+ }
@@ -0,0 +1,7 @@
1
+ #child {
2
+ background: #000000;
3
+ }
4
+
5
+ #parent {
6
+ background: #FFFFFF;
7
+ }
@@ -0,0 +1,7 @@
1
+ function child() {
2
+ alert('child');
3
+ }
4
+
5
+ function parent() {
6
+ alert('parent');
7
+ }
@@ -0,0 +1,2 @@
1
+ #child {
2
+ background: black; }
@@ -0,0 +1,3 @@
1
+ #element {
2
+ border: solid 1px #000000;
3
+ background: #FFFFFF; }
@@ -0,0 +1,4 @@
1
+ $black: #000000;
2
+ #child {
3
+ background: $black;
4
+ }
@@ -0,0 +1,3 @@
1
+ @mixin black-border {
2
+ border: solid 1px #000000;
3
+ }
@@ -0,0 +1,6 @@
1
+ @import "sass_mixins.scss";
2
+
3
+ #element {
4
+ @include black-border;
5
+ background: #FFFFFF;
6
+ }
@@ -0,0 +1,5 @@
1
+ module Fixtures
2
+ def load_fixture(fixture_name)
3
+ File.read File.join(File.dirname(__FILE__), 'fixtures', fixture_name)
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ Feature: Javascript serving
2
+
3
+ Scenario: Simple javascript serving
4
+ Given "child.js" is located at "javascripts" in the asset location
5
+ When a request is made for "javascripts/child.js"
6
+ Then the response body should look like "child.js"
7
+
8
+ Scenario: Simple javascript combining
9
+ Given "parent.js" is located at "javascripts" in the asset location
10
+ And "child.js" is located at "javascripts" in the asset location
11
+ When a request is made for "javascripts/parent.js"
12
+ Then the response body should look like "parent_with_child.js"
13
+
14
+ Scenario: Nested stylesheet combining
15
+ Given "grand_parent.js" is located at "javascripts" in the asset location
16
+ And "parent.js" is located at "javascripts" in the asset location
17
+ And "child.js" is located at "javascripts" in the asset location
18
+ When a request is made for "javascripts/grand_parent.js"
19
+ Then the response body should look like "grand_parent_with_parent_with_child.js"
@@ -0,0 +1,13 @@
1
+ Feature: Response headers
2
+
3
+ Scenario: Responses should have the correct headers
4
+ Given "child.js" is located at "javascripts" in the asset location
5
+ When a request is made for "javascripts/child.js"
6
+ Then the response code should be "200"
7
+ And the "Content-Length" header should be "40"
8
+ And the "Content-Type" header should be "text/javascript"
9
+
10
+ Scenario: Content should be cached
11
+ Given "child.js" is located at "javascripts" in the asset location
12
+ When a request is made for "javascripts/child.js"
13
+ Then the "Cache-Control" header should be "public, max-age=86400"
@@ -0,0 +1,13 @@
1
+ Feature: Sass integration
2
+
3
+ Scenario: Simple sass stylesheet serving
4
+ Given "sass_child.scss" is located at "stylesheets" in the asset location
5
+ When a request is made for "stylesheets/sass_child.css"
6
+ Then the response body should look like "processed_sass_child.css"
7
+
8
+ Scenario: Combined sass stylesheet serving
9
+ Given "sass_mixins.scss" is located at "stylesheets" in the asset location
10
+ And "sass_which_uses_mixin.scss" is located at "stylesheets" in the asset location
11
+ When a request is made for "stylesheets/sass_which_uses_mixin.css"
12
+ Then the response body should look like "processed_sass_which_uses_mixin.css"
13
+
@@ -0,0 +1,24 @@
1
+ Given /^"([^"]*)" is located at "([^"]*)" in the asset location$/ do |filename, folder|
2
+ cp fixture_path(filename), temp_path(folder)
3
+ end
4
+
5
+ When /^a request is made for "([^"]*)"$/ do |path|
6
+ get path
7
+ end
8
+
9
+ Then /^the response body should look like "([^"]*)"$/ do |filename|
10
+ last_response.body.should == load_fixture(filename)
11
+ end
12
+
13
+ Then /^the response code should be "([^\"]*)"$/ do |response_code|
14
+ last_response.status.should == response_code.to_i
15
+ end
16
+
17
+ When /^the "([^\"]*)" header should be "([^\"]*)"$/ do |header_key, expected_value|
18
+ last_response.headers[header_key].should == expected_value
19
+ end
20
+
21
+ Then /^the response should be from a call back into rack$/ do
22
+ last_response.status.should == 404
23
+ last_response.body.should include("Sinatra doesn't know this ditty.")
24
+ end
@@ -0,0 +1,31 @@
1
+ Feature: Stylesheet serving
2
+
3
+ Scenario: Simple stylesheet serving
4
+ Given "child.css" is located at "stylesheets" in the asset location
5
+ When a request is made for "stylesheets/child.css"
6
+ Then the response body should look like "child.css"
7
+
8
+ Scenario: Simple stylesheet combining
9
+ Given "parent.css" is located at "stylesheets" in the asset location
10
+ And "child.css" is located at "stylesheets" in the asset location
11
+ When a request is made for "stylesheets/parent.css"
12
+ Then the response body should look like "parent_with_child.css"
13
+
14
+ Scenario: Nested stylesheet combining
15
+ Given "grand_parent.css" is located at "stylesheets" in the asset location
16
+ And "parent.css" is located at "stylesheets" in the asset location
17
+ And "child.css" is located at "stylesheets" in the asset location
18
+ When a request is made for "stylesheets/grand_parent.css"
19
+ Then the response body should look like "grand_parent_with_parent_with_child.css"
20
+
21
+ Scenario: Stylesheet responses should have the correct headers
22
+ Given "child.css" is located at "stylesheets" in the asset location
23
+ When a request is made for "stylesheets/child.css"
24
+ Then the response code should be "200"
25
+ And the "Content-Length" header should be "35"
26
+ And the "Content-Type" header should be "text/css"
27
+
28
+ Scenario: Stylesheets should be cached
29
+ Given "child.css" is located at "stylesheets" in the asset location
30
+ When a request is made for "stylesheets/child.css"
31
+ Then the "Cache-Control" header should be "public, max-age=86400"
@@ -0,0 +1,5 @@
1
+ Feature: Stylo cannot serve an asset
2
+
3
+ Scenario: Stylo calls back into rack
4
+ When a request is made for "javascripts/i-dont-exist.js"
5
+ Then the response should be from a call back into rack
@@ -0,0 +1,24 @@
1
+ require 'rack/test'
2
+ require 'sinatra'
3
+
4
+ require File.join(File.dirname(__FILE__), '../../spec/spec_helper')
5
+
6
+ class MyApp < Sinatra::Application
7
+
8
+ end
9
+
10
+ World do
11
+ include Rack::Test::Methods
12
+ include StyloSpecHelpers
13
+ include FileUtils
14
+
15
+ def app
16
+ @app ||= Rack::Builder.new do
17
+ use Stylo::Rack
18
+ run MyApp
19
+ end
20
+ end
21
+
22
+ reset_paths
23
+ end
24
+
@@ -0,0 +1,9 @@
1
+ module Stylo
2
+ class AssetLoader
3
+ def self.load_content(path)
4
+ path = File.join(Stylo::Config.asset_location, path)
5
+
6
+ File.read(path) if File.exist?(path)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Stylo
2
+ class Combiner
3
+ def initialize(asset_directory, require_pattern)
4
+ @require_pattern = require_pattern
5
+ @asset_directory = asset_directory
6
+ end
7
+
8
+ def process(content)
9
+ content.gsub @require_pattern do |match|
10
+ process(AssetLoader.load_content(File.join(@asset_directory, $1)))
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Stylo
2
+ class Config
3
+ class << self
4
+ attr_accessor :asset_location
5
+
6
+ def pipeline
7
+ [Stylo::PipelineSteps::Stylesheet.new,
8
+ Stylo::PipelineSteps::Javascript.new,
9
+ Stylo::PipelineSteps::Sass.new,
10
+ Stylo::PipelineSteps::Caching.new]
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Stylo
2
+ module PipelineSteps
3
+ class Caching
4
+ def call(response)
5
+ if response.has_content?
6
+ response.set_header('Cache-Control', 'public, max-age=86400')
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Stylo
2
+ module PipelineSteps
3
+ class Javascript
4
+ REQUIRE_PATTERN = /\/\/\/require "(.*)";/
5
+
6
+ def call(response)
7
+ return if response.has_content? || response.extension != '.js'
8
+ return unless content = AssetLoader.load_content(response.path)
9
+ combined_content = Combiner.new(File.dirname(response.path), REQUIRE_PATTERN).process(content)
10
+
11
+ response.set_body combined_content, :javascript
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'sass'
2
+
3
+ module Stylo
4
+ module PipelineSteps
5
+ class Sass
6
+ def call(response)
7
+ return if response.has_content? || response.extension != '.css'
8
+ content = AssetLoader.load_content(response.path.gsub('.css', '.scss'))
9
+ return if content.nil?
10
+
11
+ combined_content = Combiner.new(File.dirname(response.path), REQUIRE_PATTERN).process(content)
12
+ processed_content = ::Sass::Engine.new(combined_content, {:syntax => :scss}).render
13
+
14
+ response.set_body(processed_content, :css)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ module Stylo
2
+ module PipelineSteps
3
+ REQUIRE_PATTERN = /@import "(.*)";/
4
+
5
+ class Stylesheet
6
+ def call(response)
7
+ return if response.has_content? || response.extension != '.css'
8
+ return unless content = AssetLoader.load_content(response.path)
9
+ combined_content = Combiner.new(File.dirname(response.path), REQUIRE_PATTERN).process(content)
10
+
11
+ response.set_body combined_content, :css
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ module Stylo
2
+ class Processor
3
+ def initialize
4
+ end
5
+
6
+ def get_stylesheet_path(asset)
7
+ File.join(Stylo::Config.asset_location, asset)
8
+ end
9
+
10
+ def process_asset(asset)
11
+ asset_path = get_stylesheet_path(asset)
12
+ asset_extension = File.extname(asset_path)
13
+
14
+ file_content = get_file_content(asset_path)
15
+ if !file_content.nil?
16
+ asset_dir = File.dirname(asset_path)
17
+
18
+ import_regex = asset_extension == '.css' ? /@import "(.*)";/ : /\/\/\/require "(.*)";/
19
+
20
+ process_requires file_content, asset_dir, import_regex
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
26
+ def get_file_content(asset_path)
27
+ return nil if !File.exist?(asset_path)
28
+
29
+ File.read(asset_path)
30
+ end
31
+
32
+ private
33
+
34
+ def process_requires(contents, base_path, import_regex)
35
+ contents.gsub import_regex do |match|
36
+ import_path = File.join(base_path, $1)
37
+ import_content = get_file_content(import_path)
38
+
39
+ if !import_content.nil?
40
+ process_requires(import_content, base_path, import_regex)
41
+ else
42
+ raise "Cannot find file to import '#{$1}'."
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/stylo/rack.rb ADDED
@@ -0,0 +1,20 @@
1
+ module Stylo
2
+ class Rack
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ path = env["PATH_INFO"]
9
+
10
+ response = Response.new(path)
11
+ Stylo::Config.pipeline.each { |step| step.call(response) }
12
+
13
+ if response.has_content?
14
+ response.build
15
+ else
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'rails'
3
+
4
+ module Stylo
5
+ class Railtie < Rails::Railtie
6
+ initializer "stylo.initialize" do |app|
7
+ Stylo::Config.asset_location ||= File.join(app.root, 'public')
8
+ app.middleware.insert_before ActionDispatch::Static, Stylo::Rack
9
+ end
10
+ end
11
+ end
12
+ rescue LoadError
13
+ end
@@ -0,0 +1,41 @@
1
+ module Stylo
2
+ class Response
3
+ def initialize(path)
4
+ @headers = {}
5
+ @path = path
6
+ end
7
+
8
+ attr_reader :body, :headers, :path
9
+
10
+ def has_content?
11
+ !body.nil?
12
+ end
13
+
14
+ def extension
15
+ File.extname @path
16
+ end
17
+
18
+ def set_body(content, content_type)
19
+ @body = content
20
+
21
+ set_header 'Content-Length', content.length.to_s
22
+ set_header "Content-Type", case content_type
23
+ when :css
24
+ 'text/css'
25
+ when :javascript
26
+ 'text/javascript'
27
+ else
28
+ raise "Unknown content type #{content_type}"
29
+ end
30
+ end
31
+
32
+ def set_header(name, value)
33
+ @headers[name] = value
34
+ end
35
+
36
+ def build
37
+ [200, headers, body]
38
+ end
39
+ end
40
+
41
+ end
data/lib/stylo.rb ADDED
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ Dir.glob(File.join(File.dirname(__FILE__), '**/*.rb')).each { |f| require f }
4
+
5
+
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Stylo::AssetLoader do
4
+ describe "load_content" do
5
+ it "should return nil if the content does not exist" do
6
+ Stylo::AssetLoader.load_content('javascripts/i_dont_exist.js').should be_nil
7
+ end
8
+
9
+ it "should return the file content if the content exists" do
10
+ content = "alert('hello');"
11
+ write_content(temp_path('javascripts/i_exist.js'), content)
12
+
13
+ Stylo::AssetLoader.load_content('javascripts/i_exist.js').should == content
14
+ end
15
+ end
16
+ end