class SC::Helpers::Minifier
Helper to minify JavaScript files. You can either call the class method minify, or use << to add to the queue. If you add to the queue, you have no knowledge of when the minification will finish.
Public Class Methods
<<(path)
click to toggle source
CLASS HELPERS
# File lib/sproutcore/helpers/minifier.rb, line 8 def self.<<(path) SC::Helpers::Minifier.instance << path end
instance()
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 22 def self.instance @@instance = SC::Helpers::Minifier.new if @@instance.nil? @@instance end
minify(path)
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 12 def self.minify(path) SC::Helpers::Minifier.instance.minify path end
new()
click to toggle source
MINIFICATION MANAGER
# File lib/sproutcore/helpers/minifier.rb, line 30 def initialize @queue = [] @working_minifiers = [] @max_minifiers = 4 @safety = Mutex.new end
wait()
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 16 def self.wait SC::Helpers::Minifier.instance.wait end
Public Instance Methods
<<(item)
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 43 def <<(item) @queue << item _process_queue end
_process_queue()
click to toggle source
If the queue is not empty, and there are any available workers, spawn min(queue.length, max_minifiers) minifiers. They'll handle the queue on their own.
# File lib/sproutcore/helpers/minifier.rb, line 52 def _process_queue [@queue.length, @max_minifiers - @working_minifiers.length].min.times { _spawn_minifier } end
_spawn_minifier()
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 58 def _spawn_minifier thread = Thread.new { @working_minifiers << Thread.current while @queue.length > 0 queue = @safety.synchronize { queue = @queue.clone @queue.clear queue } next if queue.length == 0 minify(queue) # NOTE: MORE ITEMS COULD BE ADDED TO QUEUE BY NOW, # SO WE LOOP. end @working_minifiers.delete Thread.current } end
minify(paths)
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 82 def minify(paths) if not paths.kind_of?(Array) paths = [paths] end # Split paths into HTML and JS html_paths = paths.select {|p| p =~ /\.html$/} js_paths = paths.select {|p| p =~ /\.js$/} if html_paths.length > 0 command = %{java -jar "#{SC.html_jar}" "#{html_paths.join '" "'}" 2>&1} output = `#{command}` SC.logger.info output if $?.exitstatus != 0 SC.logger.fatal(output) SC.logger.fatal("!!!! Minifying failed. Please check that your JS code is valid.") SC.logger.fatal("!!!! Failed compiling #{paths}") exit(1) end end if js_paths.length > 0 js_paths.each {|p| p = Pathname.new(p).relative_path_from(Pathname.new(Dir.getwd)) command = %{java -Xmx512m -XX:MaxPermSize=256m -jar "#{SC.js_jar}" -o "#{p}" "#{p}" 2>&1} output = `#{command}` SC.logger.info output if $?.exitstatus != 0 SC.logger.fatal(output) SC.logger.fatal("!!!! Minifying failed. Please check that your JS code is valid.") SC.logger.fatal("!!!! Failed compiling #{paths}") exit(1) end } end # SCyui doesn't need a -o. It writes to the original path. end
wait()
click to toggle source
# File lib/sproutcore/helpers/minifier.rb, line 38 def wait @working_minifiers.each {|m| m.join } end