Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 95c3b9ade2e3db5d356dd31971a4f4f84abed944 @thegorgon thegorgon committed Oct 23, 2011
10 Gemfile
@@ -0,0 +1,10 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem 'sprockets', '~> 2.0'
+gem 'uglifier'
+gem 'closure-compiler'
+gem 'yui-compressor', :require => "yui/compressor"
+gem 'execjs'
+gem 'therubyracer'
No changes.
@@ -0,0 +1 @@
+require 'sinatra/sprockets'
@@ -0,0 +1,51 @@
+require 'sinatra/sprockets/configuration'
+require 'sinatra/sprockets/asset_paths'
+require 'sinatra/sprockets/helpers'
+require 'sinatra/sprockets/static_compiler'
+
+module Sinatra
+ module Sprockets
+ def self.configure(&block)
+ block.call(config)
+ raise ArgumentError, "Cannot initialize Sprockets Environment without an app reference" if config.app.nil?
+
+ @environment = ::Sprockets::Environment.new(config.app.root)
+
+ config.paths.each do |path|
+ environment.append_path(File.join(config.app.root, path))
+ end
+
+ if config.compress_assets?
+ environment.js_compressor = Closure::Compiler.new
+ environment.css_compressor = YUI::CssCompressor.new
+ else
+ environment.js_compressor = false
+ environment.css_compressor = false
+ end
+
+ if config.manifest_path
+ path = File.join(config.app.root, config.manifest_path, "manifest.yml")
+ else
+ path = File.join(config.app.settings.public_path, 'assets', "manifest.yml")
+ end
+
+ if File.exist?(path)
+ YAML.load_file(path).each do |path, value|
+ config.digests[path] = value
+ end
+ end
+
+ environment.context_class.instance_eval do
+ include Helpers
+ end
+ end
+
+ def self.config
+ @config ||= Configuration.new
+ end
+
+ def self.environment
+ @environment
+ end
+ end
+end
@@ -0,0 +1,131 @@
+module Sinatra
+ module Sprockets
+ class AssetPaths
+ attr_reader :config
+
+ class AssetNotPrecompiledError < StandardError; end
+
+ def initialize(config)
+ @config = config
+ end
+
+ def asset_for(source, ext)
+ source = source.to_s
+ return nil if is_uri?(source)
+ source = rewrite_extension(source, nil, ext)
+ config.environment[source]
+ rescue ::Sprockets::FileOutsidePaths
+ nil
+ end
+
+ def digest_for(logical_path)
+ if config.digest_assets? && config.digests && (digest = config.digests[logical_path])
+ digest
+ else
+ if config.compile_assets?
+ if config.digest_assets? && asset = config.environment[logical_path]
+ asset.digest_path
+ else
+ logical_path
+ end
+ else
+ raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
+ end
+ end
+ end
+
+ def compute_public_path(source, dir, options = {})
+ source = source.to_s
+ unless is_uri?(source)
+ source = rewrite_extension(source, dir, options[:ext]) if options[:ext]
+ source = rewrite_asset_path(source, dir, options)
+ source = rewrite_relative_url_root(source, config.relative_url_root)
+ source = rewrite_host_and_protocol(source, options[:protocol])
+ end
+ source
+ end
+
+ def is_uri?(path)
+ path =~ %r{^[-a-z]+://|^cid:|^//}
+ end
+
+ def rewrite_host_and_protocol(source, protocol = nil)
+ host = compute_asset_host(source)
+ if host && !is_uri?(host)
+ if (protocol || default_protocol) == :request && !has_request?
+ host = nil
+ else
+ host = "#{compute_protocol(protocol)}#{host}"
+ end
+ end
+ host ? "#{host}#{source}" : source
+ end
+
+ def rewrite_relative_url_root(source, relative_url_root)
+ relative_url_root && !source.starts_with?("#{relative_url_root}/") ? "#{relative_url_root}#{source}" : source
+ end
+
+ def rewrite_asset_path(source, dir, options = {})
+ if source[0] == ?/
+ source
+ else
+ source = digest_for(source) unless options[:digest] == false
+ source = File.join(dir, source)
+ source = "/#{source}" unless source =~ /^\//
+ source
+ end
+ end
+
+ def rewrite_extension(source, dir, ext)
+ if ext && File.extname(source).empty?
+ "#{source}.#{ext}"
+ else
+ source
+ end
+ end
+
+ def compute_asset_host(source)
+ if host = config.host
+ if host.respond_to?(:call)
+ args = [source]
+ arity = arity_of(host)
+ if arity > 1 && request.nil?
+ invalid_asset_host!("Remove the second argument to your asset_host Proc if you do not need the request.")
+ end
+ args << current_request if (arity > 1 || arity < 0) && has_request?
+ host.call(*args)
+ else
+ (host =~ /%d/) ? host % (Zlib.crc32(source) % 4) : host
+ end
+ end
+ end
+
+ def default_protocol
+ config.default_protocol || (request.nil?? :relative : :request)
+ end
+
+ def compute_protocol(protocol)
+ protocol ||= default_protocol
+ case protocol
+ when :request
+ if request.nil?
+ invalid_asset_host!("The protocol requested was :request. Consider using :relative instead.")
+ end
+ request.protocol
+ when :relative
+ "//"
+ else
+ "#{protocol}://"
+ end
+ end
+
+ def arity_of(callable)
+ callable.respond_to?(:arity) ? callable.arity : callable.method(:call).arity
+ end
+
+ def invalid_asset_host!(help_message)
+ raise ActionController::RoutingError, "This asset host cannot be computed without a request in scope. #{help_message}"
+ end
+ end
+ end
+end
@@ -0,0 +1,69 @@
+module Sinatra
+ module Sprockets
+ class Configuration
+ DEFAULTS = {
+ :digest => true,
+ :debug => false,
+ :compile => true,
+ :compress => true,
+ :prefix => "assets",
+ :host => nil,
+ :relative_url_root => ENV['RACK_RELATIVE_URL_ROOT'],
+ :precompile => [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ],
+ :manifest_path => "public/assets",
+ :app => nil
+ }
+ OPTIONS = DEFAULTS.keys
+
+ OPTIONS.each do |option|
+ define_method "#{option}=" do |value|
+ (@_config ||= {})[option] = value
+ end
+
+ define_method "#{option}" do
+ (@_config ||= {})[option]
+ end
+ end
+
+ [:digest, :debug, :compile, :compress].each do |option|
+ define_method "#{option}_assets?" do
+ !! @_config[option]
+ end
+ end
+
+ def initialize
+ @_config = DEFAULTS
+ @_digests = {}
+ @_paths = []
+ end
+
+ def digests
+ @_digests
+ end
+
+ def digests=(value)
+ @_digests = value
+ end
+
+ def append_path(path)
+ @_paths << path
+ end
+
+ def paths=(value)
+ @_paths = value.to_a
+ end
+
+ def paths
+ @_paths
+ end
+
+ def inspect
+ @_config.inspect
+ end
+
+ def environment
+ Sinatra::Sprockets.environment
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 95c3b9a

Please sign in to comment.