Last active
December 24, 2015 17:39
-
-
Save SergXIIIth/6837303 to your computer and use it in GitHub Desktop.
Compile assets (Coffee, Sass, Serenadejs) in pure Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler/setup' | |
require 'coffee-script' | |
require 'sass' | |
require 'fileutils' | |
require 'execjs' | |
require 'multi_json' | |
support_pattern = '{**/*.serenade,**/*.coffee,**/*.sass}' | |
desc 'Run support task' | |
task 'support' do | |
system %Q(RACK_ENV=development bundle exec rerun --pattern '#{support_pattern}' 'rake compile_assets') | |
end | |
desc 'Compile assets CoffeScirpt, Sass, Serenade views' | |
task 'compile_assets' do | |
def path(relative) | |
root = File.dirname(__FILE__) | |
File.join(root, relative) | |
end | |
def compile(dirs, target_file) | |
target_file = path(target_file) | |
dirs.each do |pattern| | |
files = Dir.glob(path(pattern)) | |
unless uptodate?(target_file, files) | |
compiled_content = files.map do |file_path| | |
puts "Compile #{file_path}" | |
yield(File.read(file_path), file_path) | |
end.join(' ') | |
FileUtils.mkdir_p(File.dirname(target_file)) | |
File.write(target_file, compiled_content) | |
end | |
end | |
end | |
def compile_coffee(dirs, target_file) | |
compile(dirs, target_file) do |content| | |
CoffeeScript.compile(content) | |
end | |
end | |
def compile_sass(dirs, target_file) | |
compile(dirs, target_file) do |content| | |
Sass::Engine.new(content).render | |
end | |
end | |
def compile_serenade(dirs, target_file, base_path) | |
def parse(content) | |
serenadejs_path = path('/public/js/serenade.js') | |
context = ExecJS.compile(File.read(serenadejs_path)) | |
code = "Serenade.view(#{MultiJson.dump(content)}).parse()" | |
context.eval(code) | |
end | |
def render(name, template_ast) | |
"Serenade.view(#{MultiJson.dump(name)}, #{MultiJson.dump(template_ast)});" | |
end | |
def name(template_path, base_path) | |
absolute_base_path = path(base_path) | |
name = template_path.sub(absolute_base_path, '') | |
name.sub('/', '_').sub('.serenade', '') | |
end | |
compile(dirs, target_file) do |content, path| | |
render(name(path, base_path), parse(content)) | |
end | |
end | |
compile_coffee [ 'views/js/**/*.coffee' ], 'public/scripts.js' | |
compile_serenade [ 'views/js/templates/**/*.serenade' ], 'public/templates.js', 'views/js/templates/' | |
compile_sass [ 'views/css/**/*.sass' ], 'public/styles.css' | |
puts "CoffeeScript, Sass, Serenade complied" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment