Navigation Menu

Skip to content

Commit

Permalink
Adding asset_host support (picks up from Rails if not specified), spe…
Browse files Browse the repository at this point in the history
…cs running under Ruby 1.8.7
  • Loading branch information
winton committed Dec 6, 2010
1 parent 1db5510 commit 357545d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
34 changes: 24 additions & 10 deletions lib/smart_asset.rb
Expand Up @@ -14,7 +14,7 @@
class SmartAsset
class <<self

attr_accessor :cache, :config, :dest, :env, :pub, :root, :sources
attr_accessor :asset_host, :cache, :config, :dest, :env, :pub, :root, :sources

BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
Expand All @@ -37,8 +37,11 @@ def compress(type)
files.each do |file|
if File.exists?(source = "#{dir}/#{file}.#{ext}")
modified = `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}`
next if modified.empty?
modified = Time.parse(modified).utc.strftime("%Y%m%d%H%M%S")
if modified.strip.empty?
modified = Time.now.utc.strftime("%Y%m%d%H%M%S")
else
modified = Time.parse(modified).utc.strftime("%Y%m%d%H%M%S")
end
file = file.to_s.gsub('/', '_')
unless File.exists?(destination = "#{@dest}/#{modified}_#{file}.#{ext}")
create_package = true
Expand Down Expand Up @@ -77,31 +80,42 @@ def load_config(root, relative_config=nil)
@root = File.expand_path(root)
@config = YAML::load(File.read("#{@root}/#{relative_config}"))

@config['asset_host'] ||= ActionController::Base.asset_host rescue nil
@config['public'] ||= 'public'
@config['destination'] ||= 'packaged'
@config['sources'] ||= {}
@config['sources']['javascripts'] ||= "javascripts"
@config['sources']['stylesheets'] ||= "stylesheets"

@asset_host = @config['asset_host']
@sources = @config['sources']

@pub = File.expand_path("#{@root}/#{@config['public']}")
@dest = "#{@pub}/#{@config['destination']}"
@sources = @config['sources']
end

def paths(type, match)
ext = ext_from_type type
match = match.to_s

@cache ||= {}
@cache[type] ||= {}

if @cache[type][match]
@cache[type][match]
elsif @env.intern == :production
return @cache[type][match]
end

host =
@asset_host.respond_to?(:keys) ?
@asset_host[@env.to_s] :
@asset_host

ext = ext_from_type type

if @env.intern == :production
match = match.gsub('/', '_')
@cache[type][match] =
if result = Dir["#{@dest}/*_#{match}.#{ext}"].sort.last
[ result.gsub(@pub, '') ]
[ "#{host}#{result.gsub(@pub, '')}" ]
else
[]
end
Expand All @@ -110,13 +124,13 @@ def paths(type, match)
if package.to_s == match
files.collect do |file|
file = "/#{@sources[type]}/#{file}.#{ext}"
file if File.exists?("#{@pub}/#{file}")
"#{host}#{file}" if File.exists?("#{@pub}/#{file}")
end
elsif files
files.collect do |file|
if file.to_s == match
file = "/#{@sources[type]}/#{file}.#{ext}"
file if File.exists?("#{@pub}/#{file}")
"#{host}#{file}" if File.exists?("#{@pub}/#{file}")
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/assets.yml
@@ -1,3 +1,5 @@
asset_host:
production: http://asset-host.com
public: spec/fixtures/assets
destination: compressed
sources:
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/rails3/Gemfile
@@ -1,5 +1,6 @@
source 'http://rubygems.org'

gem 'framework_fixture', '0.1.2'
gem 'rails', '3.0.3'
gem 'rspec'

Expand Down
42 changes: 22 additions & 20 deletions spec/smart_asset_spec.rb
Expand Up @@ -18,7 +18,9 @@
end

