Module: Guard::CoffeeScript::Runner
- Defined in:
- lib/guard/coffeescript/runner.rb
Class Method Summary (collapse)
-
+ (Array<String, String>) compile(filename, options)
private
Compile the CoffeeScript and generate the source map.
-
+ (Array<Array<String>, Array<String>] the result for the compilation run) compile_files(files, watchers, options)
private
Compiles all CoffeeScript files and writes the JavaScript files.
-
+ (Object) detect_nested_directories(watchers, files, options)
private
Detects the output directory for each CoffeeScript file.
-
+ (Object) javascript_file_name(file, directory)
private
Calculates the output filename from the coffescript filename and the output directory.
-
+ (Object) notify_result(changed_files, errors, options = { })
private
Writes console and system notifications about the result of the compilation.
-
+ (Object) notify_start(files, options)
private
Generates a start compilation notification.
-
+ (Object) options_for_file(file, options)
private
Gets the CoffeeScript compilation options.
-
+ (Object) options_for_source_map(filename, options)
private
Gets the CoffeeScript source map options.
-
+ (Object) remove(files, watchers, options = { })
The remove function deals with CoffeeScript file removal by locating the output javascript file and removing it.
-
+ (Array<Array<String>, Boolean>) run(files, watchers, options = { })
The CoffeeScript runner handles the CoffeeScript compilation, creates nested directories and the output file, writes the result to the console and triggers optional system notifications.
-
+ (String) write_javascript_file(js, map, file, directory, options)
private
Analyzes the CoffeeScript compilation output and creates the nested directories and writes the output file.
Class Method Details
+ (Array<String, String>) compile(filename, options) (private)
Compile the CoffeeScript and generate the source map.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/guard/coffeescript/runner.rb', line 117 def compile(filename, ) file = File.read(filename) = (file, ) if [:source_map] .merge! (filename, ) result = ::CoffeeScript.compile(file, ) js, map = result['js'], result['v3SourceMap'] else js = ::CoffeeScript.compile(file, ) end [js, map] end |
+ (Array<Array<String>, Array<String>] the result for the compilation run) compile_files(files, watchers, options) (private)
Compiles all CoffeeScript files and writes the JavaScript files.
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 |
# File 'lib/guard/coffeescript/runner.rb', line 82 def compile_files(files, watchers, ) errors = [] changed_files = [] directories = detect_nested_directories(watchers, files, ) directories.each do |directory, scripts| scripts.each do |file| begin js, map = compile(file, ) changed_files << write_javascript_file(js, map, file, directory, ) rescue => e = file + ': ' + e..to_s if [:error_to_js] = "throw \"#{ }\";" changed_files << write_javascript_file(, nil, file, directory, ) end errors << Formatter.error() end end end [changed_files.flatten.compact, errors] end |
+ (Object) detect_nested_directories(watchers, files, options) (private)
Detects the output directory for each CoffeeScript file. Builds the product of all watchers and assigns to each directory the files to which it belongs to.
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/guard/coffeescript/runner.rb', line 217 def detect_nested_directories(watchers, files, ) return { [:output] => files } if [:shallow] directories = { } watchers.product(files).each do |watcher, file| if matches = file.match(watcher.pattern) target = matches[1] ? File.join([:output], File.dirname(matches[1])).gsub(/\/\.$/, '') : [:output] || File.dirname(file) if directories[target] directories[target] << file else directories[target] = [file] end end end directories end |
+ (Object) javascript_file_name(file, directory) (private)
Calculates the output filename from the coffescript filename and the output directory
203 204 205 |
# File 'lib/guard/coffeescript/runner.rb', line 203 def javascript_file_name(file, directory) File.join(directory, File.basename(file.gsub(/(js\.coffee|coffee)$/, 'js'))) end |
+ (Object) notify_result(changed_files, errors, options = { }) (private)
Writes console and system notifications about the result of the compilation.
244 245 246 247 248 249 250 251 252 |
# File 'lib/guard/coffeescript/runner.rb', line 244 def notify_result(changed_files, errors, = { }) if !errors.empty? Formatter.notify(errors.join("\n"), :title => 'CoffeeScript results', :image => :failed, :priority => 2) elsif ![:hide_success] = "Successfully #{ [:noop] ? 'verified' : 'generated' } #{ changed_files.join(', ') }" Formatter.success() Formatter.notify(, :title => 'CoffeeScript results') end end |
+ (Object) notify_start(files, options) (private)
Generates a start compilation notification.
70 71 72 73 |
# File 'lib/guard/coffeescript/runner.rb', line 70 def notify_start(files, ) = [:message] || ([:noop] ? 'Verify ' : 'Compile ') + files.join(', ') Formatter.info(, :reset => true) end |
+ (Object) options_for_file(file, options) (private)
Gets the CoffeeScript compilation options.
137 138 139 140 141 142 143 144 145 |
# File 'lib/guard/coffeescript/runner.rb', line 137 def (file, ) return unless [:bare].respond_to? :include? = .clone filename = file[/([^\/]*)\.coffee/] [:bare] = [:bare].include?(filename) end |
+ (Object) options_for_source_map(filename, options) (private)
Gets the CoffeeScript source map options.
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/guard/coffeescript/runner.rb', line 152 def (filename, ) # if :input was provided, make all filenames relative to that filename = Pathname.new(filename).relative_path_from(Pathname.new([:input])).to_s if [:input] { :sourceMap => true, :generatedFile => filename.gsub(/(js\.coffee|coffee)$/, 'js'), :sourceFiles => [filename], :sourceRoot => [:source_root] || [:input] || '', } end |
+ (Object) remove(files, watchers, options = { })
The remove function deals with CoffeeScript file removal by locating the output javascript file and removing it.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/guard/coffeescript/runner.rb', line 41 def remove(files, watchers, = { }) removed_files = [] directories = detect_nested_directories(watchers, files, ) directories.each do |directory, scripts| scripts.each do |file| javascript = javascript_file_name(file, directory) if File.exists?(javascript) FileUtils.remove_file(javascript) removed_files << javascript end end end if removed_files.length > 0 = "Removed #{ removed_files.join(', ') }" Formatter.success() Formatter.notify(, :title => 'CoffeeScript results') end end |
+ (Array<Array<String>, Boolean>) run(files, watchers, options = { })
The CoffeeScript runner handles the CoffeeScript compilation, creates nested directories and the output file, writes the result to the console and triggers optional system notifications.
24 25 26 27 28 29 30 |
# File 'lib/guard/coffeescript/runner.rb', line 24 def run(files, watchers, = { }) notify_start(files, ) changed_files, errors = compile_files(files, watchers, ) notify_result(changed_files, errors, ) [changed_files, errors.empty?] end |
+ (String) write_javascript_file(js, map, file, directory, options) (private)
Analyzes the CoffeeScript compilation output and creates the nested directories and writes the output file.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/guard/coffeescript/runner.rb', line 175 def write_javascript_file(js, map, file, directory, ) directory = Dir.pwd if !directory || directory.empty? filename = javascript_file_name(file, directory) return filename if [:noop] if [:source_map] map_name = filename + '.map' js += "\n/*\n//@ sourceMappingURL=#{File.basename(map_name)}\n*/\n" end FileUtils.mkdir_p(File.(directory)) if !File.directory?(directory) File.open(File.(filename), 'w') { |f| f.write(js) } if [:source_map] File.open(File.(map_name), 'w') { |f| f.write(map) } [filename, map_name] else filename end end |