-
Notifications
You must be signed in to change notification settings - Fork 1
/
coffeecompiler.rb
53 lines (45 loc) · 1.41 KB
/
coffeecompiler.rb
1
2
3
4
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'fileutils'
require 'set'
require 'find'
require 'open3'
class CoffeeCompiler
attr_reader :plugin, :force
def initialize(plugin = nil, force = false)
@plugin = plugin
@force = force
end
def compile(filename, outputDir = nil)
FileUtils.mkpath outputDir if outputDir
return if doesnNeedCompiling(filename, outputDir)
args = ["coffee"]
args.push '-r', plugin if plugin
args.push '-o', outputDir if outputDir
args.push '-c', filename
Open3.popen3 *args do |stdin, stdout, stderr|
error_message = stderr.read
unless error_message.empty?
msg = "CoffeeScript compile failed. #{error_message}"
msg += "\n Command line: #{args.inspect}"
raise msg
end
end
end
def doesnNeedCompiling(filename, outputDir)
return false if force
jsName = filename.gsub(".coffee", ".js")
outputDir = File.dirname(filename) if outputDir.nil?
cacheName = File.join outputDir, File.basename(jsName)
exists = File.exist?(cacheName)
return exists && File.stat(cacheName).mtime >= File.stat(filename).mtime
end
def compileAll(baseFile, coffeeDir, outDir)
Dir["#{coffeeDir}/**/*.coffee"].each do |f|
subPath = File.dirname f.gsub /^#{coffeeDir}\//, ''
dir = "#{outDir}/#{subPath}"
compile f, dir
end
end
def compileDir(dir, outputDir = nil)
Dir["#{dir}/**/*.coffee"].each {|f| compile f, outputDir}
end
end