Skip to content

Commit

Permalink
getting coffeescript and handlebars to work with plugins
Browse files Browse the repository at this point in the history
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 <[email protected]>
Tested-by: Hudson <[email protected]>
  • Loading branch information
jtolio committed Dec 30, 2011
1 parent d32988b commit 1bca358
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 4 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ group :development do
gem 'sexp_processor', '3.0.5'
gem 'ya2yaml', '0.30'
gem 'guard'
gem 'guard-coffeescript'
end

group :redis do
Expand Down
4 changes: 4 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
guard 'coffeescript', :input => 'app/coffeescripts', :output => 'public/javascripts/compiled'
guard 'coffeescript', :input => 'spec/coffeescripts', :output => 'spec/javascripts'
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 changes: 200 additions & 0 deletions guard/coffeescript.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lib/handlebars/handlebars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def compile_file(file, root_path, compiled_path)
path = "#{compiled_path}/#{id}.js"
dir = File.dirname(path)
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)
File.open(path, 'w') { |file| file.write(js) }
end

def compile_template(source, id)
def compile_template(source, id, plugin=nil)
require 'execjs'
# if the first letter of the template name is "_", register it as a partial
# ex: _foobar.handlebars or subfolder/_something.handlebars
Expand All @@ -52,7 +52,7 @@ def compile_template(source, id)
!(function(){
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
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}'];
});
})();
Expand Down

0 comments on commit 1bca358

Please sign in to comment.