Created
November 13, 2008 16:25
-
-
Save collin/24480 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Merb::BootLoader.after_app_loads do | |
require 'red' | |
require 'merb_red' | |
# Update on every request in development mode | |
# But only on app load for production | |
Red::MerbPlugin.ensure_installation! | |
if Merb.env == 'development' | |
Merb.logger.info "Hooking Red Into Request" | |
# I'm looking at how the sass/plugin does re-generating on every request | |
# I'll work something up tonight | |
DmGraph::Main.use_red_dev_filter! | |
else | |
Red.update_javascripts | |
end | |
end |
This file contains hidden or 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
module Red | |
def self.update_javascripts | |
Merb.logger.info "Updating JavaScripts" | |
red_dir = "#{Merb.root}/public/javascripts/red/" | |
(Dir.glob("%s/*.red" % red_dir) + Dir.glob("%s/*.rb" % red_dir)).each do |filepath| | |
Merb.logger.info("Updating JavaScript: #{filepath}") | |
basename = File.basename(filepath).gsub(/\.(rb|red)/,'') | |
if self.update?(basename) | |
Red.init(filepath) | |
js_output = File.read(filepath).translate_to_sexp_array.red! | |
ruby_js = compile_ruby_js_source | |
pre = Red.debug ? "try{" : "" | |
post = Red.debug ? "}catch(e){if(e.__class__){m$raise(e);};$ee=e;var m=e.message.match(/([^\\$]+)\\.m\\$(\\w+)\\sis\\snot\\sa\\sfunction/);if(m){m$raise(c$NoMethodError,$q('undefined method \"'+m[2]+'\" for '+m[1]));};var c=e.message.match(/([\\s\\S]+)\\sis\\sundefined/);if(c){c=c[1].replace(/\\./g,'::').replace(/c\\$/g,'');m$raise(c$NameError,$q('uninitialized constant '+c));};}" : "" | |
File.open("public/javascripts/%s.js" % basename, 'w') {|f| f.write(pre + ruby_js + js_output + post)} | |
end | |
end | |
end | |
def self.update?(basename) | |
return true unless File.exists?('public/javascripts/%s.js' % basename) | |
return (File.mtime('public/javascripts/red/%s.red' % basename) rescue File.mtime('public/javascripts/red/%s.rb' % basename)) > File.mtime('public/javascripts/%s.js' % basename) | |
end | |
module MerbPlugin | |
def self.root | |
@root||= Pathname.new(Merb.root/'public'/'javascripts'/'red') | |
end | |
def self.ensure_installation! | |
installed = root.instance_eval { mkdir unless exist? } | |
Merb.logger.info "Installed Red::Merb" unless installed.nil? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment