jasmine-headless-webkit/lib/jasmine/headless/test_file.rb

59 lines
1.7 KiB
Ruby
Raw Normal View History

2011-11-17 21:18:25 +00:00
require 'rainbow'
2011-11-18 03:16:04 +00:00
require 'sprockets/directive_processor'
2011-11-17 21:18:25 +00:00
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
case File.extname(path)
when '.coffee'
begin
cache = Jasmine::Headless::CoffeeScriptCache.new(path)
source = cache.handle
if cache.cached?
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
<script type="text/javascript">
window.CSTF['#{File.split(cache.cache_file).last}'] = '#{path}';
</script>}
else
%{<script type="text/javascript">#{source}</script>}
end
rescue CoffeeScript::CompilationError => ne
2011-11-18 03:16:04 +00:00
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.dup.to_s.color(:white) ]
2011-11-17 21:18:25 +00:00
raise ne
rescue StandardError => e
puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ]
raise e
end
when '.js'
%{<script type="text/javascript" src="#{path}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{path}" type="text/css" />}
end
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|
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}/}, '')
end
[ type, name ]
end
end
2011-11-16 20:28:02 +00:00
end
end
2011-11-18 03:16:04 +00:00