it "should populate @config" do
SmartAsset.config.should == {"public"=>"spec/fixtures/assets",
SmartAsset.config.should == {
"asset_host"=>{"production"=>"http://asset-host.com"},
"public"=>"spec/fixtures/assets",
"destination"=>"compressed",
"sources"=>{"javascripts"=>"javascripts", "stylesheets"=>"stylesheets"},
"javascripts"=>
Expand Down Expand Up @@ -85,18 +87,18 @@

it "should generate correct file sizes" do
@files.each_with_index do |file, i|
File.new("#{@dest}/#{@versions[i]}_#{file}").size.should > 0
File.size("#{@dest}/#{@versions[i]}_#{file}").should > 0
end
total = 0
(2..3).each do |i|
total += File.new("#{@dest}/#{@versions[i]}_#{@files[i]}").size
total += File.size("#{@dest}/#{@versions[i]}_#{@files[i]}")
end
total.should == File.new("#{@dest}/#{@versions[1]}_#{@files[1]}").size
total.should == File.size("#{@dest}/#{@versions[1]}_#{@files[1]}")
total = 0
(4..5).each do |i|
total += File.new("#{@dest}/#{@versions[i]}_#{@files[i]}").size
total += File.size("#{@dest}/#{@versions[i]}_#{@files[i]}")
end
total.should == File.new("#{@dest}/#{@versions[0]}_#{@files[0]}").size
total.should == File.size("#{@dest}/#{@versions[0]}_#{@files[0]}")
end

unless ENV['FAST']
Expand Down Expand Up @@ -129,18 +131,18 @@

it "should generate correct file sizes" do
@files.each_with_index do |file, i|
File.new("#{@dest}/#{@versions[i]}_#{file}").size.should > 0
File.size("#{@dest}/#{@versions[i]}_#{file}").should > 0
end
total = 0
(2..3).each_with_index do |i|
total += File.new("#{@dest}/#{@versions[i]}_#{@files[i]}").size
(2..3).each_with_index do |i, x|
total += File.size("#{@dest}/#{@versions[i]}_#{@files[i]}")
end
total.should == File.new("#{@dest}/#{@versions[1]}_#{@files[1]}").size
total.should == File.size("#{@dest}/#{@versions[1]}_#{@files[1]}")
total = 0
(4..5).each do |i|
total += File.new("#{@dest}/#{@versions[i]}_#{@files[i]}").size
total += File.size("#{@dest}/#{@versions[i]}_#{@files[i]}")
end
total.should == File.new("#{@dest}/#{@versions[0]}_#{@files[0]}").size
total.should == File.size("#{@dest}/#{@versions[0]}_#{@files[0]}")
end

unless ENV['FAST']
Expand Down Expand Up @@ -191,26 +193,26 @@

it "should return compressed paths" do
SmartAsset.paths('javascripts', :package).should == [
"/compressed/20101130061253_package.js"
"http://asset-host.com/compressed/20101130061253_package.js"
]
SmartAsset.paths('javascripts', 'jquery/jquery').should == [
"/compressed/20101130061253_jquery_jquery.js"
"http://asset-host.com/compressed/20101130061253_jquery_jquery.js"
]
SmartAsset.paths('stylesheets', :package).should == [
"/compressed/20101130061253_package.css"
"http://asset-host.com/compressed/20101130061253_package.css"
]
SmartAsset.paths('stylesheets', 960).should == [
"/compressed/20101128112833_960.css"
"http://asset-host.com/compressed/20101128112833_960.css"
]
end

it "should populate @cache" do
SmartAsset.cache.should == {"javascripts"=>
{"package"=>["/compressed/20101130061253_package.js"],
"jquery_jquery"=>["/compressed/20101130061253_jquery_jquery.js"]},
{"package"=>["http://asset-host.com/compressed/20101130061253_package.js"],
"jquery_jquery"=>["http://asset-host.com/compressed/20101130061253_jquery_jquery.js"]},
"stylesheets"=>
{"package"=>["/compressed/20101130061253_package.css"],
"960"=>["/compressed/20101128112833_960.css"]}}
{"package"=>["http://asset-host.com/compressed/20101130061253_package.css"],
"960"=>["http://asset-host.com/compressed/20101128112833_960.css"]}}
end
end
end
Expand Down

0 comments on commit 357545d

Please sign in to comment.