Class: AssetWatcher

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(config = {}) ⇒ AssetWatcher

Returns a new instance of AssetWatcher.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/asset_watcher.rb', line 25

def initialize config = {}
  current_dir = `pwd`.strip
  default_config = {
    :src_dir => current_dir + "/src",
    :dest_dir => current_dir,
  }
  _config = default_config.dup

  _config[:src_dir]  = ARGV[1] if ARGV.count >= 2
  _config[:dest_dir] = ARGV[2] if ARGV.count >= 3

  # configuration file
  yaml_path = current_dir + '/' + @@yaml_path
  if File.exist? yaml_path
    yaml_config = YAML.load_file yaml_path
    yaml_config.symbolize_keys!
    _config.merge! yaml_config
  end

  @config = _config.merge config
end

Instance Method Details

#compile(src_path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/asset_watcher.rb', line 60

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
    else false
    end
  else
    false
  end
end

#compile_coffee(src_path) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/asset_watcher.rb', line 72

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

  command = "coffee -b -o #{dest_dir} #{src_path}"
  stdin, stdout, stderr = *Open3.popen3(command)
  stdout.each { |line| puts line }
  error = stderr.read

  unless result = error.empty?
    puts error
    puts "fail to compile '#{src_path}'"
  end
  return result
end

#compile_haml(src_path) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/asset_watcher.rb', line 94

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



51
52
53
54
55
56
57
58
# File 'lib/asset_watcher.rb', line 51

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

#extsObject



21
22
23
# File 'lib/asset_watcher.rb', line 21

def exts
  @@ext_map.keys
end

#target_filesObject



47
48
49
# File 'lib/asset_watcher.rb', line 47

def target_files
  Dir[*exts.map { |ext| "#{src_dir}/**/*\.#{ext}" }]
end