diff --git a/bin/jasmine b/bin/jasmine index 2b2ee91..cc597ec 100755 --- a/bin/jasmine +++ b/bin/jasmine @@ -37,9 +37,9 @@ if ARGV[0] == 'init' rails_tasks_dir = dest_path('lib/tasks') if File.exist?(rails_tasks_dir) copy_unless_exists('lib/tasks/jasmine.rake') - copy_unless_exists('spec/javascripts/support/sources-rails.yaml', 'spec/javascripts/support/sources.yaml') + copy_unless_exists('spec/javascripts/support/jasmine-rails.yaml', 'spec/javascripts/support/jasmine.yaml') else - copy_unless_exists('spec/javascripts/support/sources.yaml') + copy_unless_exists('spec/javascripts/support/jasmine.yaml') write_mode = 'w' if File.exist?(dest_path('Rakefile')) load dest_path('Rakefile') diff --git a/generators/jasmine/jasmine_generator.rb b/generators/jasmine/jasmine_generator.rb index 755b122..a87decc 100644 --- a/generators/jasmine/jasmine_generator.rb +++ b/generators/jasmine/jasmine_generator.rb @@ -9,7 +9,7 @@ class JasmineGenerator < Rails::Generator::Base m.directory "spec/javascripts/support" m.file "spec/javascripts/support/jasmine_config.rb", "spec/javascripts/support/jasmine_config.rb" m.file "spec/javascripts/support/jasmine_spec.rb", "spec/javascripts/support/jasmine_spec.rb" - m.file "spec/javascripts/support/sources-rails.yaml", "spec/javascripts/support/sources.yaml" + m.file "spec/javascripts/support/jasmine-rails.yaml", "spec/javascripts/support/jasmine.yaml" m.directory "lib/tasks" m.file "lib/tasks/jasmine.rake", "lib/tasks/jasmine.rake" diff --git a/generators/jasmine/templates/spec/javascripts/support/sources-rails.yaml b/generators/jasmine/templates/spec/javascripts/support/jasmine-rails.yaml similarity index 71% rename from generators/jasmine/templates/spec/javascripts/support/sources-rails.yaml rename to generators/jasmine/templates/spec/javascripts/support/jasmine-rails.yaml index e43b817..8dacb55 100644 --- a/generators/jasmine/templates/spec/javascripts/support/sources-rails.yaml +++ b/generators/jasmine/templates/spec/javascripts/support/jasmine-rails.yaml @@ -1,8 +1,10 @@ -sources: +src_files: - javascripts/prototype.js - javascripts/effects.js - javascripts/controls.js - javascripts/dragdrop.js - javascripts/application.js +spec_files: + - **/*.js src_dir: public -spec_dir: spec/javascripts \ No newline at end of file +spec_dir: spec/javascripts diff --git a/generators/jasmine/templates/spec/javascripts/support/jasmine.yaml b/generators/jasmine/templates/spec/javascripts/support/jasmine.yaml new file mode 100644 index 0000000..09706ed --- /dev/null +++ b/generators/jasmine/templates/spec/javascripts/support/jasmine.yaml @@ -0,0 +1,10 @@ +#src_files: +# - lib/source1.js +# - lib/source2.js +# - dist/**/*.js +#stylesheets: +# - css/style.css +#spec_files: +# - +#src_dir: +#spec_dir: spec/javascripts \ No newline at end of file diff --git a/generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb b/generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb index 633b2e0..efbb174 100644 --- a/generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb +++ b/generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb @@ -2,22 +2,58 @@ require 'jasmine' class Jasmine::Config - def project_root - File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..")) - end - -# Return an array of files to include before jasmine specs. Override if needed. -# def src_files -# match_files(src_dir, "**/*.js") +# def project_root +# Dir.pwd # end -# Path to your JavaScript source files +# Path to your jasmine.yaml +# def simple_config_file +# File.join(project_root, 'spec/javascripts/support/jasmine.yaml') +# end + +# Source directory path. Your src_files must be returned relative to this path. # def src_dir -# File.join(project_root, "public") +# if simple_config['src_dir'] +# File.join(project_root, simple_config['src_dir']) +# else +# project_root +# end # end -# Path to your JavaScript specs +# Spec directory path. Your spec_files must be returned relative to this path. # def spec_dir -# File.join(project_root, 'spec/javascripts') +# if simple_config['spec_dir'] +# File.join(project_root, simple_config['spec_dir']) +# else +# File.join(project_root, 'spec/javascripts') +# end # end + +# Return an array of filepaths relative to src_dir to include before jasmine specs. +# def src_files +# files = [] +# if simple_config['src_files'] +# files = simple_config['src_files'].collect {|filepath| Dir.glob(filepath)} +# end +# files +# end + +# Return an array of filepaths relative to spec_dir to include before jasmine specs. +# def spec_files +# files = match_files(spec_dir, "**/*.js") +# if simple_config['spec_files'] +# files = simple_config['spec_files'].collect {|filepath| Dir.glob(filepath)} +# end +# files +# end + +# Return an array of filepaths relative to src_dir to include before jasmine specs. +# def stylesheets +# files = [] +# if simple_config['stylesheets'] +# files = simple_config['stylesheets'].collect {|filepath| Dir.glob(filepath)} +# end +# files +# end + end diff --git a/generators/jasmine/templates/spec/javascripts/support/sources.yaml b/generators/jasmine/templates/spec/javascripts/support/sources.yaml deleted file mode 100644 index 9ffe511..0000000 --- a/generators/jasmine/templates/spec/javascripts/support/sources.yaml +++ /dev/null @@ -1,5 +0,0 @@ -#sources: -# - lib/source1.js -# - lib/source2.js -#src_dir: -#spec_dir: spec/javascripts \ No newline at end of file diff --git a/jasmine.gemspec b/jasmine.gemspec index c432a53..95ca5a8 100644 --- a/jasmine.gemspec +++ b/jasmine.gemspec @@ -23,10 +23,10 @@ Gem::Specification.new do |s| "generators/jasmine/templates/lib/tasks/jasmine.rake", "generators/jasmine/templates/spec/javascripts/ExampleSpec.js", "generators/jasmine/templates/spec/javascripts/SpecHelper.js", + "generators/jasmine/templates/spec/javascripts/support/jasmine-rails.yaml", + "generators/jasmine/templates/spec/javascripts/support/jasmine.yaml", "generators/jasmine/templates/spec/javascripts/support/jasmine_config.rb", "generators/jasmine/templates/spec/javascripts/support/jasmine_spec.rb", - "generators/jasmine/templates/spec/javascripts/support/sources-rails.yaml", - "generators/jasmine/templates/spec/javascripts/support/sources.yaml", "jasmine/contrib/ruby/jasmine_runner.rb", "jasmine/contrib/ruby/jasmine_spec_builder.rb", "jasmine/contrib/ruby/run.html", diff --git a/lib/jasmine/config.rb b/lib/jasmine/config.rb index 655a229..16705fc 100644 --- a/lib/jasmine/config.rb +++ b/lib/jasmine/config.rb @@ -71,37 +71,11 @@ module Jasmine @client.eval_js(script) end - def stylesheets - [] - end - - def src_files - [] - end - - def spec_files - raise "You need to declare a spec_files method in #{self.class}!" - end - - def match_files(dir, pattern) + def match_files(dir, patterns) dir = File.expand_path(dir) - Dir.glob(File.join(dir, pattern)).collect {|f| f.sub("#{dir}/", "")}.sort - end - - def project_root - Dir.pwd - end - - def src_dir - if simple_config['src_dir'] - File.join(project_root, simple_config['src_dir']) - else - project_root - end - end - - def simple_config_file - File.join(project_root, 'spec/javascripts/support/sources.yaml') + patterns.collect do |pattern| + Dir.glob(File.join(dir, pattern)).collect {|f| f.sub("#{dir}/", "")}.sort + end.flatten end def simple_config @@ -109,21 +83,6 @@ module Jasmine config || {} end - def src_files - simple_config['sources'] || [] - end - - def spec_dir - if simple_config['spec_dir'] - File.join(project_root, simple_config['spec_dir']) - else - File.join(project_root, 'spec/javascripts') - end - end - - def spec_files - match_files(spec_dir, "**/*.js") - end def spec_path "/__spec__" @@ -144,8 +103,61 @@ module Jasmine src_files.collect {|f| "/" + f } + spec_files.collect {|f| File.join(spec_path, f) } end + def css_files + stylesheets.collect {|f| "/" + f } + end + def spec_files_full_paths spec_files.collect {|spec_file| File.join(spec_dir, spec_file) } end + + def project_root + Dir.pwd + end + + def simple_config_file + File.join(project_root, 'spec/javascripts/support/jasmine.yaml') + end + + def src_dir + if simple_config['src_dir'] + File.join(project_root, simple_config['src_dir']) + else + project_root + end + end + + def spec_dir + if simple_config['spec_dir'] + File.join(project_root, simple_config['spec_dir']) + else + File.join(project_root, 'spec/javascripts') + end + end + + def src_files + files = [] + if simple_config['src_files'] + files = match_files(src_dir, simple_config['src_files']) + end + files + end + + def spec_files + files = match_files(spec_dir, "**/*.js") + if simple_config['spec_files'] + files = match_files(spec_dir, simple_config['spec_files']) + end + files + end + + def stylesheets + files = [] + if simple_config['stylesheets'] + files = match_files(src_dir, simple_config['stylesheets']) + end + files + end + end end \ No newline at end of file diff --git a/lib/jasmine/server.rb b/lib/jasmine/server.rb index 93480e9..76f47d9 100644 --- a/lib/jasmine/server.rb +++ b/lib/jasmine/server.rb @@ -18,7 +18,7 @@ module Jasmine #noinspection RubyUnusedLocalVariable def run jasmine_files = @jasmine_files - css_files = @jasmine_stylesheets + (@config.stylesheets || []) + css_files = @jasmine_stylesheets + (@config.css_files || []) js_files = @config.js_files body = ERB.new(File.read(File.join(File.dirname(__FILE__), "run.html.erb"))).result(binding) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 997c8e1..a35ed6d 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -12,6 +12,7 @@ describe Jasmine::Config do it "if sources.yaml not found" do File.stub!(:exist?).and_return(false) @config.src_files.should == [] + @config.stylesheets.should == [] @config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js'] @config.mappings.should == { '/__root__' => @config.project_root, @@ -20,8 +21,10 @@ describe Jasmine::Config do end it "if sources.yaml is empty" do + @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/jasmine.yaml')) YAML.stub!(:load).and_return(false) @config.src_files.should == [] + @config.stylesheets.should == [] @config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js'] @config.mappings.should == { '/__root__' => @config.project_root, @@ -29,8 +32,8 @@ describe Jasmine::Config do } end - it "using default sources.yaml" do - @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/sources.yaml')) + it "using default jasmine.yaml" do + @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/jasmine.yaml')) @config.src_files.should == [] @config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js'] @config.mappings.should == { @@ -39,14 +42,31 @@ describe Jasmine::Config do } end - it "using rails sources.yaml" do - @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/sources-rails.yaml')) + it "simple_config stylesheets" do + @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/jasmine.yaml')) + YAML.stub!(:load).and_return({'stylesheets' => ['foo.css', 'bar.css']}) + Dir.stub!(:glob).and_return do |glob_string| + glob_string + end + @config.stylesheets.should == ['foo.css', 'bar.css'] + end + + it "using rails jasmine.yaml" do + original_glob = Dir.method(:glob) + Dir.stub!(:glob).and_return do |glob_string| + if glob_string =~ /public/ + glob_string + else + original_glob.call(glob_string) + end + end + @config.stub!(:simple_config_file).and_return(File.join(@template_dir, 'spec/javascripts/support/jasmine-rails.yaml')) + @config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js'] @config.src_files.should == ['javascripts/prototype.js', 'javascripts/effects.js', 'javascripts/controls.js', 'javascripts/dragdrop.js', 'javascripts/application.js'] - @config.spec_files.should == ['javascripts/ExampleSpec.js', 'javascripts/SpecHelper.js'] @config.js_files.should == [ '/javascripts/prototype.js', '/javascripts/effects.js',