require 'barber'
require 'coffee-script'
require 'fileutils'
require 'sass'
module Pyro
class Asset
attr_accessor :file, :build_dir, :working_dir, :helper_args
def initialize(args)
args.each { |k, v| instance_variable_set("@#{k}", v) }
end
def mtime
File.mtime file
end
def basename
File.basename file
end
def dirname
File.dirname file
end
def extname
File.extname file
end
def name
basename.sub(extname, '')
end
def timestamp
mtime.strftime "%Y%m%d%H%M%S"
end
def relative_dir
dirname.sub("#{working_dir}/", '')
end
def relative_file
if helper_args[:target]
helper_args[:target]
else
"#{relative_dir}/#{name}#{compiled_ext}"
end
end
def compiled_ext
case extname
when '.coffee', '.hbs', '.handlebars'
'.js'
when '.scss'
'.css'
else
extname
end
end
def contents
contents = File.read file
case extname
when '.coffee'
contents = CoffeeScript.compile(contents, bare: true)
when '.scss'
contents = Sass::Engine.new(contents, { syntax: :scss }).render
when '.hbs', '.handlebars'
contents = Barber::Ember::FilePrecompiler.call(contents)
contents = "Ember.TEMPLATES['#{template_name}'] = #{contents}"
end
contents
end
def template_name
if helper_args[:name]
helper_args[:name]
elsif helper_args[:src]
name
elsif helper_args[:dir]
file.sub("#{working_dir}/#{helper_args[:dir]}/", '').sub(extname, '')
else
'name-not-found'
end
end
def generate_file
FileUtils.mkdir_p "#{build_dir}/#{relative_dir}"
File.open("#{build_dir}/#{relative_file}", 'a+') { |f| f.write contents }
end
def generate_link
case compiled_ext
when '.js'
"\n"
when '.css'
"\n"
end
end
end
end