Module: Sidekick::Actions::Compile

Defined in:
lib/sidekick/actions/compile.rb

Instance Method Summary collapse

Instance Method Details

#auto_compile(source, target) ⇒ Object

watches for changes matching the ‘source` glob, and compiles to `target`, replacing ’:name’ in ‘target` with the basename of the changed file.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sidekick/actions/compile.rb', line 40

def auto_compile(source, target)
  watch(source) do |files|
    files.each do |file|
      if File.exists?(file)
          t = target.gsub(':name', File.basename(file, '.*'))
          compile file, t
          log "render #{file} => #{t}"
      end
    end
  end
end

#auto_compress(target, *sources) ⇒ Object

Watch for changes to several source files, concatenating to ‘target_base.concat.type` and then compressing as per file type.



56
57
58
59
60
61
62
63
64
65
# File 'lib/sidekick/actions/compile.rb', line 56

def auto_compress(target, *sources)
  fmt      = target.match(/\.[^\.]+$/).to_s
  cat_path = target.sub(/#{fmt}$/, '.concat' + fmt)

  watch(*sources) do |files|
    concat(cat_path, *sources)
    compress(cat_path, target)
    log "compress => #{target}"
  end
end

#compile(source, target) ⇒ Object

Compile a template using the ‘tilt` gem.



11
12
13
14
15
16
# File 'lib/sidekick/actions/compile.rb', line 11

def compile(source, target)
  needs 'tilt', 'to compile templates'
  handling Exception, source do
    write_file target, Tilt.new(source).render
  end
end

#compress(source, target, opts = {}) ⇒ Object

compress a file using a suitable compressor for that file type.



20
21
22
23
24
25
26
27
28
29
# File 'lib/sidekick/actions/compile.rb', line 20

def compress(source, target, opts={})
  handling Exception, source do
    case fmt = source.match(/\.[^\.]+$/).to_s
      when '.js'
        needs 'uglifier', 'to compress javascript'
        write_file target, Uglifier.new(opts).compile(File.read(source))
      else raise "Does not know how to compress #{fmt} files."
    end
  end
end

#concat(target, *sources) ⇒ Object

Concatenate a bunch of files, preserving order



32
33
34
# File 'lib/sidekick/actions/compile.rb', line 32

def concat(target, *sources)
  write_file target, sources.map {|p| File.read(p) }.join("\n")
end

#write_file(path, data) ⇒ Object

write a file to disk



6
7
8
# File 'lib/sidekick/actions/compile.rb', line 6

def write_file(path, data)
  File.open(path, 'w') {|f| f.write(data) }
end