forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Tested-by: Hudson <[email protected]>
- Loading branch information
Showing
4 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
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
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
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 |
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
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 |
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