Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Added 'watch'-mode to rebuild assets as they change
Browse files Browse the repository at this point in the history
  • Loading branch information
RKushnir committed Mar 5, 2012
1 parent 2a57c36 commit 2fba3d1
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
15 changes: 13 additions & 2 deletions bin/evax
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ rescue LoadError
end

if ( ARGV.size < 2 )
Evax::Logger.log "use: $ evax <config_file_path> <relative_path>"
usage = "Usage:\n"\
"$ evax <config_file_path> <relative_path> [<options>]\n"\
"options:\n"\
"\t-w\t- watch assets for changes and rebuild when necessary"
Evax::Logger.log usage
exit 1
end

Evax.new( ARGV[0], ARGV[1] ).build
evax = Evax.new( ARGV[0], ARGV[1] )
options = ARGV[2..-1]

if options.include?("-w")
evax.run_as_daemon
else
evax.run_once
end
1 change: 1 addition & 0 deletions evax.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "delorean", "1.1.1"

s.add_dependency "uglifier", "1.2.3"
s.add_dependency "watchr", "0.7"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
46 changes: 42 additions & 4 deletions lib/evax.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "yaml"
require "uglifier"
require "watchr"

require_relative "evax/version"
require_relative "evax/css_minifier"
Expand All @@ -16,6 +17,14 @@ def initialize( config_file = "config/assets.yml", relative_path = "public/asset
Evax::Logger.log "Relative Path: #{self.relative_path}"
end

def run_once
build
end

def run_as_daemon
watch
end

def config
default_opts = { "compress" => true }
default_opts.merge(YAML::load_file( @config_file ))
Expand All @@ -32,17 +41,30 @@ def build
build_css if css_configured?
end

def build_js
config["javascripts"].each_key do |group_name|
def watch
script = Watchr::Script.new
script.watch( asset_files_regex(:javascripts) ) {|asset| build_js( js_groups_for( asset[0] ) ) } if js_configured?
script.watch( asset_files_regex(:stylesheets) ) {|asset| build_css( css_groups_for( asset[0] ) ) } if css_configured?
Watchr::Controller.new( script, Watchr.handler.new ).run
end

def build_js( group_names=[] )
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"]

write_output( "#{group_name}.js", result_string )
end
end

def build_css
config["stylesheets"].each_key do |group_name|
def build_css( group_names=[] )
groups = config["stylesheets"]
groups.select!{|k, v| group_names.include? k } if group_names.any?

groups.each_key do |group_name|
result_string = join( :stylesheets, group_name )
result_string = Evax.compress_css( result_string ) if config["compress"]

Expand Down Expand Up @@ -79,4 +101,20 @@ def css_configured?
!config["stylesheets"].nil?
end

def asset_files_regex(type)
config[type.to_s].values.flatten.uniq.map {|path| Regexp.quote( path ) }.join( "|" )
end

def js_groups_for( file )
filter_groups_by_file( :javascripts, file )
end

def css_groups_for( file )
filter_groups_by_file( :stylesheets, file )
end

def filter_groups_by_file(type, file)
config[type.to_s].select {|k, v| v.include? file }.keys
end

end
34 changes: 32 additions & 2 deletions test/evax_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,21 @@ def test_write_output
end
end

def test_build_js
def test_build_js_all_groups
evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." )
evax.expects( :write_output ).with( "js_one.js", File.read( "#{FIXTURES}/js_one.compress.js" ) )
evax.expects( :write_output ).with( "js_two.js", File.read( "#{FIXTURES}/js_two.compress.js" ) )

evax.build_js
end

def test_build_js_selected_groups
evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." )
evax.expects( :write_output ).with( "js_one.js", File.read( "#{FIXTURES}/js_one.compress.js" ) ).once

evax.build_js(["js_one"])
end

def test_build_js_compress_off
evax = Evax.new( "#{FIXTURES}/assets_compress_off.yml", "#{File.dirname(__FILE__)}/.." )
Evax.expects( :compress_js ).never
Expand All @@ -91,14 +98,21 @@ def test_build_js_compress_off
evax.build_js
end

def test_build_css
def test_build_css_all_groups
evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." )
evax.expects( :write_output ).with( "css_one.css", File.read( "#{FIXTURES}/css_one.compress.css" ) )
evax.expects( :write_output ).with( "css_two.css", File.read( "#{FIXTURES}/css_two.compress.css" ) )

evax.build_css
end

def test_build_css_selected_groups
evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." )
evax.expects( :write_output ).with( "css_one.css", File.read( "#{FIXTURES}/css_one.compress.css" ) ).once

evax.build_css(["css_one"])
end

def test_build_css_off
evax = Evax.new( "#{FIXTURES}/assets_compress_off.yml", "#{File.dirname(__FILE__)}/.." )
Evax.expects( :compress_css ).never
Expand Down Expand Up @@ -134,4 +148,20 @@ def test_build
evax.build
end

def test_watch
evax = Evax.new( "#{FIXTURES}/assets.yml", "#{File.dirname(__FILE__)}/.." )
Watchr::Controller.any_instance.stubs(:run)

watchr = mock()
Watchr::Script.stubs(:new => watchr)
watchr.expects(:watch).with do |pattern, block|
pattern == "test/fixtures/javascripts/one\\.js|test/fixtures/javascripts/two\\.js|test/fixtures/javascripts/three\\.js|test/fixtures/javascripts/four\\.js"
end
watchr.expects(:watch).with do |pattern, block|
pattern == "test/fixtures/stylesheets/one\\.css|test/fixtures/stylesheets/two\\.css|test/fixtures/stylesheets/three\\.css|test/fixtures/stylesheets/four\\.css"
end

evax.watch
end

end

0 comments on commit 2fba3d1

Please sign in to comment.