require_self

This commit is contained in:
John Bintz 2011-11-21 20:18:11 -05:00
parent 4727ad5b7b
commit 7206341768
2 changed files with 51 additions and 6 deletions

View File

@ -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

View File

@ -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