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

264 lines
6.5 KiB
Ruby
Raw Normal View History

require 'jasmine-core'
require 'time'
require 'multi_json'
2011-11-20 16:56:25 +00:00
require 'set'
require 'sprockets'
2011-11-21 15:32:49 +00:00
require 'sprockets/engines'
2011-06-10 14:04:21 +00:00
2011-11-16 20:28:02 +00:00
module Jasmine::Headless
2011-06-10 14:04:21 +00:00
class FilesList
2011-10-26 12:45:23 +00:00
class << self
2011-11-18 03:16:04 +00:00
def vendor_asset_paths
return @vendor_asset_paths if @vendor_asset_paths
2011-10-26 12:45:23 +00:00
require 'rubygems'
raise StandardError.new("A newer version of Rubygems is required to use vendored assets. Please upgrade.") if !Gem::Specification.respond_to?(:map)
2011-11-18 03:16:04 +00:00
@vendor_asset_paths = []
Gem::Specification.map { |spec|
path = File.join(spec.gem_dir, 'vendor/assets/javascripts')
File.directory?(path) ? path : nil
}.compact
2011-10-26 12:45:23 +00:00
end
def reset!
@vendor_asset_paths = nil
2011-11-21 15:32:49 +00:00
# register haml-sprockets if it's available...
%w{haml-sprockets}.each do |library|
begin
require library
rescue LoadError
end
end
# ...and unregister ones we don't want/need
Sprockets.instance_eval do
%w{less sass scss erb str}.each do |extension|
@engines.delete(".#{extension}")
end
register_engine '.coffee', Jasmine::Headless::CoffeeTemplate
end
end
2011-10-26 12:45:23 +00:00
2011-11-21 15:32:49 +00:00
def default_files
%w{jasmine.js jasmine-html jasmine.css jasmine-extensions intense headless_reporter_result jasmine.HeadlessConsoleReporter jsDump beautify-html}
end
end
2011-06-10 14:04:21 +00:00
PLEASE_WAIT_IM_WORKING_TIME = 2
2011-11-21 15:32:49 +00:00
attr_reader :required_files, :potential_files_to_filter
2011-06-10 14:04:21 +00:00
def initialize(options = {})
@options = options
2011-11-18 03:16:04 +00:00
2011-11-21 15:32:49 +00:00
@required_files = []
@potential_files_to_filter = []
2011-11-18 03:16:04 +00:00
2011-11-21 15:32:49 +00:00
self.class.default_files.each do |file|
@required_files << RequiredFile.new(*path_searcher.find(file.dup), self)
end
2011-11-18 03:16:04 +00:00
2011-06-10 14:04:21 +00:00
use_config! if config?
end
2011-11-17 21:18:25 +00:00
def files
2011-11-21 15:32:49 +00:00
required_files.collect { |file| file.file_paths }.flatten.uniq
2011-11-17 21:18:25 +00:00
end
2011-11-21 15:32:49 +00:00
def spec_files
filter_for_requested_specs(required_files.find_all(&:spec_file?).collect(&:path))
2011-11-17 21:18:25 +00:00
end
2011-11-21 15:32:49 +00:00
def filtered_files
filter_for_requested_specs(files)
2011-11-17 21:18:25 +00:00
end
2011-11-18 03:16:04 +00:00
def search_paths
2011-11-20 00:15:38 +00:00
return @search_paths if @search_paths
@search_paths = [ Jasmine::Core.path ]
2011-11-21 15:32:49 +00:00
@search_paths += src_dir.collect { |dir| File.expand_path(dir) }
@search_paths += spec_dir.collect { |dir| File.expand_path(dir) }
2011-11-20 00:15:38 +00:00
@search_paths += self.class.vendor_asset_paths
@search_paths
2011-11-18 03:16:04 +00:00
end
2011-11-21 15:32:49 +00:00
def path_searcher
@path_searcher ||= PathSearcher.new(self)
end
def has_spec_outside_scope?
2011-11-21 15:32:49 +00:00
if is_outside_scope = !spec_filter.empty?
is_outside_scope = spec_dir.any? do |dir|
spec_file_searches.any? do |search|
!spec_files.any? { |file| File.fnmatch?(File.join(dir, search), file) }
end
end
end
is_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
2011-11-21 15:32:49 +00:00
@spec_file_line_numbers ||= Hash[spec_files.collect { |file|
2011-11-17 21:18:25 +00:00
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-11-21 15:32:49 +00:00
files.collect do |file|
if alert_time && alert_time < Time.now
puts "Rebuilding cache, please wait..."
alert_time = nil
end
2011-11-18 21:00:24 +00:00
search_paths.collect do |path|
2011-11-21 15:32:49 +00:00
if file[%r{^#{path}}]
Jasmine::Headless::RequiredFile.new(file, path, self).to_html
2011-11-18 21:00:24 +00:00
end
end.compact.first
2011-11-21 15:32:49 +00:00
end.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
2011-11-17 21:18:25 +00:00
SEARCH_ROOTS = {
'src_files' => 'src_dir',
'stylesheets' => 'src_dir',
'helpers' => 'spec_dir',
'spec_files' => 'spec_dir'
}
2011-06-10 14:04:21 +00:00
def use_config!
2011-11-17 21:18:25 +00:00
@config = @options[:config].dup
2011-11-21 15:32:49 +00:00
@searches = {}
@potential_files_to_filter = []
2011-11-17 21:18:25 +00:00
2011-11-21 15:32:49 +00:00
%w{src_files stylesheets helpers spec_files}.each do |type|
if data = @config[type]
dirs = send(SEARCH_ROOTS[type])
add_files(@searches[type] = data.flatten, type, dirs)
2011-11-17 21:18:25 +00:00
end
end
end
2011-11-21 15:32:49 +00:00
def add_files(patterns, type, dirs)
dirs.each do |dir|
patterns.each do |search|
search = File.expand_path(File.join(dir, search))
2011-11-18 03:16:04 +00:00
2011-11-21 15:32:49 +00:00
Dir[search].find_all { |file| file[extension_filter] }.each do |path|
@required_files << (file = RequiredFile.new(path, dir, self))
2011-11-17 21:18:25 +00:00
2011-11-21 15:32:49 +00:00
if type == 'spec_files'
file.spec_file = true
@potential_files_to_filter << path
end
2011-11-20 00:15:38 +00:00
end
2011-11-17 21:18:25 +00:00
end
end
2011-11-16 20:28:02 +00:00
2011-11-17 21:18:25 +00:00
if type == 'spec_files'
2011-11-21 15:32:49 +00:00
spec_filter.each do |path|
@required_files << (file = RequiredFile.new(path, nil, self))
file.spec_file = true
@potential_files_to_filter << path
2011-11-17 21:18:25 +00:00
end
end
2011-11-21 15:32:49 +00:00
@required_files.uniq!(&:path)
2011-06-10 14:04:21 +00:00
end
def config?
@options[:config]
end
def expanded_dir(path)
Dir[path].collect { |file| File.expand_path(file) }.find_all { |path| File.file?(path) && path[extension_filter] }
end
2011-11-17 21:18:25 +00:00
2011-11-21 15:32:49 +00:00
def extension_filter
%r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$}
2011-11-17 21:18:25 +00:00
end
2011-11-21 15:32:49 +00:00
def add_file(file)
@files << file
2011-11-17 21:18:25 +00:00
end
def include_spec_file?(file)
spec_filter.empty? || spec_filter.include?(file)
end
2011-11-18 03:16:04 +00:00
def src_dir
config_dir_or_pwd('src_dir')
end
def spec_dir
config_dir_or_pwd('spec_dir')
end
2011-11-21 15:32:49 +00:00
def spec_file_searches
@searches['spec_files']
end
2011-11-18 03:16:04 +00:00
def config_dir_or_pwd(dir)
found_dir = Dir.pwd
if @options[:config]
found_dir = @options[:config][dir] || found_dir
end
2011-11-21 15:32:49 +00:00
[ found_dir ].flatten.collect { |dir| File.expand_path(dir) }
end
def filter_for_requested_specs(files)
files.find_all do |file|
if potential_files_to_filter.include?(file)
spec_filter.empty? || spec_filter.any? { |pattern| File.fnmatch?(pattern, file) }
else
true
end
end
2011-11-18 03:16:04 +00:00
end
2011-06-10 14:04:21 +00:00
end
end