forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffeescript.rb
230 lines (191 loc) · 7.99 KB
/
coffeescript.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#
# 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 = partition_by_plugin(files) do |prefix, plugin_files|
compile_files(plugin_files, watchers, options.merge(:output => "#{prefix}#{options[:output]}"))
end
notify_result(changed_files, errors, options)
[changed_files, errors.empty?]
rescue ExecJS::RuntimeError => e
Formatter.error("ExecJS engine error: " + e.message)
end
private
def partition_by_plugin(files, &block)
groupings = files.inject({}) { |hash, file|
prefix = file =~ %r{\Avendor/plugins/.*?/} ? $& : ''
hash[prefix] ||= []
hash[prefix] << file
hash
}
groupings.map(&block).inject([[], []]) { |ary, ret|
ary[0].concat ret[0]
ary[1].concat ret[1]
ary
}
end
def notify_start(files, options)
message = options[:message] || (options[:noop] ? 'Verify ' : 'Compile ') + files.join(', ')
Formatter.info(message, :reset => true)
end
require 'parallel'
require 'lib/canvas/coffee_script'
def compile_files(files, watchers, options)
errors = []
changed_files = []
directories = detect_nested_directories(watchers, files, options)
if Canvas::CoffeeScript.coffee_script_binary_is_available?
Parallel.each(directories.map, :in_threads => Parallel.processor_count) do |(directory, scripts)|
FileUtils.mkdir_p(File.expand_path(directory)) if !File.directory?(directory) && !options[:noop]
system('coffee', '-m', '-c', '-o', directory, *scripts)
if $?.exitstatus != 0
Formatter.error("Unable to compile coffeescripts in #{directory}")
else
changed_files.concat(scripts.map { |script| File.join(directory, File.basename(script.gsub(/(js\.coffee|coffee)$/, 'js'))) })
end
end
else
directories.each do |directory, scripts|
Parallel.each(scripts, :in_threads => Parallel.processor_count) 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
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{\A(?:vendor/plugins/.*?/)?#{ Regexp.escape(options.delete(:input)) }/(.+\.coffee)\z})
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