Skip to content

Instantly share code, notes, and snippets.

@rubyist
Created August 15, 2011 18:06
Show Gist options
  • Save rubyist/1147332 to your computer and use it in GitHub Desktop.
Save rubyist/1147332 to your computer and use it in GitHub Desktop.
coffee compiling rakefile
require 'coffee_script'
require 'rake/clean'
def coffee_to_js(coffee)
File.join(BUILDDIR, File.dirname(coffee.split('/')[1..-1].join('/')), File.basename(coffee).ext('js'))
end
JSFILE = 'app.js'
JSPATH = "_attachments/js/app/#{JSFILE}"
COFFEE = FileList['coffeescript/**/*.coffee']
BUILDDIR = 'js'
JS = COFFEE.collect { |fn| coffee_to_js(fn) }
CLEAN.include(JS, BUILDDIR)
CLOBBER.include(JSPATH)
task :build => [JSFILE]
file JSFILE => JS do
puts "assembling #{JSFILE}"
`cat #{JS.join(' ')} > #{JSPATH}`
end
directory BUILDDIR
rule '.js' => lambda { |objfile| find_source(objfile) } do |t|
Rake::Task[BUILDDIR].invoke
outfile = coffee_to_js(t.source)
mkdir_p File.dirname(outfile)
puts "#{t.source} -> #{outfile}"
f = open(outfile, 'w')
f.write(CoffeeScript.compile(open(t.source)))
f.flush
f.close
end
def find_source(objfile)
base = File.basename(objfile, '.js')
COFFEE.find { |s| File.basename(s, '.coffee') == base }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment