2011-09-27 03:25:00 +08:00
|
|
|
require 'guard'
|
|
|
|
require 'guard/guard'
|
2011-10-14 02:32:41 +08:00
|
|
|
require 'fileutils'
|
2014-03-07 00:47:05 +08:00
|
|
|
require 'handlebars_tasks'
|
2015-07-21 03:03:18 +08:00
|
|
|
require 'parallel'
|
2011-09-27 03:25:00 +08:00
|
|
|
|
|
|
|
module Guard
|
|
|
|
class JST < Guard
|
2011-10-14 02:32:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
:hide_success => false,
|
|
|
|
:all_on_start => false
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialize Guard::JST.
|
|
|
|
#
|
|
|
|
# @param [Array<Guard::Watcher>] watchers the watchers in the Guard block
|
|
|
|
# @param [Hash] options the options for the Guard
|
|
|
|
# @option options [String] :input the input directory
|
|
|
|
# @option options [String] :output the output directory
|
|
|
|
# @option options [Boolean] :hide_success hide success message notification
|
|
|
|
# @option options [Boolean] :all_on_start generate all JavaScripts files on start
|
|
|
|
#
|
|
|
|
def initialize(watchers = [], options = {})
|
|
|
|
watchers = [] if !watchers
|
|
|
|
defaults = DEFAULT_OPTIONS.clone
|
|
|
|
|
|
|
|
if options[:input]
|
|
|
|
defaults.merge!({ :output => options[:input] })
|
2013-05-01 03:47:24 +08:00
|
|
|
watchers << ::Guard::Watcher.new(%r{\A(?:vendor/plugins/.*?/)?#{ Regexp.escape(options[:input]) }/(.+\.handlebars)\z})
|
2011-10-14 02:32:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
super(watchers, defaults.merge(options))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Gets called once when Guard starts.
|
|
|
|
#
|
|
|
|
# @raise [:task_has_failed] when stop has failed
|
|
|
|
#
|
|
|
|
def start
|
|
|
|
run_all if options[:all_on_start]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Gets called when watched paths and files have changes.
|
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the changed paths and files
|
|
|
|
# @raise [:task_has_failed] when stop has failed
|
|
|
|
#
|
2011-09-27 03:25:00 +08:00
|
|
|
# Compiles templates from app/views/jst to public/javascripts/jst
|
|
|
|
def run_on_change(paths)
|
2013-05-01 03:47:24 +08:00
|
|
|
paths = paths.map{ |path|
|
|
|
|
prefix = path =~ %r{\Avendor/plugins/.*?/} ? $& : ''
|
|
|
|
[prefix, path]
|
|
|
|
}
|
|
|
|
Parallel.each(paths, :in_threads => Parallel.processor_count) do |prefix, path|
|
2011-10-06 07:52:53 +08:00
|
|
|
begin
|
2013-04-27 06:44:35 +08:00
|
|
|
UI.info "Compiling: #{path}"
|
2014-03-07 00:47:05 +08:00
|
|
|
HandlebarsTasks::Handlebars.compile_file path, "#{prefix}app/views/jst", "#{prefix}#{@options[:output]}"
|
2011-10-06 07:52:53 +08:00
|
|
|
rescue Exception => e
|
2014-03-06 03:50:21 +08:00
|
|
|
::Guard::UI.error(e.to_s, :title => path.sub('app/views/jst/', ''), :image => :failed)
|
2011-10-06 07:52:53 +08:00
|
|
|
end
|
2011-09-27 03:25:00 +08:00
|
|
|
end
|
|
|
|
end
|
2011-10-06 07:52:53 +08:00
|
|
|
|
2011-10-14 02:32:41 +08:00
|
|
|
# Gets called when all files should be regenerated.
|
|
|
|
#
|
|
|
|
# @raise [:task_has_failed] when stop has failed
|
|
|
|
#
|
|
|
|
def run_all
|
2013-04-27 06:44:35 +08:00
|
|
|
UI.info "Compiling all handlebars templates in #{@options[:input]} to #{@options[:output]}"
|
2015-04-28 00:59:20 +08:00
|
|
|
FileUtils.rm_r @options[:output] if File.exist?(@options[:output])
|
2014-03-07 00:47:05 +08:00
|
|
|
HandlebarsTasks::Handlebars.compile @options[:input], @options[:output]
|
2013-04-27 06:44:35 +08:00
|
|
|
UI.info "Successfully compiled all handlebars templates in #{@options[:input]}"
|
2011-10-14 02:32:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Called on file(s) deletions that the Guard watches.
|
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the deleted files or paths
|
|
|
|
# @raise [:task_has_failed] when run_on_change has failed
|
|
|
|
#
|
|
|
|
def run_on_deletion(paths)
|
2013-04-27 06:44:35 +08:00
|
|
|
paths.each do |file|
|
|
|
|
javascript = file.sub(%r{\A#{Regexp.escape(@options[:input])}/(.*?)\.handlebars}, "#{@options[:output]}/\\1.js")
|
|
|
|
UI.info "Removing: #{javascript}"
|
2015-04-28 00:59:20 +08:00
|
|
|
File.delete(javascript) if File.exist?(javascript)
|
2013-04-27 06:44:35 +08:00
|
|
|
end
|
2011-10-14 02:32:41 +08:00
|
|
|
end
|
|
|
|
|
2011-09-27 03:25:00 +08:00
|
|
|
end
|
|
|
|
end
|