From 6696bc6fafbb5438aa264317609550381050327c Mon Sep 17 00:00:00 2001 From: Marc Roberts Date: Thu, 28 Oct 2010 17:55:58 +0100 Subject: [PATCH 1/7] add ?xxxxx timestamp suffixes to images referenced in css, when compressing --- lib/synthesis/asset_package.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/synthesis/asset_package.rb b/lib/synthesis/asset_package.rb index 99c9f7a..48c620c 100644 --- a/lib/synthesis/asset_package.rb +++ b/lib/synthesis/asset_package.rb @@ -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 From 92bd55d11ba43f0aecebd8068634a22b61e03e9b Mon Sep 17 00:00:00 2001 From: Anders Jacobsson Date: Fri, 22 Oct 2010 03:56:58 +0800 Subject: [PATCH 2/7] Rails 3 XSS fixes (using html_safe) --- lib/synthesis/asset_package_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 1fab677b2671c39db8a66313c9cf6ebdccef9170 Mon Sep 17 00:00:00 2001 From: grosser Date: Thu, 19 Aug 2010 02:54:25 +0800 Subject: [PATCH 3/7] make readme readable on github --- Rakefile | 2 +- README => Readme.rdoc | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename README => Readme.rdoc (100%) 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 From cd1e12de4a593d129c840ebd9e4e1ad09152ef3a Mon Sep 17 00:00:00 2001 From: grosser Date: Thu, 19 Aug 2010 04:00:36 +0800 Subject: [PATCH 4/7] make test/asset_packager_test.rb run standalone --- lib/synthesis/asset_package.rb | 4 ++-- test/asset_packager_test.rb | 8 +++----- test/test_helper.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 test/test_helper.rb diff --git a/lib/synthesis/asset_package.rb b/lib/synthesis/asset_package.rb index 48c620c..7993014 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 = "" 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..cfaec61 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,18 @@ +require 'rubygems' + +`rm -rf test/fake_root` +`mkdir -p test/fake_root/tmp` + +class Rails + def self.root + File.expand_path("test/fake_root") + end +end + +require 'action_view' + +$LOAD_PATH << 'lib' +require 'init' + +require 'test/unit' +require 'mocha' From d1fb124d7546b29eedf397b59627a66ebbc6b6f9 Mon Sep 17 00:00:00 2001 From: grosser Date: Thu, 19 Aug 2010 04:02:05 +0800 Subject: [PATCH 5/7] remove jsmin from top-level namespace --- lib/synthesis/asset_package.rb | 2 +- lib/{ => synthesis}/jsmin.rb | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/{ => synthesis}/jsmin.rb (100%) diff --git a/lib/synthesis/asset_package.rb b/lib/synthesis/asset_package.rb index 7993014..7fe7287 100644 --- a/lib/synthesis/asset_package.rb +++ b/lib/synthesis/asset_package.rb @@ -153,7 +153,7 @@ def compressed_file end def compress_js(source) - jsmin_path = File.join(File.dirname(__FILE__), '..', 'jsmin.rb') + jsmin_path = File.join(File.dirname(__FILE__), 'jsmin.rb') tmp_path = "#{Rails.root}/tmp/#{@target}_packaged" # write out to a temp file 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 From a83b511114e8618c83d93ec95527ab3776c00860 Mon Sep 17 00:00:00 2001 From: grosser Date: Thu, 19 Aug 2010 04:12:20 +0800 Subject: [PATCH 6/7] add bundler to the mix, make test/asset_package_helper_development_test.rb run without rails environment --- Gemfile | 6 ++++ Gemfile.lock | 33 +++++++++++++++++++ test/asset_package_helper_development_test.rb | 15 +++------ test/test_helper.rb | 18 +++++++++- 4 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..100d967 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source 'http://rubygems.org' + +gem 'actionpack', '2.3.8', :require => 'action_pack' +gem 'rails', '2.3.8' +gem 'rack' +gem 'mocha' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..734904b --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,33 @@ +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 + actionpack (= 2.3.8) + mocha + rack + rails (= 2.3.8) 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/test_helper.rb b/test/test_helper.rb index cfaec61..87d65fb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,16 +3,32 @@ `rm -rf test/fake_root` `mkdir -p test/fake_root/tmp` -class Rails +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' From 4ec5f378e6f50790c6a7db472f3c618aa25db982 Mon Sep 17 00:00:00 2001 From: grosser Date: Thu, 19 Aug 2010 04:15:36 +0800 Subject: [PATCH 7/7] make test/asset_package_helper_production_test.rb run without rails environment --- Gemfile | 1 - Gemfile.lock | 1 - test/asset_package_helper_production_test.rb | 17 ++++------------- test/test_helper.rb | 3 +++ 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 100d967..5d5945b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,5 @@ source 'http://rubygems.org' -gem 'actionpack', '2.3.8', :require => 'action_pack' gem 'rails', '2.3.8' gem 'rack' gem 'mocha' diff --git a/Gemfile.lock b/Gemfile.lock index 734904b..421be26 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,7 +27,6 @@ PLATFORMS ruby DEPENDENCIES - actionpack (= 2.3.8) mocha rack rails (= 2.3.8) 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/test_helper.rb b/test/test_helper.rb index 87d65fb..9ba73f8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,3 +32,6 @@ def self.env 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