2011-06-19 19:08:18 +00:00
begin
require 'jasmine'
rescue NameError = > e
if e . message [ 'ActiveSupport::Concern' ]
$stderr . puts " [%s] %s (%s) " % [ " jasmine-gem " . color ( :red ) , e . message . color ( :white ) , e . class . name . color ( :yellow ) ]
$stderr . puts " #{ 'Jasmine' . color ( :red ) } believes Rails 3 is available. Try using #{ 'Bundler' . color ( :green ) } and running via #{ 'bundle exec' . color ( :green ) } . "
else
raise e
end
end
2011-06-10 14:04:21 +00:00
module Jasmine
class FilesList
attr_reader :files , :filtered_files
DEFAULT_FILES = [
File . join ( Jasmine . root , " lib/jasmine.js " ) ,
File . join ( Jasmine . root , " lib/jasmine-html.js " ) ,
File . expand_path ( '../../../jasmine/jasmine.headless-reporter.js' , __FILE__ )
]
def initialize ( options = { } )
@options = options
2011-06-10 15:02:26 +00:00
@files = DEFAULT_FILES . dup
@filtered_files = @files . dup
2011-06-10 14:04:21 +00:00
use_config! if config?
2011-06-10 15:35:54 +00:00
@code_for_file = { }
2011-06-10 14:04:21 +00:00
end
def use_spec? ( file )
spec_filter . empty? || spec_filter . include? ( file )
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
private
def to_html ( files )
2011-06-10 18:18:35 +00:00
coffeescript_run = [ ]
2011-06-10 14:04:21 +00:00
files . collect { | file |
2011-06-10 18:29:16 +00:00
next @code_for_file [ file ] if @code_for_file [ file ]
2011-06-10 18:18:35 +00:00
coffeescript_run << file if ( ext = File . extname ( file ) ) == '.coffee'
output = [ ]
if ( files . last == file or ext != '.coffee' ) and ! coffeescript_run . empty?
output << ensure_coffeescript_run! ( coffeescript_run )
end
if ext != '.coffee'
output << case File . extname ( file )
when '.js'
%{ <script type="text/javascript" src=" #{ file } "></script> }
when '.css'
%{ <link rel="stylesheet" href=" #{ file } " type="text/css" /> }
2011-06-10 14:04:21 +00:00
end
2011-06-10 18:18:35 +00:00
end
2011-06-10 18:29:16 +00:00
@code_for_file [ file ] = output if output . length == 1
2011-06-10 18:25:08 +00:00
output
2011-06-10 18:18:35 +00:00
} . flatten . reject ( & :empty? )
end
def ensure_coffeescript_run! ( files )
data = StringIO . new
2011-06-19 18:51:47 +00:00
files . each { | file | data << File . read ( file ) << " \n " }
2011-06-10 18:18:35 +00:00
data . rewind
%{ <script type="text/javascript"> #{ CoffeeScript . compile ( data ) } </script> }
rescue CoffeeScript :: CompilationError = > e
2011-06-10 21:14:50 +00:00
files . each do | file |
begin
CoffeeScript . compile ( fh = File . open ( file ) )
rescue CoffeeScript :: CompilationError = > ne
puts " [%s] %s: %s " % [ 'coffeescript' . color ( :red ) , file . color ( :yellow ) , ne . message . to_s . color ( :white ) ]
2011-06-17 14:27:00 +00:00
raise ne
2011-06-10 21:14:50 +00:00
ensure
fh . close
end
end
2011-06-17 13:34:18 +00:00
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 18:18:35 +00:00
ensure
files . clear
2011-06-10 14:04:21 +00:00
end
def spec_filter
@options [ :only ] || [ ]
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 ]
data [ searches ] . collect do | search |
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
if searches == 'spec_files'
found_files = found_files . find_all { | file | use_spec? ( file ) }
end
@filtered_files += found_files
end
end
end
end
def config?
@options [ :config ]
end
end
end