2011-07-19 14:41:23 +00:00
|
|
|
require 'jasmine-core'
|
2011-08-04 14:18:05 +00:00
|
|
|
require 'iconv'
|
2011-09-02 15:31:58 +00:00
|
|
|
require 'time'
|
2011-06-10 14:04:21 +00:00
|
|
|
|
|
|
|
module Jasmine
|
|
|
|
class FilesList
|
2011-06-23 13:29:07 +00:00
|
|
|
attr_reader :files, :filtered_files, :spec_outside_scope
|
2011-06-10 14:04:21 +00:00
|
|
|
|
|
|
|
DEFAULT_FILES = [
|
2011-06-30 19:27:57 +00:00
|
|
|
File.join(Jasmine::Core.path, "jasmine.js"),
|
|
|
|
File.join(Jasmine::Core.path, "jasmine-html.js"),
|
2011-06-10 14:04:21 +00:00
|
|
|
File.expand_path('../../../jasmine/jasmine.headless-reporter.js', __FILE__)
|
|
|
|
]
|
|
|
|
|
2011-09-02 15:31:58 +00:00
|
|
|
PLEASE_WAIT_IM_WORKING_TIME = 2
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def initialize(options = {})
|
|
|
|
@options = options
|
2011-06-10 15:02:26 +00:00
|
|
|
@files = DEFAULT_FILES.dup
|
|
|
|
@filtered_files = @files.dup
|
2011-06-23 13:29:07 +00:00
|
|
|
@spec_outside_scope = false
|
2011-07-13 18:42:47 +00:00
|
|
|
@spec_files = []
|
2011-06-10 14:04:21 +00:00
|
|
|
use_config! if config?
|
|
|
|
end
|
|
|
|
|
2011-06-23 13:29:07 +00:00
|
|
|
def has_spec_outside_scope?
|
|
|
|
@spec_outside_scope
|
|
|
|
end
|
|
|
|
|
2011-06-10 15:02:26 +00:00
|
|
|
def filtered?
|
|
|
|
files != filtered_files
|
|
|
|
end
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def files_to_html
|
2011-06-10 15:02:26 +00:00
|
|
|
to_html(files)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_files_to_html
|
|
|
|
to_html(filtered_files)
|
|
|
|
end
|
|
|
|
|
2011-07-13 18:42:47 +00:00
|
|
|
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?
|
2011-07-13 18:42:47 +00:00
|
|
|
[ file, lines ]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
}.compact]
|
|
|
|
end
|
|
|
|
|
2011-06-10 15:02:26 +00:00
|
|
|
private
|
|
|
|
def to_html(files)
|
2011-09-02 15:31:58 +00:00
|
|
|
alert_time = Time.now + PLEASE_WAIT_IM_WORKING_TIME
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
files.collect { |file|
|
2011-09-02 15:31:58 +00:00
|
|
|
if alert_time && alert_time < Time.now
|
|
|
|
puts "Rebuilding cache, please wait..."
|
|
|
|
alert_time = nil
|
|
|
|
end
|
|
|
|
|
2011-09-01 13:38:53 +00:00
|
|
|
case File.extname(file)
|
|
|
|
when '.coffee'
|
|
|
|
begin
|
|
|
|
%{<script type="text/javascript">#{Jasmine::Headless::CoffeeScriptCache.for(file)}</script>}
|
|
|
|
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
|
2011-09-01 13:38:53 +00:00
|
|
|
when '.js'
|
|
|
|
%{<script type="text/javascript" src="#{file}"></script>}
|
|
|
|
when '.css'
|
|
|
|
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
|
2011-06-10 18:18:35 +00:00
|
|
|
end
|
2011-09-02 15:31:58 +00:00
|
|
|
}.flatten.compact.reject(&:empty?)
|
2011-06-10 18:18:35 +00:00
|
|
|
end
|
|
|
|
|
2011-06-10 14:04:21 +00:00
|
|
|
def spec_filter
|
2011-06-30 10:41:32 +00:00
|
|
|
@spec_filter ||= (@options[:only] ? @options[:only].collect { |path| Dir[path] }.flatten : [])
|
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' ], [ 'helpers', 'spec_dir' ], [ 'spec_files', 'spec_dir' ] ].each do |searches, root|
|
|
|
|
if data[searches]
|
2011-08-10 17:11:05 +00:00
|
|
|
data[searches].flatten.collect do |search|
|
2011-06-10 14:04:21 +00:00
|
|
|
path = search
|
|
|
|
path = File.join(data[root], path) if data[root]
|
2011-06-11 13:54:15 +00:00
|
|
|
found_files = Dir[path] - @files
|
2011-06-10 14:04:21 +00:00
|
|
|
|
|
|
|
@files += found_files
|
|
|
|
|
2011-07-13 18:42:47 +00:00
|
|
|
if searches == 'spec_files'
|
2011-09-01 14:39:29 +00:00
|
|
|
@spec_files += spec_filter
|
2011-07-13 18:42:47 +00:00
|
|
|
end
|
|
|
|
|
2011-06-23 13:29:07 +00:00
|
|
|
@filtered_files += (if searches == 'spec_files'
|
|
|
|
@spec_outside_scope = ((spec_filter | found_files).sort != found_files.sort)
|
2011-06-29 14:38:15 +00:00
|
|
|
spec_filter.empty? ? found_files : (spec_filter || found_files)
|
2011-06-23 13:29:07 +00:00
|
|
|
else
|
|
|
|
found_files
|
|
|
|
end)
|
2011-06-10 14:04:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config?
|
|
|
|
@options[:config]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|