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

177 lines
5.4 KiB
Ruby
Raw Normal View History

require 'jasmine-core'
require 'time'
require 'multi_json'
2011-06-10 14:04:21 +00:00
module Jasmine
class FilesList
attr_reader :files, :spec_files, :filtered_files, :spec_outside_scope
2011-06-10 14:04:21 +00:00
2011-10-26 12:45:23 +00:00
class << self
def find_vendored_asset_paths(*names)
require 'rubygems'
raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:map)
all_spec_files.find_all do |file|
names.any? { |name| file["/#{name}.js"] }
end
end
def all_spec_files
@all_spec_files ||= Gem::Specification.map { |spec| spec.files.find_all { |file|
file["vendor/assets/javascripts"]
}.compact.collect { |file| File.join(spec.gem_dir, file) } }.flatten
end
end
2011-06-10 14:04:21 +00:00
DEFAULT_FILES = [
File.join(Jasmine::Core.path, "jasmine.js"),
File.join(Jasmine::Core.path, "jasmine-html.js"),
2011-10-26 12:45:23 +00:00
File.join(Jasmine::Core.path, "jasmine.css")
] + %w{jasmine-extensions intense headless_reporter_result jasmine.headless-reporter jsDump beautify-html}.collect { |name|
Jasmine::Headless.root.join("vendor/assets/javascripts/#{name}.js").to_s
}
2011-06-10 14:04:21 +00:00
PLEASE_WAIT_IM_WORKING_TIME = 2
2011-06-10 14:04:21 +00:00
def initialize(options = {})
@options = options
@files = DEFAULT_FILES.dup
@filtered_files = @files.dup
@spec_outside_scope = false
@spec_files = []
2011-06-10 14:04:21 +00:00
use_config! if config?
end
def has_spec_outside_scope?
@spec_outside_scope
end
def filtered?
files != filtered_files
end
2011-06-10 14:04:21 +00:00
def files_to_html
to_html(files)
end
def filtered_files_to_html
to_html(filtered_files)
end
def spec_file_line_numbers
@spec_file_line_numbers ||= Hash[@spec_files.collect { |file|
if File.exist?(file)
2011-09-01 14:39:29 +00:00
if !(lines = Jasmine::Headless::SpecFileAnalyzer.for(file)).empty?
[ file, lines ]
end
else
nil
end
}.compact]
end
private
def to_html(files)
alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME
2011-06-10 14:04:21 +00:00
files.collect { |file|
if alert_time && alert_time < Time.now
puts "Rebuilding cache, please wait..."
alert_time = nil
end
source = nil
result = case File.extname(file)
when '.coffee'
begin
cache = Jasmine::Headless::CoffeeScriptCache.new(file)
source = cache.handle
if cache.cached?
2011-10-26 12:45:23 +00:00
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
<script type="text/javascript">
2011-10-26 12:55:44 +00:00
window.CSTF['#{File.split(cache.cache_file).last}'] = '#{file}';
2011-10-26 12:45:23 +00:00
</script>}
else
%{<script type="text/javascript">#{source}</script>}
end
rescue CoffeeScript::CompilationError => ne
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), ne.message.to_s.color(:white) ]
raise ne
rescue StandardError => e
puts "[%s] Error in compiling one of the followng: %s" % [ 'coffeescript'.color(:red), files.join(' ').color(:yellow) ]
raise e
2011-06-10 14:04:21 +00:00
end
when '.js'
%{<script type="text/javascript" src="#{file}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
end
result
}.flatten.compact.reject(&:empty?)
end
2011-06-10 14:04:21 +00:00
def spec_filter
return @spec_filter if @spec_filter
@spec_filter = begin
if @options[:only]
@options[:only].collect { |path| expanded_dir(path) }.flatten
else
[]
end
end
2011-06-10 14:04:21 +00:00
end
def use_config!
@filtered_files = @files.dup
data = @options[:config].dup
[ [ 'src_files', 'src_dir' ], [ 'stylesheets', 'src_dir' ], [ 'vendored_helpers' ], [ 'helpers', 'spec_dir' ], [ 'spec_files', 'spec_dir' ] ].each do |searches, root|
2011-06-10 14:04:21 +00:00
if data[searches]
case searches
when 'vendored_helpers'
data[searches].each do |name|
found_files = self.class.find_vendored_asset_path(name)
2011-06-10 14:04:21 +00:00
@files += found_files
@filtered_files += found_files
end
else
data[searches].flatten.collect do |search|
path = search
path = File.join(data[root], path) if data[root]
found_files = expanded_dir(path) - @files
@files += found_files
if searches == 'spec_files'
@spec_files += spec_filter.empty? ? found_files : (found_files & spec_filter)
end
@filtered_files += begin
if searches == 'spec_files'
@spec_outside_scope = ((spec_filter | found_files).sort != found_files.sort)
spec_filter.empty? ? found_files : (spec_filter || found_files)
else
found_files
end
end
end
2011-06-10 14:04:21 +00:00
end
end
end
end
def config?
@options[:config]
end
def expanded_dir(path)
Dir[path].collect { |file| File.expand_path(file) }
end
2011-06-10 14:04:21 +00:00
end
end