class AssetWatcher

Public Class Methods

new(config = {}) click to toggle source
# 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

Public Instance Methods

compile(src_path) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/asset_watcher.rb, line 20
def exts
  @@ext_map.keys
end
target_files() click to toggle source
# File lib/asset_watcher.rb, line 42
def target_files
  Dir[*exts.map { |ext| "#{src_dir}/**/*\.#{ext}" }]
end