Class: Convey::Compiler

Inherits:
Object
  • Object
show all
Includes:
Convey
Defined in:
lib/convey/compiler.rb

Constant Summary

Constants included from Convey

VERSION

Class Method Summary collapse

Methods included from Convey

#coffee_installed?

Class Method Details

.compile!(base_path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/convey/compiler.rb', line 5

def self.compile!(base_path)
  `rm -rf #{base_path}/compiled`
  Dir.mkdir("#{base_path}/compiled")
  files = Dir.glob("#{base_path}/**/*")
  root = "#{base_path}/compiled"
  files.each do |path|

    if File.directory?(path)
      unless path =~ /compiled/
        Dir.mkdir("#{root}/#{path.gsub!(base_path, "")}")
      end
      next
    end

    meta = Convey::Compiler.process_file(path)
    local_path = meta[:path].gsub(base_path, "")

    if meta[:path] != path
      puts "Compiled #{path.gsub(base_path, "")} to /compiled#{local_path}"
    else
      puts "Copied #{path.gsub(base_path, "")} to /compiled#{local_path}"
    end

    File.open("#{root}#{local_path}", 'w') {|f| f.write(meta[:data]) }
  end
end

.process_file(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/convey/compiler.rb', line 32

def self.process_file(path)
  meta = {}
  if File.exists?(path)
    if path =~ /\.haml/
      meta[:data] = Haml::Engine.new( File.read(path) ).render
      meta[:type] = 'html'
      meta[:path] = path.gsub(".haml", ".html")
    elsif path =~ /\.sass/
      meta[:data] = Sass::Engine.new( File.read(path) , :syntax => :sass).render
      meta[:type] = 'css'
      meta[:path] = path.gsub(".sass", ".css")
    elsif path =~ /\.coffee/
      if coffee_installed?
        meta[:data] = `coffee -p #{path}`
        meta[:type] = 'javascript'
        meta[:path] = path.gsub(".coffee", ".js")
      else
        raise "CoffeeScript not installed! Run `brew install coffee` on OS X."
      end
    else
      meta = {:data => File.read(path), :path => path, :type => path.split('.').last}
    end
  else
    raise "Path '#{path}' doesn't exist."
  end

  return meta
end