getting coffeescript and handlebars to work with plugins

test plan:
  * checkout patchset 1 from https://gerrit.instructure.com/7469
  * symlink the canvalytics repo into vendor/plugins/
  * symlink public/plugins/canvalytics to vendor/plugins/canvalytics/public/
  * run guard, regenerate all files
  * make sure the route /analytics/course/<id>/user/<id> renders handlebars

unit tests not provided for this scaffolding stuff

Change-Id: Ibf626555cbb79a5a97d67286ef4a7d8f28f53de8
Reviewed-on: https://gerrit.instructure.com/7470
Reviewed-by: Ryan Florence <ryanf@instructure.com>
Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
JT Olds 2011-12-13 14:13:45 -07:00
parent d32988b7fd
commit 1bca3581c2
4 changed files with 207 additions and 4 deletions

View File

@ -84,7 +84,6 @@ group :development do
gem 'sexp_processor', '3.0.5' gem 'sexp_processor', '3.0.5'
gem 'ya2yaml', '0.30' gem 'ya2yaml', '0.30'
gem 'guard' gem 'guard'
gem 'guard-coffeescript'
end end
group :redis do group :redis do

View File

@ -1,3 +1,7 @@
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled' guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'
guard 'coffeescript', :input => 'spec/coffeescripts', :output => 'spec/javascripts' guard 'coffeescript', :input => 'spec/coffeescripts', :output => 'spec/javascripts'
guard 'jst', :input => 'app/views/jst', :output => 'public/javascripts/jst' guard 'jst', :input => 'app/views/jst', :output => 'public/javascripts/jst'
Dir[File.join(File.dirname(__FILE__),'vendor/plugins/*/Guardfile')].each do |g|
eval(File.read(g))
end

200
guard/coffeescript.rb Normal file
View File

