File: assets.rb

package info (click to toggle)
ruby-blade 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 208 kB
  • sloc: ruby: 819; makefile: 3; sh: 3
file content (88 lines) | stat: -rw-r--r-- 1,938 bytes parent folder | download | duplicates (3)
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
require "sprockets"

module Blade::Assets
  autoload :Builder, "blade/assets/builder"

  extend self

  def environment
    @environment ||= Sprockets::Environment.new do |env|
      env.cache = Sprockets::Cache::FileStore.new(Blade.tmp_path)

      %w( blade user adapter ).each do |name|
        send("#{name}_load_paths").each do |path|
          env.append_path(path)
        end
      end

      env.context_class.class_eval do
        delegate :logical_paths, to: Blade::Assets
      end
    end
  end

  def build
    if Blade.config.build
      Builder.new(environment).build
    end
  end

  def logical_paths(type = nil)
    paths = Blade.config.logical_paths
    paths.select! { |path| File.extname(path) == ".#{type}" } if type
    paths
  end

  def blade_load_paths
    [ Blade.root_path.join("assets") ]
  end

  def user_load_paths
    Blade.config.load_paths.flat_map do |load_path|
      if load_path.is_a?(Hash)
        load_path.flat_map do |gem_name, paths|
          Array(paths).map{ |path| gem_pathname(gem_name).join(path) }
        end
      else
        Pathname.new(load_path)
      end
    end
  end

  def adapter_load_paths
    gem_name = "blade-#{Blade.config.framework}_adapter"
    [ gem_pathname(gem_name).join("assets") ]
  end

  def watch_logical_paths
    @mtimes = get_mtimes

    EM.add_periodic_timer(1) do
      mtimes = get_mtimes
      unless mtimes == @mtimes
        @mtimes = mtimes
        Blade.publish("/assets", changed: @mtimes)
      end
    end
  end

  private
    def get_mtimes
      {}.tap do |mtimes|
        Blade.config.logical_paths.each do |path|
          mtimes[path] = get_mtime(path)
        end
      end
    end

    def get_mtime(logical_path)
      environment[logical_path].mtime
    rescue Exception => e
      e.to_s
    end

    def gem_pathname(gem_name)
      gemspec = Gem::Specification.find_by_name(gem_name)
      Pathname.new(gemspec.gem_dir)
    end
end