Class: SparkEngine::Assets::AssetType

Inherits:
Object
  • Object
show all
Defined in:
lib/spark_engine/plugin/assets/asset.rb

Direct Known Subclasses

Javascripts, Stylesheets, Svgs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, base) ⇒ AssetType

Returns a new instance of AssetType.



6
7
8
9
# File 'lib/spark_engine/plugin/assets/asset.rb', line 6

def initialize(plugin, base)
  @base = base
  @plugin = plugin
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



4
5
6
# File 'lib/spark_engine/plugin/assets/asset.rb', line 4

def base
  @base
end

#pluginObject (readonly)

Returns the value of attribute plugin.



4
5
6
# File 'lib/spark_engine/plugin/assets/asset.rb', line 4

def plugin
  @plugin
end

Instance Method Details

#build_failure(file) ⇒ Object



49
50
51
52
53
# File 'lib/spark_engine/plugin/assets/asset.rb', line 49

def build_failure(file)
  msg = "\nFAILED TO BUILD"
  msg += ": #{local_path(file)}" if file
  log_error msg
end

#build_success(file) ⇒ Object



45
46
47
# File 'lib/spark_engine/plugin/assets/asset.rb', line 45

def build_success(file)
  log_success "Built: #{local_path(file)}"
end

#change(modified, added, removed) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/spark_engine/plugin/assets/asset.rb', line 131

def change(modified, added, removed)
  return if (Time.now.to_i - @last_build) < @throttle

  puts "Added: #{file_event(added)}".colorize(:light_green)        unless added.empty?
  puts "Removed: #{file_event(removed)}".colorize(:light_red)      unless removed.empty?
  puts "Modified: #{file_event(modified)}".colorize(:light_yellow) unless modified.empty?

  build
  @last_build = Time.now.to_i
end

#compress(file) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/spark_engine/plugin/assets/asset.rb', line 149

def compress(file)
  return unless SparkEngine.production?

  mtime = File.mtime(file)
  gz_file = "#{file}.gz"
  return if File.exist?(gz_file) && File.mtime(gz_file) >= mtime

  File.open(gz_file, "wb") do |dest|
    gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
    gz.mtime = mtime.to_i
    IO.copy_stream(open(file), gz)
    gz.close
  end

  File.utime(mtime, mtime, gz_file)
end

#destination(path) ⇒ Object



88
89
90
# File 'lib/spark_engine/plugin/assets/asset.rb', line 88

def destination(path)
  plugin.asset_path(versioned(path))
end

#file_event(files) ⇒ Object



142
143
144
145
146
147
# File 'lib/spark_engine/plugin/assets/asset.rb', line 142

def file_event(files)
  list = files.flat_map { |f| f.sub(base+'/', '') }.join("  \n")
  list = "  \n#{files}" if 1 < files.size

  list
end

#filter_files(names) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/spark_engine/plugin/assets/asset.rb', line 25

def filter_files(names)

  # Filter names based on asset file locations
  find_files.select do |f|
    names.include?(File.basename(f).sub(/(\..+)$/,''))
  end
end

#find_filesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/spark_engine/plugin/assets/asset.rb', line 11

def find_files
  if @files
    @files
  else
    files = Dir[File.join(base, "*.#{ext}")].reject do |f|
      # Filter out partials
      File.basename(f).start_with?('_')
    end

    @files = files if SparkEngine.production?
    files
  end
end

#find_node_module(cmd) ⇒ Object

Determine if an NPM module is installed by checking paths with ‘npm bin` Returns path to binary if installed



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spark_engine/plugin/assets/asset.rb', line 65

def find_node_module(cmd)
  require 'open3'
  (@modules ||= {})[cmd] ||= begin

    local = "$(npm bin)/#{cmd}"
    global = "$(npm -g bin)/#{cmd}"
    
    if Open3.capture3(local)[2].success?
      local
    elsif Open3.capture3(global)[2].success?
      global
    end

  end
end

#local_path(file) ⇒ Object



37
38
39
# File 'lib/spark_engine/plugin/assets/asset.rb', line 37

def local_path(file)
  destination(file).sub(plugin.root + '/', '')
end

#log_error(msg) ⇒ Object



59
60
61
# File 'lib/spark_engine/plugin/assets/asset.rb', line 59

def log_error( msg )
  STDERR.print msg.to_s.colorize(:red)
end

#log_success(msg) ⇒ Object



55
56
57
# File 'lib/spark_engine/plugin/assets/asset.rb', line 55

def log_success( msg )
  STDOUT.print msg.to_s.colorize(:green)
end

#npm_command(cmd) ⇒ Object



81
82
83
84
85
86
# File 'lib/spark_engine/plugin/assets/asset.rb', line 81

def npm_command(cmd)
  cmd = cmd.split(' ')
  if path = find_node_module(cmd.shift)
    system "#{path} #{cmd.join(' ')}"
  end
end

#pathname(file) ⇒ Object



41
42
43
# File 'lib/spark_engine/plugin/assets/asset.rb', line 41

def pathname(file)
  destination(file).sub(plugin.destination, '')
end

#url(path) ⇒ Object



92
93
94
# File 'lib/spark_engine/plugin/assets/asset.rb', line 92

def url(path)
  plugin.asset_url(versioned(path))
end

#urls(names = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/spark_engine/plugin/assets/asset.rb', line 96

def urls(names=nil)
  # If names are passed, look at the basename minus
  # the extension as build files may have
  # different extensions than sources
  names = [names].flatten.compact.uniq.map do |n|
    File.basename(n).sub(/(\..+)$/,'')
  end

  # Return all asset urls if none were specifically chosen
  if names.empty?
    find_files.map{ |file| url(file) }

  # Filter files based on name
  else
    filter_files(names).map{ |file| url(file) }
  end
end

#versioned(path) ⇒ Object



33
34
35
# File 'lib/spark_engine/plugin/assets/asset.rb', line 33

def versioned(path)
  File.basename(path).sub(/(\.\w+)$/, '-'+plugin.version+'\1')
end

#watchObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/spark_engine/plugin/assets/asset.rb', line 114

def watch

  @throttle = 4
  @last_build = 0
  
  puts "Watching for changes to #{base.sub(plugin.root+'/', '')}...".colorize(:light_yellow)

  Thread.new {
    listener = Listen.to(base) do |modified, added, removed|
      change(modified, added, removed)
    end

    listener.start # not blocking
    sleep
  }
end