module Assets::Watchify

Constants

Folder
Helper
VERSION

Public Class Methods

boot!() click to toggle source
# File lib/assets/watchify.rb, line 30
def self.boot!
  @prefix=Rails.application.config.assets.prefix
  @path=@prefix+'/'+Folder
  @root=Rails.root.join "public"+@path
  return unless use?
  require_relative "watchify/main"
  start!
end
clean() click to toggle source
# File lib/assets/watchify/main.rb, line 10
def self.clean
  Dir.glob @root.join '**', '*' do |f|
    File.delete f rescue nil
  end
end
jstag(name) click to toggle source
# File lib/assets/watchify/main.rb, line 24
def self.jstag name
  return unless String===name
  t1 = Time.now
  name+='.js' if File.extname(name).empty?
  begin
    z = Rails.application.assets[name]
  rescue=> e
    return "<div class='alert alert-error'><pre>#{e.message}</pre></div>"
  end

  t2 = Time.now

  unless File.exist? js = @root.join(z.digest_path)
    File.delete @bundles[name], "#{@bundles[name]}.map" rescue nil if @bundles[name]
    @bundles[name] = js
    FileUtils.mkpath js.dirname
    jsf = File.open js, 'w+'
    map = SourceMap.new file: js.basename, source_root: @prefix, generated_output: jsf
    z.to_a.each {|d| map.add_generated d.source, source: d.logical_path+'?body=1'}
    map.save "#{js}.map"
    jsf.puts
    jsf.puts "//# sourceMappingURL=#{File::basename js}.map"
    jsf.close
    t3 = Time.now
    puts "#{self} built '#{name}' (#{ms t2-t1}+#{ms t3-t2}=#{ms t3-t1})..."
  end
  "<script src='#{@path}/#{z.digest_path}'></script><!-- #{z.length}/#{z.to_a.length} -->"
end
mkdir() click to toggle source
# File lib/assets/watchify.rb, line 39
def self.mkdir
  FileUtils.mkpath @root
end
ms(seconds) click to toggle source
# File lib/assets/watchify/main.rb, line 20
def self.ms seconds
  seconds.round 3
end
path?(f) click to toggle source
# File lib/assets/watchify/main.rb, line 57
def self.path? f
  Pathname.new(f).realpath.ascend do |z|
    return false if 'gems'==z.basename.to_s
    return false if Pathname.glob(z+'*.gemspec').any?
    return true if z.join('Gemfile').file?
  end
  true
end
ping() click to toggle source
# File lib/assets/watchify/main.rb, line 53
def self.ping
  @bundles.keys.each{|name| jstag name}
end
preload(*args) click to toggle source
# File lib/assets/watchify.rb, line 19
def self.preload *args
  args=['application.js'] if args.empty?
  args.flatten.compact.each{|js| @bundles[js]=nil}
end
rmdir() click to toggle source
# File lib/assets/watchify/main.rb, line 16
def self.rmdir
  FileUtils.remove_entry @root
end
start!() click to toggle source
# File lib/assets/watchify/main.rb, line 66
def self.start!
  clean
  Rails.application.config.assets.debug = false # Let CSS to glue either

  paths=Rails.application.config.assets.paths.select{|f| path? f}

  Thread.new do
    listener = Listen.to paths do |m,a,r|
      m.each {|f| puts "~ #{f}"}
      a.each {|f| puts "+ #{f}"}
      r.each {|f| puts "- #{f}"}
      ping
    end
    listener.start
    puts "#{self} listening to changes in #{paths.count} folders..."
    at_exit do
      puts "#{self } stop listening to folders..."
      listener.stop
      clean
    end
    sleep 1
    ping
  end
end
use?() click to toggle source
# File lib/assets/watchify.rb, line 24
def self.use?
  defined?(Rails::Server) &&
  'development'==Rails.env &&
  @root.directory?
end