@ -0,0 +1,200 @@
#
# Copyright (c) 2010 - 2011 Michael Kessler
# with modifications by Instructure Inc, 2011
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
require 'guard'
require 'guard/guard'
require 'guard/watcher'
require 'coffee_script'
module Guard
class CoffeeScript < Guard
module Formatter
class << self
def info(message, options = { }); ::Guard::UI.info(message, options); end
def error(message, options = { }); ::Guard::UI.error(color(message, ';31'), options); end
def success(message, options = { }); ::Guard::UI.info(color(message, ';32'), options); end
def notify(message, options = { }); ::Guard::Notifier.notify(message, options); end
private
def color(text, color_code); ::Guard::UI.send(:color_enabled?) ? "\e[0#{ color_code }m#{ text }\e[0m" : text; end
end
end
module Runner
class << self
def run(files, watchers, options = { })
notify_start(files, options)
changed_files, errors = compile_files(files, watchers, options)
notify_result(changed_files, errors, options)
[changed_files, errors.empty?]
rescue ExecJS::RuntimeError => e
Formatter.error("ExecJS engine error: " + e.message)
end
private
def notify_start(files, options)
message = options[:message] || (options[:noop] ? 'Verify ' : 'Compile ') + files.join(', ')
Formatter.info(message, :reset => true)
end
def compile_files(files, watchers, options)
errors = []
changed_files = []
directories = detect_nested_directories(watchers, files, options)
directories.each do |directory, scripts|
scripts.each do |file|
begin
content = compile(file, options)
changed_files << write_javascript_file(content, file, directory, options)
rescue Exception => e
error_message = file + ': ' + e.message.to_s
errors << error_message
Formatter.error(error_message)
end
end
end
[changed_files.compact, errors]
end
def compile(file, options)
file_options = options_for_file(file, options)
::CoffeeScript.compile(File.read(file), file_options)
end
def options_for_file(file, options)
return options unless options[:bare].respond_to? :include?
file_options = options.clone
filename = file[/([^\/]*)\.coffee/]
file_options[:bare] = file_options[:bare].include?(filename)
file_options
end
def write_javascript_file(content, file, directory, options)
FileUtils.mkdir_p(File.expand_path(directory)) if !File.directory?(directory) && !options[:noop]
filename = File.join(directory, File.basename(file.gsub(/(js\.coffee|coffee)$/, 'js')))
File.open(File.expand_path(filename), 'w') { |f| f.write(content) } if !options[:noop]
filename
end
def detect_nested_directories(watchers, files, options)
return { options[:output] => files } if options[:shallow]
directories = { }
watchers.product(files).each do |watcher, file|
if matches = file.match(watcher.pattern)
target = matches[1] ? File.join(options[:output], File.dirname(matches[1])).gsub(/\/\.$/, '') : options[:output]
if directories[target]
directories[target] << file
else
directories[target] = [file]
end
end
end
directories
end
def notify_result(changed_files, errors, options = { })
if !errors.empty?
Formatter.notify(errors.join("\n"), :title => 'CoffeeScript results', :image => :failed, :priority => 2)
elsif !options[:hide_success]
message = "Successfully #{ options[:noop] ? 'verified' : 'generated' } #{ changed_files.join(', ') }"
Formatter.success(message)
Formatter.notify(message, :title => 'CoffeeScript results')
end
end
end
end
DEFAULT_OPTIONS = {
:bare => false,
:shallow => false,
:hide_success => false,
:noop => false,
:all_on_start => false
}
def initialize(watchers = [], options = {})
watchers = [] if !watchers
defaults = DEFAULT_OPTIONS.clone
if options[:input]
defaults.merge!({ :output => options[:input] })
watchers << ::Guard::Watcher.new(%r{^#{ options.delete(:input) }/(.+\.coffee)$})
end
super(watchers, defaults.merge(options))
end
def start
run_all if options[:all_on_start]
end
def run_all
run_on_change(Watcher.match_files(self, Dir.glob(File.join('**/*/**', '*.coffee'))))
end
def run_on_change(paths)
changed_files, success = Runner.run(clean(paths), watchers, options)
notify changed_files
throw :task_has_failed unless success
end
def run_on_deletion(paths)
clean(paths).each do |file|
javascript = file.gsub(/(js\.coffee|coffee)$/, 'js')
File.remove(javascript) if File.exists?(javascript)
end
end
private
def notify(changed_files)
::Guard.guards.each do |guard|
paths = Watcher.match_files(guard, changed_files)
guard.run_on_change paths unless paths.empty?
end
end
def clean(paths)
paths.uniq!
paths.compact!
paths.select { |p| p =~ /.coffee$/ && File.exists?(p) }
end
end
end

View File

@ -31,12 +31,12 @@ class Handlebars
path = "#{compiled_path}/#{id}.js" path = "#{compiled_path}/#{id}.js"
dir = File.dirname(path) dir = File.dirname(path)
source = File.read(file) source = File.read(file)
js = compile_template(source, id) js = compile_template(source, id, compiled_path =~ /vendor\/plugins\/([^\/]*)\// ? $1 : nil)
FileUtils.mkdir_p(dir) unless File.exists?(dir) FileUtils.mkdir_p(dir) unless File.exists?(dir)
File.open(path, 'w') { |file| file.write(js) } File.open(path, 'w') { |file| file.write(js) }
end end
def compile_template(source, id) def compile_template(source, id, plugin=nil)
require 'execjs' require 'execjs'
# if the first letter of the template name is "_", register it as a partial # if the first letter of the template name is "_", register it as a partial
# ex: _foobar.handlebars or subfolder/_something.handlebars # ex: _foobar.handlebars or subfolder/_something.handlebars
@ -52,7 +52,7 @@ class Handlebars
!(function(){ !(function(){
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['#{id}'] = template(#{template});#{partial_registration} templates['#{id}'] = template(#{template});#{partial_registration}
define('jst/#{id}', ['compiled/handlebars_helpers'], function (Handlebars) { define('#{plugin ? plugin + "/" : ""}jst/#{id}', ['compiled/handlebars_helpers'], function (Handlebars) {
return templates['#{id}']; return templates['#{id}'];
}); });
})(); })();