2011-11-17 21:18:25 +00:00
|
|
|
require 'rainbow'
|
2011-11-18 21:00:24 +00:00
|
|
|
require 'sprockets'
|
|
|
|
|
2011-11-16 20:28:02 +00:00
|
|
|
module Jasmine::Headless
|
|
|
|
class TestFile
|
2011-11-18 03:16:04 +00:00
|
|
|
attr_reader :path, :source_root
|
2011-11-16 20:28:02 +00:00
|
|
|
|
2011-11-18 03:16:04 +00:00
|
|
|
def initialize(path, source_root = nil)
|
|
|
|
@path, @source_root = path, source_root
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
2011-11-17 21:18:25 +00:00
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
self.path == other.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_html
|
2011-11-19 17:45:40 +00:00
|
|
|
process_data_by_filename(path)
|
2011-11-17 21:18:25 +00:00
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
|
|
|
|
def dependencies
|
|
|
|
return @dependencies if @dependencies
|
|
|
|
|
|
|
|
processor = Sprockets::DirectiveProcessor.new(path)
|
|
|
|
@dependencies = processor.directives.collect do |_, type, name|
|
2011-11-18 16:50:48 +00:00
|
|
|
if name[%r{^\.}]
|
2011-11-18 03:16:04 +00:00
|
|
|
name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '')
|
2011-11-20 16:56:25 +00:00
|
|
|
else
|
|
|
|
raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree'
|
2011-11-18 03:16:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
[ type, name ]
|
|
|
|
end
|
|
|
|
end
|
2011-11-18 21:00:24 +00:00
|
|
|
|
|
|
|
def logical_path
|
|
|
|
path.gsub(%r{^#{source_root}/}, '').gsub(%r{\..+$}, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def read
|
|
|
|
File.read(path)
|
|
|
|
end
|
2011-11-19 17:45:40 +00:00
|
|
|
|
|
|
|
def process_data_by_filename(path, data = nil)
|
|
|
|
case extension = File.extname(path)
|
|
|
|
when ''
|
|
|
|
data || ''
|
|
|
|
when '.js'
|
|
|
|
data || %{<script type="text/javascript" src="#{path}"></script>}
|
|
|
|
when '.css'
|
|
|
|
data || %{<link rel="stylesheet" href="#{path}" type="text/css" />}
|
|
|
|
else
|
|
|
|
if engine = Sprockets.engines(extension)
|
|
|
|
data = engine.new(path) { data || read }.render(self)
|
2011-11-20 00:15:38 +00:00
|
|
|
data = %{<script type="text/javascript">#{data}</script>} if extension == '.jst'
|
2011-11-19 17:45:40 +00:00
|
|
|
|
|
|
|
process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data)
|
|
|
|
else
|
|
|
|
data || ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-16 20:28:02 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-18 03:16:04 +00:00
|
|
|
|