diff --git a/evax.gemspec b/evax.gemspec index 6c0aec4..e37bc6a 100644 --- a/evax.gemspec +++ b/evax.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |s| s.rubyforge_project = "evax" - s.add_development_dependency "bundler", "1.1.0" + s.add_development_dependency "bundler", "1.3.5" s.add_development_dependency "rake", "0.9.2.2" s.add_development_dependency "mocha", "0.10.0" s.add_development_dependency "delorean", "1.1.1" diff --git a/lib/evax.rb b/lib/evax.rb index 0d06790..5a37d3b 100644 --- a/lib/evax.rb +++ b/lib/evax.rb @@ -26,7 +26,7 @@ def run_as_daemon end def config - default_opts = { "compress" => true } + default_opts = { "compress" => true, "compiled_datetime" => false } default_opts.merge(YAML::load_file( @config_file )) end @@ -49,12 +49,15 @@ def watch end def build_js( group_names=[] ) + options = symbolize_hash( config["options"] || {} ) + groups = config["javascripts"] groups.select!{|k, v| group_names.include? k } if group_names.any? groups.each_key do |group_name| result_string = join( :javascripts, group_name ) - result_string = Evax.compress_js( result_string ) if config["compress"] + result_string = Evax.compress_js( result_string, options ) if config["compress"] + result_string = Evax.compiled_datetime( result_string ) if config["compiled_datetime"] write_output( "#{group_name}.js", result_string ) end @@ -67,6 +70,7 @@ def build_css( group_names=[] ) groups.each_key do |group_name| result_string = join( :stylesheets, group_name ) result_string = Evax.compress_css( result_string ) if config["compress"] + result_string = Evax.compiled_datetime( result_string ) if config["compiled_datetime"] write_output( "#{group_name}.css", result_string ) end @@ -81,9 +85,13 @@ def write_output( file_name, string ) FileUtils.mkdir_p path File.open( file_path, 'w' ) { |f| f.write string } end + + def self.compiled_datetime( result_string ) + "/* Compiled on #{Time.new} */\n#{result_string}" + end - def self.compress_js( js_string ) - opts = { :copyright => false } + def self.compress_js( js_string, options = {} ) + opts = { :copyright => false }.merge!( options ) Uglifier.compile( js_string, opts ) end @@ -93,6 +101,13 @@ def self.compress_css( css_string ) private + def symbolize_hash(hash) + hash.inject({}) do |symbol_hash, (k,v)| + symbol_hash[k.to_sym] = v.is_a?(Hash) ? symbolize_hash(v) : v + symbol_hash + end + end + def js_configured? !config["javascripts"].nil? end @@ -117,4 +132,4 @@ def filter_groups_by_file(type, file) config[type.to_s].select {|k, v| v.include? file }.keys end -end +end \ No newline at end of file diff --git a/test/evax_test.rb b/test/evax_test.rb index e426944..efc2a86 100644 --- a/test/evax_test.rb +++ b/test/evax_test.rb @@ -28,6 +28,11 @@ def test_read_config_file_compress_off assert_equal( false, evax.config["compress"] ) end + def test_read_config_file_compiled_datetime + evax = Evax.new( "#{FIXTURES}/assets_compiled_datetime.yml") + assert_equal( true, evax.config["compiled_datetime"] ) + end + def test_join evax = Evax.new( "#{FIXTURES}/assets.yml", "/wadus" ) @@ -59,6 +64,17 @@ def test_compress_js assert_equal( File.read( "#{FIXTURES}/js_one.compress.js" ), result) end + def test_compress_js_receives_options + + evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." ) + Evax.expects( :compress_js ).at_least_once.with do |js, opts| + opts == { :max_line_length => 800, + :beautify_options => + { :indent_level => 4 }} + end + evax.build_js + end + def test_compress_css result = Evax.compress_css( File.read( "#{FIXTURES}/css_one.css" ) ) diff --git a/test/fixtures/assets.yml b/test/fixtures/assets.yml index 432fb7d..7c00f1b 100644 --- a/test/fixtures/assets.yml +++ b/test/fixtures/assets.yml @@ -1,5 +1,8 @@ output_path: tmp/ - +options: + max_line_length: 800 + beautify_options: + indent_level: 4 javascripts: js_one: - test/fixtures/javascripts/one.js diff --git a/test/fixtures/assets_compiled_datetime.yml b/test/fixtures/assets_compiled_datetime.yml new file mode 100644 index 0000000..23037d2 --- /dev/null +++ b/test/fixtures/assets_compiled_datetime.yml @@ -0,0 +1,19 @@ +compress: on +compiled_datetime: true +output_path: tmp/ + +javascripts: + js_one: + - test/fixtures/javascripts/one.js + - test/fixtures/javascripts/two.js + - test/fixtures/javascripts/three.js + js_two: + - test/fixtures/javascripts/four.js + +stylesheets: + css_one: + - test/fixtures/stylesheets/one.css + - test/fixtures/stylesheets/two.css + css_two: + - test/fixtures/stylesheets/three.css + - test/fixtures/stylesheets/four.css