Module: Assets::Watchify

Defined in:
lib/assets/watchify/version.rb,
lib/assets/watchify/main.rb,
lib/assets/watchify.rb

Defined Under Namespace

Modules: Helper

Constant Summary collapse

VERSION =
"0.2.1"
Folder =
'w6y'

Class Method Summary collapse

Class Method Details

.boot!Object



30
31
32
33
34
35
36
37
# 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

.cleanObject



10
11
12
13
14
# 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) ⇒ Object



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
# 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

.mkdirObject



39
40
41
# File 'lib/assets/watchify.rb', line 39

def self.mkdir
  FileUtils.mkpath @root
end

.ms(seconds) ⇒ Object



20
21
22
# File 'lib/assets/watchify/main.rb', line 20

def self.ms seconds
  seconds.round 3
end

.path?(f) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# 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

.pingObject



53
54
55
# File 'lib/assets/watchify/main.rb', line 53

def self.ping
  @bundles.keys.each{|name| jstag name}
end

.preload(*args) ⇒ Object



19
20
21
22
# 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

.rmdirObject



16
17
18
# File 'lib/assets/watchify/main.rb', line 16

def self.rmdir
  FileUtils.remove_entry @root
end

.start!Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/assets/watchify.rb', line 24

def self.use?
  defined?(Rails::Server) &&
  'development'==Rails.env &&
  @root.directory?
end