diff --git a/.gitignore b/.gitignore index 2fc547fe..75b7c6b0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ example raws test/precache +Gemfile.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..884fc6a2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - "1.8.7" + - "1.9.2" + - "1.9.3" diff --git a/Gemfile b/Gemfile index acab7bcb..42364b3e 100644 --- a/Gemfile +++ b/Gemfile @@ -10,10 +10,11 @@ group :development, :test do gem "yui-compressor", "0.9.6" gem "closure-compiler", "1.1.6" gem "uglifier", "1.2.4" - gem "sass", "3.1.7" + gem "sass", "~>3.2" end group :development do gem "RedCloth", "4.2.9" gem "redgreen", "1.2.2" + gem "yard" end diff --git a/Rakefile b/Rakefile index 277dd2de..8082a163 100644 --- a/Rakefile +++ b/Rakefile @@ -4,8 +4,7 @@ desc 'Run all tests' task :test, [:path] do |task, args| ENV['RAILS_ENV'] = 'test' $LOAD_PATH.unshift(File.expand_path('test')) - require 'redgreen' unless Gem::Specification.find_all_by_name('redgreen').empty? - require 'test/unit' + if args[:path] require args[:path] else @@ -26,12 +25,12 @@ namespace :gem do desc 'Build and install the jammit gem' task :install do sh "gem build jammit.gemspec" - sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc" + sh "gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc" end desc 'Uninstall the jammit gem' task :uninstall do - sh "sudo gem uninstall -x jammit" + sh "gem uninstall -x jammit" end end diff --git a/bin/jammit b/bin/jammit index b38a294a..40c2f50d 100755 --- a/bin/jammit +++ b/bin/jammit @@ -1,7 +1,8 @@ -#!/usr/bin/env ruby -rrubygems +#!/usr/bin/env ruby +require 'rubygems' require 'pathname' APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath) require File.join(APP_ROOT, '../lib/jammit/command_line.rb') -Jammit::CommandLine.new \ No newline at end of file +Jammit::CommandLine.new diff --git a/jammit.gemspec b/jammit.gemspec index c3d36917..e742d5a3 100644 --- a/jammit.gemspec +++ b/jammit.gemspec @@ -1,6 +1,11 @@ +lib = File.expand_path('../lib/', __FILE__) +$:.unshift lib unless $:.include?(lib) + +require 'jammit/version' + Gem::Specification.new do |s| s.name = 'jammit' - s.version = '0.6.5' # Keep version in sync with jammit.rb + s.version = Jammit::VERSION s.date = '2011-11-30' s.homepage = "http://documentcloud.github.com/jammit/" diff --git a/lib/jammit.rb b/lib/jammit.rb index 5cd9615f..5b97171d 100644 --- a/lib/jammit.rb +++ b/lib/jammit.rb @@ -4,8 +4,6 @@ # to all of the configuration options. module Jammit - VERSION = "0.6.5" - ROOT = File.expand_path(File.dirname(__FILE__) + '/..') ASSET_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ENV['RAILS_ROOT'] || ".") unless defined?(ASSET_ROOT) diff --git a/lib/jammit/command_line.rb b/lib/jammit/command_line.rb index 07e3ef19..83c8d0bf 100644 --- a/lib/jammit/command_line.rb +++ b/lib/jammit/command_line.rb @@ -81,4 +81,4 @@ def parse_options end -end \ No newline at end of file +end diff --git a/lib/jammit/dependencies.rb b/lib/jammit/dependencies.rb index 9714bd8a..25adcdc0 100644 --- a/lib/jammit/dependencies.rb +++ b/lib/jammit/dependencies.rb @@ -7,6 +7,11 @@ require 'pathname' require 'fileutils' +# Pretty dependent on gems +unless defined? Gem + require 'rubygems' +end + # Try Uglifier. begin require 'uglifier' @@ -44,6 +49,7 @@ # Jammit Core: require 'jsmin' require 'cssmin' +require 'jammit/version' require 'jammit/jsmin_compressor' require 'jammit/cssmin_compressor' require 'jammit/compressor' diff --git a/lib/jammit/packager.rb b/lib/jammit/packager.rb index 26cc7313..184cc4ec 100644 --- a/lib/jammit/packager.rb +++ b/lib/jammit/packager.rb @@ -33,14 +33,21 @@ def initialize # changed since their last package build. def precache_all(output_dir=nil, base_url=nil) output_dir ||= File.join(Jammit.public_root, Jammit.package_path) - cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) } + + mtime = @force ? Time.now : nil + + cacheable(:js, output_dir).each do |p| + cache(p, 'js', pack_javascripts(p), output_dir, nil, mtime) + end + cacheable(:css, output_dir).each do |p| - cache(p, 'css', pack_stylesheets(p), output_dir) + cache(p, 'css', pack_stylesheets(p), output_dir, nil, mtime) + if Jammit.embed_assets - cache(p, 'css', pack_stylesheets(p, :datauri), output_dir, :datauri) + cache(p, 'css', pack_stylesheets(p, :datauri), output_dir, :datauri, mtime) if Jammit.mhtml_enabled raise MissingConfiguration, "A --base-url option is required in order to generate MHTML." unless base_url - mtime = latest_mtime package_for(p, :css)[:paths] + mtime = latest_mtime package_for(p, :css)[:paths] unless @force asset_url = "#{base_url}#{Jammit.asset_url(p, :css, :mhtml, mtime)}" cache(p, 'css', pack_stylesheets(p, :mhtml, asset_url), output_dir, :mhtml, mtime) end diff --git a/lib/jammit/version.rb b/lib/jammit/version.rb new file mode 100644 index 00000000..933ad51b --- /dev/null +++ b/lib/jammit/version.rb @@ -0,0 +1,3 @@ +module Jammit + VERSION = "0.6.5" +end diff --git a/test/fixtures/jammed/jst_test_from_cli.js b/test/fixtures/jammed/jst_test_from_cli.js new file mode 100644 index 00000000..067df6c7 --- /dev/null +++ b/test/fixtures/jammed/jst_test_from_cli.js @@ -0,0 +1 @@ +(function(){window.JST=window.JST||{};var a=function(c){var b=new Function("obj","var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('"+c.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/<%=([\s\S]+?)%>/g,function(d,e){return"',"+e.replace(/\\'/g,"'")+",'"}).replace(/<%([\s\S]+?)%>/g,function(d,e){return"');"+e.replace(/\\'/g,"'").replace(/[\r\n\t]/g," ")+"__p.push('"}).replace(/\r/g,"\\r").replace(/\n/g,"\\n").replace(/\t/g,"\\t")+"');}return __p.join('');");return b};window.JST.template1=a('<%= saying_something %>');window.JST.template2=a('<% _([1,2,3]).each(function(num) { %>\n
  • \n <%= num %>\n
  • \n<% }) %>')})(); \ No newline at end of file diff --git a/test/integration/test_command_line.rb b/test/integration/test_command_line.rb new file mode 100644 index 00000000..42516166 --- /dev/null +++ b/test/integration/test_command_line.rb @@ -0,0 +1,53 @@ +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' +require 'zlib' + +class CommandLineTest < Test::Unit::TestCase + JAMMIT = "bundle exec bin/jammit" + + def setup + ENV['RAILS_ROOT'] = 'test' + end + + def teardown + begin + FileUtils.rm_r('test/precache') + rescue Errno::ENOENT + end + end + + def test_version_and_help_can_run + assert system("#{ JAMMIT } -v > /dev/null") + assert system("#{ JAMMIT } -h > /dev/null") + end + + def test_jammit_precaching + system("#{ JAMMIT } -c test/config/assets.yml -o test/precache -u http://www.example.com") + assert_equal PRECACHED_FILES, glob('test/precache/*') + + assert_equal zlib_read('test/precache/css_test-datauri.css.gz'), + File.read('test/fixtures/jammed/css_test-datauri.css') + + assert_equal zlib_read('test/precache/jst_test.js.gz'), + File.read('test/fixtures/jammed/jst_test_from_cli.js') + + assert_equal zlib_read('test/precache/js_test_with_templates.js.gz'), + File.read('test/fixtures/jammed/js_test_with_templates.js') + end + + def test_lazy_precaching + system("#{ JAMMIT } -c test/config/assets.yml -o test/precache -u http://www.example.com") + assert_equal PRECACHED_FILES, glob('test/precache/*') + mtime = File.mtime(PRECACHED_FILES.first) + system("#{ JAMMIT } -c test/config/assets.yml -o test/precache -u http://www.example.com") + assert_equal File.mtime(PRECACHED_FILES.first), mtime + system("#{ JAMMIT } -c test/config/assets.yml -o test/precache -u http://www.example.com --force") + new_mtime = File.mtime(PRECACHED_FILES.first) + assert new_mtime > mtime, + "#{ PRECACHED_FILES.first } mtime - #{ new_mtime } - greater than #{ mtime }" + end + + def zlib_read(filename) + Zlib::GzipReader.open(filename) {|f| f.read } + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb index b1fe3497..65b968d2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,6 @@ Encoding.default_external = 'ascii' if defined? Encoding +require 'test/unit' require 'logger' ASSET_ROOT = File.expand_path(File.dirname(__FILE__)) diff --git a/test/unit/command_line_test.rb b/test/unit/command_line_test.rb deleted file mode 100644 index 08ec9ec2..00000000 --- a/test/unit/command_line_test.rb +++ /dev/null @@ -1,35 +0,0 @@ -require 'test_helper' -require 'zlib' - -class CommandLineTest < Test::Unit::TestCase - - def teardown - begin - FileUtils.rm_r('test/precache') - rescue Errno::ENOENT - end - end - - def test_version_and_help_can_run - assert system('bin/jammit -v') && system('bin/jammit -h') - end - - def test_jammit_precaching - system('bin/jammit -c test/config/assets.yml -o test/precache -u http://www.example.com') - assert PRECACHED_FILES == glob('test/precache/*') - assert Zlib::GzipReader.open('test/precache/css_test-datauri.css.gz') {|f| f.read } == File.read('test/fixtures/jammed/css_test-datauri.css') - assert Zlib::GzipReader.open('test/precache/jst_test.js.gz') {|f| f.read } == File.read('test/fixtures/jammed/jst_test.js') - assert Zlib::GzipReader.open('test/precache/js_test_with_templates.js.gz') {|f| f.read } == File.read('test/fixtures/jammed/js_test_with_templates.js') - end - - def test_lazy_precaching - system('bin/jammit -c test/config/assets.yml -o test/precache -u http://www.example.com') - assert PRECACHED_FILES == glob('test/precache/*') - mtime = File.mtime(PRECACHED_FILES.first) - system('bin/jammit -c test/config/assets.yml -o test/precache -u http://www.example.com') - assert File.mtime(PRECACHED_FILES.first) == mtime - system('bin/jammit -c test/config/assets.yml -o test/precache -u http://www.example.com --force') - assert File.mtime(PRECACHED_FILES.first) > mtime - end - -end diff --git a/test/unit/test_closure_compressor.rb b/test/unit/test_closure_compressor.rb index 6a3dacbc..f2691611 100644 --- a/test/unit/test_closure_compressor.rb +++ b/test/unit/test_closure_compressor.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class ClosureCompressorTest < Test::Unit::TestCase diff --git a/test/unit/test_compressor.rb b/test/unit/test_compressor.rb index eec8ad67..f0b25d23 100644 --- a/test/unit/test_compressor.rb +++ b/test/unit/test_compressor.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class CompressorTest < Test::Unit::TestCase diff --git a/test/unit/test_configuration.rb b/test/unit/test_configuration.rb index 1376a855..1c3726aa 100644 --- a/test/unit/test_configuration.rb +++ b/test/unit/test_configuration.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class BrokenConfigurationTest < Test::Unit::TestCase @@ -26,8 +26,15 @@ def test_disabled_compression # Nothing should change with jst. packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst')) assert_equal packed, File.read('test/fixtures/jammed/jst_test.js') + # CSS packed = @compressor.compress_css(glob('test/fixtures/src/*.css')) - assert_equal packed, File.open('test/fixtures/jammed/css_test-uncompressed.css', 'rb') {|f| f.read } + uncompressed_css = File.read('test/fixtures/jammed/css_test-uncompressed.css') + + # fun with unicode + if uncompressed_css.respond_to?(:encoding) + uncompressed_css.force_encoding("UTF-8") + end + assert_equal packed, uncompressed_css end def test_css_compression diff --git a/test/unit/test_in_the_wrong_directory.rb b/test/unit/test_in_the_wrong_directory.rb index cc7a3929..0338e646 100644 --- a/test/unit/test_in_the_wrong_directory.rb +++ b/test/unit/test_in_the_wrong_directory.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class WrongDirectoryTest < Test::Unit::TestCase diff --git a/test/unit/test_jammit_controller.rb b/test/unit/test_jammit_controller.rb index 3a66ab5b..116c25d8 100644 --- a/test/unit/test_jammit_controller.rb +++ b/test/unit/test_jammit_controller.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' require 'active_support' require 'action_pack' require 'action_controller' diff --git a/test/unit/test_jammit_helpers.rb b/test/unit/test_jammit_helpers.rb index c7ca9739..33a837a5 100644 --- a/test/unit/test_jammit_helpers.rb +++ b/test/unit/test_jammit_helpers.rb @@ -1,9 +1,13 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' require 'action_pack' require 'action_view' require 'action_view/base' require 'action_controller' require 'action_controller/base' + +# fix for ruby 1.9.3 compatibility with rails 2.3.14 +MissingSourceFile::REGEXPS << [/^cannot load such file -- (.+)$/i, 1] + require 'action_view/test_case' require 'jammit/controller' require 'jammit/helper' diff --git a/test/unit/test_packager.rb b/test/unit/test_packager.rb index 38743ba6..34de48bd 100644 --- a/test/unit/test_packager.rb +++ b/test/unit/test_packager.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' require 'zlib' class PackagerTest < Test::Unit::TestCase diff --git a/test/unit/test_sass_compressor.rb b/test/unit/test_sass_compressor.rb index 3b2b5761..8c5b2b3d 100644 --- a/test/unit/test_sass_compressor.rb +++ b/test/unit/test_sass_compressor.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class SassCompressorTest < Test::Unit::TestCase def test_css_compression @@ -7,4 +7,4 @@ def test_css_compression assert_equal File.read('test/fixtures/jammed/css_test-sass.css'), packed end -end \ No newline at end of file +end diff --git a/test/unit/test_uglifier.rb b/test/unit/test_uglifier.rb index 61ee4124..5901045e 100644 --- a/test/unit/test_uglifier.rb +++ b/test/unit/test_uglifier.rb @@ -1,4 +1,4 @@ -require 'test_helper' +$: << File.expand_path(File.dirname(__FILE__) + "/..") ; require 'test_helper' class UglifierText < Test::Unit::TestCase @@ -21,4 +21,4 @@ def test_jst_compilation assert packed == File.read('test/fixtures/jammed/jst_test.js') end -end \ No newline at end of file +end