class SparkEngine::Assets::AssetType

Attributes

base[R]
plugin[R]

Public Class Methods

new(plugin, base) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 6
def initialize(plugin, base)
  @base = base
  @plugin = plugin
end

Public Instance Methods

build_failure(file) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 88
def destination(path)
  plugin.asset_path(versioned(path))
end
file_event(files) click to toggle source
# 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) click to toggle source
# 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_files() click to toggle source
# 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) click to toggle source

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

# 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) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 37
def local_path(file)
  destination(file).sub(plugin.root + '/', '')
end
log_error( msg ) click to toggle source
# 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 ) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 41
def pathname(file)
  destination(file).sub(plugin.destination, '')
end
url(path) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 92
def url(path)
  plugin.asset_url(versioned(path))
end
urls(names=nil) click to toggle source
# 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) click to toggle source
# File lib/spark_engine/plugin/assets/asset.rb, line 33
def versioned(path)
  File.basename(path).sub(/(\.\w+)$/, '-'+plugin.version+'\1')
end
watch() click to toggle source
# 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