Class: AssetWatcher
- Inherits:
-
Object
- Object
- AssetWatcher
- Defined in:
- lib/asset_watcher.rb
Constant Summary collapse
- @@yaml_path =
"config/asset_watch.yml"
- @@attributes =
%w|src_dir dest_dir|.map(&:to_sym).freeze
- @@ext_map =
{ 'coffee' => 'js', 'haml' => 'html', }
Instance Method Summary collapse
- #compile(src_path) ⇒ Object
- #compile_coffee(src_path) ⇒ Object
- #compile_haml(src_path) ⇒ Object
- #destination(full_path) ⇒ Object
- #exts ⇒ Object
-
#initialize(config = {}) ⇒ AssetWatcher
constructor
A new instance of AssetWatcher.
- #target_files ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ AssetWatcher
Returns a new instance of AssetWatcher.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/asset_watcher.rb', line 24 def initialize config = {} current_dir = `pwd`.strip default_config = { :src_dir => current_dir + "/src", :dest_dir => current_dir + "/public", } # configuration file yaml_path = current_dir + '/' + @@yaml_path if File.exist? yaml_path yaml_config = YAML.load_file yaml_path yaml_config.symbolize_keys! default_config.merge! yaml_config end @config = default_config.merge config end |
Instance Method Details
#compile(src_path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/asset_watcher.rb', line 55 def compile src_path if src_path =~ /\.(#{exts.join '|'})$/ case $1 when 'coffee' then compile_coffee src_path when 'haml' then compile_haml src_path end else false end end |
#compile_coffee(src_path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/asset_watcher.rb', line 66 def compile_coffee src_path return false unless File.exist? src_path and src_path =~ /\.coffee/ dest_dir = File.dirname destination(src_path) puts "Compile '#{src_path}' to '#{dest_dir}'" if `which coffee`.blank? raise "'coffee' command is not found!" end `coffee -b -o #{dest_dir} #{src_path}` true end |
#compile_haml(src_path) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/asset_watcher.rb', line 80 def compile_haml src_path return false unless File.exist? src_path and src_path =~ /\.haml/ dest_path = destination src_path puts "Compile '#{src_path}' to '#{dest_path}'" # http://stackoverflow.com/questions/4549598/using-haml-with-custom-filters haml_engine = Haml::Engine.new File.read(src_path) dest_dir = File.dirname dest_path FileUtils.makedirs dest_dir unless File.directory? dest_dir File.open dest_path, 'w' do |file| file.write haml_engine.render end true end |
#destination(full_path) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/asset_watcher.rb', line 46 def destination full_path if File.exist? full_path and full_path =~ /\.(#{exts.join '|'})/ ext = $1 relative_path = full_path.sub src_dir, '' relative_path.sub! /#{ext}$/, @@ext_map[ext] dest_dir + relative_path end end |
#exts ⇒ Object
20 21 22 |
# File 'lib/asset_watcher.rb', line 20 def exts @@ext_map.keys end |
#target_files ⇒ Object
42 43 44 |
# File 'lib/asset_watcher.rb', line 42 def target_files Dir[*exts.map { |ext| "#{src_dir}/**/*\.#{ext}" }] end |