From 7206341768e4a958688bab21fa85106a42c083d7 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 21 Nov 2011 20:18:11 -0500 Subject: [PATCH] require_self --- lib/jasmine/headless/required_file.rb | 28 ++++++++++++++---- .../jasmine/headless/required_file_spec.rb | 29 ++++++++++++++++++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/lib/jasmine/headless/required_file.rb b/lib/jasmine/headless/required_file.rb index dd28b82..8b6401b 100644 --- a/lib/jasmine/headless/required_file.rb +++ b/lib/jasmine/headless/required_file.rb @@ -37,15 +37,26 @@ module Jasmine::Headless end def file_paths - (dependencies.collect(&:file_paths) + [ path ]).flatten + paths = dependencies.collect(&:file_paths).flatten + + if @insert_after + paths.insert(paths.index(@insert_after) + 1, path) + else + paths << path + end + + paths end def dependencies return @dependencies if @dependencies processor = Sprockets::DirectiveProcessor.new(path) + + last_file_added = nil + @dependencies = processor.directives.collect do |line, type, name| - if name[%r{^\.}] + if name && name[%r{^\.}] name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') else raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree' @@ -55,14 +66,21 @@ module Jasmine::Headless when 'require' [ name ] when 'require_tree' - Dir[File.join(source_root, name, '**/*')].find_all { |path| - File.file?(path) && path[extension_filter] + Dir[File.join(source_root, name, '**/*')].find_all { |found_path| + found_path != path && File.file?(found_path) && found_path[extension_filter] }.sort.collect { |path| path.gsub(%r{^#{source_root}/}, '') } + when 'require_self' + @insert_after = last_file_added + [] + else + [] end files.collect do |file| if result = path_searcher.find(file) - self.class.new(*[ result, self ].flatten) + new_file = self.class.new(*[ result, self ].flatten) + last_file_added = new_file.path + new_file else raise Sprockets::FileNotFound.new("Could not find #{file}, referenced from #{path}:#{line}") end diff --git a/spec/lib/jasmine/headless/required_file_spec.rb b/spec/lib/jasmine/headless/required_file_spec.rb index 6820173..ea98ea1 100644 --- a/spec/lib/jasmine/headless/required_file_spec.rb +++ b/spec/lib/jasmine/headless/required_file_spec.rb @@ -134,11 +134,15 @@ describe Jasmine::Headless::RequiredFile do let(:path) { path_file } let(:path_file) { File.join(dir, 'path.js') } let(:req_file) { File.join(dir, "req.js") } + let(:other_file) { File.join(dir, "other.js") } + + let(:content) { "//= #{directive} '#{req_name}'\njavascript" } before do FileUtils.mkdir_p dir - File.open(path_file, 'wb') { |fh| fh.print "//= #{directive} '#{req_name}'\njavascript" } + File.open(path_file, 'wb') { |fh| fh.print content } File.open(req_file, 'wb') + File.open(other_file, 'wb') end subject { file.dependencies } @@ -194,6 +198,29 @@ describe Jasmine::Headless::RequiredFile do end end end + + context 'require_self' do + subject { file.file_paths } + + let(:content) do + <<-ENDTXT +//= require #{dirname}/req +//= require_self +//= require #{dirname}/other +ENDTXT + end + + before do + path_searcher.expects(:find).with(File.join(dirname, 'req')).returns([ req_file, source_root ]) + path_searcher.expects(:find).with(File.join(dirname, 'other')).returns([ other_file, source_root ]) + end + + it { should == [ + req_file, + path_file, + other_file + ] } + end end describe '#file_paths' do