diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..5d5945b --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source 'http://rubygems.org' + +gem 'rails', '2.3.8' +gem 'rack' +gem 'mocha' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..421be26 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,32 @@ +GEM + remote: http://rubygems.org/ + specs: + actionmailer (2.3.8) + actionpack (= 2.3.8) + actionpack (2.3.8) + activesupport (= 2.3.8) + rack (~> 1.1.0) + activerecord (2.3.8) + activesupport (= 2.3.8) + activeresource (2.3.8) + activesupport (= 2.3.8) + activesupport (2.3.8) + mocha (0.9.8) + rake + rack (1.1.0) + rails (2.3.8) + actionmailer (= 2.3.8) + actionpack (= 2.3.8) + activerecord (= 2.3.8) + activeresource (= 2.3.8) + activesupport (= 2.3.8) + rake (>= 0.8.3) + rake (0.8.7) + +PLATFORMS + ruby + +DEPENDENCIES + mocha + rack + rails (= 2.3.8) diff --git a/Rakefile b/Rakefile index ca20585..7db3793 100644 --- a/Rakefile +++ b/Rakefile @@ -17,6 +17,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'AssetPackager' rdoc.options << '--line-numbers' << '--inline-source' - rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('Readme.rdoc') rdoc.rdoc_files.include('lib/**/*.rb') end diff --git a/README b/Readme.rdoc similarity index 100% rename from README rename to Readme.rdoc diff --git a/lib/synthesis/asset_package.rb b/lib/synthesis/asset_package.rb index 99c9f7a..7fe7287 100644 --- a/lib/synthesis/asset_package.rb +++ b/lib/synthesis/asset_package.rb @@ -153,14 +153,14 @@ def compressed_file end def compress_js(source) - jsmin_path = "#{Rails.root}/vendor/plugins/asset_packager/lib" + jsmin_path = File.join(File.dirname(__FILE__), 'jsmin.rb') tmp_path = "#{Rails.root}/tmp/#{@target}_packaged" # write out to a temp file File.open("#{tmp_path}_uncompressed.js", "w") {|f| f.write(source) } # compress file with JSMin library - `ruby #{jsmin_path}/jsmin.rb <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n` + `ruby #{jsmin_path} <#{tmp_path}_uncompressed.js >#{tmp_path}_compressed.js \n` # read it back in and trim it result = "" @@ -180,6 +180,23 @@ def compress_css(source) source.gsub!(/\n$/, "") # remove last break source.gsub!(/ \{ /, " {") # trim inside brackets source.gsub!(/; \}/, "}") # trim inside brackets + + # add timestamps to images in css + source.gsub!(/url\(['"]?([^'"\)]+?(?:gif|png|jpe?g))['"]?\)/i) do |match| + + file = $1 + path = File.join(Rails.root, 'public') + + if file.starts_with?('/') + path = File.join(path, file) + else + path = File.join(path, 'stylesheets', file) + end + + + match.gsub(file, "#{file}?#{File.new(path).mtime.to_i}") + end + source end diff --git a/lib/synthesis/asset_package_helper.rb b/lib/synthesis/asset_package_helper.rb index 77674f4..2c3d51e 100644 --- a/lib/synthesis/asset_package_helper.rb +++ b/lib/synthesis/asset_package_helper.rb @@ -21,7 +21,7 @@ def javascript_include_merged(*sources) AssetPackage.targets_from_sources("javascripts", sources) : AssetPackage.sources_from_targets("javascripts", sources)) - sources.collect {|source| javascript_include_tag(source, options) }.join("\n") + sources.collect {|source| javascript_include_tag(source, options) }.join("\n").html_safe end def stylesheet_link_merged(*sources) @@ -32,7 +32,7 @@ def stylesheet_link_merged(*sources) AssetPackage.targets_from_sources("stylesheets", sources) : AssetPackage.sources_from_targets("stylesheets", sources)) - sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n") + sources.collect { |source| stylesheet_link_tag(source, options) }.join("\n").html_safe end end diff --git a/lib/jsmin.rb b/lib/synthesis/jsmin.rb similarity index 100% rename from lib/jsmin.rb rename to lib/synthesis/jsmin.rb diff --git a/test/asset_package_helper_development_test.rb b/test/asset_package_helper_development_test.rb index 4e2b919..cb935ed 100644 --- a/test/asset_package_helper_development_test.rb +++ b/test/asset_package_helper_development_test.rb @@ -1,13 +1,6 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') - -ENV['RAILS_ENV'] = "development" -require File.dirname(__FILE__) + '/../../../../config/environment' -require 'test/unit' -require 'rubygems' -require 'mocha' - -require 'action_controller/test_process' +require 'test/test_helper' +Rails.env = 'development' ActionController::Base.logger = nil ActionController::Routing::Routes.reload rescue nil @@ -19,8 +12,8 @@ class AssetPackageHelperDevelopmentTest < Test::Unit::TestCase include Synthesis::AssetPackageHelper def setup - Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets" - Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml") + Synthesis::AssetPackage.asset_base_path = "test/assets" + Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml") Synthesis::AssetPackage.any_instance.stubs(:log) diff --git a/test/asset_package_helper_production_test.rb b/test/asset_package_helper_production_test.rb index 94e1362..85e5f1d 100644 --- a/test/asset_package_helper_production_test.rb +++ b/test/asset_package_helper_production_test.rb @@ -1,14 +1,5 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') - -require File.dirname(__FILE__) + '/../../../../config/environment' -require 'test/unit' -require 'rubygems' -require 'mocha' - -require 'action_controller/test_process' - -ActionController::Base.logger = nil -ActionController::Routing::Routes.reload rescue nil +require 'test/test_helper' +Rails.env = 'development' class AssetPackageHelperProductionTest < Test::Unit::TestCase include ActionController::Assertions::DomAssertions @@ -20,8 +11,8 @@ class AssetPackageHelperProductionTest < Test::Unit::TestCase cattr_accessor :packages_built def setup - Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets" - Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml") + Synthesis::AssetPackage.asset_base_path = "test/assets" + Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml") Synthesis::AssetPackage.any_instance.stubs(:log) self.stubs(:should_merge?).returns(true) diff --git a/test/asset_packager_test.rb b/test/asset_packager_test.rb index dbeb52b..5645c97 100644 --- a/test/asset_packager_test.rb +++ b/test/asset_packager_test.rb @@ -1,13 +1,11 @@ -require File.dirname(__FILE__) + '/../../../../config/environment' -require 'test/unit' -require 'mocha' +require 'test/test_helper' class AssetPackagerTest < Test::Unit::TestCase include Synthesis def setup - Synthesis::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets" - Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml") + Synthesis::AssetPackage.asset_base_path = "test/assets" + Synthesis::AssetPackage.asset_packages_yml = YAML.load_file("test/asset_packages.yml") Synthesis::AssetPackage.any_instance.stubs(:log) Synthesis::AssetPackage.build_all diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..9ba73f8 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,37 @@ +require 'rubygems' + +`rm -rf test/fake_root` +`mkdir -p test/fake_root/tmp` + +module Rails + def self.root + File.expand_path("test/fake_root") + end + + def self.backtrace_cleaner + ActiveSupport::BacktraceCleaner.new + end + + def self.env=(x) + @env = x + end + + def self.env + @env + end +end + +require 'rack' +require 'action_view' +require 'action_controller' +require 'rails/backtrace_cleaner' + +$LOAD_PATH << 'lib' +require 'init' + +require 'test/unit' +require 'action_controller/test_process' +require 'mocha' + +ActionController::Base.logger = nil +ActionController::Routing::Routes.reload rescue nil \ No newline at end of file