Skip to content

Commit

Permalink
Configurable destination paths
Browse files Browse the repository at this point in the history
  • Loading branch information
winton committed Dec 7, 2010
1 parent e4961d7 commit 735bce8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
32 changes: 20 additions & 12 deletions lib/smart_asset.rb
Expand Up @@ -14,7 +14,7 @@
class SmartAsset
class <<self

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

BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
Expand All @@ -27,9 +27,10 @@ def binary(root, relative_config=nil)
end

def compress(type)
dest = @dest[type]
dir = "#{@pub}/#{@sources[type]}"
ext = ext_from_type type
FileUtils.mkdir_p @dest
FileUtils.mkdir_p dest
(@config[type] || {}).each do |package, files|
create_package = false
compressed = {}
Expand All @@ -43,26 +44,27 @@ def compress(type)
modified = Time.parse(modified).utc.strftime("%Y%m%d%H%M%S")
end
file = file.to_s.gsub('/', '_')
unless File.exists?(destination = "#{@dest}/#{modified}_#{package}_#{file}.#{ext}")
unless File.exists?(destination = "#{dest}/#{modified}_#{package}_#{file}.#{ext}")
create_package = true
Dir["#{@dest}/*[0-9]_#{package}_#{file}.#{ext}"].each do |old|
Dir["#{dest}/*[0-9]_#{package}_#{file}.#{ext}"].each do |old|
FileUtils.rm old
end
puts "\nCompressing #{source}..."
if ext == 'js'
cmd = "java -jar #{CLOSURE_COMPILER} --js #{source} --js_output_file #{destination} --warning_level QUIET"
warning = ENV['WARN'] ? nil : " --warning_level QUIET"
cmd = "java -jar #{CLOSURE_COMPILER} --js #{source} --js_output_file #{destination}#{warn}"
elsif ext == 'css'
cmd = "java -jar #{YUI_COMPRESSOR} #{source} -o #{destination}"
end
puts cmd
puts cmd if ENV['DEBUG']
`#{cmd}`
end
compressed[destination] = modified
end
end
if modified = compressed.values.compact.sort.last
old_packages = "#{@dest}/*[0-9]_#{package}.#{ext}"
package = "#{@dest}/#{modified}_#{package}.#{ext}"
old_packages = "#{dest}/*[0-9]_#{package}.#{ext}"
package = "#{dest}/#{modified}_#{package}.#{ext}"
if create_package || !File.exists?(package)
Dir[old_packages].each do |old|
FileUtils.rm old
Expand All @@ -85,15 +87,17 @@ def load_config(root, relative_config=nil)
@config['asset_host'] ||= ActionController::Base.asset_host rescue nil
@config['environments'] ||= %w(production)
@config['public'] ||= 'public'
@config['destination'] ||= 'packaged'
@config['destination'] ||= {}
@config['destination']['javascripts'] ||= 'javascripts/packaged'
@config['destination']['stylesheets'] ||= 'stylesheets/packaged'
@config['sources'] ||= {}
@config['sources']['javascripts'] ||= "javascripts"
@config['sources']['stylesheets'] ||= "stylesheets"

# Convert from asset packager syntax
%w(javascripts stylesheets).each do |type|
if @config[type].respond_to?(:pop)
@config[type] = @config[type].inject({}) do |package, hash|
@config[type] = @config[type].inject({}) do |hash, package|
hash.merge! package
end
end
Expand All @@ -104,7 +108,10 @@ def load_config(root, relative_config=nil)
@sources = @config['sources']

@pub = File.expand_path("#{@root}/#{@config['public']}")
@dest = "#{@pub}/#{@config['destination']}"
@dest = %w(javascripts stylesheets).inject({}) do |hash, type|
hash[type] = "#{@pub}/#{@config['destination'][type]}"
hash
end
end

def paths(type, match)
Expand All @@ -122,12 +129,13 @@ def paths(type, match)
@asset_host[@env.to_s] :
@asset_host

dest = @dest[type]
ext = ext_from_type type

if @envs.include?(@env.to_s)
match = match.gsub('/', '_')
@cache[type][match] =
if result = Dir["#{@dest}/*_#{match}.#{ext}"].sort.last
if result = Dir["#{dest}/*_#{match}.#{ext}"].sort.last
[ "#{host}#{result.gsub(@pub, '')}" ]
else
[]
Expand Down
4 changes: 3 additions & 1 deletion spec/fixtures/assets.yml
@@ -1,7 +1,9 @@
asset_host:
production: http://asset-host.com
public: spec/fixtures/assets
destination: compressed
destination:
javascripts: compressed
stylesheets: compressed
sources:
javascripts: javascripts
stylesheets: stylesheets
Expand Down
23 changes: 19 additions & 4 deletions spec/smart_asset_spec.rb
Expand Up @@ -20,7 +20,7 @@
it "should populate @config" do
SmartAsset.config.should == {
"asset_host"=>{"production"=>"http://asset-host.com"},
"destination"=>"compressed",
"destination"=>{"javascripts"=>"compressed", "stylesheets"=>"compressed"},
"environments"=>["production"],
"public"=>"spec/fixtures/assets",
"sources"=>{"javascripts"=>"javascripts", "stylesheets"=>"stylesheets"},
Expand All @@ -33,13 +33,28 @@
"empty_package"=>nil,
"non_existent_package"=>["does_not_exist"]}}
end

it "should populate @asset_host" do
SmartAsset.asset_host.should == {"production"=>"http://asset-host.com"}
end

it "should populate @dest" do
SmartAsset.dest.should == {
'javascripts' => "#{$root}/spec/fixtures/assets/compressed",
'stylesheets' => "#{$root}/spec/fixtures/assets/compressed"
}
end

it "should populate @envs" do
SmartAsset.envs.should == ["production"]
end

it "should populate @pub" do
SmartAsset.pub.should == "#{$root}/spec/fixtures/assets"
end

it "should populate @dest" do
SmartAsset.dest.should == "#{$root}/spec/fixtures/assets/compressed"
it "should populate @root" do
SmartAsset.root.should == $root
end

it "should populate @sources" do
Expand Down

0 comments on commit 735bce8

Please sign in to comment.